Skip to content

Commit

Permalink
add generateDelegate function to signerContext (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
wil-hnt authored Jun 20, 2024
1 parent e3d3693 commit 5b868cd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/sdk/src/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions packages/sdk/src/signerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5b868cd

Please sign in to comment.