jaelis-node 1.10.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 +146 -445
- package/bin/jaelis-node.js +79 -504
- package/lib/index.js +31 -2840
- 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,819 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JAELIS FAST SERIALIZATION
|
|
3
|
-
*
|
|
4
|
-
* High-performance serialization using:
|
|
5
|
-
* - CBOR-X: 3-10x faster than JSON
|
|
6
|
-
* - MessagePack: 37% smaller than JSON
|
|
7
|
-
* - Zero-copy deserialization (Wincode-inspired)
|
|
8
|
-
*
|
|
9
|
-
* Benchmarks:
|
|
10
|
-
* ┌──────────────┬────────────┬────────────┬──────────────┐
|
|
11
|
-
* │ Format │ Encode │ Decode │ Size │
|
|
12
|
-
* ├──────────────┼────────────┼────────────┼──────────────┤
|
|
13
|
-
* │ JSON │ 1x │ 1x │ 1x │
|
|
14
|
-
* │ CBOR-X │ 3-10x │ 3-10x │ 0.7x │
|
|
15
|
-
* │ MessagePack │ 2-5x │ 2-5x │ 0.63x │
|
|
16
|
-
* │ Zero-copy │ 100x+ │ 100x+ │ 1x (binary) │
|
|
17
|
-
* └──────────────┴────────────┴────────────┴──────────────┘
|
|
18
|
-
*
|
|
19
|
-
* @version 0.1.0
|
|
20
|
-
* @author Mario Papaleo - JAELIS Foundation
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
// Try to load fast serializers
|
|
24
|
-
let Encoder, Decoder, pack, unpack;
|
|
25
|
-
try {
|
|
26
|
-
const cborx = require('cbor-x');
|
|
27
|
-
Encoder = cborx.Encoder;
|
|
28
|
-
Decoder = cborx.Decoder;
|
|
29
|
-
} catch (e) {
|
|
30
|
-
// Will use fallback
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const msgpackr = require('msgpackr');
|
|
35
|
-
pack = msgpackr.pack;
|
|
36
|
-
unpack = msgpackr.unpack;
|
|
37
|
-
} catch (e) {
|
|
38
|
-
// Will use fallback
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Custom type handlers for blockchain types
|
|
43
|
-
*/
|
|
44
|
-
const CUSTOM_TYPES = {
|
|
45
|
-
// BigInt (not natively supported in JSON/CBOR)
|
|
46
|
-
BIGINT: 0x01,
|
|
47
|
-
|
|
48
|
-
// Uint256 (32-byte big int)
|
|
49
|
-
UINT256: 0x02,
|
|
50
|
-
|
|
51
|
-
// Address (20 bytes for EVM, 32 for Solana)
|
|
52
|
-
ADDRESS: 0x03,
|
|
53
|
-
|
|
54
|
-
// Bytes32
|
|
55
|
-
BYTES32: 0x04,
|
|
56
|
-
|
|
57
|
-
// Contract call data
|
|
58
|
-
CALLDATA: 0x05,
|
|
59
|
-
|
|
60
|
-
// Transaction
|
|
61
|
-
TRANSACTION: 0x06,
|
|
62
|
-
|
|
63
|
-
// State proof
|
|
64
|
-
STATE_PROOF: 0x07
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Zero-Copy Buffer View
|
|
69
|
-
*
|
|
70
|
-
* Inspired by Wincode - provides views into binary data without copying
|
|
71
|
-
*/
|
|
72
|
-
class ZeroCopyView {
|
|
73
|
-
constructor(buffer, offset = 0, length = -1) {
|
|
74
|
-
this.buffer = buffer;
|
|
75
|
-
this.offset = offset;
|
|
76
|
-
this.length = length === -1 ? buffer.length - offset : length;
|
|
77
|
-
this.position = 0;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Read primitives without copying
|
|
81
|
-
readUint8() {
|
|
82
|
-
const value = this.buffer[this.offset + this.position];
|
|
83
|
-
this.position++;
|
|
84
|
-
return value;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
readUint16LE() {
|
|
88
|
-
const value = this.buffer.readUInt16LE(this.offset + this.position);
|
|
89
|
-
this.position += 2;
|
|
90
|
-
return value;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
readUint32LE() {
|
|
94
|
-
const value = this.buffer.readUInt32LE(this.offset + this.position);
|
|
95
|
-
this.position += 4;
|
|
96
|
-
return value;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
readUint64LE() {
|
|
100
|
-
const value = this.buffer.readBigUInt64LE(this.offset + this.position);
|
|
101
|
-
this.position += 8;
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
readInt32LE() {
|
|
106
|
-
const value = this.buffer.readInt32LE(this.offset + this.position);
|
|
107
|
-
this.position += 4;
|
|
108
|
-
return value;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
readInt64LE() {
|
|
112
|
-
const value = this.buffer.readBigInt64LE(this.offset + this.position);
|
|
113
|
-
this.position += 8;
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
readFloat32LE() {
|
|
118
|
-
const value = this.buffer.readFloatLE(this.offset + this.position);
|
|
119
|
-
this.position += 4;
|
|
120
|
-
return value;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
readFloat64LE() {
|
|
124
|
-
const value = this.buffer.readDoubleLE(this.offset + this.position);
|
|
125
|
-
this.position += 8;
|
|
126
|
-
return value;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Zero-copy slice - returns a view, not a copy
|
|
130
|
-
slice(length) {
|
|
131
|
-
const view = new ZeroCopyView(this.buffer, this.offset + this.position, length);
|
|
132
|
-
this.position += length;
|
|
133
|
-
return view;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Get underlying buffer (for when you really need it)
|
|
137
|
-
toBuffer() {
|
|
138
|
-
return this.buffer.slice(this.offset, this.offset + this.length);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Read variable-length integer (compact encoding)
|
|
142
|
-
readVarInt() {
|
|
143
|
-
const first = this.readUint8();
|
|
144
|
-
|
|
145
|
-
if (first < 0xFD) {
|
|
146
|
-
return first;
|
|
147
|
-
} else if (first === 0xFD) {
|
|
148
|
-
return this.readUint16LE();
|
|
149
|
-
} else if (first === 0xFE) {
|
|
150
|
-
return this.readUint32LE();
|
|
151
|
-
} else {
|
|
152
|
-
return Number(this.readUint64LE());
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Read length-prefixed bytes (zero-copy)
|
|
157
|
-
readBytes() {
|
|
158
|
-
const length = this.readVarInt();
|
|
159
|
-
return this.slice(length);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Read length-prefixed string
|
|
163
|
-
readString() {
|
|
164
|
-
const bytes = this.readBytes();
|
|
165
|
-
return bytes.toBuffer().toString('utf8');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Read BigInt (256-bit)
|
|
169
|
-
readUint256() {
|
|
170
|
-
const bytes = this.slice(32);
|
|
171
|
-
let result = 0n;
|
|
172
|
-
for (let i = 31; i >= 0; i--) {
|
|
173
|
-
result = (result << 8n) | BigInt(bytes.buffer[bytes.offset + i]);
|
|
174
|
-
}
|
|
175
|
-
return result;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Read address (20 or 32 bytes)
|
|
179
|
-
readAddress(length = 20) {
|
|
180
|
-
const bytes = this.slice(length);
|
|
181
|
-
return '0x' + bytes.toBuffer().toString('hex');
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
remaining() {
|
|
185
|
-
return this.length - this.position;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
hasMore() {
|
|
189
|
-
return this.position < this.length;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Zero-Copy Buffer Writer
|
|
195
|
-
*/
|
|
196
|
-
class ZeroCopyWriter {
|
|
197
|
-
constructor(initialSize = 1024) {
|
|
198
|
-
this.buffer = Buffer.alloc(initialSize);
|
|
199
|
-
this.position = 0;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
_ensureCapacity(needed) {
|
|
203
|
-
if (this.position + needed > this.buffer.length) {
|
|
204
|
-
const newSize = Math.max(this.buffer.length * 2, this.position + needed);
|
|
205
|
-
const newBuffer = Buffer.alloc(newSize);
|
|
206
|
-
this.buffer.copy(newBuffer);
|
|
207
|
-
this.buffer = newBuffer;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
writeUint8(value) {
|
|
212
|
-
this._ensureCapacity(1);
|
|
213
|
-
this.buffer[this.position++] = value;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
writeUint16LE(value) {
|
|
217
|
-
this._ensureCapacity(2);
|
|
218
|
-
this.buffer.writeUInt16LE(value, this.position);
|
|
219
|
-
this.position += 2;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
writeUint32LE(value) {
|
|
223
|
-
this._ensureCapacity(4);
|
|
224
|
-
this.buffer.writeUInt32LE(value, this.position);
|
|
225
|
-
this.position += 4;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
writeUint64LE(value) {
|
|
229
|
-
this._ensureCapacity(8);
|
|
230
|
-
this.buffer.writeBigUInt64LE(BigInt(value), this.position);
|
|
231
|
-
this.position += 8;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
writeInt32LE(value) {
|
|
235
|
-
this._ensureCapacity(4);
|
|
236
|
-
this.buffer.writeInt32LE(value, this.position);
|
|
237
|
-
this.position += 4;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
writeInt64LE(value) {
|
|
241
|
-
this._ensureCapacity(8);
|
|
242
|
-
this.buffer.writeBigInt64LE(BigInt(value), this.position);
|
|
243
|
-
this.position += 8;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
writeFloat64LE(value) {
|
|
247
|
-
this._ensureCapacity(8);
|
|
248
|
-
this.buffer.writeDoubleLE(value, this.position);
|
|
249
|
-
this.position += 8;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
writeBuffer(buffer) {
|
|
253
|
-
this._ensureCapacity(buffer.length);
|
|
254
|
-
buffer.copy(this.buffer, this.position);
|
|
255
|
-
this.position += buffer.length;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
writeVarInt(value) {
|
|
259
|
-
if (value < 0xFD) {
|
|
260
|
-
this.writeUint8(value);
|
|
261
|
-
} else if (value <= 0xFFFF) {
|
|
262
|
-
this.writeUint8(0xFD);
|
|
263
|
-
this.writeUint16LE(value);
|
|
264
|
-
} else if (value <= 0xFFFFFFFF) {
|
|
265
|
-
this.writeUint8(0xFE);
|
|
266
|
-
this.writeUint32LE(value);
|
|
267
|
-
} else {
|
|
268
|
-
this.writeUint8(0xFF);
|
|
269
|
-
this.writeUint64LE(value);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
writeBytes(buffer) {
|
|
274
|
-
this.writeVarInt(buffer.length);
|
|
275
|
-
this.writeBuffer(buffer);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
writeString(str) {
|
|
279
|
-
const buffer = Buffer.from(str, 'utf8');
|
|
280
|
-
this.writeBytes(buffer);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
writeUint256(value) {
|
|
284
|
-
this._ensureCapacity(32);
|
|
285
|
-
let v = BigInt(value);
|
|
286
|
-
for (let i = 0; i < 32; i++) {
|
|
287
|
-
this.buffer[this.position + i] = Number(v & 0xffn);
|
|
288
|
-
v >>= 8n;
|
|
289
|
-
}
|
|
290
|
-
this.position += 32;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
writeAddress(address, length = 20) {
|
|
294
|
-
const hex = address.startsWith('0x') ? address.slice(2) : address;
|
|
295
|
-
const buffer = Buffer.from(hex.padStart(length * 2, '0'), 'hex');
|
|
296
|
-
this._ensureCapacity(length);
|
|
297
|
-
buffer.copy(this.buffer, this.position, 0, length);
|
|
298
|
-
this.position += length;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
toBuffer() {
|
|
302
|
-
return this.buffer.slice(0, this.position);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* JAELIS Serializer
|
|
308
|
-
*
|
|
309
|
-
* Fast serialization with multiple format support
|
|
310
|
-
*/
|
|
311
|
-
class Serializer {
|
|
312
|
-
constructor(config = {}) {
|
|
313
|
-
this.config = {
|
|
314
|
-
format: config.serializer || 'cbor',
|
|
315
|
-
zeroCopy: config.zeroCopy !== false,
|
|
316
|
-
...config
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
// Initialize encoders based on available libraries
|
|
320
|
-
if (this.config.format === 'cbor' && Encoder) {
|
|
321
|
-
this.cborEncoder = new Encoder({
|
|
322
|
-
structures: [],
|
|
323
|
-
useRecords: false
|
|
324
|
-
});
|
|
325
|
-
this.cborDecoder = new Decoder();
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
// Stats
|
|
329
|
-
this.stats = {
|
|
330
|
-
encodes: 0,
|
|
331
|
-
decodes: 0,
|
|
332
|
-
bytesEncoded: 0,
|
|
333
|
-
bytesDecoded: 0,
|
|
334
|
-
totalEncodeTime: 0,
|
|
335
|
-
totalDecodeTime: 0
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
console.log(`[Serializer] Using ${this.config.format.toUpperCase()} format`);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Serialize data to binary
|
|
343
|
-
*
|
|
344
|
-
* @param {any} data - Data to serialize
|
|
345
|
-
* @returns {Buffer} Serialized data
|
|
346
|
-
*/
|
|
347
|
-
serialize(data) {
|
|
348
|
-
const start = Date.now();
|
|
349
|
-
|
|
350
|
-
let result;
|
|
351
|
-
|
|
352
|
-
switch (this.config.format) {
|
|
353
|
-
case 'cbor':
|
|
354
|
-
result = this._serializeCBOR(data);
|
|
355
|
-
break;
|
|
356
|
-
case 'msgpack':
|
|
357
|
-
result = this._serializeMsgPack(data);
|
|
358
|
-
break;
|
|
359
|
-
case 'zerocopy':
|
|
360
|
-
result = this._serializeZeroCopy(data);
|
|
361
|
-
break;
|
|
362
|
-
case 'json':
|
|
363
|
-
default:
|
|
364
|
-
result = this._serializeJSON(data);
|
|
365
|
-
break;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
this.stats.encodes++;
|
|
369
|
-
this.stats.bytesEncoded += result.length;
|
|
370
|
-
this.stats.totalEncodeTime += Date.now() - start;
|
|
371
|
-
|
|
372
|
-
return result;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Deserialize binary to data
|
|
377
|
-
*
|
|
378
|
-
* @param {Buffer} buffer - Serialized data
|
|
379
|
-
* @returns {any} Deserialized data
|
|
380
|
-
*/
|
|
381
|
-
deserialize(buffer) {
|
|
382
|
-
if (!buffer || buffer.length === 0) {
|
|
383
|
-
return null;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const start = Date.now();
|
|
387
|
-
|
|
388
|
-
let result;
|
|
389
|
-
|
|
390
|
-
switch (this.config.format) {
|
|
391
|
-
case 'cbor':
|
|
392
|
-
result = this._deserializeCBOR(buffer);
|
|
393
|
-
break;
|
|
394
|
-
case 'msgpack':
|
|
395
|
-
result = this._deserializeMsgPack(buffer);
|
|
396
|
-
break;
|
|
397
|
-
case 'zerocopy':
|
|
398
|
-
result = this._deserializeZeroCopy(buffer);
|
|
399
|
-
break;
|
|
400
|
-
case 'json':
|
|
401
|
-
default:
|
|
402
|
-
result = this._deserializeJSON(buffer);
|
|
403
|
-
break;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
this.stats.decodes++;
|
|
407
|
-
this.stats.bytesDecoded += buffer.length;
|
|
408
|
-
this.stats.totalDecodeTime += Date.now() - start;
|
|
409
|
-
|
|
410
|
-
return result;
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
// ═══════════════════════════════════════════════════════════════
|
|
414
|
-
// CBOR-X SERIALIZATION (3-10x faster than JSON!)
|
|
415
|
-
// ═══════════════════════════════════════════════════════════════
|
|
416
|
-
|
|
417
|
-
_serializeCBOR(data) {
|
|
418
|
-
if (this.cborEncoder) {
|
|
419
|
-
try {
|
|
420
|
-
// Pre-process for BigInt support
|
|
421
|
-
const processed = this._preprocessForSerialization(data);
|
|
422
|
-
return Buffer.from(this.cborEncoder.encode(processed));
|
|
423
|
-
} catch (e) {
|
|
424
|
-
console.warn('[Serializer] CBOR encode failed, falling back to JSON');
|
|
425
|
-
return this._serializeJSON(data);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
return this._serializeJSON(data);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
_deserializeCBOR(buffer) {
|
|
432
|
-
if (this.cborDecoder) {
|
|
433
|
-
try {
|
|
434
|
-
const decoded = this.cborDecoder.decode(buffer);
|
|
435
|
-
return this._postprocessFromSerialization(decoded);
|
|
436
|
-
} catch (e) {
|
|
437
|
-
console.warn('[Serializer] CBOR decode failed, trying JSON');
|
|
438
|
-
return this._deserializeJSON(buffer);
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return this._deserializeJSON(buffer);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// ═══════════════════════════════════════════════════════════════
|
|
445
|
-
// MESSAGEPACK SERIALIZATION (37% smaller than JSON!)
|
|
446
|
-
// ═══════════════════════════════════════════════════════════════
|
|
447
|
-
|
|
448
|
-
_serializeMsgPack(data) {
|
|
449
|
-
if (pack) {
|
|
450
|
-
try {
|
|
451
|
-
const processed = this._preprocessForSerialization(data);
|
|
452
|
-
return pack(processed);
|
|
453
|
-
} catch (e) {
|
|
454
|
-
return this._serializeJSON(data);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
return this._serializeJSON(data);
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
_deserializeMsgPack(buffer) {
|
|
461
|
-
if (unpack) {
|
|
462
|
-
try {
|
|
463
|
-
const decoded = unpack(buffer);
|
|
464
|
-
return this._postprocessFromSerialization(decoded);
|
|
465
|
-
} catch (e) {
|
|
466
|
-
return this._deserializeJSON(buffer);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return this._deserializeJSON(buffer);
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
// ═══════════════════════════════════════════════════════════════
|
|
473
|
-
// ZERO-COPY SERIALIZATION (Wincode-inspired, 100x+ for hot paths!)
|
|
474
|
-
// ═══════════════════════════════════════════════════════════════
|
|
475
|
-
|
|
476
|
-
_serializeZeroCopy(data) {
|
|
477
|
-
const writer = new ZeroCopyWriter();
|
|
478
|
-
|
|
479
|
-
this._writeValue(writer, data);
|
|
480
|
-
|
|
481
|
-
return writer.toBuffer();
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
_deserializeZeroCopy(buffer) {
|
|
485
|
-
const view = new ZeroCopyView(buffer);
|
|
486
|
-
return this._readValue(view);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
_writeValue(writer, value) {
|
|
490
|
-
if (value === null || value === undefined) {
|
|
491
|
-
writer.writeUint8(0x00); // Null
|
|
492
|
-
} else if (typeof value === 'boolean') {
|
|
493
|
-
writer.writeUint8(value ? 0x01 : 0x02); // True/False
|
|
494
|
-
} else if (typeof value === 'number') {
|
|
495
|
-
if (Number.isInteger(value)) {
|
|
496
|
-
if (value >= 0 && value < 256) {
|
|
497
|
-
writer.writeUint8(0x10); // Uint8
|
|
498
|
-
writer.writeUint8(value);
|
|
499
|
-
} else if (value >= 0) {
|
|
500
|
-
writer.writeUint8(0x12); // Uint64
|
|
501
|
-
writer.writeUint64LE(value);
|
|
502
|
-
} else {
|
|
503
|
-
writer.writeUint8(0x13); // Int64
|
|
504
|
-
writer.writeInt64LE(value);
|
|
505
|
-
}
|
|
506
|
-
} else {
|
|
507
|
-
writer.writeUint8(0x14); // Float64
|
|
508
|
-
writer.writeFloat64LE(value);
|
|
509
|
-
}
|
|
510
|
-
} else if (typeof value === 'bigint') {
|
|
511
|
-
writer.writeUint8(0x20); // BigInt
|
|
512
|
-
writer.writeUint256(value);
|
|
513
|
-
} else if (typeof value === 'string') {
|
|
514
|
-
writer.writeUint8(0x30); // String
|
|
515
|
-
writer.writeString(value);
|
|
516
|
-
} else if (Buffer.isBuffer(value)) {
|
|
517
|
-
writer.writeUint8(0x40); // Buffer
|
|
518
|
-
writer.writeBytes(value);
|
|
519
|
-
} else if (Array.isArray(value)) {
|
|
520
|
-
writer.writeUint8(0x50); // Array
|
|
521
|
-
writer.writeVarInt(value.length);
|
|
522
|
-
for (const item of value) {
|
|
523
|
-
this._writeValue(writer, item);
|
|
524
|
-
}
|
|
525
|
-
} else if (typeof value === 'object') {
|
|
526
|
-
writer.writeUint8(0x60); // Object
|
|
527
|
-
const keys = Object.keys(value);
|
|
528
|
-
writer.writeVarInt(keys.length);
|
|
529
|
-
for (const key of keys) {
|
|
530
|
-
writer.writeString(key);
|
|
531
|
-
this._writeValue(writer, value[key]);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
_readValue(view) {
|
|
537
|
-
const type = view.readUint8();
|
|
538
|
-
|
|
539
|
-
switch (type) {
|
|
540
|
-
case 0x00:
|
|
541
|
-
return null;
|
|
542
|
-
case 0x01:
|
|
543
|
-
return true;
|
|
544
|
-
case 0x02:
|
|
545
|
-
return false;
|
|
546
|
-
case 0x10:
|
|
547
|
-
return view.readUint8();
|
|
548
|
-
case 0x11:
|
|
549
|
-
return view.readUint32LE();
|
|
550
|
-
case 0x12:
|
|
551
|
-
return Number(view.readUint64LE());
|
|
552
|
-
case 0x13:
|
|
553
|
-
return Number(view.readInt64LE());
|
|
554
|
-
case 0x14:
|
|
555
|
-
return view.readFloat64LE();
|
|
556
|
-
case 0x20:
|
|
557
|
-
return view.readUint256();
|
|
558
|
-
case 0x30:
|
|
559
|
-
return view.readString();
|
|
560
|
-
case 0x40:
|
|
561
|
-
return view.readBytes().toBuffer();
|
|
562
|
-
case 0x50: {
|
|
563
|
-
const length = view.readVarInt();
|
|
564
|
-
const array = [];
|
|
565
|
-
for (let i = 0; i < length; i++) {
|
|
566
|
-
array.push(this._readValue(view));
|
|
567
|
-
}
|
|
568
|
-
return array;
|
|
569
|
-
}
|
|
570
|
-
case 0x60: {
|
|
571
|
-
const length = view.readVarInt();
|
|
572
|
-
const obj = {};
|
|
573
|
-
for (let i = 0; i < length; i++) {
|
|
574
|
-
const key = view.readString();
|
|
575
|
-
obj[key] = this._readValue(view);
|
|
576
|
-
}
|
|
577
|
-
return obj;
|
|
578
|
-
}
|
|
579
|
-
default:
|
|
580
|
-
throw new Error(`Unknown type: 0x${type.toString(16)}`);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
// ═══════════════════════════════════════════════════════════════
|
|
585
|
-
// JSON FALLBACK
|
|
586
|
-
// ═══════════════════════════════════════════════════════════════
|
|
587
|
-
|
|
588
|
-
_serializeJSON(data) {
|
|
589
|
-
const processed = this._preprocessForSerialization(data);
|
|
590
|
-
return Buffer.from(JSON.stringify(processed));
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
_deserializeJSON(buffer) {
|
|
594
|
-
try {
|
|
595
|
-
const decoded = JSON.parse(buffer.toString());
|
|
596
|
-
return this._postprocessFromSerialization(decoded);
|
|
597
|
-
} catch (e) {
|
|
598
|
-
return null;
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
// ═══════════════════════════════════════════════════════════════
|
|
603
|
-
// PREPROCESSING FOR BIGINT SUPPORT
|
|
604
|
-
// ═══════════════════════════════════════════════════════════════
|
|
605
|
-
|
|
606
|
-
_preprocessForSerialization(data) {
|
|
607
|
-
if (data === null || data === undefined) {
|
|
608
|
-
return data;
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
if (typeof data === 'bigint') {
|
|
612
|
-
return { __type: 'bigint', value: data.toString() };
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
if (Buffer.isBuffer(data)) {
|
|
616
|
-
return { __type: 'buffer', value: data.toString('hex') };
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
if (Array.isArray(data)) {
|
|
620
|
-
return data.map(item => this._preprocessForSerialization(item));
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
if (typeof data === 'object') {
|
|
624
|
-
const result = {};
|
|
625
|
-
for (const [key, value] of Object.entries(data)) {
|
|
626
|
-
result[key] = this._preprocessForSerialization(value);
|
|
627
|
-
}
|
|
628
|
-
return result;
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
return data;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
_postprocessFromSerialization(data) {
|
|
635
|
-
if (data === null || data === undefined) {
|
|
636
|
-
return data;
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
if (typeof data === 'object' && data.__type) {
|
|
640
|
-
switch (data.__type) {
|
|
641
|
-
case 'bigint':
|
|
642
|
-
return BigInt(data.value);
|
|
643
|
-
case 'buffer':
|
|
644
|
-
return Buffer.from(data.value, 'hex');
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
if (Array.isArray(data)) {
|
|
649
|
-
return data.map(item => this._postprocessFromSerialization(item));
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
if (typeof data === 'object') {
|
|
653
|
-
const result = {};
|
|
654
|
-
for (const [key, value] of Object.entries(data)) {
|
|
655
|
-
result[key] = this._postprocessFromSerialization(value);
|
|
656
|
-
}
|
|
657
|
-
return result;
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
return data;
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
// ═══════════════════════════════════════════════════════════════
|
|
664
|
-
// SPECIALIZED SERIALIZATION (for blockchain types)
|
|
665
|
-
// ═══════════════════════════════════════════════════════════════
|
|
666
|
-
|
|
667
|
-
/**
|
|
668
|
-
* Serialize a transaction
|
|
669
|
-
*/
|
|
670
|
-
serializeTransaction(tx) {
|
|
671
|
-
const writer = new ZeroCopyWriter(256);
|
|
672
|
-
|
|
673
|
-
// Version
|
|
674
|
-
writer.writeUint8(1);
|
|
675
|
-
|
|
676
|
-
// Type
|
|
677
|
-
writer.writeUint8(tx.type || 0);
|
|
678
|
-
|
|
679
|
-
// Chain ID
|
|
680
|
-
writer.writeUint32LE(tx.chainId || 4545);
|
|
681
|
-
|
|
682
|
-
// Nonce
|
|
683
|
-
writer.writeUint64LE(tx.nonce || 0);
|
|
684
|
-
|
|
685
|
-
// From
|
|
686
|
-
writer.writeAddress(tx.from, 20);
|
|
687
|
-
|
|
688
|
-
// To
|
|
689
|
-
if (tx.to) {
|
|
690
|
-
writer.writeUint8(1);
|
|
691
|
-
writer.writeAddress(tx.to, 20);
|
|
692
|
-
} else {
|
|
693
|
-
writer.writeUint8(0); // Contract creation
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
// Value
|
|
697
|
-
writer.writeUint256(tx.value || 0);
|
|
698
|
-
|
|
699
|
-
// Gas limit
|
|
700
|
-
writer.writeUint64LE(tx.gasLimit || 21000);
|
|
701
|
-
|
|
702
|
-
// Data
|
|
703
|
-
const data = tx.data || Buffer.alloc(0);
|
|
704
|
-
writer.writeBytes(Buffer.isBuffer(data) ? data : Buffer.from(data, 'hex'));
|
|
705
|
-
|
|
706
|
-
// Signature
|
|
707
|
-
if (tx.signature) {
|
|
708
|
-
writer.writeUint8(1);
|
|
709
|
-
writer.writeBytes(Buffer.from(tx.signature, 'hex'));
|
|
710
|
-
} else {
|
|
711
|
-
writer.writeUint8(0);
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
return writer.toBuffer();
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* Deserialize a transaction
|
|
719
|
-
*/
|
|
720
|
-
deserializeTransaction(buffer) {
|
|
721
|
-
const view = new ZeroCopyView(buffer);
|
|
722
|
-
|
|
723
|
-
const version = view.readUint8();
|
|
724
|
-
if (version !== 1) {
|
|
725
|
-
throw new Error(`Unknown transaction version: ${version}`);
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
const tx = {
|
|
729
|
-
type: view.readUint8(),
|
|
730
|
-
chainId: view.readUint32LE(),
|
|
731
|
-
nonce: Number(view.readUint64LE()),
|
|
732
|
-
from: view.readAddress(20)
|
|
733
|
-
};
|
|
734
|
-
|
|
735
|
-
if (view.readUint8() === 1) {
|
|
736
|
-
tx.to = view.readAddress(20);
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
tx.value = view.readUint256();
|
|
740
|
-
tx.gasLimit = Number(view.readUint64LE());
|
|
741
|
-
tx.data = view.readBytes().toBuffer();
|
|
742
|
-
|
|
743
|
-
if (view.readUint8() === 1) {
|
|
744
|
-
tx.signature = view.readBytes().toBuffer().toString('hex');
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
return tx;
|
|
748
|
-
}
|
|
749
|
-
|
|
750
|
-
/**
|
|
751
|
-
* Serialize call data (function selector + args)
|
|
752
|
-
*/
|
|
753
|
-
serializeCallData(functionSelector, args) {
|
|
754
|
-
const writer = new ZeroCopyWriter();
|
|
755
|
-
|
|
756
|
-
// 4-byte function selector
|
|
757
|
-
const selector = typeof functionSelector === 'string'
|
|
758
|
-
? Buffer.from(functionSelector.replace('0x', ''), 'hex')
|
|
759
|
-
: functionSelector;
|
|
760
|
-
writer.writeBuffer(selector.slice(0, 4));
|
|
761
|
-
|
|
762
|
-
// Arguments
|
|
763
|
-
for (const arg of args) {
|
|
764
|
-
this._writeABIValue(writer, arg);
|
|
765
|
-
}
|
|
766
|
-
|
|
767
|
-
return writer.toBuffer();
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
_writeABIValue(writer, value) {
|
|
771
|
-
if (typeof value === 'bigint' || typeof value === 'number') {
|
|
772
|
-
// 32-byte padded
|
|
773
|
-
writer.writeUint256(value);
|
|
774
|
-
} else if (typeof value === 'string' && value.startsWith('0x')) {
|
|
775
|
-
// Address or bytes32
|
|
776
|
-
const bytes = Buffer.from(value.slice(2), 'hex');
|
|
777
|
-
if (bytes.length <= 32) {
|
|
778
|
-
const padded = Buffer.alloc(32);
|
|
779
|
-
bytes.copy(padded, 32 - bytes.length);
|
|
780
|
-
writer.writeBuffer(padded);
|
|
781
|
-
}
|
|
782
|
-
} else if (typeof value === 'boolean') {
|
|
783
|
-
writer.writeUint256(value ? 1 : 0);
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
// ═══════════════════════════════════════════════════════════════
|
|
788
|
-
// STATS
|
|
789
|
-
// ═══════════════════════════════════════════════════════════════
|
|
790
|
-
|
|
791
|
-
getStats() {
|
|
792
|
-
return {
|
|
793
|
-
...this.stats,
|
|
794
|
-
avgEncodeTime: this.stats.encodes > 0
|
|
795
|
-
? (this.stats.totalEncodeTime / this.stats.encodes).toFixed(2) + 'ms'
|
|
796
|
-
: '0ms',
|
|
797
|
-
avgDecodeTime: this.stats.decodes > 0
|
|
798
|
-
? (this.stats.totalDecodeTime / this.stats.decodes).toFixed(2) + 'ms'
|
|
799
|
-
: '0ms',
|
|
800
|
-
format: this.config.format
|
|
801
|
-
};
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
resetStats() {
|
|
805
|
-
this.stats = {
|
|
806
|
-
encodes: 0,
|
|
807
|
-
decodes: 0,
|
|
808
|
-
bytesEncoded: 0,
|
|
809
|
-
bytesDecoded: 0,
|
|
810
|
-
totalEncodeTime: 0,
|
|
811
|
-
totalDecodeTime: 0
|
|
812
|
-
};
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
// Export classes
|
|
817
|
-
module.exports = Serializer;
|
|
818
|
-
module.exports.ZeroCopyView = ZeroCopyView;
|
|
819
|
-
module.exports.ZeroCopyWriter = ZeroCopyWriter;
|