@deserialize/multi-vm-wallet 1.4.12 → 1.5.1

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 (191) hide show
  1. package/.claude/settings.local.json +7 -1
  2. package/BUILD_OPTIMIZATION_PLAN.md +640 -0
  3. package/BUILD_RESULTS.md +282 -0
  4. package/BUN_MIGRATION.md +415 -0
  5. package/CHANGELOG_SECURITY.md +573 -0
  6. package/IMPLEMENTATION_SUMMARY.md +494 -0
  7. package/SECURITY_AUDIT.md +1124 -0
  8. package/bun.lock +553 -0
  9. package/dist/IChainWallet.js +0 -5
  10. package/dist/bip32Old.js +0 -885
  11. package/dist/bip32Small.js +0 -79
  12. package/dist/bipTest.js +0 -362
  13. package/dist/constant.js +0 -17
  14. package/dist/english.js +0 -1
  15. package/dist/evm/aa-service/index.d.ts +0 -5
  16. package/dist/evm/aa-service/index.js +0 -14
  17. package/dist/evm/aa-service/lib/account-adapter.d.ts +0 -22
  18. package/dist/evm/aa-service/lib/account-adapter.js +0 -24
  19. package/dist/evm/aa-service/lib/kernel-account.d.ts +0 -30
  20. package/dist/evm/aa-service/lib/kernel-account.js +2 -67
  21. package/dist/evm/aa-service/lib/kernel-modules.d.ts +0 -177
  22. package/dist/evm/aa-service/lib/kernel-modules.js +4 -202
  23. package/dist/evm/aa-service/lib/session-keys.d.ts +0 -118
  24. package/dist/evm/aa-service/lib/session-keys.js +7 -151
  25. package/dist/evm/aa-service/lib/type.d.ts +0 -55
  26. package/dist/evm/aa-service/lib/type.js +0 -10
  27. package/dist/evm/aa-service/services/account-abstraction.d.ts +0 -426
  28. package/dist/evm/aa-service/services/account-abstraction.js +0 -461
  29. package/dist/evm/aa-service/services/bundler.d.ts +0 -6
  30. package/dist/evm/aa-service/services/bundler.js +0 -54
  31. package/dist/evm/evm.d.ts +10 -67
  32. package/dist/evm/evm.js +339 -102
  33. package/dist/evm/index.js +0 -3
  34. package/dist/evm/script.js +3 -17
  35. package/dist/evm/smartWallet.d.ts +0 -173
  36. package/dist/evm/smartWallet.js +0 -206
  37. package/dist/evm/smartWallet.types.d.ts +0 -6
  38. package/dist/evm/smartWallet.types.js +0 -8
  39. package/dist/evm/transaction.utils.d.ts +0 -242
  40. package/dist/evm/transaction.utils.js +4 -320
  41. package/dist/evm/transactionParsing.d.ts +0 -11
  42. package/dist/evm/transactionParsing.js +28 -147
  43. package/dist/evm/utils.d.ts +0 -46
  44. package/dist/evm/utils.js +1 -57
  45. package/dist/helpers/index.d.ts +0 -4
  46. package/dist/helpers/index.js +8 -44
  47. package/dist/helpers/routeScan.js +0 -1
  48. package/dist/index.js +0 -1
  49. package/dist/old.js +0 -884
  50. package/dist/price.js +0 -1
  51. package/dist/price.types.js +0 -2
  52. package/dist/rate-limiter.d.ts +28 -0
  53. package/dist/rate-limiter.js +95 -0
  54. package/dist/retry-logic.d.ts +14 -0
  55. package/dist/retry-logic.js +120 -0
  56. package/dist/savings/index.d.ts +1 -0
  57. package/dist/savings/index.js +16 -2
  58. package/dist/savings/saving-manager.d.ts +46 -0
  59. package/dist/savings/saving-manager.js +176 -0
  60. package/dist/savings/savings-operations.d.ts +39 -0
  61. package/dist/savings/savings-operations.js +141 -0
  62. package/dist/savings/smart-savings.d.ts +0 -63
  63. package/dist/savings/smart-savings.js +0 -78
  64. package/dist/savings/types.d.ts +0 -69
  65. package/dist/savings/types.js +0 -7
  66. package/dist/savings/validation.d.ts +9 -0
  67. package/dist/savings/validation.js +85 -0
  68. package/dist/svm/constant.js +0 -1
  69. package/dist/svm/index.js +0 -1
  70. package/dist/svm/svm.d.ts +7 -13
  71. package/dist/svm/svm.js +262 -46
  72. package/dist/svm/transactionParsing.d.ts +0 -7
  73. package/dist/svm/transactionParsing.js +3 -41
  74. package/dist/svm/transactionSender.js +0 -9
  75. package/dist/svm/utils.d.ts +0 -12
  76. package/dist/svm/utils.js +9 -60
  77. package/dist/test.d.ts +0 -4
  78. package/dist/test.js +15 -95
  79. package/dist/transaction-utils.d.ts +38 -0
  80. package/dist/transaction-utils.js +168 -0
  81. package/dist/types.d.ts +36 -0
  82. package/dist/types.js +0 -1
  83. package/dist/utils.js +0 -1
  84. package/dist/vm-validation.d.ts +11 -0
  85. package/dist/vm-validation.js +151 -0
  86. package/dist/vm.d.ts +14 -16
  87. package/dist/vm.js +64 -53
  88. package/dist/walletBip32.d.ts +2 -0
  89. package/dist/walletBip32.js +31 -66
  90. package/package.json +9 -4
  91. package/test-discovery.ts +235 -0
  92. package/test-pocket-discovery.ts +84 -0
  93. package/tsconfig.json +18 -11
  94. package/tsconfig.prod.json +10 -0
  95. package/utils/IChainWallet.ts +2 -0
  96. package/utils/evm/evm.ts +560 -39
  97. package/utils/rate-limiter.ts +179 -0
  98. package/utils/retry-logic.ts +271 -0
  99. package/utils/savings/EXAMPLES.md +883 -0
  100. package/utils/savings/SECURITY.md +731 -0
  101. package/utils/savings/index.ts +1 -1
  102. package/utils/savings/saving-manager.ts +656 -0
  103. package/utils/savings/savings-operations.ts +509 -0
  104. package/utils/savings/validation.ts +187 -0
  105. package/utils/svm/svm.ts +467 -20
  106. package/utils/test.ts +26 -3
  107. package/utils/transaction-utils.ts +394 -0
  108. package/utils/types.ts +100 -0
  109. package/utils/vm-validation.ts +280 -0
  110. package/utils/vm.ts +202 -24
  111. package/utils/walletBip32.ts +63 -3
  112. package/dist/IChainWallet.js.map +0 -1
  113. package/dist/bip32.d.ts +0 -9
  114. package/dist/bip32.js +0 -172
  115. package/dist/bip32.js.map +0 -1
  116. package/dist/bip32Old.js.map +0 -1
  117. package/dist/bip32Small.js.map +0 -1
  118. package/dist/bipTest.js.map +0 -1
  119. package/dist/constant.js.map +0 -1
  120. package/dist/english.js.map +0 -1
  121. package/dist/evm/SMART_WALLET_EXAMPLES.d.ts +0 -20
  122. package/dist/evm/SMART_WALLET_EXAMPLES.js +0 -451
  123. package/dist/evm/SMART_WALLET_EXAMPLES.js.map +0 -1
  124. package/dist/evm/aa-service/index.js.map +0 -1
  125. package/dist/evm/aa-service/lib/account-adapter.js.map +0 -1
  126. package/dist/evm/aa-service/lib/kernel-account.js.map +0 -1
  127. package/dist/evm/aa-service/lib/kernel-modules.js.map +0 -1
  128. package/dist/evm/aa-service/lib/session-keys.js.map +0 -1
  129. package/dist/evm/aa-service/lib/type.js.map +0 -1
  130. package/dist/evm/aa-service/services/account-abstraction.js.map +0 -1
  131. package/dist/evm/aa-service/services/bundler.js.map +0 -1
  132. package/dist/evm/evm.js.map +0 -1
  133. package/dist/evm/index.js.map +0 -1
  134. package/dist/evm/script.js.map +0 -1
  135. package/dist/evm/smartWallet.js.map +0 -1
  136. package/dist/evm/smartWallet.types.js.map +0 -1
  137. package/dist/evm/transaction.utils.js.map +0 -1
  138. package/dist/evm/transactionParsing.js.map +0 -1
  139. package/dist/evm/utils.js.map +0 -1
  140. package/dist/helpers/index.js.map +0 -1
  141. package/dist/helpers/routeScan.js.map +0 -1
  142. package/dist/index.js.map +0 -1
  143. package/dist/old.js.map +0 -1
  144. package/dist/price.js.map +0 -1
  145. package/dist/price.types.js.map +0 -1
  146. package/dist/privacy/artifact-manager.d.ts +0 -117
  147. package/dist/privacy/artifact-manager.js +0 -251
  148. package/dist/privacy/artifact-manager.js.map +0 -1
  149. package/dist/privacy/broadcaster-client.d.ts +0 -166
  150. package/dist/privacy/broadcaster-client.js +0 -261
  151. package/dist/privacy/broadcaster-client.js.map +0 -1
  152. package/dist/privacy/index.d.ts +0 -34
  153. package/dist/privacy/index.js +0 -56
  154. package/dist/privacy/index.js.map +0 -1
  155. package/dist/privacy/network-config.d.ts +0 -57
  156. package/dist/privacy/network-config.js +0 -118
  157. package/dist/privacy/network-config.js.map +0 -1
  158. package/dist/privacy/poi-helper.d.ts +0 -161
  159. package/dist/privacy/poi-helper.js +0 -249
  160. package/dist/privacy/poi-helper.js.map +0 -1
  161. package/dist/privacy/railgun-engine.d.ts +0 -135
  162. package/dist/privacy/railgun-engine.js +0 -205
  163. package/dist/privacy/railgun-engine.js.map +0 -1
  164. package/dist/privacy/railgun-privacy-wallet.d.ts +0 -288
  165. package/dist/privacy/railgun-privacy-wallet.js +0 -539
  166. package/dist/privacy/railgun-privacy-wallet.js.map +0 -1
  167. package/dist/privacy/types.d.ts +0 -229
  168. package/dist/privacy/types.js +0 -26
  169. package/dist/privacy/types.js.map +0 -1
  170. package/dist/savings/index.js.map +0 -1
  171. package/dist/savings/saving-actions.d.ts +0 -0
  172. package/dist/savings/saving-actions.js +0 -78
  173. package/dist/savings/saving-actions.js.map +0 -1
  174. package/dist/savings/savings-manager.d.ts +0 -126
  175. package/dist/savings/savings-manager.js +0 -234
  176. package/dist/savings/savings-manager.js.map +0 -1
  177. package/dist/savings/smart-savings.js.map +0 -1
  178. package/dist/savings/types.js.map +0 -1
  179. package/dist/svm/constant.js.map +0 -1
  180. package/dist/svm/index.js.map +0 -1
  181. package/dist/svm/svm.js.map +0 -1
  182. package/dist/svm/transactionParsing.js.map +0 -1
  183. package/dist/svm/transactionSender.js.map +0 -1
  184. package/dist/svm/utils.js.map +0 -1
  185. package/dist/test.js.map +0 -1
  186. package/dist/types.js.map +0 -1
  187. package/dist/utils.js.map +0 -1
  188. package/dist/vm.js.map +0 -1
  189. package/dist/walletBip32.js.map +0 -1
  190. package/utils/savings/saving-actions.ts +0 -92
  191. package/utils/savings/savings-manager.ts +0 -271
