Skip to content

feat(smart-wallet-sdk): add 712 hashTypedData #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sdks/smart-wallet-sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export const DELEGATION_MAGIC_PREFIX = '0xef0100';
*/
export const SELF_CALL_TARGET = "0x0000000000000000000000000000000000000000"

/**
* EIP-712 domain name and version
*/
export const DOMAIN_NAME = "Uniswap Minimal Delegation"
export const DOMAIN_VERSION = "1"

/**
* Call types for smart wallet calls
* Follows ERC-7579
Expand Down
8 changes: 8 additions & 0 deletions sdks/smart-wallet-sdk/src/utils/callPlanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('CallPlanner', () => {
})
})

describe('hashTypedData', () => {
it('should hash the calls using EIP-712', () => {
const planner = new CallPlanner([{ to: TEST_ADDRESS_1, data: TEST_DATA_1, value: TEST_VALUE_1 }])
const hash = planner.hashTypedData(TEST_ADDRESS_1, 1)
expect(hash).toBeDefined()
})
})

describe('add', () => {
it('should add a new call to the calls array', () => {
const planner = new CallPlanner()
Expand Down
36 changes: 35 additions & 1 deletion sdks/smart-wallet-sdk/src/utils/callPlanner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { encodeAbiParameters } from 'viem'
import { encodeAbiParameters, hashTypedData } from 'viem'

import { DOMAIN_NAME, DOMAIN_VERSION } from '../constants'
import { Call } from '../types'

// Define the ABI parameter type for the call tuple
Expand All @@ -14,6 +15,17 @@ const CALL_ABI_PARAMS = [
}
] as const

const TYPES = {
Call: [
{ name: 'to', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'data', type: 'bytes' }
],
Execute: [
{ name: 'calls', type: 'Call[]' }
]
}

/**
* CallPlanner is used to encode a series Calls
*/
Expand Down Expand Up @@ -54,6 +66,28 @@ export class CallPlanner {
return encodeAbiParameters(CALL_ABI_PARAMS, [this.calls])
}

/**
* Hash the calls using EIP-712
* @param verifyingContract The verifying contract (should be the signer's account address)
* @param chainId The chain ID
* @returns The hash of the calls
*/
hashTypedData(verifyingContract: `0x${string}`, chainId: number): `0x${string}` {
return hashTypedData({
domain: {
name: DOMAIN_NAME,
version: DOMAIN_VERSION,
chainId,
verifyingContract
},
types: TYPES,
primaryType: 'Execute',
message: {
calls : this.calls
}
})
}

/**
* Add a command to execute a call
* @param to The target address of the call
Expand Down
Loading