jaelis-node 1.3.2 → 1.4.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 (162) hide show
  1. package/README.md +62 -1
  2. package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +454 -0
  3. package/lib/JAELIS-VM/lib/adapters/index.js +411 -0
  4. package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +457 -0
  5. package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +1097 -0
  6. package/lib/JAELIS-VM/lib/execution/engine.js +1183 -0
  7. package/lib/JAELIS-VM/lib/index.js +440 -0
  8. package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +543 -0
  9. package/lib/JAELIS-VM/lib/serialization/serializer.js +819 -0
  10. package/lib/JAELIS-VM/lib/state/state-manager.js +1116 -0
  11. package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +1222 -0
  12. package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +836 -0
  13. package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +1127 -0
  14. package/lib/JAELIS-VM/lib/unified/index.js +378 -0
  15. package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +1150 -0
  16. package/lib/JAELIS-VM/lib/unified/unified-compiler.js +1350 -0
  17. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +12 -0
  18. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +17 -0
  19. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +28 -0
  20. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +12 -0
  21. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +17 -0
  22. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +28 -0
  23. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +12 -0
  24. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +12 -0
  25. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +17 -0
  26. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +28 -0
  27. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +12 -0
  28. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +17 -0
  29. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +28 -0
  30. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +17 -0
  31. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +28 -0
  32. package/lib/JAELIS-VM/node_modules/.package-lock.json +127 -0
  33. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +1 -0
  34. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
  35. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
  36. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
  37. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +17 -0
  38. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +1 -0
  39. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
  40. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
  41. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
  42. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +17 -0
  43. package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +21 -0
  44. package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +5 -0
  45. package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +11 -0
  46. package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +60 -0
  47. package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +1 -0
  48. package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +50 -0
  49. package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +198 -0
  50. package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +21 -0
  51. package/lib/JAELIS-VM/node_modules/cbor-x/README.md +380 -0
  52. package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +11 -0
  53. package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +73 -0
  54. package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +11 -0
  55. package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +2 -0
  56. package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +1300 -0
  57. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +1244 -0
  58. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +1 -0
  59. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +2509 -0
  60. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +1 -0
  61. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +2 -0
  62. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +1 -0
  63. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +2508 -0
  64. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +1 -0
  65. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +2 -0
  66. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +1 -0
  67. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +2629 -0
  68. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +1 -0
  69. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +3343 -0
  70. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +1 -0
  71. package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +1 -0
  72. package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +1231 -0
  73. package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +79 -0
  74. package/lib/JAELIS-VM/node_modules/cbor-x/index.js +3 -0
  75. package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +85 -0
  76. package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +24 -0
  77. package/lib/JAELIS-VM/node_modules/cbor-x/package.json +94 -0
  78. package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +88 -0
  79. package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +61 -0
  80. package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +19 -0
  81. package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +201 -0
  82. package/lib/JAELIS-VM/node_modules/detect-libc/README.md +163 -0
  83. package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +14 -0
  84. package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +313 -0
  85. package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +39 -0
  86. package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +51 -0
  87. package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +24 -0
  88. package/lib/JAELIS-VM/node_modules/detect-libc/package.json +44 -0
  89. package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +21 -0
  90. package/lib/JAELIS-VM/node_modules/msgpackr/README.md +372 -0
  91. package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +11 -0
  92. package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +67 -0
  93. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +2407 -0
  94. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +1 -0
  95. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +2 -0
  96. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +1 -0
  97. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +2406 -0
  98. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +1 -0
  99. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +2 -0
  100. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +1 -0
  101. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +3320 -0
  102. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +1 -0
  103. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +4540 -0
  104. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +1 -0
  105. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +1250 -0
  106. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +1 -0
  107. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +91 -0
  108. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +91 -0
  109. package/lib/JAELIS-VM/node_modules/msgpackr/index.js +5 -0
  110. package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +87 -0
  111. package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +25 -0
  112. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +1 -0
  113. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +1 -0
  114. package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +1141 -0
  115. package/lib/JAELIS-VM/node_modules/msgpackr/package.json +104 -0
  116. package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +88 -0
  117. package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +57 -0
  118. package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +815 -0
  119. package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +3 -0
  120. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +2 -0
  121. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +2 -0
  122. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +1221 -0
  123. package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +21 -0
  124. package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +5 -0
  125. package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +13 -0
  126. package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +63 -0
  127. package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +1 -0
  128. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +12 -0
  129. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +12 -0
  130. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +17 -0
  131. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +28 -0
  132. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +12 -0
  133. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +17 -0
  134. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +28 -0
  135. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +17 -0
  136. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +28 -0
  137. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +21 -0
  138. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +58 -0
  139. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +82 -0
  140. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +19 -0
  141. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +6 -0
  142. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +236 -0
  143. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +7 -0
  144. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +32 -0
  145. package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +50 -0
  146. package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +274 -0
  147. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +21 -0
  148. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +58 -0
  149. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +77 -0
  150. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +19 -0
  151. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +224 -0
  152. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +7 -0
  153. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +32 -0
  154. package/lib/JAELIS-VM/package-lock.json +284 -0
  155. package/lib/JAELIS-VM/package.json +38 -0
  156. package/lib/JAELIS-VM/test/comprehensive.test.js +267 -0
  157. package/lib/JAELIS-VM/test/cross-chain-test.js +470 -0
  158. package/lib/JAELIS-VM/test/unified-vm-test.js +459 -0
  159. package/lib/JAELIS-VM/test/unified.test.js +166 -0
  160. package/lib/JAELIS-VM/test/vm.test.js +599 -0
  161. package/lib/index.js +240 -4
  162. package/package.json +2 -2
