Skip to content

Commit

Permalink
feat: function to create self-delegated app-side signers
Browse files Browse the repository at this point in the history
This means, they already include the self-delegation authorization from the encryptor
  • Loading branch information
cygnusv committed Nov 4, 2024
1 parent cf8547f commit 1143204
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/taco-auth/src/providers/encryptor/self-delegate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers';
import { Wallet, ethers } from 'ethers';
import { z } from 'zod';

import { AuthSignature } from '../../auth-sig';
Expand All @@ -15,6 +15,15 @@ export class SelfDelegateProvider {
this.storage = new LocalStorage();
}

public async createSelfDelegatedAppSideSigner(
ephemeralPrivateKey: any // TODO: Find a stricter type
): Promise<[ethers.Signer, AuthSignature]> {
const appSideSigner = new Wallet(ephemeralPrivateKey);
const appSideSignerAddress = await appSideSigner.getAddress();
const authSignature = await this.getOrCreateAuthSignature(appSideSignerAddress);
return [appSideSigner, authSignature];
}

public async getOrCreateAuthSignature(
ephemeralPublicKeyOrAddress: string
): Promise<AuthSignature> {
Expand Down
9 changes: 8 additions & 1 deletion packages/taco-auth/test/auth-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,18 @@ describe('encryptor self-delegate provider authorization', () => {
const provider = fakeProvider(bobSecretKeyBytes);
const signer = provider.getSigner();
const selfDelegateProvider = new SelfDelegateProvider(signer);

it('creates a new self-delegated app signer', async () => {
const appSideSignerAddress = await applicationSideSigner.getAddress();
const [newSigner, newAuthSignature] = await selfDelegateProvider.createSelfDelegatedAppSideSigner(aliceSecretKeyBytes);
expect(await newSigner.getAddress()).toEqual(appSideSignerAddress);
expect(newAuthSignature.typedData).toEqual(appSideSignerAddress);
});

const applicationSideProvider = fakeProvider(aliceSecretKeyBytes);
const applicationSideSigner = applicationSideProvider.getSigner();

it('creates a new message', async () => {
it('creates a new auth signature', async () => {
const appSideSignerAddress = await applicationSideSigner.getAddress();
const typedSignature = await selfDelegateProvider.getOrCreateAuthSignature(appSideSignerAddress);
expect(typedSignature.signature).toBeDefined();
Expand Down

0 comments on commit 1143204

Please sign in to comment.