@deserialize/multi-vm-wallet 1.2.293 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/SMART_WALLET_GUIDE.md +746 -0
  3. package/SMART_WALLET_IMPLEMENTATION.md +460 -0
  4. package/dist/IChainWallet.d.ts +4 -3
  5. package/dist/IChainWallet.js +5 -0
  6. package/dist/IChainWallet.js.map +1 -1
  7. package/dist/constant.js +17 -4
  8. package/dist/constant.js.map +1 -1
  9. package/dist/evm/SMART_WALLET_EXAMPLES.d.ts +20 -0
  10. package/dist/evm/SMART_WALLET_EXAMPLES.js +451 -0
  11. package/dist/evm/SMART_WALLET_EXAMPLES.js.map +1 -0
  12. package/dist/evm/aa-service/index.d.ts +16 -0
  13. package/dist/evm/aa-service/index.js +69 -0
  14. package/dist/evm/aa-service/index.js.map +1 -0
  15. package/dist/evm/aa-service/lib/account-adapter.d.ts +26 -0
  16. package/dist/evm/aa-service/lib/account-adapter.js +53 -0
  17. package/dist/evm/aa-service/lib/account-adapter.js.map +1 -0
  18. package/dist/evm/aa-service/lib/kernel-account.d.ts +91 -0
  19. package/dist/evm/aa-service/lib/kernel-account.js +251 -0
  20. package/dist/evm/aa-service/lib/kernel-account.js.map +1 -0
  21. package/dist/evm/aa-service/lib/kernel-modules.d.ts +240 -0
  22. package/dist/evm/aa-service/lib/kernel-modules.js +409 -0
  23. package/dist/evm/aa-service/lib/kernel-modules.js.map +1 -0
  24. package/dist/evm/aa-service/lib/session-keys.d.ts +170 -0
  25. package/dist/evm/aa-service/lib/session-keys.js +297 -0
  26. package/dist/evm/aa-service/lib/session-keys.js.map +1 -0
  27. package/dist/evm/aa-service/lib/type.d.ts +167 -0
  28. package/dist/evm/aa-service/lib/type.js +43 -0
  29. package/dist/evm/aa-service/lib/type.js.map +1 -0
  30. package/dist/evm/aa-service/services/account-abstraction.d.ts +614 -0
  31. package/dist/evm/aa-service/services/account-abstraction.js +754 -0
  32. package/dist/evm/aa-service/services/account-abstraction.js.map +1 -0
  33. package/dist/evm/aa-service/services/bundler.d.ts +29 -0
  34. package/dist/evm/aa-service/services/bundler.js +168 -0
  35. package/dist/evm/aa-service/services/bundler.js.map +1 -0
  36. package/dist/evm/evm.d.ts +68 -3
  37. package/dist/evm/evm.js +223 -8
  38. package/dist/evm/evm.js.map +1 -1
  39. package/dist/evm/index.d.ts +1 -0
  40. package/dist/evm/index.js +3 -0
  41. package/dist/evm/index.js.map +1 -1
  42. package/dist/evm/smartWallet.d.ts +265 -0
  43. package/dist/evm/smartWallet.js +675 -0
  44. package/dist/evm/smartWallet.js.map +1 -0
  45. package/dist/evm/smartWallet.types.d.ts +10 -0
  46. package/dist/evm/smartWallet.types.js +16 -0
  47. package/dist/evm/smartWallet.types.js.map +1 -0
  48. package/dist/evm/transaction.utils.d.ts +10 -10
  49. package/dist/evm/transaction.utils.js +12 -8
  50. package/dist/evm/transaction.utils.js.map +1 -1
  51. package/dist/evm/transactionParsing.js +123 -27
  52. package/dist/evm/transactionParsing.js.map +1 -1
  53. package/dist/evm/utils.d.ts +12 -1
  54. package/dist/evm/utils.js +138 -2
  55. package/dist/evm/utils.js.map +1 -1
  56. package/dist/helpers/index.d.ts +4 -1
  57. package/dist/helpers/index.js +25 -0
  58. package/dist/helpers/index.js.map +1 -1
  59. package/dist/helpers/routeScan.d.ts +191 -0
  60. package/dist/helpers/routeScan.js +114 -0
  61. package/dist/helpers/routeScan.js.map +1 -0
  62. package/dist/index.d.ts +0 -2
  63. package/dist/index.js +0 -2
  64. package/dist/index.js.map +1 -1
  65. package/dist/svm/svm.d.ts +6 -4
  66. package/dist/svm/svm.js +33 -19
  67. package/dist/svm/svm.js.map +1 -1
  68. package/dist/svm/transactionSender.js +2 -2
  69. package/dist/svm/transactionSender.js.map +1 -1
  70. package/dist/svm/utils.d.ts +20 -4
  71. package/dist/svm/utils.js +232 -12
  72. package/dist/svm/utils.js.map +1 -1
  73. package/dist/test.d.ts +1 -6
  74. package/dist/test.js +47 -16
  75. package/dist/test.js.map +1 -1
  76. package/dist/types.d.ts +169 -2
  77. package/dist/types.js.map +1 -1
  78. package/dist/vm.js +9 -7
  79. package/dist/vm.js.map +1 -1
  80. package/package.json +2 -2
  81. package/tsconfig.json +4 -3
  82. package/utils/IChainWallet.ts +4 -3
  83. package/utils/constant.ts +18 -4
  84. package/utils/evm/SMART_WALLET_EXAMPLES.ts.bak +591 -0
  85. package/utils/evm/aa-service/index.ts +85 -0
  86. package/utils/evm/aa-service/lib/account-adapter.ts +60 -0
  87. package/utils/evm/aa-service/lib/kernel-account.ts +367 -0
  88. package/utils/evm/aa-service/lib/kernel-modules.ts +598 -0
  89. package/utils/evm/aa-service/lib/session-keys.ts +389 -0
  90. package/utils/evm/aa-service/lib/type.ts +236 -0
  91. package/utils/evm/aa-service/services/account-abstraction.ts +1015 -0
  92. package/utils/evm/aa-service/services/bundler.ts +217 -0
  93. package/utils/evm/evm.ts +281 -13
  94. package/utils/evm/index.ts +5 -1
  95. package/utils/evm/smartWallet.ts +797 -0
  96. package/utils/evm/smartWallet.types.ts +33 -0
  97. package/utils/evm/transaction.utils.ts +12 -10
  98. package/utils/evm/transactionParsing.ts +153 -63
  99. package/utils/evm/utils.ts +161 -2
  100. package/utils/helpers/index.ts +13 -1
  101. package/utils/helpers/routeScan.ts +397 -0
  102. package/utils/index.ts +0 -2
  103. package/utils/svm/svm.ts +61 -14
  104. package/utils/svm/utils.ts +317 -14
  105. package/utils/test.ts +54 -18
  106. package/utils/types.ts +223 -2
