Skip to content

Commit

Permalink
wip: checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jan 2, 2025
1 parent 582cec7 commit fd88399
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 82 deletions.
11 changes: 6 additions & 5 deletions src/core/internal/account.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type * as Address from 'ox/Address'
import type * as Hex from 'ox/Hex'

import type * as Key from './key.js'
import type { PartialBy } from './types.js'

/** A delegated account. */
export type Account = {
address: Address.Address
delegation: Address.Address
keys?: readonly Key.Key[] | undefined
label?: string | undefined
sign?: (parameters: { payload: Hex.Hex }) => Hex.Hex | undefined
type: 'delegated'
}

Expand All @@ -20,8 +17,12 @@ export type Account = {
* @param account - Account to instantiate.
* @returns An instantiated delegated account.
*/
export function from<const account extends PartialBy<Account, 'type'>>(
account: account | PartialBy<Account, 'type'>,
export function from<const account extends from.Parameters>(
account: account | from.Parameters,
): account & { type: 'delegated' } {
return { ...account, type: 'delegated' } as never
}

export declare namespace from {
type Parameters = Omit<Account, 'type'>
}
74 changes: 60 additions & 14 deletions src/core/internal/delegation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ async function setup() {
value: Value.fromEther('2'),
})

const key = Key.fromSecp256k1({
privateKey,
role: 'owner',
})

const account = Account.from({
address,
delegation: state.delegation,
sign({ payload }) {
return Signature.toHex(Secp256k1.sign({ privateKey, payload }))
},
keys: [key],
})

return {
Expand All @@ -37,7 +40,7 @@ async function setup() {
}

describe('execute', () => {
describe('authorize call', () => {
describe('authorize', () => {
test('counterfactual: true, key: EOA, keysToAuthorize: [P256], executor: JSON-RPC', async () => {
const { account } = await setup()

Expand All @@ -54,7 +57,6 @@ describe('execute', () => {
}),
],
delegate: true,
executor: null,
})

expect(
Expand Down Expand Up @@ -94,7 +96,6 @@ describe('execute', () => {
to: account.address,
}),
],
executor: null,
})

expect(
Expand Down Expand Up @@ -177,14 +178,60 @@ describe('execute', () => {
sign: undefined,
})
})

test.skip('counterfactual: true, key: P256, keysToAuthorize: [P256], executor: JSON-RPC', async () => {
const { account } = await setup()

const key = Key.createP256({
role: 'admin',
})

await Delegation.execute(client, {
account,
calls: [
Call.authorize({
key,
to: account.address,
}),
],
delegate: true,
})

const nextKey = Key.createP256({
role: 'admin',
})

await Delegation.execute(client, {
account,
calls: [
Call.authorize({
key: nextKey,
to: account.address,
}),
],
key,
})

expect(
await Delegation.keyAt(client, {
account,
index: 1,
}),
).toEqual({
...nextKey,
sign: undefined,
})
})
})

describe('arbitrary calls', () => {
test('counterfactual: false, key: EOA, executor: EOA', async () => {
const { account, privateKey } = await setup()

const eoa = privateKeyToAccount(privateKey)

const authorization = await signAuthorization(client, {
account: privateKeyToAccount(privateKey),
account: eoa,
contractAddress: state.delegation,
delegate: true,
})
Expand All @@ -208,11 +255,12 @@ describe('execute', () => {
expect(balances_before[2]).toEqual(Value.fromEther('0'))

await Delegation.execute(client, {
account: privateKeyToAccount(privateKey),
account: eoa,
calls: [
{ to: alice.address, value: Value.fromEther('1') },
{ to: bob.address, value: Value.fromEther('0.5') },
],
executor: eoa,
})

const balances_after = await Promise.all([
Expand All @@ -231,7 +279,7 @@ describe('execute', () => {
})

describe('prepareExecute', () => {
describe('authorize call', () => {
describe('authorize', () => {
test('counterfactual: true, key: EOA, keysToAuthorize: [P256], executor: JSON-RPC', async () => {
const { account } = await setup()

Expand All @@ -250,12 +298,11 @@ describe('prepareExecute', () => {
}),
],
delegate: true,
executor: null,
},
)

const signatures = await Promise.all(
signPayloads.map((payload) => account.sign({ payload })),
signPayloads.map((payload) => account.keys[0].sign({ payload })),
)

await Delegation.execute(client, {
Expand Down Expand Up @@ -302,12 +349,11 @@ describe('prepareExecute', () => {
to: account.address,
}),
],
executor: null,
},
)

const signatures = await Promise.all(
signPayloads.map((payload) => account.sign({ payload })),
signPayloads.map((payload) => account.keys[0].sign({ payload })),
)

await Delegation.execute(client, {
Expand Down Expand Up @@ -349,7 +395,7 @@ describe('prepareExecute', () => {
)

const signatures = await Promise.all(
signPayloads.map((payload) => account.sign({ payload })),
signPayloads.map((payload) => account.keys[0].sign({ payload })),
)

await Delegation.execute(client, {
Expand Down
Loading

0 comments on commit fd88399

Please sign in to comment.