package/README.md CHANGED
@@ -172,7 +172,11 @@ Your node exposes standard JSON-RPC plus JAELIS-specific methods:
172
172
  | `jaelis_validateAddressSafety` | Anti-phishing check |
173
173
  | `jaelis_checkDustAttacks` | Detect address poisoning |
174
174
 
175
- ### Standard Ethereum-Compatible Methods
175
+ ### Multi-Ecosystem Compatibility Layers
176
+
177
+ JAELIS nodes support RPC methods from **every major blockchain ecosystem**. Developers can use their familiar method names - they all proxy to native `jaelis_*` methods under the hood.
178
+
179
+ #### Ethereum (eth_*)
176
180
  | Method | Description |
177
181
  |--------|-------------|
178
182
  | `eth_blockNumber` | Get latest block number |
@@ -181,6 +185,58 @@ Your node exposes standard JSON-RPC plus JAELIS-specific methods:
181
185
  | `eth_call` | Call contract (read-only) |
182
186
  | `eth_getTransactionReceipt` | Get transaction receipt |
183
187
 
188
+ #### Solana (solana_*)
189
+ | Method | Description |
190
+ |--------|-------------|
191
+ | `solana_getBalance` | Get account balance (lamports format) |
192
+ | `solana_getSlot` | Get current slot (block height) |
193
+ | `solana_getLatestBlockhash` | Get recent blockhash |
194
+ | `solana_getHealth` | Check node health |
195
+
196
+ #### Bitcoin (btc_*)
197
+ | Method | Description |
198
+ |--------|-------------|
199
+ | `btc_getbalance` | Get balance (BTC decimals) |
200
+ | `btc_getblockcount` | Get block height |
201
+ | `btc_getblock` | Get block by hash |
202
+ | `btc_gettransaction` | Get transaction |
203
+ | `btc_getblockchaininfo` | Chain info |
204
+ | `btc_estimatesmartfee` | Returns 0 (zero fees!) |
205
+
206
+ #### Move/Aptos/Sui (move_*)
207
+ | Method | Description |
208
+ |--------|-------------|
209
+ | `move_getLedgerInfo` | Get ledger info |
210
+ | `move_getAccountResource` | Get account resource |
211
+ | `move_getAccountModule` | Get account module |
212
+
213
+ #### TON/Telegram (ton_*)
214
+ | Method | Description |
215
+ |--------|-------------|
216
+ | `ton_getAddressBalance` | Get balance |
217
+ | `ton_getMasterchainInfo` | Get masterchain info |
218
+ | `ton_getAccountState` | Get account state |
219
+ | `ton_runGetMethod` | Run get method |
220
+
221
+ #### WASM/CosmWasm (wasm_*)
222
+ | Method | Description |
223
+ |--------|-------------|
224
+ | `wasm_call` | Call contract |
225
+ | `wasm_query` | Query contract state |
226
+ | `wasm_deploy` | Deploy WASM contract |
227
+ | `wasm_execute` | Execute contract |
228
+ | `wasm_getCode` | Get contract code |
229
+
230
+ #### StarkNet/Cairo (starknet_*)
231
+ | Method | Description |
232
+ |--------|-------------|
233
+ | `starknet_chainId` | Get chain ID |
234
+ | `starknet_blockNumber` | Get block number |
235
+ | `starknet_call` | Call contract |
236
+ | `starknet_estimateFee` | Returns 0 (zero fees!) |
237
+ | `starknet_getTransactionByHash` | Get transaction |
238
+ | `starknet_addInvokeTransaction` | Send invoke transaction |
239
+
184
240
  ## Programmatic Usage
