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,599 @@
1
+ /**
2
+ * JAELIS UNIVERSAL VM TEST SUITE
3
+ *
4
+ * Tests for cross-chain bytecode execution
5
+ *
6
+ * @version 0.1.0
7
+ * @author Mario Papaleo - JAELIS Foundation
8
+ */
9
+
10
+ const { JaelisUniversalVM } = require('../lib/index');
11
+
12
+ // Test utilities
13
+ let testsPassed = 0;
14
+ let testsFailed = 0;
15
+
16
+ function assert(condition, message) {
17
+ if (!condition) {
18
+ throw new Error(message);
19
+ }
20
+ }
21
+
22
+ function assertEqual(actual, expected, message) {
23
+ if (actual !== expected) {
24
+ throw new Error(`${message}: expected ${expected}, got ${actual}`);
25
+ }
26
+ }
27
+
28
+ async function test(name, fn) {
29
+ try {
30
+ await fn();
31
+ console.log(` ✓ ${name}`);
32
+ testsPassed++;
33
+ } catch (error) {
34
+ console.log(` ✗ ${name}`);
35
+ console.log(` Error: ${error.message}`);
36
+ testsFailed++;
37
+ }
38
+ }
39
+
40
+ // ═══════════════════════════════════════════════════════════════
41
+ // VM INITIALIZATION TESTS
42
+ // ═══════════════════════════════════════════════════════════════
43
+
44
+ async function testVMInitialization() {
45
+ console.log('\n📦 VM Initialization Tests');
46
+ console.log('─'.repeat(50));
47
+
48
+ await test('VM should initialize with default config', async () => {
49
+ const vm = new JaelisUniversalVM();
50
+ assert(vm !== null, 'VM should not be null');
51
+ assert(vm.config.executionMode === 'interpreter', 'Default mode should be interpreter');
52
+ });
53
+
54
+ await test('VM should accept custom config', async () => {
55
+ const vm = new JaelisUniversalVM({
56
+ executionMode: 'aot',
57
+ maxMemory: 512 * 1024 * 1024,
58
+ serializer: 'msgpack'
59
+ });
60
+ assertEqual(vm.config.executionMode, 'aot', 'Execution mode');
61
+ assertEqual(vm.config.serializer, 'msgpack', 'Serializer');
62
+ });
63
+
64
+ await test('VM should list all supported VMs', async () => {
65
+ const vm = new JaelisUniversalVM();
66
+ const supported = Object.keys(vm.supportedVMs);
67
+ assert(supported.includes('evm'), 'Should support EVM');
68
+ assert(supported.includes('svm'), 'Should support SVM');
69
+ assert(supported.includes('move'), 'Should support Move');
70
+ assert(supported.includes('tvm'), 'Should support TVM');
71
+ assert(supported.includes('wasm'), 'Should support WASM');
72
+ assert(supported.includes('cairo'), 'Should support Cairo');
73
+ assert(supported.includes('bitcoin'), 'Should support Bitcoin');
74
+ });
75
+ }
76
+
77
+ // ═══════════════════════════════════════════════════════════════
78
+ // COMPILER TESTS
79
+ // ═══════════════════════════════════════════════════════════════
80
+
81
+ async function testCompiler() {
82
+ console.log('\n🔧 Compiler Tests');
83
+ console.log('─'.repeat(50));
84
+
85
+ const vm = new JaelisUniversalVM();
86
+
87
+ await test('Should compile simple EVM source code', async () => {
88
+ const source = `
89
+ contract SimpleStorage {
90
+ uint256 public value;
91
+
92
+ function setValue(uint256 _value) public {
93
+ value = _value;
94
+ }
95
+
96
+ function getValue() public view returns (uint256) {
97
+ return value;
98
+ }
99
+ }
100
+ `;
101
+
102
+ const result = await vm.compile(source, 'evm');
103
+ assert(result.moduleId, 'Should have module ID');
104
+ assert(result.jir, 'Should have JIR');
105
+ assert(result.jir.bytecode, 'Should have bytecode');
106
+ assert(result.sourceVM === 'evm', 'Source VM should be evm');
107
+ });
108
+
109
+ await test('Should compile Solana Anchor source', async () => {
110
+ const source = `
111
+ #[program]
112
+ pub mod counter {
113
+ use super::*;
114
+
115
+ pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
116
+ let counter = &mut ctx.accounts.counter;
117
+ counter.count = 0;
118
+ Ok(())
119
+ }
120
+
121
+ pub fn increment(ctx: Context<Increment>) -> Result<()> {
122
+ let counter = &mut ctx.accounts.counter;
123
+ counter.count += 1;
124
+ Ok(())
125
+ }
126
+ }
127
+ `;
128
+
129
+ const result = await vm.compile(source, 'svm');
130
+ assert(result.moduleId, 'Should have module ID');
131
+ assert(result.jir, 'Should have JIR');
132
+ });
133
+
134
+ await test('Should compile Move source', async () => {
135
+ const source = `
136
+ module 0x1::counter {
137
+ struct Counter has key {
138
+ value: u64
139
+ }
140
+
141
+ public fun increment(counter: &mut Counter) {
142
+ counter.value = counter.value + 1;
143
+ }
144
+
145
+ public fun get_value(counter: &Counter): u64 {
146
+ counter.value
147
+ }
148
+ }
149
+ `;
150
+
151
+ const result = await vm.compile(source, 'move');
152
+ assert(result.moduleId, 'Should have module ID');
153
+ });
154
+
155
+ await test('Should reject unsupported VM', async () => {
156
+ let error = null;
157
+ try {
158
+ await vm.compile('code', 'unsupported_vm');
159
+ } catch (e) {
160
+ error = e;
161
+ }
162
+ assert(error !== null, 'Should throw error for unsupported VM');
163
+ });
164
+ }
165
+
166
+ // ═══════════════════════════════════════════════════════════════
167
+ // SERIALIZATION TESTS
168
+ // ═══════════════════════════════════════════════════════════════
169
+
170
+ async function testSerialization() {
171
+ console.log('\n📦 Serialization Tests');
172
+ console.log('─'.repeat(50));
173
+
174
+ const vm = new JaelisUniversalVM({ serializer: 'cbor' });
175
+
176
+ await test('Should serialize/deserialize primitives', async () => {
177
+ const testData = {
178
+ number: 42,
179
+ string: 'hello',
180
+ boolean: true,
181
+ array: [1, 2, 3]
182
+ };
183
+
184
+ const serialized = vm.serializer.serialize(testData);
185
+ assert(Buffer.isBuffer(serialized), 'Should return buffer');
186
+
187
+ const deserialized = vm.serializer.deserialize(serialized);
188
+ assertEqual(deserialized.number, testData.number, 'Number');
189
+ assertEqual(deserialized.string, testData.string, 'String');
190
+ assertEqual(deserialized.boolean, testData.boolean, 'Boolean');
191
+ assertEqual(deserialized.array.length, testData.array.length, 'Array length');
192
+ });
193
+
194
+ await test('Should handle BigInt values', async () => {
195
+ const testData = {
196
+ bigValue: 12345678901234567890n
197
+ };
198
+
199
+ const serialized = vm.serializer.serialize(testData);
200
+ const deserialized = vm.serializer.deserialize(serialized);
201
+
202
+ assert(deserialized.bigValue === testData.bigValue, 'BigInt should match');
203
+ });
204
+
205
+ await test('Should serialize transactions', async () => {
206
+ const tx = {
207
+ from: '0x1234567890123456789012345678901234567890',
208
+ to: '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd',
209
+ value: 1000000000000000000n,
210
+ nonce: 0,
211
+ chainId: 4545,
212
+ gasLimit: 21000
213
+ };
214
+
215
+ const serialized = vm.serializer.serializeTransaction(tx);
216
+ assert(Buffer.isBuffer(serialized), 'Should return buffer');
217
+
218
+ const deserialized = vm.serializer.deserializeTransaction(serialized);
219
+ assertEqual(deserialized.nonce, tx.nonce, 'Nonce');
220
+ assertEqual(deserialized.chainId, tx.chainId, 'Chain ID');
221
+ });
222
+ }
223
+
224
+ // ═══════════════════════════════════════════════════════════════
225
+ // BYTECODE TRANSLATION TESTS
226
+ // ═══════════════════════════════════════════════════════════════
227
+
228
+ async function testBytecodeTranslation() {
229
+ console.log('\n🔄 Bytecode Translation Tests');
230
+ console.log('─'.repeat(50));
231
+
232
+ const vm = new JaelisUniversalVM();
233
+
234
+ await test('Should translate simple EVM bytecode', async () => {
235
+ // Simple EVM bytecode: PUSH1 0x60 PUSH1 0x40 MSTORE
236
+ const evmBytecode = Buffer.from([0x60, 0x60, 0x60, 0x40, 0x52]);
237
+
238
+ const jir = await vm.translator.translateToJIR(evmBytecode, 'evm');
239
+
240
+ assert(jir.magic.equals(Buffer.from([0x4A, 0x49, 0x52, 0x00])), 'Should have JIR magic');
241
+ assert(jir.sourceVM === 'evm', 'Source VM should be evm');
242
+ assert(jir.bytecode.length > 0, 'Should have bytecode');
243
+ });
244
+
245
+ await test('Should translate EVM opcodes correctly', async () => {
246
+ // EVM: ADD, SUB, MUL, DIV
247
+ const evmBytecode = Buffer.from([0x01, 0x03, 0x02, 0x04, 0x00]);
248
+
249
+ const jir = await vm.translator.translateToJIR(evmBytecode, 'evm');
250
+ assert(jir.bytecode.includes(0x39) || jir.bytecode.includes(0x34), 'Should have I64_ADD or I32_ADD');
251
+ });
252
+
253
+ await test('Should translate Solana BPF bytecode', async () => {
254
+ // Simple BPF instruction (ADD_IMM)
255
+ const bpfBytecode = Buffer.alloc(16);
256
+ bpfBytecode[0] = 0x07; // ADD_IMM
257
+ bpfBytecode.writeInt32LE(100, 4); // immediate value
258
+
259
+ const jir = await vm.translator.translateToJIR(bpfBytecode, 'svm');
260
+ assert(jir.sourceVM === 'svm', 'Source VM should be svm');
261
+ });
262
+
263
+ await test('Should translate Bitcoin Script', async () => {
264
+ // OP_1 OP_2 OP_ADD OP_3 OP_EQUAL
265
+ const script = Buffer.from([0x51, 0x52, 0x93, 0x53, 0x87]);
266
+
267
+ const jir = await vm.translator.translateToJIR(script, 'bitcoin');
268
+ assert(jir.sourceVM === 'bitcoin', 'Source VM should be bitcoin');
269
+ });
270
+
271
+ await test('Should cache translations', async () => {
272
+ const bytecode = Buffer.from([0x60, 0x01, 0x60, 0x02, 0x01]);
273
+
274
+ await vm.translator.translateToJIR(bytecode, 'evm');
275
+ const stats1 = vm.translator.getStats();
276
+
277
+ await vm.translator.translateToJIR(bytecode, 'evm');
278
+ const stats2 = vm.translator.getStats();
279
+
280
+ assert(stats2.cacheHits > stats1.cacheHits, 'Should hit cache on second call');
281
+ });
282
+
283
+ await test('Should translate arguments between VMs', async () => {
284
+ // EVM address (20 bytes) to Solana (32 bytes)
285
+ const evmAddress = '0x1234567890abcdef1234567890abcdef12345678';
286
+
287
+ const translated = await vm.translator.translateArguments(
288
+ [evmAddress],
289
+ 'evm',
290
+ 'svm'
291
+ );
292
+
293
+ assert(translated[0].length > evmAddress.length, 'Should pad address for Solana');
294
+ });
295
+ }
296
+
297
+ // ═══════════════════════════════════════════════════════════════
298
+ // STATE MANAGER TESTS
299
+ // ═══════════════════════════════════════════════════════════════
300
+
301
+ async function testStateManager() {
302
+ console.log('\n💾 State Manager Tests');
303
+ console.log('─'.repeat(50));
304
+
305
+ const vm = new JaelisUniversalVM();
306
+
307
+ await test('Should create and retrieve accounts', async () => {
308
+ const address = '0x1234567890123456789012345678901234567890';
309
+
310
+ await vm.state.setBalance(address, 1000000000000000000n);
311
+ const balance = await vm.state.getBalance(address);
312
+
313
+ assertEqual(balance, 1000000000000000000n, 'Balance');
314
+ });
315
+
316
+ await test('Should handle contract storage', async () => {
317
+ const address = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd';
318
+
319
+ await vm.state.setStorage(address, 0, 42n);
320
+ const value = await vm.state.getStorage(address, 0);
321
+
322
+ assertEqual(value, 42n, 'Storage value');
323
+ });
324
+
325
+ await test('Should store and retrieve contracts', async () => {
326
+ const address = '0x1111111111111111111111111111111111111111';
327
+ const bytecode = Buffer.from([0x60, 0x60, 0x60, 0x40, 0x52]);
328
+
329
+ await vm.state.storeContract(address, {
330
+ jir: { bytecode: Buffer.from([0x00]) },
331
+ sourceVM: 'evm',
332
+ bytecode,
333
+ deployer: '0x0000000000000000000000000000000000000000'
334
+ });
335
+
336
+ const contract = await vm.state.getContract(address);
337
+ assert(contract !== null, 'Contract should exist');
338
+ assertEqual(contract.sourceVM, 'evm', 'Source VM');
339
+ });
340
+
341
+ await test('Should support checkpoints and rollback', async () => {
342
+ const address = '0x2222222222222222222222222222222222222222';
343
+
344
+ await vm.state.setStorage(address, 0, 100n);
345
+
346
+ const checkpoint = vm.state.checkpoint();
347
+
348
+ await vm.state.setStorage(address, 0, 200n);
349
+ assertEqual(await vm.state.getStorage(address, 0), 200n, 'Value after change');
350
+
351
+ await vm.state.revert(checkpoint);
352
+ assertEqual(await vm.state.getStorage(address, 0), 100n, 'Value after revert');
353
+ });
354
+
355
+ await test('Should get state root', async () => {
356
+ const root = vm.state.getStateRoot();
357
+ assert(typeof root === 'string', 'Should return string');
358
+ assert(root.startsWith('0x') || root.length === 64, 'Should be hex');
359
+ });
360
+ }
361
+
362
+ // ═══════════════════════════════════════════════════════════════
363
+ // EXECUTION ENGINE TESTS
364
+ // ═══════════════════════════════════════════════════════════════
365
+
366
+ async function testExecutionEngine() {
367
+ console.log('\n⚙️ Execution Engine Tests');
368
+ console.log('─'.repeat(50));
369
+
370
+ const vm = new JaelisUniversalVM({ executionMode: 'interpreter' });
371
+
372
+ await test('Should execute simple JIR bytecode', async () => {
373
+ // Simple JIR: push 1, push 2, add, return
374
+ const jir = {
375
+ magic: Buffer.from([0x4A, 0x49, 0x52, 0x00]),
376
+ version: [0, 1, 0],
377
+ sourceVM: 'evm',
378
+ functions: [{
379
+ name: 'main',
380
+ params: [],
381
+ returns: [{ type: 'i64' }],
382
+ code: [
383
+ 0x30, 1, 0, 0, 0, // I32_CONST 1
384
+ 0x30, 2, 0, 0, 0, // I32_CONST 2
385
+ 0x34, // I32_ADD
386
+ 0x08 // RETURN
387
+ ]
388
+ }],
389
+ memory: { initial: 1, maximum: 16 },
390
+ exports: [{ name: 'main', kind: 'function', index: 0 }],
391
+ bytecode: Buffer.from([
392
+ 0x4A, 0x49, 0x52, 0x00,
393
+ 0x30, 1, 0, 0, 0,
394
+ 0x30, 2, 0, 0, 0,
395
+ 0x34,
396
+ 0x08,
397
+ 0xFF
398
+ ])
399
+ };
400
+
401
+ const result = await vm.engine.execute(jir, 'main', [], {
402
+ caller: '0x0000000000000000000000000000000000000000'
403
+ });
404
+
405
+ assert(result.success, 'Execution should succeed');
406
+ assert(result.instructionsUsed > 0, 'Should use instructions');
407
+ });
408
+
409
+ await test('Should handle context correctly', async () => {
410
+ const jir = {
411
+ magic: Buffer.from([0x4A, 0x49, 0x52, 0x00]),
412
+ version: [0, 1, 0],
413
+ sourceVM: 'evm',
414
+ functions: [{
415
+ name: 'getCaller',
416
+ params: [],
417
+ returns: [],
418
+ code: [0x90, 0x08, 0xFF] // CALLER, RETURN, HALT
419
+ }],
420
+ memory: { initial: 1 },
421
+ exports: [{ name: 'getCaller', kind: 'function', index: 0 }],
422
+ bytecode: Buffer.from([0x4A, 0x49, 0x52, 0x00, 0x90, 0x08, 0xFF])
423
+ };
424
+
425
+ const caller = '0x1234567890123456789012345678901234567890';
426
+ const result = await vm.engine.execute(jir, 'getCaller', [], { caller });
427
+
428
+ assert(result.success, 'Should succeed');
429
+ });
430
+
431
+ await test('Should respect gas limits', async () => {
432
+ const jir = {
433
+ magic: Buffer.from([0x4A, 0x49, 0x52, 0x00]),
434
+ version: [0, 1, 0],
435
+ sourceVM: 'evm',
436
+ functions: [{
437
+ name: 'infinite',
438
+ params: [],
439
+ returns: [],
440
+ code: [0x02, 0x00, 0x06, 0] // LOOP, NOP, BR 0
441
+ }],
442
+ memory: { initial: 1 },
443
+ exports: [{ name: 'infinite', kind: 'function', index: 0 }],
444
+ bytecode: Buffer.from([0x4A, 0x49, 0x52, 0x00, 0x02, 0x00, 0x06, 0, 0xFF])
445
+ };
446
+
447
+ let error = null;
448
+ try {
449
+ await vm.engine.execute(jir, 'infinite', [], { gasLimit: 100 });
450
+ } catch (e) {
451
+ error = e;
452
+ }
453
+
454
+ assert(error !== null, 'Should throw out of gas error');
455
+ });
456
+ }
457
+
458
+ // ═══════════════════════════════════════════════════════════════
459
+ // ADAPTER TESTS
460
+ // ═══════════════════════════════════════════════════════════════
461
+
462
+ async function testAdapters() {
463
+ console.log('\n🔌 Adapter Tests');
464
+ console.log('─'.repeat(50));
465
+
466
+ const vm = new JaelisUniversalVM();
467
+ const { AdapterFactory } = require('../lib/adapters');
468
+ const factory = new AdapterFactory(vm);
469
+
470
+ await test('Should create EVM adapter', async () => {
471
+ const adapter = factory.getAdapter('evm');
472
+ assert(adapter !== null, 'Adapter should exist');
473
+ assert(typeof adapter.getFunctionSelector === 'function', 'Should have getFunctionSelector');
474
+ });
475
+
476
+ await test('EVM adapter should encode function selector', async () => {
477
+ const adapter = factory.getAdapter('evm');
478
+ const selector = adapter.getFunctionSelector('transfer(address,uint256)');
479
+
480
+ assert(Buffer.isBuffer(selector), 'Should return buffer');
481
+ assertEqual(selector.length, 4, 'Selector should be 4 bytes');
482
+ });
483
+
484
+ await test('EVM adapter should encode parameters', async () => {
485
+ const adapter = factory.getAdapter('evm');
486
+ const encoded = adapter.encodeParameters(
487
+ ['address', 'uint256'],
488
+ ['0x1234567890123456789012345678901234567890', 1000n]
489
+ );
490
+
491
+ assertEqual(encoded.length, 64, 'Should be 64 bytes (2 x 32)');
492
+ });
493
+
494
+ await test('Should create SVM adapter', async () => {
495
+ const adapter = factory.getAdapter('svm');
496
+ assert(adapter !== null, 'Adapter should exist');
497
+ assert(typeof adapter.findProgramAddress === 'function', 'Should have findProgramAddress');
498
+ });
499
+
500
+ await test('SVM adapter should find PDA', async () => {
501
+ const adapter = factory.getAdapter('svm');
502
+ const result = adapter.findProgramAddress(
503
+ [Buffer.from('seed')],
504
+ '11111111111111111111111111111111'
505
+ );
506
+
507
+ assert(result.address, 'Should have address');
508
+ assert(typeof result.bump === 'number', 'Should have bump');
509
+ });
510
+
511
+ await test('Should create Move adapter', async () => {
512
+ const adapter = factory.getAdapter('move');
513
+ assert(adapter !== null, 'Adapter should exist');
514
+ });
515
+
516
+ await test('Should create Bitcoin adapter', async () => {
517
+ const adapter = factory.getAdapter('bitcoin');
518
+ assert(adapter !== null, 'Adapter should exist');
519
+
520
+ const p2pkh = adapter.createP2PKH(Buffer.alloc(20));
521
+ assert(p2pkh.length > 0, 'Should create P2PKH script');
522
+ });
523
+ }
524
+
525
+ // ═══════════════════════════════════════════════════════════════
526
+ // CROSS-VM TESTS
527
+ // ═══════════════════════════════════════════════════════════════
528
+
529
+ async function testCrossVM() {
530
+ console.log('\n🌐 Cross-VM Tests');
531
+ console.log('─'.repeat(50));
532
+
533
+ const vm = new JaelisUniversalVM();
534
+
535
+ await test('Should map addresses across VMs', async () => {
536
+ const evmAddress = '0x1234567890123456789012345678901234567890';
537
+ const solanaAddress = '7nxQB1G2SJFXrNt8xLdwLLMQVDx9x2fKVrCzHWJNbmVf';
538
+
539
+ await vm.state.mapAddress('evm', evmAddress, 'svm', solanaAddress);
540
+
541
+ const mapped = await vm.state.getMappedAddress('evm', evmAddress, 'svm');
542
+ assertEqual(mapped, solanaAddress, 'Mapped address');
543
+ });
544
+
545
+ await test('Should translate values between VMs', async () => {
546
+ const evmValue = '0x1234567890123456789012345678901234567890';
547
+
548
+ const translated = await vm.translator.translateArguments(
549
+ [evmValue],
550
+ 'evm',
551
+ 'svm'
552
+ );
553
+
554
+ assert(translated[0].length >= evmValue.length, 'Should translate address');
555
+ });
556
+ }
557
+
558
+ // ═══════════════════════════════════════════════════════════════
559
+ // RUN ALL TESTS
560
+ // ═══════════════════════════════════════════════════════════════
561
+
562
+ async function runAllTests() {
563
+ console.log('═══════════════════════════════════════════════════════════════');
564
+ console.log(' JAELIS UNIVERSAL VM - TEST SUITE');
565
+ console.log('═══════════════════════════════════════════════════════════════');
566
+
567
+ const startTime = Date.now();
568
+
569
+ try {
570
+ await testVMInitialization();
571
+ await testCompiler();
572
+ await testSerialization();
573
+ await testBytecodeTranslation();
574
+ await testStateManager();
575
+ await testExecutionEngine();
576
+ await testAdapters();
577
+ await testCrossVM();
578
+ } catch (error) {
579
+ console.error('\n❌ Test suite crashed:', error.message);
580
+ testsFailed++;
581
+ }
582
+
583
+ const elapsed = Date.now() - startTime;
584
+
585
+ console.log('\n═══════════════════════════════════════════════════════════════');
586
+ console.log(' TEST RESULTS');
587
+ console.log('═══════════════════════════════════════════════════════════════');
588
+ console.log(` ✓ Passed: ${testsPassed}`);
589
+ console.log(` ✗ Failed: ${testsFailed}`);
590
+ console.log(` Total: ${testsPassed + testsFailed}`);
591
+ console.log(` Time: ${elapsed}ms`);
592
+ console.log('═══════════════════════════════════════════════════════════════');
593
+
594
+ if (testsFailed > 0) {
595
+ process.exit(1);
596
+ }
597
+ }
598
+
599
+ runAllTests().catch(console.error);