-
-
Notifications
You must be signed in to change notification settings - Fork 636
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
434 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[submodule "contracts/lib/forge-std"] | ||
path = contracts/lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "test/invoker"] | ||
path = test/invoker | ||
url = git@github.com:wevm/invoker.git | ||
[submodule "test/invokers"] | ||
path = test/invokers | ||
url = git@github.com:anton-rs/3074-invokers.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
export const invokerAbi = [ | ||
{ | ||
type: 'function', | ||
name: 'execute', | ||
inputs: [ | ||
{ | ||
name: 'execData', | ||
type: 'bytes', | ||
internalType: 'bytes', | ||
}, | ||
{ | ||
name: 'authority', | ||
type: 'address', | ||
internalType: 'address', | ||
}, | ||
{ | ||
name: 'signature', | ||
type: 'tuple', | ||
internalType: 'struct Auth.Signature', | ||
components: [ | ||
{ | ||
name: 'yParity', | ||
type: 'uint8', | ||
internalType: 'uint8', | ||
}, | ||
{ | ||
name: 'r', | ||
type: 'bytes32', | ||
internalType: 'bytes32', | ||
}, | ||
{ | ||
name: 's', | ||
type: 'bytes32', | ||
internalType: 'bytes32', | ||
}, | ||
], | ||
}, | ||
], | ||
outputs: [], | ||
stateMutability: 'payable', | ||
}, | ||
{ | ||
type: 'function', | ||
name: 'getDigest', | ||
inputs: [ | ||
{ | ||
name: 'execData', | ||
type: 'bytes', | ||
internalType: 'bytes', | ||
}, | ||
{ | ||
name: 'nonce', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: 'digest', | ||
type: 'bytes32', | ||
internalType: 'bytes32', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
}, | ||
{ | ||
type: 'function', | ||
name: 'nonce', | ||
inputs: [ | ||
{ | ||
name: '', | ||
type: 'address', | ||
internalType: 'address', | ||
}, | ||
], | ||
outputs: [ | ||
{ | ||
name: '', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
], | ||
stateMutability: 'view', | ||
}, | ||
{ | ||
type: 'error', | ||
name: 'BadAuth', | ||
inputs: [], | ||
}, | ||
{ | ||
type: 'error', | ||
name: 'InvalidNonce', | ||
inputs: [ | ||
{ | ||
name: 'authority', | ||
type: 'address', | ||
internalType: 'address', | ||
}, | ||
{ | ||
name: 'attempted', | ||
type: 'uint256', | ||
internalType: 'uint256', | ||
}, | ||
], | ||
}, | ||
] as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/experimental/eip3074/invokers/coders/batchInvokerCoder.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { expect, test } from 'vitest' | ||
import { BatchInvoker } from '../../../../../test/contracts/generated.js' | ||
import { anvil3074 } from '../../../../../test/src/anvil.js' | ||
import { deploy } from '../../../../../test/src/utils.js' | ||
import { parseEther } from '../../../../utils/index.js' | ||
import { batchInvokerCoder } from './batchInvokerCoder.js' | ||
|
||
const client = anvil3074.getClient() | ||
|
||
test('default', async () => { | ||
const { contractAddress: invokerAddress } = await deploy(client, { | ||
abi: BatchInvoker.abi, | ||
bytecode: BatchInvoker.bytecode.object, | ||
}) | ||
|
||
const coder = batchInvokerCoder() | ||
expect( | ||
await coder.toExecData( | ||
[ | ||
{ | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
data: '0xdeadbeef', | ||
value: parseEther('1'), | ||
}, | ||
{ to: '0xd2135CfB216b74109775236E36d4b433F1DF507B' }, | ||
{ | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
value: parseEther('1'), | ||
}, | ||
{ | ||
to: '0xd2135CfB216b74109775236E36d4b433F1DF507B', | ||
data: '0xdeadbeef', | ||
}, | ||
], | ||
{ | ||
authority: '0x0000000000000000000000000000000000000000', | ||
client, | ||
invokerAddress: invokerAddress!, | ||
}, | ||
), | ||
).toMatchInlineSnapshot( | ||
`"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000015c02d2135cfb216b74109775236e36d4b433f1df507b0000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000004deadbeef02d2135cfb216b74109775236e36d4b433f1df507b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002d2135cfb216b74109775236e36d4b433f1df507b0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000000002d2135cfb216b74109775236e36d4b433f1df507b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004deadbeef00000000"`, | ||
) | ||
}) |
46 changes: 46 additions & 0 deletions
46
src/experimental/eip3074/invokers/coders/batchInvokerCoder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { Address } from 'abitype' | ||
import { readContract } from '../../../../actions/public/readContract.js' | ||
import type { Hex } from '../../../../types/misc.js' | ||
import { encodeAbiParameters } from '../../../../utils/abi/encodeAbiParameters.js' | ||
import { encodePacked } from '../../../../utils/abi/encodePacked.js' | ||
import { concat } from '../../../../utils/data/concat.js' | ||
import { size } from '../../../../utils/data/size.js' | ||
import { invokerAbi } from '../../constants/abis.js' | ||
import { defineInvokerCoder } from './defineInvokerCoder.js' | ||
|
||
type Calls = readonly { | ||
to: Address | ||
data?: Hex | undefined | ||
value?: bigint | undefined | ||
}[] | ||
|
||
export type BatchInvokerArgs = Calls | ||
|
||
export function batchInvokerCoder() { | ||
return defineInvokerCoder({ | ||
async toExecData( | ||
args: BatchInvokerArgs, | ||
{ authority, client, invokerAddress }, | ||
) { | ||
const nonce = await readContract(client, { | ||
abi: invokerAbi, | ||
address: invokerAddress, | ||
functionName: 'nonce', | ||
args: [authority], | ||
}) | ||
|
||
const calls = concat( | ||
args.map(({ data = '0x', to, value = 0n }) => | ||
encodePacked( | ||
['uint8', 'address', 'uint256', 'uint256', 'bytes'], | ||
[2, to, value, BigInt(size(data)), data], | ||
), | ||
), | ||
) | ||
return encodeAbiParameters( | ||
[{ type: 'uint256' }, { type: 'bytes' }], | ||
[nonce, calls], | ||
) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
src/experimental/eip3074/invokers/coders/defineInvokerCoder.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test } from 'vitest' | ||
import { anvil3074 } from '../../../../../test/src/anvil.js' | ||
import { encodePacked } from '../../../../utils/index.js' | ||
import { defineInvokerCoder } from './defineInvokerCoder.js' | ||
|
||
const client = anvil3074.getClient() | ||
|
||
test('default', () => { | ||
const coder = defineInvokerCoder({ | ||
async toExecData(args: string) { | ||
return encodePacked(['string'], [args]) | ||
}, | ||
}) | ||
expect( | ||
coder.toExecData('hello world', { | ||
authority: '0x0000000000000000000000000000000000000000', | ||
client, | ||
invokerAddress: '0x0000000000000000000000000000000000000000', | ||
}), | ||
).toMatchInlineSnapshot(`"0x68656c6c6f20776f726c64"`) | ||
}) |
20 changes: 20 additions & 0 deletions
20
src/experimental/eip3074/invokers/coders/defineInvokerCoder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { Address } from 'abitype' | ||
import type { Client } from '../../../../clients/createClient.js' | ||
import type { Hex } from '../../../../types/misc.js' | ||
|
||
export type InvokerCoder<args = unknown> = { | ||
toExecData: ( | ||
args: args, | ||
options: { authority: Address; client: Client; invokerAddress: Hex }, | ||
) => Promise<Hex> | ||
} | ||
|
||
export type DefineInvokerCoderParameters<args = unknown> = InvokerCoder<args> | ||
|
||
export type DefineInvokerCoderReturnType<args = unknown> = InvokerCoder<args> | ||
|
||
export function defineInvokerCoder<args>( | ||
parameters: DefineInvokerCoderParameters<args>, | ||
): DefineInvokerCoderReturnType<args> { | ||
return parameters | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect, test } from 'vitest' | ||
import { anvil3074 } from '../../../../test/src/anvil.js' | ||
import { batchInvokerCoder } from './coders/batchInvokerCoder.js' | ||
import { getInvoker } from './getInvoker.js' | ||
|
||
const client = anvil3074.getClient({ account: true }) | ||
|
||
test('default', async () => { | ||
// Initialize an invoker with it's contract address & `args` coder. | ||
const invoker = getInvoker({ | ||
address: '0x0D44f617435088c947F00B31160f64b074e412B4', | ||
client, | ||
coder: batchInvokerCoder(), | ||
}) | ||
expect(invoker).toMatchInlineSnapshot(` | ||
{ | ||
"address": "0x0D44f617435088c947F00B31160f64b074e412B4", | ||
"execute": [Function], | ||
"sign": [Function], | ||
} | ||
`) | ||
}) |
Oops, something went wrong.