185
241
 
186
242
  ```javascript
@@ -258,6 +314,9 @@ jaelis-node status
258
314
  ```
259
315
  jaelis-data/
260
316
  ├── blockchain/ # Block and state data (LevelDB)
317
+ │ ├── state/ # Account states, contract storage
318
+ │ ├── blocks/ # Block headers and transactions
319
+ │ └── index/ # Chain indexes
261
320
  ├── node-registry/ # Node tracking and statistics
262
321
  ├── wallet-config/ # Wallet configuration
263
322
  ├── peers/ # Peer database
@@ -265,6 +324,8 @@ jaelis-data/
265
324
  └── logs/ # Node logs
266
325
  ```
267
326
 
327
+ **State Persistence:** All VM state (accounts, contracts, storage) persists to LevelDB. Your node's state survives restarts - no data loss.
328
+
268
329
  ## Environment Variables
269
330
 
270
331
  | Variable | Description | Default |
@@ -0,0 +1,454 @@
1
+ /**
2
+ * EVM ADAPTER
3
+ *
4
+ * Provides EVM-compatible interface for JAELIS Universal VM
5
+ * Supports: Ethereum, BSC, Polygon, Arbitrum, Optimism, Base, Avalanche
6
+ *
7
+ * Features:
8
+ * - Solidity ABI encoding/decoding
9
+ * - EIP-2718 transaction types
10
+ * - EIP-1559 gas pricing
11
+ * - Precompiled contracts
12
+ *
13
+ * @version 0.1.0
14
+ * @author Mario Papaleo - JAELIS Foundation
15
+ */
16
+
17
+ const crypto = require('crypto');
18
+
19
+ // Solidity ABI type encodings
20
+ const ABI_TYPES = {
21
+ uint256: { size: 32, dynamic: false },
22
+ int256: { size: 32, dynamic: false },
23
+ address: { size: 32, dynamic: false },
24
+ bool: { size: 32, dynamic: false },
25
+ bytes32: { size: 32, dynamic: false },
26
+ bytes: { size: null, dynamic: true },
27
+ string: { size: null, dynamic: true },
28
+ 'uint256[]': { size: null, dynamic: true },
29
+ 'address[]': { size: null, dynamic: true }
30
+ };
31
+
32
+ // EVM Precompiled contract addresses
33
+ const PRECOMPILES = {
34
+ '0x0000000000000000000000000000000000000001': 'ecrecover',
35
+ '0x0000000000000000000000000000000000000002': 'sha256',
36
+ '0x0000000000000000000000000000000000000003': 'ripemd160',
37
+ '0x0000000000000000000000000000000000000004': 'identity',
38
+ '0x0000000000000000000000000000000000000005': 'modexp',
39
+ '0x0000000000000000000000000000000000000006': 'ecadd',
40
+ '0x0000000000000000000000000000000000000007': 'ecmul',
41
+ '0x0000000000000000000000000000000000000008': 'ecpairing',
42
+ '0x0000000000000000000000000000000000000009': 'blake2f'
43
+ };
44
+
45
+ class EVMAdapter {
46
+ constructor(vm) {
47
+ this.vm = vm;
48
+ this.chainId = 4545; // JAELIS testnet
49
+
50
+ // Function signature cache
51
+ this.signatureCache = new Map();
52
+ }
53
+
54
+ /**
55
+ * Deploy EVM contract
56
+ */
57
+ async deploy(bytecode, constructorArgs = [], options = {}) {
58
+ // Encode constructor arguments
59
+ const encodedArgs = this.encodeParameters(
60
+ options.constructorTypes || [],
61
+ constructorArgs
62
+ );
63
+
64
+ const deployBytecode = Buffer.concat([
65
+ Buffer.from(bytecode.replace('0x', ''), 'hex'),
66
+ encodedArgs
67
+ ]);
68
+
69
+ return this.vm.deployBytecode(deployBytecode, 'evm', options);
70
+ }
71
+
72
+ /**
73
+ * Call EVM contract function
74
+ */
75
+ async call(address, functionSignature, args = [], options = {}) {
76
+ // Parse function signature
77
+ const { name, types } = this.parseFunctionSignature(functionSignature);
78
+
79
+ // Encode function call
80
+ const selector = this.getFunctionSelector(functionSignature);
81
+ const encodedArgs = this.encodeParameters(types, args);
82
+
83
+ const callData = Buffer.concat([selector, encodedArgs]);
84
+
85
+ // Execute
86
+ const result = await this.vm.execute(address, name, callData, {
87
+ ...options,
88
+ callData
89
+ });
90
+
91
+ // Decode result if return types specified
92
+ if (options.returnTypes) {
93
+ result.decoded = this.decodeParameters(options.returnTypes, result.returnValue);
94
+ }
95
+
96
+ return result;
97
+ }
98
+
99
+ /**
100
+ * Get function selector (4 bytes)
101
+ */
102
+ getFunctionSelector(signature) {
103
+ // Check cache
104
+ if (this.signatureCache.has(signature)) {
105
+ return this.signatureCache.get(signature);
106
+ }
107
+
108
+ // Normalize signature
109
+ const normalized = signature.replace(/\s/g, '');
110
+
111
+ // Keccak256 hash and take first 4 bytes
112
+ const hash = this.keccak256(Buffer.from(normalized));
113
+ const selector = hash.slice(0, 4);
114
+
115
+ this.signatureCache.set(signature, selector);
116
+ return selector;
117
+ }
118
+
119
+ /**
120
+ * Parse function signature
121
+ */
122
+ parseFunctionSignature(signature) {
123
+ const match = signature.match(/^(\w+)\((.*)\)$/);
124
+ if (!match) {
125
+ throw new Error(`Invalid function signature: ${signature}`);
126
+ }
127
+
128
+ const name = match[1];
129
+ const types = match[2] ? match[2].split(',').map(t => t.trim()) : [];
130
+
131
+ return { name, types };
132
+ }
133
+
134
+ /**
135
+ * Encode function parameters (ABI encoding)
136
+ */
137
+ encodeParameters(types, values) {
138
+ if (types.length !== values.length) {
139
+ throw new Error('Types and values length mismatch');
140
+ }
141
+
142
+ const heads = [];
143
+ const tails = [];
144
+ let tailOffset = types.length * 32; // Start of dynamic data
145
+
146
+ for (let i = 0; i < types.length; i++) {
147
+ const type = types[i];
148
+ const value = values[i];
149
+ const typeInfo = ABI_TYPES[type];
150
+
151
+ if (!typeInfo || !typeInfo.dynamic) {
152
+ // Static type - encode directly in head
153
+ heads.push(this.encodeValue(type, value));
154
+ } else {
155
+ // Dynamic type - encode offset in head, data in tail
156
+ heads.push(this.encodeUint256(tailOffset));
157
+ const encoded = this.encodeValue(type, value);
158
+ tails.push(encoded);
159
+ tailOffset += encoded.length;
160
+ }
161
+ }
162
+
163
+ return Buffer.concat([...heads, ...tails]);
164
+ }
165
+
166
+ /**
167
+ * Decode function parameters
168
+ */
169
+ decodeParameters(types, data) {
170
+ const results = [];
171
+ let offset = 0;
172
+
173
+ for (const type of types) {
174
+ const typeInfo = ABI_TYPES[type];
175
+
176
+ if (!typeInfo || !typeInfo.dynamic) {
177
+ // Static type
178
+ results.push(this.decodeValue(type, data, offset));
179
+ offset += 32;
180
+ } else {
181
+ // Dynamic type - read offset first
182
+ const dataOffset = Number(this.decodeUint256(data, offset));
183
+ results.push(this.decodeValue(type, data, dataOffset));
184
+ offset += 32;
185
+ }
186
+ }
187
+
188
+ return results;
189
+ }
190
+
191
+ /**
192
+ * Encode a single value
193
+ */
194
+ encodeValue(type, value) {
195
+ switch (type) {
196
+ case 'uint256':
197
+ case 'int256':
198
+ return this.encodeUint256(value);
199
+
200
+ case 'address':
201
+ return this.encodeAddress(value);
202
+
203
+ case 'bool':
204
+ return this.encodeUint256(value ? 1 : 0);
205
+
206
+ case 'bytes32':
207
+ return this.encodeBytes32(value);
208
+
209
+ case 'bytes':
210
+ return this.encodeBytes(value);
211
+
212
+ case 'string':
213
+ return this.encodeString(value);
214
+
215
+ case 'uint256[]':
216
+ case 'address[]':
217
+ return this.encodeArray(type.replace('[]', ''), value);
218
+
219
+ default:
220
+ throw new Error(`Unsupported type: ${type}`);
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Decode a single value
226
+ */
227
+ decodeValue(type, data, offset) {
228
+ switch (type) {
229
+ case 'uint256':
230
+ return this.decodeUint256(data, offset);
231
+
232
+ case 'int256':
233
+ return this.decodeInt256(data, offset);
234
+
235
+ case 'address':
236
+ return this.decodeAddress(data, offset);
237
+
238
+ case 'bool':
239
+ return this.decodeUint256(data, offset) !== 0n;
240
+
241
+ case 'bytes32':
242
+ return '0x' + data.slice(offset, offset + 32).toString('hex');
243
+
244
+ case 'bytes':
245
+ return this.decodeBytes(data, offset);
246
+
247
+ case 'string':
248
+ return this.decodeString(data, offset);
249
+
250
+ default:
251
+ throw new Error(`Unsupported type: ${type}`);
252
+ }
253
+ }
254
+
255
+ // ═══════════════════════════════════════════════════════════════
256
+ // ENCODING HELPERS
257
+ // ═══════════════════════════════════════════════════════════════
258
+
259
+ encodeUint256(value) {
260
+ const buf = Buffer.alloc(32);
261
+ let v = BigInt(value);
262
+ for (let i = 31; i >= 0 && v > 0n; i--) {
263
+ buf[i] = Number(v & 0xffn);
264
+ v >>= 8n;
265
+ }
266
+ return buf;
267
+ }
268
+
269
+ decodeUint256(data, offset = 0) {
270
+ let result = 0n;
271
+ for (let i = 0; i < 32; i++) {
272
+ result = (result << 8n) | BigInt(data[offset + i] || 0);
273
+ }
274
+ return result;
275
+ }
276
+
277
+ decodeInt256(data, offset = 0) {
278
+ const unsigned = this.decodeUint256(data, offset);
279
+ // Check sign bit
280
+ if (unsigned >= 2n ** 255n) {
281
+ return unsigned - 2n ** 256n;
282
+ }
283
+ return unsigned;
284
+ }
285
+
286
+ encodeAddress(address) {
287
+ const buf = Buffer.alloc(32);
288
+ const hex = address.startsWith('0x') ? address.slice(2) : address;
289
+ Buffer.from(hex.padStart(40, '0'), 'hex').copy(buf, 12);
290
+ return buf;
291
+ }
292
+
293
+ decodeAddress(data, offset = 0) {
294
+ return '0x' + data.slice(offset + 12, offset + 32).toString('hex');
295
+ }
296
+
297
+ encodeBytes32(value) {
298
+ const buf = Buffer.alloc(32);
299
+ const hex = value.startsWith('0x') ? value.slice(2) : value;
300
+ Buffer.from(hex.padEnd(64, '0'), 'hex').copy(buf);
301
+ return buf;
302
+ }
303
+
304
+ encodeBytes(value) {
305
+ const data = Buffer.isBuffer(value) ? value : Buffer.from(value.replace('0x', ''), 'hex');
306
+ const length = this.encodeUint256(data.length);
307
+
308
+ // Pad to 32-byte boundary
309
+ const padded = Buffer.alloc(Math.ceil(data.length / 32) * 32);
310
+ data.copy(padded);
311
+
312
+ return Buffer.concat([length, padded]);
313
+ }
314
+
315
+ decodeBytes(data, offset = 0) {
316
+ const length = Number(this.decodeUint256(data, offset));
317
+ return data.slice(offset + 32, offset + 32 + length);
318
+ }
319
+
320
+ encodeString(value) {
321
+ return this.encodeBytes(Buffer.from(value, 'utf8'));
322
+ }
323
+
324
+ decodeString(data, offset = 0) {
325
+ return this.decodeBytes(data, offset).toString('utf8');
326
+ }
327
+
328
+ encodeArray(elementType, values) {
329
+ const length = this.encodeUint256(values.length);
330
+ const elements = values.map(v => this.encodeValue(elementType, v));
331
+ return Buffer.concat([length, ...elements]);
332
+ }
333
+
334
+ // ═══════════════════════════════════════════════════════════════
335
+ // CRYPTO
336
+ // ═══════════════════════════════════════════════════════════════
337
+
338
+ keccak256(data) {
339
+ // Using sha3-256 as approximation
340
+ // Real implementation would use keccak
341
+ return crypto.createHash('sha3-256').update(data).digest();
342
+ }
343
+
344
+ // ═══════════════════════════════════════════════════════════════
345
+ // TRANSACTION ENCODING (EIP-2718, EIP-1559)
346
+ // ═══════════════════════════════════════════════════════════════
347
+
348
+ encodeTransaction(tx) {
349
+ // EIP-1559 transaction
350
+ const fields = [
351
+ tx.chainId || this.chainId,
352
+ tx.nonce || 0,
353
+ tx.maxPriorityFeePerGas || 0,
354
+ tx.maxFeePerGas || 0,
355
+ tx.gasLimit || 21000,
356
+ tx.to || '',
357
+ tx.value || 0,
358
+ tx.data || Buffer.alloc(0),
359
+ tx.accessList || []
360
+ ];
361
+
362
+ // RLP encode (simplified)
363
+ const encoded = this.rlpEncode(fields);
364
+
365
+ // Prepend transaction type (0x02 for EIP-1559)
366
+ return Buffer.concat([Buffer.from([0x02]), encoded]);
367
+ }
368
+
369
+ rlpEncode(input) {
370
+ if (Array.isArray(input)) {
371
+ const encoded = input.map(item => this.rlpEncode(item));
372
+ const concat = Buffer.concat(encoded);
373
+ return Buffer.concat([this.rlpEncodeLength(concat.length, 0xc0), concat]);
374
+ }
375
+
376
+ if (Buffer.isBuffer(input)) {
377
+ if (input.length === 1 && input[0] < 0x80) {
378
+ return input;
379
+ }
380
+ return Buffer.concat([this.rlpEncodeLength(input.length, 0x80), input]);
381
+ }
382
+
383
+ if (typeof input === 'number' || typeof input === 'bigint') {
384
+ if (input === 0 || input === 0n) {
385
+ return Buffer.from([0x80]);
386
+ }
387
+ const hex = input.toString(16);
388
+ const buf = Buffer.from(hex.length % 2 ? '0' + hex : hex, 'hex');
389
+ return this.rlpEncode(buf);
390
+ }
391
+
392
+ if (typeof input === 'string') {
393
+ if (input.startsWith('0x')) {
394
+ return this.rlpEncode(Buffer.from(input.slice(2), 'hex'));
395
+ }
396
+ return this.rlpEncode(Buffer.from(input));
397
+ }
398
+
399
+ return Buffer.from([0x80]);
400
+ }
401
+
402
+ rlpEncodeLength(length, offset) {
403
+ if (length < 56) {
404
+ return Buffer.from([offset + length]);
405
+ }
406
+ const hexLength = length.toString(16);
407
+ const lenOfLen = Math.ceil(hexLength.length / 2);
408
+ return Buffer.concat([
409
+ Buffer.from([offset + 55 + lenOfLen]),
410
+ Buffer.from(hexLength.padStart(lenOfLen * 2, '0'), 'hex')
411
+ ]);
412
+ }
413
+
414
+ // ═══════════════════════════════════════════════════════════════
415
+ // PRECOMPILES
416
+ // ═══════════════════════════════════════════════════════════════
417
+
418
+ async executePrecompile(address, input) {
419
+ const name = PRECOMPILES[address.toLowerCase()];
420
+ if (!name) {
421
+ throw new Error(`Unknown precompile: ${address}`);
422
+ }
423
+
424
+ switch (name) {
425
+ case 'ecrecover':
426
+ return this._ecrecover(input);
427
+ case 'sha256':
428
+ return crypto.createHash('sha256').update(input).digest();
429
+ case 'ripemd160':
430
+ const ripemd = crypto.createHash('ripemd160').update(input).digest();
431
+ return Buffer.concat([Buffer.alloc(12), ripemd]);
432
+ case 'identity':
433
+ return input;
434
+ case 'modexp':
435
+ return this._modexp(input);
436
+ default:
437
+ throw new Error(`Precompile not implemented: ${name}`);
438
+ }
439
+ }
440
+
441
+ _ecrecover(input) {
442
+ // Extract components from input
443
+ // Would need actual EC recovery implementation
444
+ return Buffer.alloc(32);
445
+ }
446
+
447
+ _modexp(input) {
448
+ // Modular exponentiation
449
+ // Would need big integer implementation
450
+ return Buffer.alloc(32);
451
+ }
452
+ }
453
+
454
+ module.exports = EVMAdapter;