|
1 | | -import { ethers } from 'ethers'; |
| 1 | +import { ethers, Wallet } from 'ethers'; |
2 | 2 | import { z } from 'zod'; |
3 | 3 |
|
| 4 | +// import { Bytes } from "@ethersproject/bytes"; |
| 5 | + |
4 | 6 | import { AuthSignature } from '../../auth-sig'; |
5 | 7 | import { LocalStorage } from '../../storage'; |
6 | 8 |
|
7 | 9 | export const ENCRYPTOR_SELF_DELEGATE_AUTH_METHOD = 'EncryptorSelfDelegate' |
8 | 10 |
|
9 | 11 | export const SelfDelegateTypedDataSchema = z.string(); |
10 | 12 |
|
| 13 | + |
| 14 | +// TODO: Create generic EncryptorSigner class/interface, which can be |
| 15 | +// instantiated with ethers' Signers and Wallets, but also with our custom |
| 16 | +// classes |
| 17 | +export class DelegatedSigner extends Wallet { // TODO: extend from generic Signer |
| 18 | + |
| 19 | + authSignature?: AuthSignature; |
| 20 | + |
| 21 | + async authenticate(selfDelegateProvider: SelfDelegateProvider){ |
| 22 | + const appSideSignerAddress = await this.getAddress(); |
| 23 | + this.authSignature = await selfDelegateProvider.getOrCreateAuthSignature(appSideSignerAddress); |
| 24 | + } |
| 25 | + |
| 26 | + override async signMessage(message: any): Promise<string> { // TODO: Restrict input type to Bytes | string |
| 27 | + if (typeof this.authSignature === 'undefined'){ |
| 28 | + throw new Error('Encryptor must authenticate app signer first'); |
| 29 | + } |
| 30 | + const appSignature = await super.signMessage(message); |
| 31 | + return appSignature.concat(this.authSignature.signature) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
11 | 36 | export class SelfDelegateProvider { |
12 | 37 | private readonly storage: LocalStorage; |
13 | 38 |
|
14 | 39 | constructor(private readonly signer: ethers.Signer) { |
15 | 40 | this.storage = new LocalStorage(); |
16 | 41 | } |
17 | 42 |
|
| 43 | + public async createSelfDelegatedAppSideSigner( |
| 44 | + ephemeralPrivateKey: any // TODO: Find a stricter type |
| 45 | + ): Promise<DelegatedSigner> { |
| 46 | + const appSideSigner = new DelegatedSigner(ephemeralPrivateKey); |
| 47 | + await appSideSigner.authenticate(this); |
| 48 | + return appSideSigner; |
| 49 | + } |
| 50 | + |
18 | 51 | public async getOrCreateAuthSignature( |
19 | 52 | ephemeralPublicKeyOrAddress: string |
20 | 53 | ): Promise<AuthSignature> { |
|
0 commit comments