Skip to content

Commit ea2654d

Browse files
authored
wallet_sign in playground (#145)
1 parent d2c9a49 commit ea2654d

File tree

5 files changed

+354
-62
lines changed

5 files changed

+354
-62
lines changed

examples/testapp/src/components/RpcMethods/RpcMethodCard.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,42 @@ export function RpcMethodCard({ format, method, params, shortcuts }) {
7676
(shortcut) => Number(shortcut.data.chainId) === hexToNumber(chainId)
7777
)?.data.chain ?? mainnet;
7878

79+
if (method.includes('wallet_sign')) {
80+
const type = data.type || (data.request as any).type;
81+
const walletSignData = data.data || (data.request as any).data;
82+
let result: string | null = null;
83+
if (type === '0x01') {
84+
result = await verifySignMsg({
85+
method: 'eth_signTypedData_v4',
86+
from: data.address?.toLowerCase(),
87+
sign: response,
88+
message: walletSignData,
89+
chain: chain as Chain,
90+
});
91+
}
92+
if (type === '0x45') {
93+
result = await verifySignMsg({
94+
method: 'personal_sign',
95+
from: data.address?.toLowerCase(),
96+
sign: response,
97+
message: walletSignData.message,
98+
chain: chain as Chain,
99+
});
100+
}
101+
if (result) {
102+
setVerifyResult(result);
103+
return;
104+
}
105+
}
106+
79107
const verifyResult = await verifySignMsg({
80108
method,
81109
from: data.address?.toLowerCase(),
82110
sign: response,
83111
message: data.message,
84112
chain: chain as Chain,
85113
});
114+
86115
if (verifyResult) {
87116
setVerifyResult(verifyResult);
88117
return;
@@ -118,7 +147,7 @@ export function RpcMethodCard({ format, method, params, shortcuts }) {
118147
}
119148
try {
120149
const response = (await provider.request({
121-
method,
150+
method: method.split('#')[0], // so we can use # to add a description in method name
122151
params: values,
123152
// biome-ignore lint/suspicious/noExplicitAny: old code, refactor soon
124153
})) as any;

examples/testapp/src/components/RpcMethods/method/ephemeralMethods.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseMessage } from '../shortcut/ShortcutType';
12
import { RpcRequestInput } from './RpcRequestInput';
23

34
const walletSendCallsEphemeral: RpcRequestInput = {
@@ -16,12 +17,48 @@ const walletSendCallsEphemeral: RpcRequestInput = {
1617
],
1718
};
1819

19-
const walletSignEphemeral: RpcRequestInput = {
20-
method: 'wallet_sign',
21-
params: [{ key: 'message', required: true }],
20+
const walletSignOldSpecEphemeral: RpcRequestInput = {
21+
method: 'wallet_sign#old',
22+
params: [
23+
{ key: 'version', required: true },
24+
{ key: 'type', required: true },
25+
{ key: 'address', required: false },
26+
{ key: 'data', required: true },
27+
{ key: 'capabilities', required: false },
28+
],
29+
format: (data: Record<string, string>) => [
30+
{
31+
version: data.version,
32+
type: data.type,
33+
address: data.address,
34+
data: parseMessage(data.data),
35+
capabilities: data.capabilities,
36+
},
37+
],
38+
};
39+
40+
const walletSignNewSpecEphemeral: RpcRequestInput = {
41+
method: 'wallet_sign#new',
42+
params: [
43+
{ key: 'version', required: true },
44+
{ key: 'request', required: true },
45+
{ key: 'address', required: false },
46+
{ key: 'capabilities', required: false },
47+
{ key: 'mutableData', required: false },
48+
],
2249
format: (data: Record<string, string>) => [
23-
`0x${Buffer.from(data.message, 'utf8').toString('hex')}`,
50+
{
51+
version: data.version,
52+
request: parseMessage(data.request),
53+
address: data.address,
54+
mutableData: data.mutableData,
55+
capabilities: data.capabilities,
56+
},
2457
],
2558
};
2659

27-
export const ephemeralMethods = [walletSendCallsEphemeral, walletSignEphemeral];
60+
export const ephemeralMethods = [
61+
walletSendCallsEphemeral,
62+
walletSignOldSpecEphemeral,
63+
walletSignNewSpecEphemeral,
64+
];

examples/testapp/src/components/RpcMethods/method/signMessageMethods.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Chain, createPublicClient, http, TypedDataDomain } from 'viem';
1+
import { Chain, TypedDataDomain, createPublicClient, http } from 'viem';
22

33
import { parseMessage } from '../shortcut/ShortcutType';
44
import { RpcRequestInput } from './RpcRequestInput';
@@ -54,12 +54,54 @@ const ethSignTypedDataV4: RpcRequestInput = {
5454
format: (data: Record<string, string>) => [data.address, parseMessage(data.message)],
5555
};
5656

57+
const walletSignOldSpec: RpcRequestInput = {
58+
method: 'wallet_sign#old',
59+
params: [
60+
{ key: 'version', required: true },
61+
{ key: 'type', required: true },
62+
{ key: 'address', required: false },
63+
{ key: 'data', required: true },
64+
{ key: 'capabilities', required: false },
65+
],
66+
format: (data: Record<string, string>) => [
67+
{
68+
version: data.version,
69+
type: data.type,
70+
address: data.address,
71+
data: parseMessage(data.data),
72+
capabilities: data.capabilities,
73+
},
74+
],
75+
};
76+
77+
const walletSignNewSpec: RpcRequestInput = {
78+
method: 'wallet_sign#new',
79+
params: [
80+
{ key: 'version', required: true },
81+
{ key: 'request', required: true },
82+
{ key: 'address', required: false },
83+
{ key: 'capabilities', required: false },
84+
{ key: 'mutableData', required: false },
85+
],
86+
format: (data: Record<string, string>) => [
87+
{
88+
version: data.version,
89+
request: parseMessage(data.request),
90+
address: data.address,
91+
mutableData: data.mutableData,
92+
capabilities: data.capabilities,
93+
},
94+
],
95+
};
96+
5797
export const signMessageMethods = [
5898
ethSign,
5999
personalSign,
60100
ethSignTypedDataV1,
61101
ethSignTypedDataV3,
62102
ethSignTypedDataV4,
103+
walletSignOldSpec,
104+
walletSignNewSpec,
63105
];
64106

65107
export const verifySignMsg = async ({
Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
import { ShortcutType } from './ShortcutType';
22

3+
const PLACEHOLDER_ADDRESS = '0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
4+
5+
const BASE_PAY_DATA = {
6+
domain: {
7+
chainId: 8453,
8+
name: 'USDC',
9+
verifyingContract: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
10+
version: '2',
11+
},
12+
message: {
13+
from: PLACEHOLDER_ADDRESS,
14+
nonce: '0xbda37619d3004dba4ac2491022bc82b7df64c2f68e8a349422c71983a80d16ca',
15+
to: '0xbc4c0191af73c4953b54f21ae0c74b31fc6cb21b',
16+
validAfter: '0',
17+
validBefore: '1914749767655',
18+
value: '10000',
19+
},
20+
primaryType: 'ReceiveWithAuthorization',
21+
types: {
22+
ReceiveWithAuthorization: [
23+
{
24+
name: 'from',
25+
type: 'address',
26+
},
27+
{
28+
name: 'to',
29+
type: 'address',
30+
},
31+
{
32+
name: 'value',
33+
type: 'uint256',
34+
},
35+
{
36+
name: 'validAfter',
37+
type: 'uint256',
38+
},
39+
{
40+
name: 'validBefore',
41+
type: 'uint256',
42+
},
43+
{
44+
name: 'nonce',
45+
type: 'bytes32',
46+
},
47+
],
48+
},
49+
};
50+
351
const walletSendCallsEphemeralShortcuts: ShortcutType[] = [
452
{
553
key: 'wallet_sendCalls',
@@ -11,16 +59,32 @@ const walletSendCallsEphemeralShortcuts: ShortcutType[] = [
1159
},
1260
];
1361

14-
const walletSignEphemeralShortcuts: ShortcutType[] = [
62+
const walletSignOldSpecEphemeralShortcuts: ShortcutType[] = [
63+
{
64+
key: 'Base Pay',
65+
data: {
66+
version: '1.0',
67+
type: '0x01',
68+
data: BASE_PAY_DATA,
69+
},
70+
},
71+
];
72+
73+
const walletSignNewSpecEphemeralShortcuts: ShortcutType[] = [
1574
{
16-
key: 'wallet_sign',
75+
key: 'Base Pay',
1776
data: {
18-
message: 'Hello, world!',
77+
version: '1.0',
78+
request: {
79+
type: '0x01',
80+
data: BASE_PAY_DATA,
81+
},
1982
},
2083
},
2184
];
2285

2386
export const ephemeralMethodShortcutsMap = {
2487
wallet_sendCalls: walletSendCallsEphemeralShortcuts,
25-
wallet_sign: walletSignEphemeralShortcuts,
88+
['wallet_sign#old']: walletSignOldSpecEphemeralShortcuts,
89+
['wallet_sign#new']: walletSignNewSpecEphemeralShortcuts,
2690
};

0 commit comments

Comments
 (0)