From 5b868cdf15a36db895b8e4b18a770b53af540dda Mon Sep 17 00:00:00 2001 From: Wil <157021769+wil-hnt@users.noreply.github.com> Date: Thu, 20 Jun 2024 10:59:01 -0300 Subject: [PATCH] add generateDelegate function to signerContext (#216) --- packages/sdk/src/sign.test.ts | 24 +++++++++++++++++++++++- packages/sdk/src/signerContext.ts | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/sign.test.ts b/packages/sdk/src/sign.test.ts index dff70dc76..b1dd2b36c 100644 --- a/packages/sdk/src/sign.test.ts +++ b/packages/sdk/src/sign.test.ts @@ -12,7 +12,12 @@ import { getPublicKey } from 'ethereum-cryptography/secp256k1' import { ethers } from 'ethers' import { EncryptedData, StreamEvent } from '@river-build/proto' import { TEST_ENCRYPTED_MESSAGE_PROPS } from './util.test' -import { SignerContext, checkDelegateSig, makeSignerContext } from './signerContext' +import { + SignerContext, + checkDelegateSig, + makeSignerDelegate, + makeSignerContext, +} from './signerContext' const log = dlog('test:sign') @@ -158,6 +163,23 @@ describe('sign', () => { ).not.toThrow() }) + test('make-signer-delegate', async () => { + const user = keys[0] + const userWallet = new ethers.Wallet(user.privateKey) + + const { signerContext, delegateWallet } = await makeSignerDelegate(userWallet) + + expect(signerContext.delegateSig).toBeDefined() + expect(() => + checkDelegateSig({ + delegatePubKey: delegateWallet.publicKey, + creatorAddress: user.address, + delegateSig: signerContext.delegateSig!, + expiryEpochMs: 0n, + }), + ).not.toThrow() + }) + const makeContext = async ( userPrivateKey: string, delegatePrivateKey?: string, diff --git a/packages/sdk/src/signerContext.ts b/packages/sdk/src/signerContext.ts index f06141fe4..3aa48cffb 100644 --- a/packages/sdk/src/signerContext.ts +++ b/packages/sdk/src/signerContext.ts @@ -101,6 +101,24 @@ export async function makeSignerContext( } } +export async function makeSignerDelegate( + signer: ethers.Signer, + expiry?: { + days?: number + hours?: number + minutes?: number + seconds?: number + }, +): Promise<{ delegateWallet: ethers.Wallet; signerContext: SignerContext }> { + const delegateWallet = ethers.Wallet.createRandom() + const signerContext = await makeSignerContext(signer, delegateWallet, expiry) + + return { + delegateWallet, + signerContext, + } +} + function makeExpiryEpochMs({ days, hours,