package/dist/evm/evm.js CHANGED
@@ -1,14 +1,10 @@
1
1
  "use strict";
2
- /**
3
- *
4
- * @param phrase this is the pass phrase for this vm
5
- * this is a class that will be responsible for creating several evm wallets code
6
- */
7
2
  Object.defineProperty(exports, "__esModule", { value: true });
8
3
  exports.EVMChainWallet = exports.EVMVM = void 0;
9
4
  const walletBip32_1 = require("../walletBip32");
10
5
  const IChainWallet_1 = require("../IChainWallet");
11
6
  const vm_1 = require("../vm");
7
+ const vm_validation_1 = require("../vm-validation");
12
8
  const ethers_1 = require("ethers");
13
9
  const utils_1 = require("./utils");
14
10
  const transactionParsing_1 = require("./transactionParsing");
@@ -17,78 +13,383 @@ const smartWallet_1 = require("./smartWallet");
17
13
  const price_1 = require("../price");
18
14
  const accounts_1 = require("viem/accounts");
19
15
  class EVMVM extends vm_1.VM {
20
- derivationPath = "m/44'/60'/0'/0/"; // Default EVM derivation path
16
+ derivationPath = "m/44'/60'/0'/0/";
21
17
  constructor(seed) {
22
18
  super(seed, "EVM");
23
19
  }
24
20
  getTokenInfo = utils_1.getTokenInfo;
25
21
  static getTokenInfo = utils_1.getTokenInfo;
26
22
  generatePrivateKey(index, seed, mnemonic, derivationPath = this.derivationPath) {
23
+ vm_validation_1.VMValidation.validateIndex(index, 'Wallet index');
27
24
  let _seed;
28
25
  if (seed) {
26
+ vm_validation_1.VMValidation.validateSeed(seed);
29
27
  _seed = seed;
30
28
  }
31
29
  else if (mnemonic) {
30
+ vm_validation_1.VMValidation.validateMnemonic(mnemonic);
32
31
  _seed = vm_1.VM.mnemonicToSeed(mnemonic);
33
32
  }
34
33
  else {
34
+ this.checkNotDisposed();
35
35
  _seed = this.seed;
36
36
  }
37
37
  const privateKey = (0, walletBip32_1.EVMDeriveChildPrivateKey)(_seed, index, derivationPath).privateKey;
38
38
  return { privateKey, index };
39
39
  }
40
- /**
41
- * Derive a savings account using BIP-44 account index
42
- *
43
- * Uses the pattern: m/44'/60'/accountIndex'/0/0
44
- * - Main wallet: m/44'/60'/0'/0/0 (account index 0)
45
- * - Savings 1: m/44'/60'/1'/0/0 (account index 1)
46
- * - Savings 2: m/44'/60'/2'/0/0 (account index 2)
47
- *
48
- * @param accountIndex - The BIP-44 account index (0 for main, 1+ for savings)
49
- * @returns Object containing privateKey, address, and derivation path
50
- */
51
- deriveSavingsAccount(accountIndex) {
52
- // Use fixed address index 0 for savings accounts
53
- // Vary only the account index (3rd position in BIP-44)
54
- const derivationPath = `m/44'/60'/${accountIndex}'/0/0`;
55
- // Derive the full path directly (no additional index appended)
56
- const result = (0, walletBip32_1.EVMDeriveChildPrivateKey)(this.seed, 0, `m/44'/60'/${accountIndex}'/0/`);
57
- const privateKey = result.privateKey;
58
- // Derive address from private key
59
- const wallet = new ethers_1.ethers.Wallet(privateKey);
60
- const address = wallet.address;
61
- return {
62
- privateKey,
63
- address,
64
- derivationPath
65
- };
40
+ static generateMnemonicFromPrivateKey(privateKey) {
41
+ return (0, walletBip32_1.EntropyToMnemonic)(privateKey);
66
42
  }
67
43
  static fromMnemonic(mnemonic) {
68
44
  const seed = vm_1.VM.mnemonicToSeed(mnemonic);
69
45
  return new EVMVM(seed);
70
46
  }
71
- static validateAddress(address) {
72
- return ethers_1.ethers.isAddress(address);
47
+ static validateAddress(address, requireChecksum = false) {
48
+ if (!ethers_1.ethers.isAddress(address)) {
49
+ return false;
50
+ }
51
+ if (requireChecksum && address !== address.toLowerCase() && address !== address.toUpperCase()) {
52
+ try {
53
+ const checksummed = ethers_1.ethers.getAddress(address);
54
+ return checksummed === address;
55
+ }
56
+ catch {
57
+ return false;
58
+ }
59
+ }
60
+ return true;
61
+ }
62
+ static normalizeAddress(address) {
63
+ if (!this.validateAddress(address)) {
64
+ throw new Error(`Invalid Ethereum address: ${address}`);
65
+ }
66
+ return ethers_1.ethers.getAddress(address);
73
67
  }
74
68
  static async getNativeBalance(address, connection) {
75
- // Implement native balance retrieval logic here
76
69
  return await (0, utils_1.getNativeBalance)(address, connection);
77
70
  }
78
71
  static async getTokenBalance(address, tokenAddress, connection) {
79
- // Implement token balance retrieval logic here
80
72
  return await (0, utils_1.getTokenBalance)(tokenAddress, address, connection);
81
73
  }
82
74
  static convertFromEntropyToPrivateKey = (entropy) => {
83
75
  return (0, viem_1.toHex)(entropy);
84
76
  };
77
+ async discoverWallets(connection, options) {
78
+ const startTime = Date.now();
79
+ const checkInParallel = options?.checkInParallel ?? true;
80
+ const opts = {
81
+ startIndex: options?.startIndex ?? 0,
82
+ maxIndex: options?.maxIndex ?? 100,
83
+ gapLimit: options?.gapLimit ?? 20,
84
+ minBalance: options?.minBalance ?? 0n,
85
+ includeZeroBalance: options?.includeZeroBalance ?? false,
86
+ includePrivateKeys: options?.includePrivateKeys ?? false,
87
+ checkInParallel,
88
+ batchSize: options?.batchSize ?? 10,
89
+ checkDelay: options?.checkDelay ?? (checkInParallel ? 200 : 50),
90
+ onProgress: options?.onProgress ?? (() => { }),
91
+ onDiscovered: options?.onDiscovered ?? (() => { }),
92
+ };
93
+ const discovered = [];
94
+ let gapCounter = 0;
95
+ let highestIndex = opts.startIndex;
96
+ let stoppedByGapLimit = false;
97
+ const isViem = 'readContract' in connection;
98
+ if (opts.checkInParallel) {
99
+ for (let i = opts.startIndex; i <= opts.maxIndex; i += opts.batchSize) {
100
+ if (gapCounter >= opts.gapLimit) {
101
+ stoppedByGapLimit = true;
102
+ break;
103
+ }
104
+ const batchIndices = Array.from({ length: Math.min(opts.batchSize, opts.maxIndex - i + 1) }, (_, idx) => i + idx);
105
+ const batchResults = await Promise.all(batchIndices.map(index => this.checkWalletBalance(index, connection, isViem)));
106
+ for (let j = 0; j < batchResults.length; j++) {
107
+ const result = batchResults[j];
108
+ const index = batchIndices[j];
109
+ highestIndex = index;
110
+ if (result) {
111
+ const meetsMinBalance = result.nativeBalance.amount >= opts.minBalance;
112
+ if (meetsMinBalance || opts.includeZeroBalance) {
113
+ if (!opts.includePrivateKeys) {
114
+ delete result.privateKey;
115
+ }
116
+ discovered.push(result);
117
+ opts.onDiscovered(result);
118
+ if (meetsMinBalance) {
119
+ gapCounter = 0;
120
+ }
121
+ else {
122
+ gapCounter++;
123
+ }
124
+ }
125
+ else {
126
+ gapCounter++;
127
+ }
128
+ }
129
+ else {
130
+ gapCounter++;
131
+ }
132
+ opts.onProgress(index + 1 - opts.startIndex, opts.maxIndex - opts.startIndex + 1, discovered.length);
133
+ if (gapCounter >= opts.gapLimit) {
134
+ stoppedByGapLimit = true;
135
+ break;
136
+ }
137
+ }
138
+ if (i + opts.batchSize <= opts.maxIndex && !stoppedByGapLimit) {
139
+ await this.sleep(opts.checkDelay);
140
+ }
141
+ }
142
+ }
143
+ else {
144
+ for (let index = opts.startIndex; index <= opts.maxIndex; index++) {
145
+ if (gapCounter >= opts.gapLimit) {
146
+ stoppedByGapLimit = true;
147
+ break;
148
+ }
149
+ const result = await this.checkWalletBalance(index, connection, isViem);
150
+ highestIndex = index;
151
+ if (result) {
152
+ const meetsMinBalance = result.nativeBalance.amount >= opts.minBalance;
153
+ if (meetsMinBalance || opts.includeZeroBalance) {
154
+ if (!opts.includePrivateKeys) {
155
+ delete result.privateKey;
156
+ }
157
+ discovered.push(result);
158
+ opts.onDiscovered(result);
159
+ if (meetsMinBalance) {
160
+ gapCounter = 0;
161
+ }
162
+ else {
163
+ gapCounter++;
164
+ }
165
+ }
166
+ else {
167
+ gapCounter++;
168
+ }
169
+ }
170
+ else {
171
+ gapCounter++;
172
+ }
173
+ opts.onProgress(index + 1 - opts.startIndex, opts.maxIndex - opts.startIndex + 1, discovered.length);
174
+ if (index < opts.maxIndex && !stoppedByGapLimit) {
175
+ await this.sleep(opts.checkDelay);
176
+ }
177
+ }
178
+ }
179
+ const totalBalance = discovered.reduce((sum, wallet) => sum + wallet.nativeBalance.amount, 0n);
180
+ const duration = Date.now() - startTime;
181
+ return {
182
+ discovered,
183
+ scannedIndices: highestIndex - opts.startIndex + 1,
184
+ highestIndex,
185
+ totalBalance,
186
+ stoppedByGapLimit,
187
+ duration,
188
+ };
189
+ }
190
+ async checkWalletBalance(index, connection, isViem, maxRetries = 3) {
191
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
192
+ try {
193
+ const { privateKey } = this.generatePrivateKey(index);
194
+ const wallet = new ethers_1.ethers.Wallet(privateKey);
195
+ const address = wallet.address;
196
+ let balanceWei;
197
+ if (isViem) {
198
+ const viemConnection = connection;
199
+ balanceWei = await viemConnection.getBalance({ address: address });
200
+ }
201
+ else {
202
+ const ethersConnection = connection;
203
+ balanceWei = await ethersConnection.getBalance(address);
204
+ }
205
+ const formatted = Number(ethers_1.ethers.formatEther(balanceWei));
206
+ return {
207
+ index,
208
+ address,
209
+ derivationPath: `${this.derivationPath}${index}'`,
210
+ nativeBalance: {
211
+ amount: balanceWei,
212
+ formatted,
213
+ symbol: 'ETH',
214
+ },
215
+ privateKey,
216
+ };
217
+ }
218
+ catch (error) {
219
+ if (attempt === maxRetries - 1) {
220
+ console.warn(`Failed to check wallet at index ${index} after ${maxRetries} attempts:`, error);
221
+ return null;
222
+ }
223
+ const delay = Math.pow(2, attempt) * 1000;
224
+ await this.sleep(delay);
225
+ }
226
+ }
227
+ return null;
228
+ }
229
+ sleep(ms) {
230
+ return new Promise(resolve => setTimeout(resolve, ms));
231
+ }
232
+ async discoverPockets(connection, options) {
233
+ const startTime = Date.now();
234
+ const walletIndex = options?.walletIndex ?? 0;
235
+ const checkInParallel = options?.checkInParallel ?? true;
236
+ const opts = {
237
+ startIndex: options?.startIndex ?? 0,
238
+ maxIndex: options?.maxIndex ?? 100,
239
+ gapLimit: options?.gapLimit ?? 20,
240
+ minBalance: options?.minBalance ?? 0n,
241
+ includeZeroBalance: options?.includeZeroBalance ?? false,
242
+ includePrivateKeys: options?.includePrivateKeys ?? false,
243
+ checkInParallel,
244
+ batchSize: options?.batchSize ?? 10,
245
+ checkDelay: options?.checkDelay ?? (checkInParallel ? 200 : 50),
246
+ onProgress: options?.onProgress ?? (() => { }),
247
+ onDiscovered: options?.onDiscovered ?? (() => { }),
248
+ walletIndex,
249
+ };
250
+ const discovered = [];
251
+ let gapCounter = 0;
252
+ let highestIndex = opts.startIndex;
253
+ let stoppedByGapLimit = false;
254
+ const isViem = 'readContract' in connection;
255
+ if (opts.checkInParallel) {
256
+ for (let i = opts.startIndex; i <= opts.maxIndex; i += opts.batchSize) {
257
+ if (gapCounter >= opts.gapLimit) {
258
+ stoppedByGapLimit = true;
259
+ break;
260
+ }
261
+ const batchIndices = Array.from({ length: Math.min(opts.batchSize, opts.maxIndex - i + 1) }, (_, idx) => i + idx);
262
+ const batchResults = await Promise.all(batchIndices.map(index => this.checkPocketBalance(index, walletIndex, connection, isViem)));
263
+ for (let j = 0; j < batchResults.length; j++) {
264
+ const result = batchResults[j];
265
+ const index = batchIndices[j];
266
+ highestIndex = index;
267
+ if (result) {
268
+ const meetsMinBalance = result.nativeBalance.amount >= opts.minBalance;
269
+ if (meetsMinBalance || opts.includeZeroBalance) {
270
+ if (!opts.includePrivateKeys) {
271
+ delete result.privateKey;
272
+ }
273
+ discovered.push(result);
274
+ opts.onDiscovered(result);
275
+ if (meetsMinBalance) {
276
+ gapCounter = 0;
277
+ }
278
+ else {
279
+ gapCounter++;
280
+ }
281
+ }
282
+ else {
283
+ gapCounter++;
284
+ }
285
+ }
286
+ else {
287
+ gapCounter++;
288
+ }
289
+ opts.onProgress(index + 1 - opts.startIndex, opts.maxIndex - opts.startIndex + 1, discovered.length);
290
+ if (gapCounter >= opts.gapLimit) {
291
+ stoppedByGapLimit = true;
292
+ break;
293
+ }
294
+ }
295
+ if (i + opts.batchSize <= opts.maxIndex && !stoppedByGapLimit) {
296
+ await this.sleep(opts.checkDelay);
297
+ }
298
+ }
299
+ }
300
+ else {
301
+ for (let index = opts.startIndex; index <= opts.maxIndex; index++) {
302
+ if (gapCounter >= opts.gapLimit) {
303
+ stoppedByGapLimit = true;
304
+ break;
305
+ }
306
+ const result = await this.checkPocketBalance(index, walletIndex, connection, isViem);
307
+ highestIndex = index;
308
+ if (result) {
309
+ const meetsMinBalance = result.nativeBalance.amount >= opts.minBalance;
310
+ if (meetsMinBalance || opts.includeZeroBalance) {
311
+ if (!opts.includePrivateKeys) {
312
+ delete result.privateKey;
313
+ }
314
+ discovered.push(result);
315
+ opts.onDiscovered(result);
316
+ if (meetsMinBalance) {
317
+ gapCounter = 0;
318
+ }
319
+ else {
320
+ gapCounter++;
321
+ }
322
+ }
323
+ else {
324
+ gapCounter++;
325
+ }
326
+ }
327
+ else {
328
+ gapCounter++;
329
+ }
330
+ opts.onProgress(index + 1 - opts.startIndex, opts.maxIndex - opts.startIndex + 1, discovered.length);
331
+ if (index < opts.maxIndex && !stoppedByGapLimit) {
332
+ await this.sleep(opts.checkDelay);
333
+ }
334
+ }
335
+ }
336
+ const totalBalance = discovered.reduce((sum, wallet) => sum + wallet.nativeBalance.amount, 0n);
337
+ const duration = Date.now() - startTime;
338
+ return {
339
+ discovered,
340
+ scannedIndices: highestIndex - opts.startIndex + 1,
341
+ highestIndex,
342
+ totalBalance,
343
+ stoppedByGapLimit,
344
+ duration,
345
+ };
346
+ }
347
+ async checkPocketBalance(accountIndex, walletIndex, connection, isViem, maxRetries = 3) {
348
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
349
+ try {
350
+ const pocketIndex = accountIndex + 1;
351
+ const derivationPath = `m/44'/60'/${pocketIndex}'/0/${walletIndex}'`;
352
+ const { privateKey } = (0, walletBip32_1.EVMDeriveChildPrivateKey)(this.seed, walletIndex, `m/44'/60'/${pocketIndex}'/0/`);
353
+ const wallet = new ethers_1.ethers.Wallet(privateKey);
354
+ const address = wallet.address;
355
+ let balanceWei;
356
+ if (isViem) {
357
+ const viemConnection = connection;
358
+ balanceWei = await viemConnection.getBalance({ address: address });
359
+ }
360
+ else {
361
+ const ethersConnection = connection;
362
+ balanceWei = await ethersConnection.getBalance(address);
363
+ }
364
+ const formatted = Number(ethers_1.ethers.formatEther(balanceWei));
365
+ return {
366
+ index: accountIndex,
367
+ address,
368
+ derivationPath,
369
+ nativeBalance: {
370
+ amount: balanceWei,
371
+ formatted,
372
+ symbol: 'ETH',
373
+ },
374
+ privateKey,
375
+ };
376
+ }
377
+ catch (error) {
378
+ if (attempt === maxRetries - 1) {
379
+ console.warn(`Failed to check pocket at account ${accountIndex} after ${maxRetries} attempts:`, error);
380
+ return null;
381
+ }
382
+ const delay = Math.pow(2, attempt) * 1000;
383
+ await this.sleep(delay);
384
+ }
385
+ }
386
+ return null;
387
+ }
85
388
  }
