jaelis-node 1.9.0 → 2.0.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.
- package/README.md +147 -433
- package/bin/jaelis-node.js +79 -504
- package/lib/index.js +31 -2740
- package/lib/node.js +271 -0
- package/lib/rpc.js +315 -0
- package/lib/storage.js +198 -0
- package/lib/sync.js +366 -0
- package/package.json +19 -53
- package/config/default.json +0 -74
- package/config/mainnet.json +0 -30
- package/config/testnet.json +0 -26
- package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +0 -454
- package/lib/JAELIS-VM/lib/adapters/index.js +0 -411
- package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +0 -457
- package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +0 -1097
- package/lib/JAELIS-VM/lib/execution/engine.js +0 -1183
- package/lib/JAELIS-VM/lib/index.js +0 -440
- package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +0 -543
- package/lib/JAELIS-VM/lib/serialization/serializer.js +0 -819
- package/lib/JAELIS-VM/lib/state/state-manager.js +0 -1116
- package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +0 -1222
- package/lib/JAELIS-VM/lib/unified/cross-chain-deploy.js +0 -1678
- package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +0 -836
- package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +0 -1127
- package/lib/JAELIS-VM/lib/unified/index.js +0 -456
- package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +0 -1150
- package/lib/JAELIS-VM/lib/unified/unified-compiler.js +0 -1350
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/.package-lock.json +0 -127
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +0 -1
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
- package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +0 -17
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +0 -1
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
- package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +0 -17
- package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +0 -5
- package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +0 -11
- package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +0 -60
- package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +0 -50
- package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +0 -198
- package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/cbor-x/README.md +0 -380
- package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +0 -11
- package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +0 -73
- package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +0 -11
- package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +0 -2
- package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +0 -1300
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +0 -1244
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +0 -2509
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +0 -2508
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +0 -2629
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +0 -3343
- package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +0 -1
- package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +0 -1231
- package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +0 -79
- package/lib/JAELIS-VM/node_modules/cbor-x/index.js +0 -3
- package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +0 -85
- package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +0 -24
- package/lib/JAELIS-VM/node_modules/cbor-x/package.json +0 -94
- package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +0 -88
- package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +0 -61
- package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +0 -19
- package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +0 -201
- package/lib/JAELIS-VM/node_modules/detect-libc/README.md +0 -163
- package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +0 -14
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +0 -313
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +0 -39
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +0 -51
- package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +0 -24
- package/lib/JAELIS-VM/node_modules/detect-libc/package.json +0 -44
- package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/msgpackr/README.md +0 -372
- package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +0 -11
- package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +0 -67
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +0 -2407
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +0 -2406
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +0 -3320
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +0 -4540
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +0 -1250
- package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +0 -91
- package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +0 -91
- package/lib/JAELIS-VM/node_modules/msgpackr/index.js +0 -5
- package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +0 -87
- package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +0 -25
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +0 -1141
- package/lib/JAELIS-VM/node_modules/msgpackr/package.json +0 -104
- package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +0 -88
- package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +0 -57
- package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +0 -815
- package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +0 -3
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +0 -2
- package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +0 -1221
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +0 -5
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +0 -13
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +0 -63
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +0 -1
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +0 -12
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +0 -58
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +0 -82
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +0 -6
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +0 -236
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +0 -32
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +0 -50
- package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +0 -274
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +0 -58
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +0 -77
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +0 -224
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
- package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +0 -32
- package/lib/JAELIS-VM/package-lock.json +0 -284
- package/lib/JAELIS-VM/package.json +0 -38
- package/lib/JAELIS-VM/test/comprehensive.test.js +0 -267
- package/lib/JAELIS-VM/test/cross-chain-test.js +0 -470
- package/lib/JAELIS-VM/test/unified-vm-test.js +0 -459
- package/lib/JAELIS-VM/test/unified.test.js +0 -166
- package/lib/JAELIS-VM/test/vm.test.js +0 -599
- package/lib/settlement-server.js +0 -999
- package/lib/vm/index.js +0 -397
|
@@ -1,1150 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JAELIS UNIFIED ABI
|
|
3
|
-
*
|
|
4
|
-
* ONE ABI TO RULE THEM ALL
|
|
5
|
-
*
|
|
6
|
-
* No more:
|
|
7
|
-
* - Solidity ABI (EVM)
|
|
8
|
-
* - Borsh (Solana)
|
|
9
|
-
* - BCS (Move)
|
|
10
|
-
* - TL-B (TON)
|
|
11
|
-
* - JSON Schema (WASM)
|
|
12
|
-
*
|
|
13
|
-
* Just: JAELIS ABI
|
|
14
|
-
*
|
|
15
|
-
* This replaces ALL adapters with ONE unified encoding/decoding system.
|
|
16
|
-
*
|
|
17
|
-
* @version 0.3.0
|
|
18
|
-
* @author Mario Papaleo - JAELIS Foundation
|
|
19
|
-
* @patent PATENT PENDING - Unified Blockchain ABI
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
const crypto = require('crypto');
|
|
23
|
-
|
|
24
|
-
// ═══════════════════════════════════════════════════════════════
|
|
25
|
-
// JAELIS UNIFIED TYPES
|
|
26
|
-
// ═══════════════════════════════════════════════════════════════
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* JAELIS Native Types
|
|
30
|
-
*
|
|
31
|
-
* These are THE types. Not EVM types, not Solana types.
|
|
32
|
-
* JAELIS types that work everywhere.
|
|
33
|
-
*/
|
|
34
|
-
const JAELIS_TYPES = {
|
|
35
|
-
// Integers (size in bytes)
|
|
36
|
-
U8: { id: 0x01, size: 1, signed: false },
|
|
37
|
-
U16: { id: 0x02, size: 2, signed: false },
|
|
38
|
-
U32: { id: 0x03, size: 4, signed: false },
|
|
39
|
-
U64: { id: 0x04, size: 8, signed: false },
|
|
40
|
-
U128: { id: 0x05, size: 16, signed: false },
|
|
41
|
-
U256: { id: 0x06, size: 32, signed: false },
|
|
42
|
-
|
|
43
|
-
I8: { id: 0x11, size: 1, signed: true },
|
|
44
|
-
I16: { id: 0x12, size: 2, signed: true },
|
|
45
|
-
I32: { id: 0x13, size: 4, signed: true },
|
|
46
|
-
I64: { id: 0x14, size: 8, signed: true },
|
|
47
|
-
I128: { id: 0x15, size: 16, signed: true },
|
|
48
|
-
I256: { id: 0x16, size: 32, signed: true },
|
|
49
|
-
|
|
50
|
-
// Floats (if needed)
|
|
51
|
-
F32: { id: 0x21, size: 4 },
|
|
52
|
-
F64: { id: 0x22, size: 8 },
|
|
53
|
-
|
|
54
|
-
// Boolean
|
|
55
|
-
BOOL: { id: 0x30, size: 1 },
|
|
56
|
-
|
|
57
|
-
// Address - ALWAYS 32 bytes in JAELIS (unified!)
|
|
58
|
-
ADDRESS: { id: 0x40, size: 32 },
|
|
59
|
-
|
|
60
|
-
// Fixed bytes
|
|
61
|
-
BYTES1: { id: 0x50, size: 1 },
|
|
62
|
-
BYTES4: { id: 0x51, size: 4 }, // Function selectors
|
|
63
|
-
BYTES8: { id: 0x52, size: 8 },
|
|
64
|
-
BYTES16: { id: 0x53, size: 16 },
|
|
65
|
-
BYTES32: { id: 0x54, size: 32 }, // Hashes
|
|
66
|
-
|
|
67
|
-
// Dynamic types
|
|
68
|
-
BYTES: { id: 0x60, size: -1, dynamic: true },
|
|
69
|
-
STRING: { id: 0x61, size: -1, dynamic: true },
|
|
70
|
-
|
|
71
|
-
// Collections
|
|
72
|
-
ARRAY: { id: 0x70, size: -1, dynamic: true },
|
|
73
|
-
MAP: { id: 0x71, size: -1, dynamic: true },
|
|
74
|
-
|
|
75
|
-
// Structs
|
|
76
|
-
STRUCT: { id: 0x80, size: -1, dynamic: true },
|
|
77
|
-
TUPLE: { id: 0x81, size: -1, dynamic: true },
|
|
78
|
-
|
|
79
|
-
// Optional
|
|
80
|
-
OPTION: { id: 0x90, size: -1, dynamic: true },
|
|
81
|
-
RESULT: { id: 0x91, size: -1, dynamic: true },
|
|
82
|
-
|
|
83
|
-
// Blockchain-specific (but unified representation!)
|
|
84
|
-
TOKEN_AMOUNT: { id: 0xA0, size: 32 }, // Always 256-bit for precision
|
|
85
|
-
SIGNATURE: { id: 0xA1, size: 65 }, // 64 bytes sig + 1 byte recovery
|
|
86
|
-
HASH: { id: 0xA2, size: 32 }, // SHA256/Keccak result
|
|
87
|
-
PUBKEY: { id: 0xA3, size: 32 }, // Ed25519/Secp256k1 pubkey
|
|
88
|
-
|
|
89
|
-
// Null
|
|
90
|
-
NULL: { id: 0xFF, size: 0 }
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// Type name lookup
|
|
94
|
-
const TYPE_BY_ID = new Map();
|
|
95
|
-
for (const [name, info] of Object.entries(JAELIS_TYPES)) {
|
|
96
|
-
TYPE_BY_ID.set(info.id, { name, ...info });
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// ═══════════════════════════════════════════════════════════════
|
|
100
|
-
// JAELIS ABI ENCODER
|
|
101
|
-
// ═══════════════════════════════════════════════════════════════
|
|
102
|
-
|
|
103
|
-
class JaelisABIEncoder {
|
|
104
|
-
constructor() {
|
|
105
|
-
this.buffer = [];
|
|
106
|
-
this.position = 0;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Encode a value to JAELIS ABI format
|
|
111
|
-
*/
|
|
112
|
-
encode(value, type) {
|
|
113
|
-
const typeInfo = typeof type === 'string' ? JAELIS_TYPES[type] : type;
|
|
114
|
-
|
|
115
|
-
if (!typeInfo) {
|
|
116
|
-
throw new Error(`Unknown type: ${type}`);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Write type ID first (self-describing format!)
|
|
120
|
-
this.writeUint8(typeInfo.id);
|
|
121
|
-
|
|
122
|
-
// Encode based on type
|
|
123
|
-
switch (typeInfo.id) {
|
|
124
|
-
// Unsigned integers
|
|
125
|
-
case JAELIS_TYPES.U8.id:
|
|
126
|
-
this.writeUint8(value);
|
|
127
|
-
break;
|
|
128
|
-
case JAELIS_TYPES.U16.id:
|
|
129
|
-
this.writeUint16(value);
|
|
130
|
-
break;
|
|
131
|
-
case JAELIS_TYPES.U32.id:
|
|
132
|
-
this.writeUint32(value);
|
|
133
|
-
break;
|
|
134
|
-
case JAELIS_TYPES.U64.id:
|
|
135
|
-
this.writeUint64(value);
|
|
136
|
-
break;
|
|
137
|
-
case JAELIS_TYPES.U128.id:
|
|
138
|
-
this.writeUint128(value);
|
|
139
|
-
break;
|
|
140
|
-
case JAELIS_TYPES.U256.id:
|
|
141
|
-
this.writeUint256(value);
|
|
142
|
-
break;
|
|
143
|
-
|
|
144
|
-
// Signed integers
|
|
145
|
-
case JAELIS_TYPES.I8.id:
|
|
146
|
-
this.writeInt8(value);
|
|
147
|
-
break;
|
|
148
|
-
case JAELIS_TYPES.I16.id:
|
|
149
|
-
this.writeInt16(value);
|
|
150
|
-
break;
|
|
151
|
-
case JAELIS_TYPES.I32.id:
|
|
152
|
-
this.writeInt32(value);
|
|
153
|
-
break;
|
|
154
|
-
case JAELIS_TYPES.I64.id:
|
|
155
|
-
this.writeInt64(value);
|
|
156
|
-
break;
|
|
157
|
-
case JAELIS_TYPES.I128.id:
|
|
158
|
-
this.writeInt128(value);
|
|
159
|
-
break;
|
|
160
|
-
case JAELIS_TYPES.I256.id:
|
|
161
|
-
this.writeInt256(value);
|
|
162
|
-
break;
|
|
163
|
-
|
|
164
|
-
// Floats
|
|
165
|
-
case JAELIS_TYPES.F32.id:
|
|
166
|
-
this.writeFloat32(value);
|
|
167
|
-
break;
|
|
168
|
-
case JAELIS_TYPES.F64.id:
|
|
169
|
-
this.writeFloat64(value);
|
|
170
|
-
break;
|
|
171
|
-
|
|
172
|
-
// Boolean
|
|
173
|
-
case JAELIS_TYPES.BOOL.id:
|
|
174
|
-
this.writeUint8(value ? 1 : 0);
|
|
175
|
-
break;
|
|
176
|
-
|
|
177
|
-
// Address (32 bytes)
|
|
178
|
-
case JAELIS_TYPES.ADDRESS.id:
|
|
179
|
-
this.writeAddress(value);
|
|
180
|
-
break;
|
|
181
|
-
|
|
182
|
-
// Fixed bytes
|
|
183
|
-
case JAELIS_TYPES.BYTES32.id:
|
|
184
|
-
this.writeBytes32(value);
|
|
185
|
-
break;
|
|
186
|
-
case JAELIS_TYPES.BYTES4.id:
|
|
187
|
-
this.writeBytes(value, 4);
|
|
188
|
-
break;
|
|
189
|
-
|
|
190
|
-
// Dynamic bytes
|
|
191
|
-
case JAELIS_TYPES.BYTES.id:
|
|
192
|
-
this.writeDynamicBytes(value);
|
|
193
|
-
break;
|
|
194
|
-
|
|
195
|
-
// String
|
|
196
|
-
case JAELIS_TYPES.STRING.id:
|
|
197
|
-
this.writeString(value);
|
|
198
|
-
break;
|
|
199
|
-
|
|
200
|
-
// Array
|
|
201
|
-
case JAELIS_TYPES.ARRAY.id:
|
|
202
|
-
this.writeArray(value.values, value.elementType);
|
|
203
|
-
break;
|
|
204
|
-
|
|
205
|
-
// Struct
|
|
206
|
-
case JAELIS_TYPES.STRUCT.id:
|
|
207
|
-
this.writeStruct(value.fields, value.types);
|
|
208
|
-
break;
|
|
209
|
-
|
|
210
|
-
// Tuple
|
|
211
|
-
case JAELIS_TYPES.TUPLE.id:
|
|
212
|
-
this.writeTuple(value.values, value.types);
|
|
213
|
-
break;
|
|
214
|
-
|
|
215
|
-
// Option
|
|
216
|
-
case JAELIS_TYPES.OPTION.id:
|
|
217
|
-
this.writeOption(value.value, value.innerType);
|
|
218
|
-
break;
|
|
219
|
-
|
|
220
|
-
// Result
|
|
221
|
-
case JAELIS_TYPES.RESULT.id:
|
|
222
|
-
this.writeResult(value.isOk, value.value, value.innerType);
|
|
223
|
-
break;
|
|
224
|
-
|
|
225
|
-
// Token amount (always U256)
|
|
226
|
-
case JAELIS_TYPES.TOKEN_AMOUNT.id:
|
|
227
|
-
this.writeUint256(value);
|
|
228
|
-
break;
|
|
229
|
-
|
|
230
|
-
// Hash
|
|
231
|
-
case JAELIS_TYPES.HASH.id:
|
|
232
|
-
this.writeBytes32(value);
|
|
233
|
-
break;
|
|
234
|
-
|
|
235
|
-
default:
|
|
236
|
-
throw new Error(`Encoding not implemented for type: ${type}`);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return this;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Encode multiple values
|
|
244
|
-
*/
|
|
245
|
-
encodeMultiple(values, types) {
|
|
246
|
-
// Write count
|
|
247
|
-
this.writeVarInt(values.length);
|
|
248
|
-
|
|
249
|
-
for (let i = 0; i < values.length; i++) {
|
|
250
|
-
this.encode(values[i], types[i]);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return this;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* Encode a function call
|
|
258
|
-
*/
|
|
259
|
-
encodeFunctionCall(functionName, args, argTypes) {
|
|
260
|
-
// Function selector (first 4 bytes of hash)
|
|
261
|
-
const selector = this.computeSelector(functionName, argTypes);
|
|
262
|
-
this.buffer.push(...selector);
|
|
263
|
-
|
|
264
|
-
// Arguments
|
|
265
|
-
this.encodeMultiple(args, argTypes);
|
|
266
|
-
|
|
267
|
-
return this;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Get encoded buffer
|
|
272
|
-
*/
|
|
273
|
-
toBuffer() {
|
|
274
|
-
return Buffer.from(this.buffer);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// ═══════════════════════════════════════════════════════════════
|
|
278
|
-
// PRIMITIVE WRITERS
|
|
279
|
-
// ═══════════════════════════════════════════════════════════════
|
|
280
|
-
|
|
281
|
-
writeUint8(value) {
|
|
282
|
-
this.buffer.push(value & 0xFF);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
writeInt8(value) {
|
|
286
|
-
this.buffer.push(value < 0 ? 256 + value : value);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
writeUint16(value) {
|
|
290
|
-
this.buffer.push(value & 0xFF);
|
|
291
|
-
this.buffer.push((value >> 8) & 0xFF);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
writeInt16(value) {
|
|
295
|
-
const v = value < 0 ? 65536 + value : value;
|
|
296
|
-
this.writeUint16(v);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
writeUint32(value) {
|
|
300
|
-
this.buffer.push(value & 0xFF);
|
|
301
|
-
this.buffer.push((value >> 8) & 0xFF);
|
|
302
|
-
this.buffer.push((value >> 16) & 0xFF);
|
|
303
|
-
this.buffer.push((value >> 24) & 0xFF);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
writeInt32(value) {
|
|
307
|
-
const v = value < 0 ? 4294967296 + value : value;
|
|
308
|
-
this.writeUint32(v);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
writeUint64(value) {
|
|
312
|
-
const v = BigInt(value);
|
|
313
|
-
for (let i = 0; i < 8; i++) {
|
|
314
|
-
this.buffer.push(Number((v >> BigInt(i * 8)) & 0xFFn));
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
writeInt64(value) {
|
|
319
|
-
const v = BigInt(value);
|
|
320
|
-
for (let i = 0; i < 8; i++) {
|
|
321
|
-
this.buffer.push(Number((v >> BigInt(i * 8)) & 0xFFn));
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
writeUint128(value) {
|
|
326
|
-
const v = BigInt(value);
|
|
327
|
-
for (let i = 0; i < 16; i++) {
|
|
328
|
-
this.buffer.push(Number((v >> BigInt(i * 8)) & 0xFFn));
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
writeUint256(value) {
|
|
333
|
-
const v = BigInt(value);
|
|
334
|
-
for (let i = 0; i < 32; i++) {
|
|
335
|
-
this.buffer.push(Number((v >> BigInt(i * 8)) & 0xFFn));
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
writeInt128(value) {
|
|
340
|
-
let v = BigInt(value);
|
|
341
|
-
// Handle negative values with two's complement
|
|
342
|
-
if (v < 0n) {
|
|
343
|
-
v = (1n << 128n) + v;
|
|
344
|
-
}
|
|
345
|
-
for (let i = 0; i < 16; i++) {
|
|
346
|
-
this.buffer.push(Number((v >> BigInt(i * 8)) & 0xFFn));
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
writeInt256(value) {
|
|
351
|
-
let v = BigInt(value);
|
|
352
|
-
// Handle negative values with two's complement
|
|
353
|
-
if (v < 0n) {
|
|
354
|
-
v = (1n << 256n) + v;
|
|
355
|
-
}
|
|
356
|
-
for (let i = 0; i < 32; i++) {
|
|
357
|
-
this.buffer.push(Number((v >> BigInt(i * 8)) & 0xFFn));
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
writeFloat32(value) {
|
|
362
|
-
const buf = Buffer.alloc(4);
|
|
363
|
-
buf.writeFloatLE(value);
|
|
364
|
-
for (let i = 0; i < 4; i++) {
|
|
365
|
-
this.buffer.push(buf[i]);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
writeFloat64(value) {
|
|
370
|
-
const buf = Buffer.alloc(8);
|
|
371
|
-
buf.writeDoubleLE(value);
|
|
372
|
-
for (let i = 0; i < 8; i++) {
|
|
373
|
-
this.buffer.push(buf[i]);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
writeVarInt(value) {
|
|
378
|
-
if (value < 0xFD) {
|
|
379
|
-
this.writeUint8(value);
|
|
380
|
-
} else if (value <= 0xFFFF) {
|
|
381
|
-
this.writeUint8(0xFD);
|
|
382
|
-
this.writeUint16(value);
|
|
383
|
-
} else if (value <= 0xFFFFFFFF) {
|
|
384
|
-
this.writeUint8(0xFE);
|
|
385
|
-
this.writeUint32(value);
|
|
386
|
-
} else {
|
|
387
|
-
this.writeUint8(0xFF);
|
|
388
|
-
this.writeUint64(value);
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
writeAddress(address) {
|
|
393
|
-
// Normalize to 32 bytes
|
|
394
|
-
let hex = address;
|
|
395
|
-
if (typeof address === 'string') {
|
|
396
|
-
hex = address.startsWith('0x') ? address.slice(2) : address;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// Pad to 64 hex chars (32 bytes)
|
|
400
|
-
hex = hex.padStart(64, '0');
|
|
401
|
-
|
|
402
|
-
for (let i = 0; i < 32; i++) {
|
|
403
|
-
this.buffer.push(parseInt(hex.substr(i * 2, 2), 16));
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
writeBytes32(value) {
|
|
408
|
-
let bytes;
|
|
409
|
-
if (typeof value === 'string') {
|
|
410
|
-
const hex = value.startsWith('0x') ? value.slice(2) : value;
|
|
411
|
-
bytes = Buffer.from(hex.padStart(64, '0'), 'hex');
|
|
412
|
-
} else if (Buffer.isBuffer(value)) {
|
|
413
|
-
bytes = value;
|
|
414
|
-
} else {
|
|
415
|
-
bytes = Buffer.alloc(32);
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// Ensure 32 bytes
|
|
419
|
-
const padded = Buffer.alloc(32);
|
|
420
|
-
bytes.copy(padded, 32 - bytes.length);
|
|
421
|
-
|
|
422
|
-
for (let i = 0; i < 32; i++) {
|
|
423
|
-
this.buffer.push(padded[i]);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
writeBytes(value, length) {
|
|
428
|
-
let bytes;
|
|
429
|
-
if (typeof value === 'string') {
|
|
430
|
-
const hex = value.startsWith('0x') ? value.slice(2) : value;
|
|
431
|
-
bytes = Buffer.from(hex, 'hex');
|
|
432
|
-
} else if (Buffer.isBuffer(value)) {
|
|
433
|
-
bytes = value;
|
|
434
|
-
} else {
|
|
435
|
-
bytes = Buffer.alloc(length);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
for (let i = 0; i < length; i++) {
|
|
439
|
-
this.buffer.push(bytes[i] || 0);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
writeDynamicBytes(value) {
|
|
444
|
-
let bytes;
|
|
445
|
-
if (typeof value === 'string') {
|
|
446
|
-
const hex = value.startsWith('0x') ? value.slice(2) : value;
|
|
447
|
-
bytes = Buffer.from(hex, 'hex');
|
|
448
|
-
} else if (Buffer.isBuffer(value)) {
|
|
449
|
-
bytes = value;
|
|
450
|
-
} else {
|
|
451
|
-
bytes = Buffer.alloc(0);
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
// Length prefix
|
|
455
|
-
this.writeVarInt(bytes.length);
|
|
456
|
-
|
|
457
|
-
// Data
|
|
458
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
459
|
-
this.buffer.push(bytes[i]);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
writeString(value) {
|
|
464
|
-
const bytes = Buffer.from(value, 'utf8');
|
|
465
|
-
this.writeVarInt(bytes.length);
|
|
466
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
467
|
-
this.buffer.push(bytes[i]);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
writeArray(values, elementType) {
|
|
472
|
-
this.writeVarInt(values.length);
|
|
473
|
-
for (const value of values) {
|
|
474
|
-
this.encode(value, elementType);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
writeStruct(fields, types) {
|
|
479
|
-
const keys = Object.keys(fields);
|
|
480
|
-
this.writeVarInt(keys.length);
|
|
481
|
-
|
|
482
|
-
for (let i = 0; i < keys.length; i++) {
|
|
483
|
-
// Write field name
|
|
484
|
-
this.writeString(keys[i]);
|
|
485
|
-
// Write field value
|
|
486
|
-
this.encode(fields[keys[i]], types[i]);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
writeOption(value, innerType) {
|
|
491
|
-
if (value === null || value === undefined) {
|
|
492
|
-
this.writeUint8(0); // None
|
|
493
|
-
} else {
|
|
494
|
-
this.writeUint8(1); // Some
|
|
495
|
-
this.encode(value, innerType);
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
writeTuple(values, types) {
|
|
500
|
-
// Tuple: fixed-size collection of heterogeneous values
|
|
501
|
-
this.writeVarInt(values.length);
|
|
502
|
-
for (let i = 0; i < values.length; i++) {
|
|
503
|
-
this.encode(values[i], types[i]);
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
writeResult(isOk, value, innerType) {
|
|
508
|
-
// Result: Ok(value) or Err(error)
|
|
509
|
-
if (isOk) {
|
|
510
|
-
this.writeUint8(1); // Ok
|
|
511
|
-
if (value !== undefined) {
|
|
512
|
-
this.encode(value, innerType);
|
|
513
|
-
}
|
|
514
|
-
} else {
|
|
515
|
-
this.writeUint8(0); // Err
|
|
516
|
-
if (value !== undefined) {
|
|
517
|
-
this.encode(value, innerType);
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
computeSelector(functionName, argTypes) {
|
|
523
|
-
const signature = `${functionName}(${argTypes.join(',')})`;
|
|
524
|
-
const hash = crypto.createHash('sha256').update(signature).digest();
|
|
525
|
-
return Array.from(hash.slice(0, 4));
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
// ═══════════════════════════════════════════════════════════════
|
|
530
|
-
// JAELIS ABI DECODER
|
|
531
|
-
// ═══════════════════════════════════════════════════════════════
|
|
532
|
-
|
|
533
|
-
class JaelisABIDecoder {
|
|
534
|
-
constructor(buffer) {
|
|
535
|
-
this.buffer = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
|
|
536
|
-
this.position = 0;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
/**
|
|
540
|
-
* Decode a value from JAELIS ABI format
|
|
541
|
-
* Returns { value, type } for self-describing format
|
|
542
|
-
*/
|
|
543
|
-
decode() {
|
|
544
|
-
// Read type ID
|
|
545
|
-
const typeId = this.readUint8();
|
|
546
|
-
const typeInfo = TYPE_BY_ID.get(typeId);
|
|
547
|
-
|
|
548
|
-
if (!typeInfo) {
|
|
549
|
-
throw new Error(`Unknown type ID: 0x${typeId.toString(16)}`);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
let value;
|
|
553
|
-
|
|
554
|
-
// Decode based on type
|
|
555
|
-
switch (typeId) {
|
|
556
|
-
case JAELIS_TYPES.U8.id:
|
|
557
|
-
value = this.readUint8();
|
|
558
|
-
break;
|
|
559
|
-
case JAELIS_TYPES.U16.id:
|
|
560
|
-
value = this.readUint16();
|
|
561
|
-
break;
|
|
562
|
-
case JAELIS_TYPES.U32.id:
|
|
563
|
-
value = this.readUint32();
|
|
564
|
-
break;
|
|
565
|
-
case JAELIS_TYPES.U64.id:
|
|
566
|
-
value = this.readUint64();
|
|
567
|
-
break;
|
|
568
|
-
case JAELIS_TYPES.U128.id:
|
|
569
|
-
value = this.readUint128();
|
|
570
|
-
break;
|
|
571
|
-
case JAELIS_TYPES.U256.id:
|
|
572
|
-
case JAELIS_TYPES.TOKEN_AMOUNT.id:
|
|
573
|
-
value = this.readUint256();
|
|
574
|
-
break;
|
|
575
|
-
|
|
576
|
-
case JAELIS_TYPES.I8.id:
|
|
577
|
-
value = this.readInt8();
|
|
578
|
-
break;
|
|
579
|
-
case JAELIS_TYPES.I16.id:
|
|
580
|
-
value = this.readInt16();
|
|
581
|
-
break;
|
|
582
|
-
case JAELIS_TYPES.I32.id:
|
|
583
|
-
value = this.readInt32();
|
|
584
|
-
break;
|
|
585
|
-
case JAELIS_TYPES.I64.id:
|
|
586
|
-
value = this.readInt64();
|
|
587
|
-
break;
|
|
588
|
-
case JAELIS_TYPES.I128.id:
|
|
589
|
-
value = this.readInt128();
|
|
590
|
-
break;
|
|
591
|
-
case JAELIS_TYPES.I256.id:
|
|
592
|
-
value = this.readInt256();
|
|
593
|
-
break;
|
|
594
|
-
|
|
595
|
-
// Floats
|
|
596
|
-
case JAELIS_TYPES.F32.id:
|
|
597
|
-
value = this.readFloat32();
|
|
598
|
-
break;
|
|
599
|
-
case JAELIS_TYPES.F64.id:
|
|
600
|
-
value = this.readFloat64();
|
|
601
|
-
break;
|
|
602
|
-
|
|
603
|
-
case JAELIS_TYPES.BOOL.id:
|
|
604
|
-
value = this.readUint8() !== 0;
|
|
605
|
-
break;
|
|
606
|
-
|
|
607
|
-
case JAELIS_TYPES.ADDRESS.id:
|
|
608
|
-
case JAELIS_TYPES.PUBKEY.id:
|
|
609
|
-
value = this.readAddress();
|
|
610
|
-
break;
|
|
611
|
-
|
|
612
|
-
case JAELIS_TYPES.BYTES32.id:
|
|
613
|
-
case JAELIS_TYPES.HASH.id:
|
|
614
|
-
value = this.readBytes32();
|
|
615
|
-
break;
|
|
616
|
-
|
|
617
|
-
case JAELIS_TYPES.BYTES4.id:
|
|
618
|
-
value = this.readBytes(4);
|
|
619
|
-
break;
|
|
620
|
-
|
|
621
|
-
case JAELIS_TYPES.BYTES.id:
|
|
622
|
-
value = this.readDynamicBytes();
|
|
623
|
-
break;
|
|
624
|
-
|
|
625
|
-
case JAELIS_TYPES.STRING.id:
|
|
626
|
-
value = this.readString();
|
|
627
|
-
break;
|
|
628
|
-
|
|
629
|
-
case JAELIS_TYPES.ARRAY.id:
|
|
630
|
-
value = this.readArray();
|
|
631
|
-
break;
|
|
632
|
-
|
|
633
|
-
case JAELIS_TYPES.STRUCT.id:
|
|
634
|
-
value = this.readStruct();
|
|
635
|
-
break;
|
|
636
|
-
|
|
637
|
-
case JAELIS_TYPES.TUPLE.id:
|
|
638
|
-
value = this.readTuple();
|
|
639
|
-
break;
|
|
640
|
-
|
|
641
|
-
case JAELIS_TYPES.OPTION.id:
|
|
642
|
-
value = this.readOption();
|
|
643
|
-
break;
|
|
644
|
-
|
|
645
|
-
case JAELIS_TYPES.RESULT.id:
|
|
646
|
-
value = this.readResult();
|
|
647
|
-
break;
|
|
648
|
-
|
|
649
|
-
case JAELIS_TYPES.NULL.id:
|
|
650
|
-
value = null;
|
|
651
|
-
break;
|
|
652
|
-
|
|
653
|
-
default:
|
|
654
|
-
throw new Error(`Decoding not implemented for type ID: 0x${typeId.toString(16)}`);
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
return { value, type: typeInfo.name };
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
/**
|
|
661
|
-
* Decode multiple values
|
|
662
|
-
*/
|
|
663
|
-
decodeMultiple() {
|
|
664
|
-
const count = this.readVarInt();
|
|
665
|
-
const values = [];
|
|
666
|
-
|
|
667
|
-
for (let i = 0; i < count; i++) {
|
|
668
|
-
values.push(this.decode());
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
return values;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
/**
|
|
675
|
-
* Decode function call
|
|
676
|
-
*/
|
|
677
|
-
decodeFunctionCall() {
|
|
678
|
-
// Read selector (4 bytes)
|
|
679
|
-
const selector = this.readBytes(4);
|
|
680
|
-
|
|
681
|
-
// Decode arguments
|
|
682
|
-
const args = this.decodeMultiple();
|
|
683
|
-
|
|
684
|
-
return { selector, args };
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
// ═══════════════════════════════════════════════════════════════
|
|
688
|
-
// PRIMITIVE READERS
|
|
689
|
-
// ═══════════════════════════════════════════════════════════════
|
|
690
|
-
|
|
691
|
-
readUint8() {
|
|
692
|
-
return this.buffer[this.position++];
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
readInt8() {
|
|
696
|
-
const v = this.readUint8();
|
|
697
|
-
return v > 127 ? v - 256 : v;
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
readUint16() {
|
|
701
|
-
const v = this.buffer[this.position] | (this.buffer[this.position + 1] << 8);
|
|
702
|
-
this.position += 2;
|
|
703
|
-
return v;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
readInt16() {
|
|
707
|
-
const v = this.readUint16();
|
|
708
|
-
return v > 32767 ? v - 65536 : v;
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
readUint32() {
|
|
712
|
-
const v = this.buffer[this.position] |
|
|
713
|
-
(this.buffer[this.position + 1] << 8) |
|
|
714
|
-
(this.buffer[this.position + 2] << 16) |
|
|
715
|
-
(this.buffer[this.position + 3] << 24);
|
|
716
|
-
this.position += 4;
|
|
717
|
-
return v >>> 0;
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
readInt32() {
|
|
721
|
-
const v = this.readUint32();
|
|
722
|
-
return v > 2147483647 ? v - 4294967296 : v;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
readUint64() {
|
|
726
|
-
let result = 0n;
|
|
727
|
-
for (let i = 0; i < 8; i++) {
|
|
728
|
-
result |= BigInt(this.buffer[this.position + i]) << BigInt(i * 8);
|
|
729
|
-
}
|
|
730
|
-
this.position += 8;
|
|
731
|
-
return result;
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
readInt64() {
|
|
735
|
-
const v = this.readUint64();
|
|
736
|
-
if (v >= 2n ** 63n) {
|
|
737
|
-
return v - 2n ** 64n;
|
|
738
|
-
}
|
|
739
|
-
return v;
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
readUint128() {
|
|
743
|
-
let result = 0n;
|
|
744
|
-
for (let i = 0; i < 16; i++) {
|
|
745
|
-
result |= BigInt(this.buffer[this.position + i]) << BigInt(i * 8);
|
|
746
|
-
}
|
|
747
|
-
this.position += 16;
|
|
748
|
-
return result;
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
readUint256() {
|
|
752
|
-
let result = 0n;
|
|
753
|
-
for (let i = 0; i < 32; i++) {
|
|
754
|
-
result |= BigInt(this.buffer[this.position + i]) << BigInt(i * 8);
|
|
755
|
-
}
|
|
756
|
-
this.position += 32;
|
|
757
|
-
return result;
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
readVarInt() {
|
|
761
|
-
const first = this.readUint8();
|
|
762
|
-
if (first < 0xFD) {
|
|
763
|
-
return first;
|
|
764
|
-
} else if (first === 0xFD) {
|
|
765
|
-
return this.readUint16();
|
|
766
|
-
} else if (first === 0xFE) {
|
|
767
|
-
return this.readUint32();
|
|
768
|
-
} else {
|
|
769
|
-
return Number(this.readUint64());
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
readAddress() {
|
|
774
|
-
const bytes = this.buffer.slice(this.position, this.position + 32);
|
|
775
|
-
this.position += 32;
|
|
776
|
-
return '0x' + bytes.toString('hex');
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
readBytes32() {
|
|
780
|
-
const bytes = this.buffer.slice(this.position, this.position + 32);
|
|
781
|
-
this.position += 32;
|
|
782
|
-
return '0x' + bytes.toString('hex');
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
readBytes(length) {
|
|
786
|
-
const bytes = this.buffer.slice(this.position, this.position + length);
|
|
787
|
-
this.position += length;
|
|
788
|
-
return bytes;
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
readDynamicBytes() {
|
|
792
|
-
const length = this.readVarInt();
|
|
793
|
-
const bytes = this.buffer.slice(this.position, this.position + length);
|
|
794
|
-
this.position += length;
|
|
795
|
-
return bytes;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
readString() {
|
|
799
|
-
const length = this.readVarInt();
|
|
800
|
-
const bytes = this.buffer.slice(this.position, this.position + length);
|
|
801
|
-
this.position += length;
|
|
802
|
-
return bytes.toString('utf8');
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
readArray() {
|
|
806
|
-
const count = this.readVarInt();
|
|
807
|
-
const values = [];
|
|
808
|
-
for (let i = 0; i < count; i++) {
|
|
809
|
-
values.push(this.decode());
|
|
810
|
-
}
|
|
811
|
-
return values;
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
readStruct() {
|
|
815
|
-
const count = this.readVarInt();
|
|
816
|
-
const result = {};
|
|
817
|
-
for (let i = 0; i < count; i++) {
|
|
818
|
-
const key = this.readString();
|
|
819
|
-
const value = this.decode();
|
|
820
|
-
result[key] = value;
|
|
821
|
-
}
|
|
822
|
-
return result;
|
|
823
|
-
}
|
|
824
|
-
|
|
825
|
-
readOption() {
|
|
826
|
-
const hasValue = this.readUint8() !== 0;
|
|
827
|
-
if (hasValue) {
|
|
828
|
-
return this.decode();
|
|
829
|
-
}
|
|
830
|
-
return null;
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
readInt128() {
|
|
834
|
-
let result = 0n;
|
|
835
|
-
for (let i = 0; i < 16; i++) {
|
|
836
|
-
result |= BigInt(this.buffer[this.position + i]) << BigInt(i * 8);
|
|
837
|
-
}
|
|
838
|
-
this.position += 16;
|
|
839
|
-
// Handle two's complement for negative values
|
|
840
|
-
if (result >= (1n << 127n)) {
|
|
841
|
-
result = result - (1n << 128n);
|
|
842
|
-
}
|
|
843
|
-
return result;
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
readInt256() {
|
|
847
|
-
let result = 0n;
|
|
848
|
-
for (let i = 0; i < 32; i++) {
|
|
849
|
-
result |= BigInt(this.buffer[this.position + i]) << BigInt(i * 8);
|
|
850
|
-
}
|
|
851
|
-
this.position += 32;
|
|
852
|
-
// Handle two's complement for negative values
|
|
853
|
-
if (result >= (1n << 255n)) {
|
|
854
|
-
result = result - (1n << 256n);
|
|
855
|
-
}
|
|
856
|
-
return result;
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
readFloat32() {
|
|
860
|
-
const bytes = this.buffer.slice(this.position, this.position + 4);
|
|
861
|
-
this.position += 4;
|
|
862
|
-
return bytes.readFloatLE(0);
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
readFloat64() {
|
|
866
|
-
const bytes = this.buffer.slice(this.position, this.position + 8);
|
|
867
|
-
this.position += 8;
|
|
868
|
-
return bytes.readDoubleLE(0);
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
readTuple() {
|
|
872
|
-
const count = this.readVarInt();
|
|
873
|
-
const values = [];
|
|
874
|
-
for (let i = 0; i < count; i++) {
|
|
875
|
-
values.push(this.decode());
|
|
876
|
-
}
|
|
877
|
-
return values;
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
readResult() {
|
|
881
|
-
const isOk = this.readUint8() !== 0;
|
|
882
|
-
const value = this.decode();
|
|
883
|
-
return {
|
|
884
|
-
isOk,
|
|
885
|
-
value: value.value,
|
|
886
|
-
type: value.type
|
|
887
|
-
};
|
|
888
|
-
}
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
// ═══════════════════════════════════════════════════════════════
|
|
892
|
-
// JAELIS ABI (MAIN INTERFACE)
|
|
893
|
-
// ═══════════════════════════════════════════════════════════════
|
|
894
|
-
|
|
895
|
-
class JaelisABI {
|
|
896
|
-
/**
|
|
897
|
-
* Encode a value
|
|
898
|
-
*/
|
|
899
|
-
static encode(value, type) {
|
|
900
|
-
const encoder = new JaelisABIEncoder();
|
|
901
|
-
encoder.encode(value, type);
|
|
902
|
-
return encoder.toBuffer();
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
/**
|
|
906
|
-
* Encode multiple values
|
|
907
|
-
*/
|
|
908
|
-
static encodeMultiple(values, types) {
|
|
909
|
-
const encoder = new JaelisABIEncoder();
|
|
910
|
-
encoder.encodeMultiple(values, types);
|
|
911
|
-
return encoder.toBuffer();
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
/**
|
|
915
|
-
* Encode a function call
|
|
916
|
-
*/
|
|
917
|
-
static encodeFunctionCall(functionName, args, argTypes) {
|
|
918
|
-
const encoder = new JaelisABIEncoder();
|
|
919
|
-
encoder.encodeFunctionCall(functionName, args, argTypes);
|
|
920
|
-
return encoder.toBuffer();
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
/**
|
|
924
|
-
* Decode a value
|
|
925
|
-
*/
|
|
926
|
-
static decode(buffer) {
|
|
927
|
-
const decoder = new JaelisABIDecoder(buffer);
|
|
928
|
-
return decoder.decode();
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
/**
|
|
932
|
-
* Decode multiple values
|
|
933
|
-
*/
|
|
934
|
-
static decodeMultiple(buffer) {
|
|
935
|
-
const decoder = new JaelisABIDecoder(buffer);
|
|
936
|
-
return decoder.decodeMultiple();
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
/**
|
|
940
|
-
* Decode a function call
|
|
941
|
-
*/
|
|
942
|
-
static decodeFunctionCall(buffer) {
|
|
943
|
-
const decoder = new JaelisABIDecoder(buffer);
|
|
944
|
-
return decoder.decodeFunctionCall();
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
/**
|
|
948
|
-
* Compute function selector
|
|
949
|
-
*/
|
|
950
|
-
static computeSelector(functionName, argTypes = []) {
|
|
951
|
-
const signature = `${functionName}(${argTypes.join(',')})`;
|
|
952
|
-
return crypto.createHash('sha256').update(signature).digest().slice(0, 4);
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
/**
|
|
956
|
-
* Get type info
|
|
957
|
-
*/
|
|
958
|
-
static getType(typeName) {
|
|
959
|
-
return JAELIS_TYPES[typeName];
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
/**
|
|
963
|
-
* Get all types
|
|
964
|
-
*/
|
|
965
|
-
static get TYPES() {
|
|
966
|
-
return JAELIS_TYPES;
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
// ═══════════════════════════════════════════════════════════════
|
|
971
|
-
// HELPER: CONVERT FROM OTHER ABIS
|
|
972
|
-
// ═══════════════════════════════════════════════════════════════
|
|
973
|
-
|
|
974
|
-
class ABIConverter {
|
|
975
|
-
/**
|
|
976
|
-
* Convert Solidity ABI to JAELIS ABI
|
|
977
|
-
*/
|
|
978
|
-
static fromSolidityABI(solidityABI) {
|
|
979
|
-
const jaelisABI = [];
|
|
980
|
-
|
|
981
|
-
for (const item of solidityABI) {
|
|
982
|
-
if (item.type === 'function') {
|
|
983
|
-
jaelisABI.push({
|
|
984
|
-
type: 'function',
|
|
985
|
-
name: item.name,
|
|
986
|
-
inputs: item.inputs.map(i => ({
|
|
987
|
-
name: i.name,
|
|
988
|
-
type: this.solidityTypeToJaelis(i.type)
|
|
989
|
-
})),
|
|
990
|
-
outputs: (item.outputs || []).map(o => ({
|
|
991
|
-
name: o.name,
|
|
992
|
-
type: this.solidityTypeToJaelis(o.type)
|
|
993
|
-
})),
|
|
994
|
-
selector: JaelisABI.computeSelector(
|
|
995
|
-
item.name,
|
|
996
|
-
item.inputs.map(i => this.solidityTypeToJaelis(i.type))
|
|
997
|
-
)
|
|
998
|
-
});
|
|
999
|
-
} else if (item.type === 'event') {
|
|
1000
|
-
jaelisABI.push({
|
|
1001
|
-
type: 'event',
|
|
1002
|
-
name: item.name,
|
|
1003
|
-
inputs: item.inputs.map(i => ({
|
|
1004
|
-
name: i.name,
|
|
1005
|
-
type: this.solidityTypeToJaelis(i.type),
|
|
1006
|
-
indexed: i.indexed
|
|
1007
|
-
}))
|
|
1008
|
-
});
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
|
|
1012
|
-
return jaelisABI;
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
/**
|
|
1016
|
-
* Convert Solidity type to JAELIS type
|
|
1017
|
-
*/
|
|
1018
|
-
static solidityTypeToJaelis(solidityType) {
|
|
1019
|
-
const typeMap = {
|
|
1020
|
-
'uint8': 'U8',
|
|
1021
|
-
'uint16': 'U16',
|
|
1022
|
-
'uint32': 'U32',
|
|
1023
|
-
'uint64': 'U64',
|
|
1024
|
-
'uint128': 'U128',
|
|
1025
|
-
'uint256': 'U256',
|
|
1026
|
-
'int8': 'I8',
|
|
1027
|
-
'int16': 'I16',
|
|
1028
|
-
'int32': 'I32',
|
|
1029
|
-
'int64': 'I64',
|
|
1030
|
-
'int128': 'I128',
|
|
1031
|
-
'int256': 'I256',
|
|
1032
|
-
'bool': 'BOOL',
|
|
1033
|
-
'address': 'ADDRESS',
|
|
1034
|
-
'bytes': 'BYTES',
|
|
1035
|
-
'bytes4': 'BYTES4',
|
|
1036
|
-
'bytes32': 'BYTES32',
|
|
1037
|
-
'string': 'STRING'
|
|
1038
|
-
};
|
|
1039
|
-
|
|
1040
|
-
// Handle arrays
|
|
1041
|
-
if (solidityType.endsWith('[]')) {
|
|
1042
|
-
return 'ARRAY';
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
|
-
return typeMap[solidityType] || 'U256';
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
/**
|
|
1049
|
-
* Convert Anchor/Borsh IDL to JAELIS ABI
|
|
1050
|
-
*/
|
|
1051
|
-
static fromAnchorIDL(idl) {
|
|
1052
|
-
const jaelisABI = [];
|
|
1053
|
-
|
|
1054
|
-
for (const instruction of idl.instructions || []) {
|
|
1055
|
-
jaelisABI.push({
|
|
1056
|
-
type: 'function',
|
|
1057
|
-
name: instruction.name,
|
|
1058
|
-
inputs: (instruction.args || []).map(a => ({
|
|
1059
|
-
name: a.name,
|
|
1060
|
-
type: this.rustTypeToJaelis(a.type)
|
|
1061
|
-
})),
|
|
1062
|
-
outputs: [],
|
|
1063
|
-
selector: JaelisABI.computeSelector(instruction.name, [])
|
|
1064
|
-
});
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
return jaelisABI;
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
|
-
/**
|
|
1071
|
-
* Convert Rust type to JAELIS type
|
|
1072
|
-
*/
|
|
1073
|
-
static rustTypeToJaelis(rustType) {
|
|
1074
|
-
const typeMap = {
|
|
1075
|
-
'u8': 'U8',
|
|
1076
|
-
'u16': 'U16',
|
|
1077
|
-
'u32': 'U32',
|
|
1078
|
-
'u64': 'U64',
|
|
1079
|
-
'u128': 'U128',
|
|
1080
|
-
'i8': 'I8',
|
|
1081
|
-
'i16': 'I16',
|
|
1082
|
-
'i32': 'I32',
|
|
1083
|
-
'i64': 'I64',
|
|
1084
|
-
'i128': 'I128',
|
|
1085
|
-
'bool': 'BOOL',
|
|
1086
|
-
'Pubkey': 'ADDRESS',
|
|
1087
|
-
'String': 'STRING'
|
|
1088
|
-
};
|
|
1089
|
-
|
|
1090
|
-
if (typeof rustType === 'object') {
|
|
1091
|
-
if (rustType.vec) return 'ARRAY';
|
|
1092
|
-
if (rustType.option) return 'OPTION';
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
|
-
return typeMap[rustType] || 'U256';
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
/**
|
|
1099
|
-
* Convert Move ABI to JAELIS ABI
|
|
1100
|
-
*/
|
|
1101
|
-
static fromMoveABI(moveABI) {
|
|
1102
|
-
const jaelisABI = [];
|
|
1103
|
-
|
|
1104
|
-
for (const func of moveABI.exposed_functions || []) {
|
|
1105
|
-
jaelisABI.push({
|
|
1106
|
-
type: 'function',
|
|
1107
|
-
name: func.name,
|
|
1108
|
-
inputs: (func.params || []).map((p, i) => ({
|
|
1109
|
-
name: `arg${i}`,
|
|
1110
|
-
type: this.moveTypeToJaelis(p)
|
|
1111
|
-
})),
|
|
1112
|
-
outputs: (func.return || []).map((r, i) => ({
|
|
1113
|
-
name: `ret${i}`,
|
|
1114
|
-
type: this.moveTypeToJaelis(r)
|
|
1115
|
-
})),
|
|
1116
|
-
selector: JaelisABI.computeSelector(func.name, [])
|
|
1117
|
-
});
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
return jaelisABI;
|
|
1121
|
-
}
|
|
1122
|
-
|
|
1123
|
-
/**
|
|
1124
|
-
* Convert Move type to JAELIS type
|
|
1125
|
-
*/
|
|
1126
|
-
static moveTypeToJaelis(moveType) {
|
|
1127
|
-
const typeMap = {
|
|
1128
|
-
'u8': 'U8',
|
|
1129
|
-
'u16': 'U16',
|
|
1130
|
-
'u32': 'U32',
|
|
1131
|
-
'u64': 'U64',
|
|
1132
|
-
'u128': 'U128',
|
|
1133
|
-
'u256': 'U256',
|
|
1134
|
-
'bool': 'BOOL',
|
|
1135
|
-
'address': 'ADDRESS',
|
|
1136
|
-
'vector<u8>': 'BYTES',
|
|
1137
|
-
'0x1::string::String': 'STRING'
|
|
1138
|
-
};
|
|
1139
|
-
|
|
1140
|
-
return typeMap[moveType] || 'U256';
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
module.exports = {
|
|
1145
|
-
JaelisABI,
|
|
1146
|
-
JaelisABIEncoder,
|
|
1147
|
-
JaelisABIDecoder,
|
|
1148
|
-
ABIConverter,
|
|
1149
|
-
JAELIS_TYPES
|
|
1150
|
-
};
|