@@ -0,0 +1,591 @@
1
+ /**
2
+ * Smart Wallet Usage Examples
3
+ *
4
+ * Comprehensive examples demonstrating all smart wallet features
5
+ */
6
+
7
+ import { EVMChainWallet } from "./evm";
8
+ import { parseEther, parseUnits, encodeFunctionData, parseAbi } from "viem";
9
+ import { ChainWalletConfig } from "../types";
10
+
11
+ // ============================================
12
+ // Example 1: Basic Smart Wallet Setup
13
+ // ============================================
14
+
15
+ async function example1_BasicSetup() {
16
+ console.log("\n=== Example 1: Basic Setup ===\n");
17
+
18
+ // Create regular EVM wallet
19
+ const config: ChainWalletConfig = {
20
+ name: "Sepolia",
21
+ chainId: 11155111,
22
+ rpcUrl: "https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
23
+ explorerUrl: "https://sepolia.etherscan.io",
24
+ nativeToken: {
25
+ name: "Ethereum",
26
+ symbol: "ETH",
27
+ decimals: 18,
28
+ address: "native"
29
+ }
30
+ };
31
+
32
+ const privateKey = "0x..."; // Your private key
33
+ const evmWallet = new EVMChainWallet(config, privateKey, 0);
34
+
35
+ console.log("EOA Address:", evmWallet.address);
36
+
37
+ // Extend with smart wallet capabilities
38
+ const smartWallet = await evmWallet.extend({
39
+ bundlerProvider: "pimlico",
40
+ apiKey: process.env.PIMLICO_API_KEY!
41
+ });
42
+
43
+ console.log("Smart Wallet Address:", smartWallet.getAddress());
44
+
45
+ // Get account info
46
+ const info = await smartWallet.getAccountInfo();
47
+ console.log("Account Info:", {
48
+ address: info.address,
49
+ owner: info.ownerAddress,
50
+ chain: info.chain.name,
51
+ delegated: info.isDelegated,
52
+ balance: info.balance.toString()
53
+ });
54
+ }
55
+
56
+ // ============================================
57
+ // Example 2: Single Transaction
58
+ // ============================================
59
+
60
+ async function example2_SingleTransaction(smartWallet: any) {
61
+ console.log("\n=== Example 2: Single Transaction ===\n");
62
+
63
+ const recipient = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
64
+ const amount = parseEther("0.01");
65
+
66
+ const result = await smartWallet.sendTransaction(recipient, amount);
67
+
68
+ if (result.success) {
69
+ console.log("āœ… Transaction successful!");
70
+ console.log("UserOp Hash:", result.userOpHash);
71
+ console.log("Transaction Hash:", result.transactionHash);
72
+ } else {
73
+ console.error("āŒ Transaction failed:", result.error);
74
+ }
75
+ }
76
+
77
+ // ============================================
78
+ // Example 3: Batch Transactions
79
+ // ============================================
80
+
81
+ async function example3_BatchTransactions(smartWallet: any) {
82
+ console.log("\n=== Example 3: Batch Transactions ===\n");
83
+
84
+ // Send ETH to multiple recipients in ONE transaction
85
+ const recipients = [
86
+ "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
87
+ "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
88
+ "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2"
89
+ ];
90
+
91
+ const calls = recipients.map((recipient, i) =>
92
+ smartWallet.prepareCall(
93
+ recipient,
94
+ parseEther(`0.0${i + 1}`) // 0.01, 0.02, 0.03 ETH
95
+ )
96
+ );
97
+
98
+ console.log(`Sending to ${recipients.length} recipients in one transaction...`);
99
+
100
+ const result = await smartWallet.sendBatchTransaction(calls);
101
+
102
+ if (result.success) {
103
+ console.log("āœ… Batch transaction successful!");
104
+ console.log("Transaction Hash:", result.transactionHash);
105
+ console.log("šŸ’° Gas saved: ~80% compared to individual transactions");
106
+ } else {
107
+ console.error("āŒ Batch transaction failed:", result.error);
108
+ }
109
+ }
110
+
111
+ // ============================================
112
+ // Example 4: Batch with Contract Interactions
113
+ // ============================================
114
+
115
+ async function example4_BatchWithContracts(smartWallet: any) {
116
+ console.log("\n=== Example 4: Batch with Contracts ===\n");
117
+
118
+ const USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"; // Sepolia USDC
119
+ const recipient = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
120
+
121
+ // Prepare USDC transfer calldata
122
+ const transferCalldata = encodeFunctionData({
123
+ abi: parseAbi(["function transfer(address to, uint256 amount)"]),
124
+ functionName: "transfer",
125
+ args: [recipient, parseUnits("10", 6)] // 10 USDC
126
+ });
127
+
128
+ // Batch: Send ETH + Send USDC in one transaction
129
+ const calls = [
130
+ smartWallet.prepareCall(recipient, parseEther("0.01")),
131
+ smartWallet.prepareCall(USDC_ADDRESS, 0n, transferCalldata)
132
+ ];
133
+
134
+ console.log("Sending ETH + USDC in one transaction...");
135
+
136
+ const result = await smartWallet.sendBatchTransaction(calls);
137
+
138
+ if (result.success) {
139
+ console.log("āœ… Batch successful!");
140
+ console.log("Sent 0.01 ETH + 10 USDC in one transaction");
141
+ }
142
+ }
143
+
144
+ // ============================================
145
+ // Example 5: Session Keys - Complete Workflow
146
+ // ============================================
147
+
148
+ async function example5_SessionKeys(smartWallet: any) {
149
+ console.log("\n=== Example 5: Session Keys ===\n");
150
+
151
+ // STEP 1: Agent generates session key
152
+ console.log("Step 1: Agent generates session key");
153
+ const sessionKey = await smartWallet.generateSessionKey();
154
+
155
+ console.log("Session Key Address:", sessionKey.address);
156
+ console.log("Session Key Private Key:", sessionKey.privateKey);
157
+ console.log("āš ļø Store private key securely!");
158
+
159
+ // STEP 2: Owner approves session key with permissions
160
+ console.log("\nStep 2: Owner approves session key");
161
+
162
+ const USDC_ADDRESS = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238";
163
+
164
+ const approval = await smartWallet.approveSessionKey({
165
+ sessionKeyAddress: sessionKey.address,
166
+ permissions: [
167
+ // Max 100 USDC per transfer
168
+ smartWallet.createUSDCPermission(USDC_ADDRESS, "100"),
169
+ // Max 0.1 ETH per transfer
170
+ smartWallet.createETHPermission("0.1")
171
+ ]
172
+ });
173
+
174
+ console.log("āœ… Session key approved!");
175
+ console.log("Approval string length:", approval.length);
176
+ console.log("Share this approval with the agent →", approval.substring(0, 50) + "...");
177
+
178
+ // STEP 3: Agent uses session key
179
+ console.log("\nStep 3: Agent uses session key");
180
+
181
+ // Recreate session key from private key
182
+ const recreatedKey = await smartWallet.recreateSessionKey(sessionKey.privateKey);
183
+
184
+ // Use the session key
185
+ await smartWallet.useSessionKey({
186
+ approval,
187
+ sessionKeySigner: recreatedKey.signer
188
+ });
189
+
190
+ console.log("āœ… Now using session key");
191
+ console.log("Session key active:", smartWallet.isUsingSessionKey());
192
+
193
+ // Agent can now send transactions within limits
194
+ const recipient = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
195
+
196
+ const result = await smartWallet.sendTransaction(
197
+ recipient,
198
+ parseEther("0.05") // Within 0.1 ETH limit
199
+ );
200
+
201
+ if (result.success) {
202
+ console.log("āœ… Transaction sent with session key!");
203
+ }
204
+
205
+ // STEP 4: Clear session key
206
+ smartWallet.clearSessionKey();
207
+ console.log("\nSession key cleared:", !smartWallet.isUsingSessionKey());
208
+ }
209
+
210
+ // ============================================
211
+ // Example 6: Session Keys - Sudo Mode
212
+ // ============================================
213
+
214
+ async function example6_SessionKeysSudo(smartWallet: any) {
215
+ console.log("\n=== Example 6: Session Keys (Sudo Mode) ===\n");
216
+
217
+ const sessionKey = await smartWallet.generateSessionKey();
218
+
219
+ // Approve with unrestricted sudo access
220
+ // āš ļø Only use for testing or trusted agents!
221
+ const approval = await smartWallet.approveSessionKey({
222
+ sessionKeyAddress: sessionKey.address,
223
+ useSudoPolicy: true // Unrestricted access
224
+ });
225
+
226
+ console.log("āœ… Session key approved with SUDO access");
227
+ console.log("āš ļø This session key has UNRESTRICTED access!");
228
+
229
+ await smartWallet.useSessionKey({
230
+ approval,
231
+ sessionKeySigner: sessionKey.signer
232
+ });
233
+
234
+ console.log("Session key can now perform any transaction");
235
+ }
236
+
237
+ // ============================================
238
+ // Example 7: Module Management
239
+ // ============================================
240
+
241
+ async function example7_ModuleManagement(smartWallet: any) {
242
+ console.log("\n=== Example 7: Module Management ===\n");
243
+
244
+ const validatorAddress = "0x..."; // Your validator module address
245
+
246
+ // Check if module is installed
247
+ const isInstalled = await smartWallet.isModuleInstalled(
248
+ "validator",
249
+ validatorAddress
250
+ );
251
+
252
+ console.log("Module installed:", isInstalled);
253
+
254
+ if (!isInstalled) {
255
+ // Install module
256
+ console.log("Installing module...");
257
+
258
+ const result = await smartWallet.installModule({
259
+ moduleType: "validator",
260
+ moduleAddress: validatorAddress,
261
+ initData: "0x" // Module-specific init data
262
+ });
263
+
264
+ if (result.success) {
265
+ console.log("āœ… Module installed!");
266
+ }
267
+ } else {
268
+ // Uninstall module
269
+ console.log("Uninstalling module...");
270
+
271
+ const result = await smartWallet.uninstallModule({
272
+ moduleType: "validator",
273
+ moduleAddress: validatorAddress
274
+ });
275
+
276
+ if (result.success) {
277
+ console.log("āœ… Module uninstalled!");
278
+ }
279
+ }
280
+ }
281
+
282
+ // ============================================
283
+ // Example 8: Batch Module Operations
284
+ // ============================================
285
+
286
+ async function example8_BatchModules(smartWallet: any) {
287
+ console.log("\n=== Example 8: Batch Module Operations ===\n");
288
+
289
+ const validator1 = "0x...";
290
+ const validator2 = "0x...";
291
+ const hook1 = "0x...";
292
+
293
+ // Install multiple modules in one transaction
294
+ const calls = [
295
+ smartWallet.prepareInstallModule({
296
+ moduleType: "validator",
297
+ moduleAddress: validator1
298
+ }),
299
+ smartWallet.prepareInstallModule({
300
+ moduleType: "validator",
301
+ moduleAddress: validator2
302
+ }),
303
+ smartWallet.prepareInstallModule({
304
+ moduleType: "hook",
305
+ moduleAddress: hook1
306
+ })
307
+ ];
308
+
309
+ console.log("Installing 3 modules in one transaction...");
310
+
311
+ const result = await smartWallet.sendBatchTransaction(calls);
312
+
313
+ if (result.success) {
314
+ console.log("āœ… All modules installed!");
315
+ }
316
+ }
317
+
318
+ // ============================================
319
+ // Example 9: Multi-Signature
320
+ // ============================================
321
+
322
+ async function example9_MultiSignature(smartWallet: any) {
323
+ console.log("\n=== Example 9: Multi-Signature ===\n");
324
+
325
+ const owner1 = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
326
+ const owner2 = "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4";
327
+ const owner3 = "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2";
328
+
329
+ // Enable 2-of-3 multi-sig
330
+ const result = await smartWallet.enableMultiSig({
331
+ owners: [owner1, owner2, owner3],
332
+ threshold: 2 // Require 2 signatures
333
+ });
334
+
335
+ if (result.success) {
336
+ console.log("āœ… Multi-sig enabled!");
337
+ console.log("Threshold: 2 of 3 owners required");
338
+ }
339
+ }
340
+
341
+ // ============================================
342
+ // Example 10: Gas Sponsorship (Paymaster)
343
+ // ============================================
344
+
345
+ async function example10_Paymaster(smartWallet: any) {
346
+ console.log("\n=== Example 10: Gas Sponsorship ===\n");
347
+
348
+ const paymasterUrl = "https://api.pimlico.io/v2/sepolia/paymaster";
349
+
350
+ // Enable paymaster
351
+ smartWallet.setPaymaster(paymasterUrl);
352
+
353
+ console.log("āœ… Paymaster enabled");
354
+ console.log("Has paymaster:", smartWallet.hasPaymaster());
355
+
356
+ // User doesn't pay gas!
357
+ const result = await smartWallet.sendTransaction(
358
+ "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
359
+ parseEther("0.01")
360
+ );
361
+
362
+ if (result.success) {
363
+ console.log("āœ… Transaction sponsored! User paid $0 in gas");
364
+ }
365
+
366
+ // Disable paymaster
367
+ smartWallet.clearPaymaster();
368
+ console.log("Paymaster disabled");
369
+ }
370
+
371
+ // ============================================
372
+ // Example 11: Check Account Status
373
+ // ============================================
374
+
375
+ async function example11_AccountStatus(smartWallet: any) {
376
+ console.log("\n=== Example 11: Account Status ===\n");
377
+
378
+ // Get full account info
379
+ const info = await smartWallet.getAccountInfo();
380
+
381
+ console.log("Account Information:");
382
+ console.log(" Address:", info.address);
383
+ console.log(" Owner:", info.ownerAddress);
384
+ console.log(" Chain:", info.chain.name, `(${info.chain.id})`);
385
+ console.log(" EntryPoint:", info.entryPointVersion);
386
+ console.log(" Delegated:", info.isDelegated);
387
+ console.log(" Balance:", info.balance.toString(), "wei");
388
+
389
+ // Get balance
390
+ const balance = await smartWallet.getBalance();
391
+ console.log("\nBalance:", balance.formatted, "ETH");
392
+
393
+ // Check delegation
394
+ const isDelegated = await smartWallet.isAccountDelegated();
395
+ console.log("Account delegated:", isDelegated);
396
+
397
+ // Get bundler info
398
+ const bundlerInfo = smartWallet.getBundlerInfo();
399
+ console.log("\nBundler:");
400
+ console.log(" Provider:", bundlerInfo.provider);
401
+ console.log(" URL:", bundlerInfo.url);
402
+
403
+ // Check session key status
404
+ console.log("\nSession key active:", smartWallet.isUsingSessionKey());
405
+
406
+ // Check paymaster status
407
+ console.log("Paymaster enabled:", smartWallet.hasPaymaster());
408
+ }
409
+
410
+ // ============================================
411
+ // Example 12: Dual Mode (EOA + Smart Wallet)
412
+ // ============================================
413
+
414
+ async function example12_DualMode() {
415
+ console.log("\n=== Example 12: Dual Mode ===\n");
416
+
417
+ const config: ChainWalletConfig = {
418
+ name: "Sepolia",
419
+ chainId: 11155111,
420
+ rpcUrl: "https://sepolia.infura.io/v3/YOUR_INFURA_KEY",
421
+ explorerUrl: "https://sepolia.etherscan.io",
422
+ nativeToken: {
423
+ name: "Ethereum",
424
+ symbol: "ETH",
425
+ decimals: 18,
426
+ address: "native"
427
+ }
428
+ };
429
+
430
+ const privateKey = "0x...";
431
+ const evmWallet = new EVMChainWallet(config, privateKey, 0);
432
+
433
+ // Enable smart wallet
434
+ const smartWallet = await evmWallet.extend({
435
+ bundlerProvider: "pimlico",
436
+ apiKey: process.env.PIMLICO_API_KEY!
437
+ });
438
+
439
+ console.log("EOA Address:", evmWallet.address);
440
+ console.log("Smart Wallet Address:", smartWallet.getAddress());
441
+
442
+ // Both modes work simultaneously!
443
+
444
+ // Use regular EOA for simple transfers
445
+ console.log("\n1. Using EOA (regular transaction):");
446
+ await evmWallet.transferNative("0x...", 0.01);
447
+ console.log("āœ… EOA transaction sent");
448
+
449
+ // Use smart wallet for batch operations
450
+ console.log("\n2. Using Smart Wallet (batched UserOp):");
451
+ const calls = [
452
+ smartWallet.prepareCall("0x...", parseEther("0.01")),
453
+ smartWallet.prepareCall("0x...", parseEther("0.02"))
454
+ ];
455
+ await smartWallet.sendBatchTransaction(calls);
456
+ console.log("āœ… Smart wallet batch sent");
457
+
458
+ // Use regular EOA for swaps
459
+ console.log("\n3. Using EOA (swap):");
460
+ await evmWallet.swap(
461
+ { address: "0xTokenIn", name: "TokenIn", symbol: "TKN", decimals: 18 },
462
+ "0xTokenOut",
463
+ 1
464
+ );
465
+ console.log("āœ… EOA swap executed");
466
+
467
+ console.log("\n✨ Both EOA and Smart Wallet work together!");
468
+ }
469
+
470
+ // ============================================
471
+ // Example 13: Error Handling
472
+ // ============================================
473
+
474
+ async function example13_ErrorHandling(smartWallet: any) {
475
+ console.log("\n=== Example 13: Error Handling ===\n");
476
+
477
+ try {
478
+ const result = await smartWallet.sendTransaction(
479
+ "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
480
+ parseEther("1000") // Insufficient balance
481
+ );
482
+
483
+ if (result.success) {
484
+ console.log("āœ… Success:", result.transactionHash);
485
+ } else {
486
+ console.log("āŒ Transaction failed:", result.error);
487
+
488
+ // Handle specific errors
489
+ if (result.error?.includes("insufficient funds")) {
490
+ console.log("→ Account needs more ETH");
491
+ } else if (result.error?.includes("AA23")) {
492
+ console.log("→ UserOperation validation failed");
493
+ } else if (result.error?.includes("AA21")) {
494
+ console.log("→ UserOperation expired");
495
+ }
496
+ }
497
+ } catch (error: any) {
498
+ console.error("Unexpected error:", error.message);
499
+ }
500
+ }
501
+
502
+ // ============================================
503
+ // Example 14: Automated Trading Bot
504
+ // ============================================
505
+
506
+ async function example14_TradingBot(smartWallet: any) {
507
+ console.log("\n=== Example 14: Automated Trading Bot ===\n");
508
+
509
+ // Generate session key for bot
510
+ const sessionKey = await smartWallet.generateSessionKey();
511
+
512
+ console.log("Bot session key:", sessionKey.address);
513
+
514
+ // Owner approves with trading limits
515
+ const approval = await smartWallet.approveSessionKey({
516
+ sessionKeyAddress: sessionKey.address,
517
+ permissions: [
518
+ smartWallet.createUSDCPermission(
519
+ "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238",
520
+ "1000" // Max 1000 USDC per trade
521
+ ),
522
+ smartWallet.createETHPermission("1.0") // Max 1 ETH per trade
523
+ ]
524
+ });
525
+
526
+ console.log("āœ… Trading bot approved with limits");
527
+
528
+ // Bot uses session key
529
+ const botKey = await smartWallet.recreateSessionKey(sessionKey.privateKey);
530
+ await smartWallet.useSessionKey({
531
+ approval,
532
+ sessionKeySigner: botKey.signer
533
+ });
534
+
535
+ // Bot executes trades
536
+ console.log("\nšŸ¤– Bot executing automated trades...");
537
+
538
+ const dexAddress = "0x..."; // DEX router address
539
+ const swapCalldata = "0x..."; // Swap transaction data
540
+
541
+ const result = await smartWallet.sendTransaction(dexAddress, 0n, swapCalldata);
542
+
543
+ if (result.success) {
544
+ console.log("āœ… Bot trade executed:", result.transactionHash);
545
+ }
546
+
547
+ console.log("\nšŸ’” Bot can trade within limits without owner approval!");
548
+ }
549
+
550
+ // ============================================
551
+ // Main Runner
552
+ // ============================================
553
+
554
+ async function runAllExamples() {
555
+ console.log("šŸš€ Smart Wallet Examples\n");
556
+
557
+ try {
558
+ // await example1_BasicSetup();
559
+
560
+ // Note: Uncomment individual examples to run them
561
+ // Make sure to provide valid private keys and API keys
562
+
563
+ console.log("\nāœ… All examples completed!");
564
+ console.log("\nTo run an example:");
565
+ console.log("1. Uncomment the example in runAllExamples()");
566
+ console.log("2. Set your private key and API key");
567
+ console.log("3. Run: npx tsx SMART_WALLET_EXAMPLES.ts");
568
+ } catch (error) {
569
+ console.error("Error:", error);
570
+ }
571
+ }
572
+
573
+ // Uncomment to run
574
+ // runAllExamples();
575
+
576
+ export {
577
+ example1_BasicSetup,
578
+ example2_SingleTransaction,
579
+ example3_BatchTransactions,
580
+ example4_BatchWithContracts,
581
+ example5_SessionKeys,
582
+ example6_SessionKeysSudo,
583
+ example7_ModuleManagement,
584
+ example8_BatchModules,
585
+ example9_MultiSignature,
586
+ example10_Paymaster,
587
+ example11_AccountStatus,
588
+ example12_DualMode,
589
+ example13_ErrorHandling,
590
+ example14_TradingBot
591
+ };
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Main Export File
3
+ *
4
+ * Easy imports for using the Account Abstraction services
5
+ */
6
+
7
+ // Singleton Service (Primary Interface)
8
+ export { AccountAbstractionService } from './services/account-abstraction';
9
+ export type {
10
+ AAServiceConfig,
11
+ CreateAccountOptions,
12
+ SendTransactionOptions,
13
+ SendBatchTransactionOptions,
14
+ WaitForReceiptOptions
15
+ } from './services/account-abstraction';
16
+
17
+ export * from './lib/type'
18
+
19
+ // Bundler Services (Advanced Usage)
20
+ export {
21
+ BundlerManager,
22
+ createBundlerManager,
23
+ createBundlerService
24
+ } from './services/bundler';
25
+ export type { BundlerConfig, BundlerService } from './services/bundler';
26
+
27
+ // Kernel Account Utilities (Advanced Usage)
28
+ export {
29
+ createKernel7702Account,
30
+ createKernelAuthorization,
31
+ sendKernelTransaction,
32
+ sendBatchTransaction,
33
+ waitForKernelReceipt,
34
+ sendSponsoredBatchTransaction
35
+ } from './lib/kernel-account';
36
+
37
+
38
+ // Module Management (ERC-7579)
39
+ export {
40
+ // Prepare functions (for batching)
41
+ prepareInstallModule,
42
+ prepareUninstallModule,
43
+ prepareInstallSessionKey,
44
+ prepareRevokeSessionKey,
45
+ prepareInstallMultiSigValidator,
46
+ // Execute functions (immediate execution)
47
+ installModule,
48
+ uninstallModule,
49
+ isModuleInstalled,
50
+ createSessionKey,
51
+ revokeSessionKey,
52
+ installMultiSigValidator,
53
+ MODULE_TYPE_IDS
54
+ } from './lib/kernel-modules';
55
+ export type {
56
+ ModuleType,
57
+ InstallModuleConfig,
58
+ UninstallModuleConfig,
59
+ ModuleStatus,
60
+ SessionKeyPermission,
61
+ CreateSessionKeyConfig
62
+ } from './lib/kernel-modules';
63
+
64
+ // EIP-7702 Session Keys (NEW Pattern)
65
+ export {
66
+ generateSessionKey,
67
+ recreateSessionKey,
68
+ createSessionKeyApproval,
69
+ deserializeSessionKey,
70
+ createSessionKeyClient,
71
+ createUSDCTransferPermission,
72
+ createETHTransferPermission
73
+ } from './lib/session-keys';
74
+ export type {
75
+ SessionKeyInfo,
76
+ SessionKeyPermissionRule,
77
+ CreateSessionKeyApprovalOptions,
78
+ DeserializedSessionKey
79
+ } from './lib/session-keys';
80
+
81
+ // Account Adapters (ZeroDev Integration)
82
+ export {
83
+ adaptZeroDevAccount,
84
+ adaptSessionKeyAccount
85
+ } from './lib/account-adapter';