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
@@ -0,0 +1,819 @@
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;