-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrun-evm-client-smoke.mjs
More file actions
executable file
·262 lines (239 loc) · 8.27 KB
/
Copy pathrun-evm-client-smoke.mjs
File metadata and controls
executable file
·262 lines (239 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env node
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const skillDir = path.resolve(scriptDir, '..');
const repoRoot = path.resolve(skillDir, '../../..');
const evmClientPackage = path.join(repoRoot, 'packages/guardian-evm-client');
const clientDist = path.join(evmClientPackage, 'dist/index.js');
const workspaceRequire = createRequire(pathToFileURL(clientDist));
const viemEntry = workspaceRequire.resolve('viem');
const viemAccountsEntry = workspaceRequire.resolve('viem/accounts');
const {
GuardianEvmClient,
normalizeEvmAddress,
signProposalHash,
} = await import(pathToFileURL(clientDist).href);
const {
createPublicClient,
http,
keccak256,
parseAbi,
stringToHex,
} = await import(pathToFileURL(viemEntry).href);
const { privateKeyToAccount } = await import(pathToFileURL(viemAccountsEntry).href);
const validatorAbi = parseAbi([
'function getSignerCount(address account) view returns (uint256)',
'function threshold(address account) view returns (uint64)',
]);
const accountAbi = parseAbi([
'function isModuleInstalled(uint256 moduleTypeId, address module, bytes additionalContext) view returns (bool)',
]);
const entrypointAbi = parseAbi([
'function getNonce(address sender, uint192 key) view returns (uint256)',
]);
const defaultEntryPointAddress = '0x433709009b8330fda32311df1c2afa402ed8d009';
const guardianUrl = envValue('GUARDIAN_URL', 'http://127.0.0.1:3000');
const rpcUrl = envValue('EVM_RPC_URL', 'http://127.0.0.1:8545');
const chainId = envNumber('EVM_CHAIN_ID', 31337);
const smartAccountAddress = normalizeEvmAddress(requiredEnv('EVM_ACCOUNT_ADDRESS'));
const validatorAddress = normalizeEvmAddress(requiredEnv('EVM_VALIDATOR_ADDRESS'));
const entrypointAddress = normalizeEvmAddress(
envValue('EVM_ENTRYPOINT_ADDRESS', defaultEntryPointAddress)
);
const signerOneKey = envValue(
'EVM_SIGNER_ONE_PRIVATE_KEY',
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80'
);
const signerTwoKey = envValue(
'EVM_SIGNER_TWO_PRIVATE_KEY',
'0x59c6995e998f97a5a0044966f094538d6f72b1d7c9d767f50fe9dc23c64fe4'
);
const signerOneAccount = privateKeyToAccount(signerOneKey);
const signerTwoAccount = privateKeyToAccount(signerTwoKey);
const signerOne = normalizeEvmAddress(signerOneAccount.address);
const signerTwo = normalizeEvmAddress(signerTwoAccount.address);
const userOpHash = envValue(
'EVM_USER_OP_HASH',
keccak256(stringToHex(`guardian-evm-smoke:${Date.now()}`))
);
const payload = envValue(
'EVM_OPAQUE_PAYLOAD',
JSON.stringify({ kind: 'userOperation', smoke: true, userOpHash })
);
const nonce = envValue('EVM_NONCE', '0');
const publicClient = createPublicClient({ transport: http(rpcUrl) });
const rpcProvider = jsonRpcProvider(rpcUrl);
const chainIdFromRpc = await rpcProvider.request({ method: 'eth_chainId' });
const rpcChainId = Number.parseInt(String(chainIdFromRpc), 16);
assert(rpcChainId === chainId, `RPC chain ID ${rpcChainId} does not match EVM_CHAIN_ID ${chainId}`);
const installed = await publicClient.readContract({
address: smartAccountAddress,
abi: accountAbi,
functionName: 'isModuleInstalled',
args: [1n, validatorAddress, '0x'],
});
assert(installed === true, 'validator is not installed on the smoke smart account');
const signerCount = await publicClient.readContract({
address: validatorAddress,
abi: validatorAbi,
functionName: 'getSignerCount',
args: [smartAccountAddress],
});
const threshold = await publicClient.readContract({
address: validatorAddress,
abi: validatorAbi,
functionName: 'threshold',
args: [smartAccountAddress],
});
const entrypointNonce = await publicClient.readContract({
address: entrypointAddress,
abi: entrypointAbi,
functionName: 'getNonce',
args: [smartAccountAddress, 0n],
});
assert(Number(signerCount) >= 2, `validator signer count is ${signerCount}`);
assert(Number(threshold) <= Number(signerCount), `threshold ${threshold} exceeds signer count ${signerCount}`);
assert(entrypointNonce === 0n, `entrypoint nonce is ${entrypointNonce}`);
const clientOne = new GuardianEvmClient({
guardianUrl,
provider: localWalletProvider(rpcProvider, signerOneAccount),
signerAddress: signerOne,
});
const clientTwo = new GuardianEvmClient({
guardianUrl,
provider: localWalletProvider(rpcProvider, signerTwoAccount),
signerAddress: signerTwo,
});
await clientOne.login();
await clientTwo.login();
await clientOne.configure({
chainId,
smartAccountAddress,
multisigValidatorAddress: validatorAddress,
});
const accountId = clientOne.accountId(chainId, smartAccountAddress);
const initialSignature = await signProposalHash(clientOne.provider, signerOne, userOpHash);
const created = await clientOne.createProposal({
accountId,
chainId,
smartAccountAddress,
userOpHash,
payload,
nonce,
signature: initialSignature,
ttlSeconds: 900,
});
const listed = await clientOne.listProposals(accountId);
assert(
listed.some((proposal) => proposal.proposalId === created.proposalId),
'created proposal was not returned by listProposals'
);
const secondSignature = await signProposalHash(clientTwo.provider, signerTwo, userOpHash);
await clientTwo.approveProposal(accountId, created.proposalId, {
signature: secondSignature,
});
const fetched = await clientOne.getProposal(accountId, created.proposalId);
assert(
fetched.signatures.length >= 2,
`expected at least 2 signatures, got ${fetched.signatures.length}`
);
const executable = await clientOne.getExecutableProposal(accountId, created.proposalId);
assert(executable.hash === userOpHash, 'executable hash mismatch');
assert(executable.payload === payload, 'executable payload mismatch');
writeSummary({
guardianUrl,
rpcUrl,
chainId,
smartAccountAddress,
validatorAddress,
entrypointAddress,
signerOne,
signerTwo,
signerCount: signerCount.toString(),
threshold: threshold.toString(),
accountId,
proposalId: created.proposalId,
signatureCount: fetched.signatures.length,
executableSigners: executable.signers,
});
function localWalletProvider(rpcProvider, account) {
return {
async request({ method, params = [] }) {
if (method === 'eth_requestAccounts') {
return [account.address];
}
if (method === 'eth_signTypedData_v4') {
const typedData = JSON.parse(String(params[1]));
return account.signTypedData({
domain: typedData.domain,
types: { GuardianEvmSession: typedData.types.GuardianEvmSession },
primaryType: typedData.primaryType,
message: typedData.message,
});
}
if (method === 'eth_sign') {
return account.sign({ hash: String(params[1]) });
}
return rpcProvider.request({ method, params });
},
};
}
function jsonRpcProvider(url) {
let id = 0;
return {
async request({ method, params = [] }) {
id += 1;
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id, method, params }),
});
const text = await response.text();
let body;
try {
body = JSON.parse(text);
} catch {
throw new Error(`RPC ${method} returned non-JSON response: ${text}`);
}
if (!response.ok) {
throw new Error(`RPC ${method} HTTP ${response.status}: ${text}`);
}
if (body.error) {
throw new Error(`RPC ${method} error ${body.error.code ?? ''}: ${body.error.message}`);
}
return body.result;
},
};
}
function envValue(name, fallback) {
const value = process.env[name];
return value && value.length > 0 ? value : fallback;
}
function requiredEnv(name) {
const value = process.env[name];
if (!value || value.length === 0) {
throw new Error(`${name} is required`);
}
return value;
}
function envNumber(name, fallback) {
const value = process.env[name];
if (!value || value.length === 0) {
return fallback;
}
const parsed = Number(value);
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
throw new Error(`${name} must be a positive safe integer`);
}
return parsed;
}
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
function writeSummary(summary) {
console.log(JSON.stringify(summary, null, 2));
}