86
389
  exports.EVMVM = EVMVM;
87
390
  class EVMChainWallet extends IChainWallet_1.ChainWallet {
88
391
  wallet;
89
392
  smartWallet;
90
- // private savingsManager?: SavingsManager
91
- // private smartSavingsManager?: SmartSavingsManager
92
393
  constructor(config, privateKey, index) {
93
394
  privateKey = privateKey.startsWith('0x') ? privateKey : `0x${privateKey}`;
94
395
  super(config, privateKey, index);
@@ -105,33 +406,15 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
105
406
  this.address = account.address;
106
407
  this.privateKey = privateKey;
107
408
  }
108
- // ============================================
109
- // Smart Wallet Extension Methods
110
- // ============================================
111
- /**
112
- * Check if Account Abstraction is supported on this chain
113
- * @returns true if aaSupport is enabled in config
114
- */
115
409
  isAASupportedByChain() {
116
410
  return this.config.aaSupport?.enabled === true;
117
411
  }
118
412
  convertFromEntropyToPrivateKey = (entropy) => {
119
413
  return (0, viem_1.toHex)(entropy);
120
414
  };
121
- /**
122
- * Check if smart wallet is initialized and ready for AA operations
123
- * @returns true if extend() has been called and smart wallet exists
124
- */
125
415
  isSmartWalletInitialized() {
126
416
  return !!this.smartWallet;
127
417
  }
128
- // createSavingsManager(mnemonic: string, index: number = 0, chain: ChainWalletConfig) {
129
- // return new SavingsManager(mnemonic, index, (chain))
130
- // }
131
- /**
132
- * Validate that AA is available for sponsored transactions
133
- * @throws Error with helpful message if AA is not available
134
- */
135
418
  validateAAAvailability() {
136
419
  if (!this.isSmartWalletInitialized()) {
137
420
  if (this.isAASupportedByChain()) {
@@ -147,26 +430,8 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
147
430
  }
148
431
  }
149
432
  }
150
- /**
151
- * Extend wallet with smart wallet capabilities
152
- * Enables Account Abstraction (EIP-4337) and EIP-7702 features
153
- *
154
- * @param options - Smart wallet configuration (optional if bundlerUrl is in config)
155
- * @returns EVMSmartWallet instance
156
- *
157
- * @example
158
- * // Using bundlerUrl from chainConfig
159
- * const smartWallet = await evmWallet.extend();
160
- *
161
- * @example
162
- * // Or provide bundlerUrl directly
163
- * const smartWallet = await evmWallet.extend({
164
- * bundlerUrl: 'https://api.pimlico.io/v2/...'
165
- * });
166
- */
167
433
  async extend(options = {}) {
168
434
  if (!this.smartWallet) {
169
- // Merge options with aaSupport config (options take priority)
170
435
  const mergedOptions = {
171
436
  ...options,
172
437
  aaConfig: this.config.aaSupport,
@@ -174,14 +439,12 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
174
439
  paymasterUrl: options.paymasterUrl || this.config.aaSupport?.paymasterUrl,
175
440
  entryPointVersion: options.entryPointVersion || this.config.aaSupport?.entryPoints?.[0]?.version
176
441
  };
177
- // Validate bundlerUrl is available
178
442
  if (!mergedOptions.bundlerUrl) {
179
443
  throw new Error('bundlerUrl is required to enable smart wallet features.\n' +
180
444
  'Provide it via:\n' +
181
445
  '1. extend({ bundlerUrl: "..." }), OR\n' +
182
446
  '2. Configure aaSupport in your chain config');
183
447
  }
184
- // Create viem chain object from config
185
448
  const chain = {
186
449
  id: this.config.chainId,
187
450
  name: this.config.name,
@@ -204,34 +467,18 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
204
467
  testnet: this.config.testnet || false
205
468
  };
206
469
  this.smartWallet = new smartWallet_1.EVMSmartWallet(this.privateKey, chain, mergedOptions);
207
- // Auto-initialize if option is set (default: true)
208
470
  if (options.autoInitialize !== false) {
209
471
  await this.smartWallet.initialize();
210
472
  }
211
473
  }
212
474
  return this.smartWallet;
213
475
  }
214
- /**
215
- * Check if smart wallet is enabled
216
- *
217
- * @returns true if extend() has been called
218
- */
219
476
  hasSmartWallet() {
220
477
  return !!this.smartWallet;
221
478
  }
222
- /**
223
- * Get smart wallet instance
224
- *
225
- * @returns EVMSmartWallet instance or undefined
226
- */
227
479
  getSmartWallet() {
228
480
  return this.smartWallet;
229
481
  }
230
- /**
231
- * Get smart wallet address (if initialized)
232
- *
233
- * @returns Smart wallet address or undefined
234
- */
235
482
  getSmartWalletAddress() {
236
483
  if (!this.smartWallet) {
237
484
  return undefined;
@@ -243,9 +490,6 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
243
490
  return undefined;
244
491
  }
245
492
  }
246
- // ============================================
247
- // Existing Wallet Methods
248
- // ============================================
249
493
  getWallet() {
250
494
  return this.wallet;
251
495
  }
@@ -253,22 +497,18 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
253
497
  return this.address;
254
498
  }
