Skip to content

Commit fa6633c

Browse files
committed
feat: function to create self-delegated app-side signers
This means, they already include the self-delegation authorization from the encryptor
1 parent 409aa5e commit fa6633c

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

packages/taco-auth/src/providers/encryptor/self-delegate.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
1-
import { ethers } from 'ethers';
1+
import { ethers, Wallet } from 'ethers';
22
import { z } from 'zod';
33

4+
// import { Bytes } from "@ethersproject/bytes";
5+
46
import { AuthSignature } from '../../auth-sig';
57
import { LocalStorage } from '../../storage';
68

79
export const ENCRYPTOR_SELF_DELEGATE_AUTH_METHOD = 'EncryptorSelfDelegate'
810

911
export const SelfDelegateTypedDataSchema = z.string();
1012

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+
1136
export class SelfDelegateProvider {
1237
private readonly storage: LocalStorage;
1338

1439
constructor(private readonly signer: ethers.Signer) {
1540
this.storage = new LocalStorage();
1641
}
1742

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+
1851
public async getOrCreateAuthSignature(
1952
ephemeralPublicKeyOrAddress: string
2053
): Promise<AuthSignature> {

packages/taco-auth/test/auth-provider.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,18 @@ describe('encryptor self-delegate provider authorization', () => {
140140
const provider = fakeProvider(bobSecretKeyBytes);
141141
const signer = provider.getSigner();
142142
const selfDelegateProvider = new SelfDelegateProvider(signer);
143+
144+
it('creates a new self-delegated app signer', async () => {
145+
const appSideSignerAddress = await applicationSideSigner.getAddress();
146+
const [newSigner, newAuthSignature] = await selfDelegateProvider.createSelfDelegatedAppSideSigner(aliceSecretKeyBytes);
147+
expect(await newSigner.getAddress()).toEqual(appSideSignerAddress);
148+
expect(newAuthSignature.typedData).toEqual(appSideSignerAddress);
149+
});
143150

144151
const applicationSideProvider = fakeProvider(aliceSecretKeyBytes);
145152
const applicationSideSigner = applicationSideProvider.getSigner();
146153

147-
it('creates a new message', async () => {
154+
it('creates a new auth signature', async () => {
148155
const appSideSignerAddress = await applicationSideSigner.getAddress();
149156
const typedSignature = await selfDelegateProvider.getOrCreateAuthSignature(appSideSignerAddress);
150157
expect(typedSignature.signature).toBeDefined();

0 commit comments

Comments
 (0)