255
499
  async getNativeBalance() {
256
- // Implement native balance retrieval logic here
257
500
  return await EVMVM.getNativeBalance(this.address, this.connection);
258
501
  }
259
502
  async getTokenBalance(tokenAddress) {
260
- // Implement token balance retrieval logic here
261
503
  return await EVMVM.getTokenBalance(this.address, tokenAddress, this.connection);
262
504
  }
263
505
  async getTokenInfo(tokenAddress) {
264
506
  return await EVMVM.getTokenInfo(tokenAddress, this.connection);
265
507
  }
266
508
  async discoverToken() {
267
- // Implement token discovery logic here
268
509
  return await (0, utils_1.discoverTokens)(this.address, this.config);
269
510
  }
270
511
  async discoverNFT() {
271
- // Implement NFT discovery logic here
272
512
  return await (0, utils_1.discoverNFTs)(this.address, this.config);
273
513
  }
274
514
  async transferNative(to, amount) {
@@ -300,11 +540,9 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
300
540
  }
301
541
  return result.data;
302
542
  }
303
- // Updated swap method signature to match base class so created another method to use it inside swap
304
543
  async swap(tokenAddress, to, amount, slippage = 50) {
305
544
  throw new Error("Not Implemented");
306
545
  }
307
- // Helper method for EVMChainWallet class
308
546
  fail(message) {
309
547
  return {
310
548
  success: false,
@@ -329,4 +567,3 @@ class EVMChainWallet extends IChainWallet_1.ChainWallet {
329
567
  }
330
568
  }
331
569
  exports.EVMChainWallet = EVMChainWallet;
332
- //# sourceMappingURL=evm.js.map
package/dist/evm/index.js CHANGED
@@ -17,6 +17,3 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./evm"), exports);
18
18
  __exportStar(require("./utils"), exports);
19
19
  __exportStar(require("./smartWallet"), exports);
20
- // Note: AA service types are re-exported through smartWallet.types.ts
21
- // Direct AA service exports would cause duplicate export errors
22
- //# sourceMappingURL=index.js.map
@@ -2,12 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.quickSwapSetup = exports.quote = void 0;
4
4
  const ethers_1 = require("ethers");
5
- // Simplified ABI for Uniswap V3 Router
6
5
  const ROUTER_ABI = [
7
6
  'function exactInputSingle((address tokenIn, address tokenIn, uint24 fee, address recipient, uint256 deadline, uint256 amountIn, uint256 amountOutMinimum, uint160 sqrtPriceLimitX96)) external payable returns (uint256 amountOut)',
8
7
  'function exactOutputSingle((address tokenIn, address tokenIn, uint24 fee, address recipient, uint256 deadline, uint256 amountOut, uint256 amountInMaximum, uint160 sqrtPriceLimitX96)) external payable returns (uint256 amountIn)'
9
8
  ];
10
- // ERC20 ABI (simplified)
11
9
  const ERC20_ABI = [
12
10
  'function approve(address spender, uint256 amount) external returns (bool)',
13
11
  'function allowance(address owner, address spender) external view returns (uint256)',
@@ -42,18 +40,13 @@ const quote = async (quoterAddress, rpc, tokenIn, tokenOut, amountIn, fee, sqrtP
42
40
  };
43
41
  exports.quote = quote;
44
42
  const quickSwapSetup = async (params) => {
45
- const { privateKey, tokenIn, tokenOut, amountIn, tokenInDecimal, adminAddress, routerAddress, rpcUrl, minAmountOut, feeTier = 100 // 0.01% fee tier
46
- } = params;
43
+ const { privateKey, tokenIn, tokenOut, amountIn, tokenInDecimal, adminAddress, routerAddress, rpcUrl, minAmountOut, feeTier = 100 } = params;
47
44
  try {
48
- // Setup provider and wallet
49
45
  const provider = new ethers_1.ethers.JsonRpcProvider(rpcUrl);
50
46
  const wallet = new ethers_1.ethers.Wallet(privateKey, provider);
51
- // Create contract instances
52
47
  const router = new ethers_1.ethers.Contract(routerAddress, ROUTER_ABI, wallet);
53
48
  const tokenInContract = new ethers_1.ethers.Contract(tokenIn, ERC20_ABI, wallet);
54
- // Convert amount to wei
55
- const amountInWei = ethers_1.ethers.parseUnits(amountIn, tokenInDecimal); // Assuming 18 decimals
56
- // Check and approve token allowance
49
+ const amountInWei = ethers_1.ethers.parseUnits(amountIn, tokenInDecimal);
57
50
  const allowance = await tokenInContract.allowance(wallet.address, routerAddress);
58
51
  if (allowance < amountInWei) {
59
52
  console.log('Approving tokens...');
@@ -61,18 +54,14 @@ const quickSwapSetup = async (params) => {
61
54
  await approveTx.wait();
62
55
  console.log('Approval confirmed');
63
56
  }
64
- // Calculate amount with fee (0.01% to admin)
65
- const feeAmount = amountInWei * BigInt(1) / BigInt(10000); // 0.01%
57
+ const feeAmount = amountInWei * BigInt(1) / BigInt(10000);
66
58
  const amountAfterFee = amountInWei - feeAmount;
67
- // Transfer fee to admin
68
59
  if (feeAmount > 0) {
69
60
  console.log(`Transferring ${ethers_1.ethers.formatUnits(feeAmount, tokenIn)} fee to admin`);
70
61
  const feeTransferTx = await tokenInContract.transfer(adminAddress, feeAmount);
71
62
  await feeTransferTx.wait();
72
63
  }
73
- // Set deadline (10 minutes from now)
74
64
  const deadline = Math.floor(Date.now() / 1000) + 600;
75
- // Prepare swap parameters
76
65
  const swapParams = {
77
66
  tokenIn: tokenIn,
78
67
  tokenOut: tokenOut,
@@ -84,12 +73,10 @@ const quickSwapSetup = async (params) => {
84
73
  sqrtPriceLimitX96: 0
85
74
  };
86
75
  console.log('Executing swap...');
87
- // Execute swap
88
76
  const swapTx = await router.exactInputSingle(swapParams);
89
77
  const receipt = await swapTx.wait();
90
78
  console.log('Swap successful!');
91
79
  console.log('Transaction hash:', receipt.hash);
92
- // Check final balances
93
80
  const tokenOutContract = new ethers_1.ethers.Contract(tokenOut, ERC20_ABI, wallet);
94
81
  const finalBalance = await tokenOutContract.balanceOf(wallet.address);
95
82
  console.log(`Final ${tokenOut} balance:`, ethers_1.ethers.formatUnits(finalBalance, 18));
@@ -108,4 +95,3 @@ const quickSwapSetup = async (params) => {
108
95
  }
109
96
  };
110
97
  exports.quickSwapSetup = quickSwapSetup;
111
- //# sourceMappingURL=script.js.map