jaelis-node 1.3.1 → 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 +72 -2
  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,411 @@
1
+ /**
2
+ * JAELIS VM ADAPTERS
3
+ *
4
+ * Provides chain-specific interfaces for the Universal VM
5
+ *
6
+ * @version 0.1.0
7
+ * @author Mario Papaleo - JAELIS Foundation
8
+ */
9
+
10
+ const EVMAdapter = require('./evm-adapter');
11
+ const SVMAdapter = require('./svm-adapter');
12
+
13
+ /**
14
+ * Move VM Adapter (Aptos/Sui)
15
+ */
16
+ class MoveAdapter {
17
+ constructor(vm) {
18
+ this.vm = vm;
19
+ this.modules = new Map();
20
+ this.resources = new Map();
21
+ }
22
+
23
+ async publishModule(bytecode, options = {}) {
24
+ const moduleAddress = options.address || this.generateModuleAddress(bytecode);
25
+
26
+ this.modules.set(moduleAddress, {
27
+ bytecode,
28
+ metadata: options.metadata || {}
29
+ });
30
+
31
+ return this.vm.deployBytecode(bytecode, 'move', {
32
+ ...options,
33
+ deployer: moduleAddress
34
+ });
35
+ }
36
+
37
+ async executeFunction(moduleAddress, moduleName, functionName, typeArgs = [], args = [], options = {}) {
38
+ const callData = this.encodeEntryFunction(moduleName, functionName, typeArgs, args);
39
+
40
+ return this.vm.execute(moduleAddress, functionName, callData, options);
41
+ }
42
+
43
+ encodeEntryFunction(moduleName, functionName, typeArgs, args) {
44
+ // BCS encoding for Move
45
+ const buffers = [];
46
+
47
+ // Module name
48
+ buffers.push(this.bcsEncodeString(moduleName));
49
+
50
+ // Function name
51
+ buffers.push(this.bcsEncodeString(functionName));
52
+
53
+ // Type arguments count
54
+ buffers.push(Buffer.from([typeArgs.length]));
55
+
56
+ // Arguments count
57
+ buffers.push(Buffer.from([args.length]));
58
+
59
+ // Encode each argument
60
+ for (const arg of args) {
61
+ buffers.push(this.bcsEncodeValue(arg));
62
+ }
63
+
64
+ return Buffer.concat(buffers);
65
+ }
66
+
67
+ bcsEncodeString(str) {
68
+ const strBuf = Buffer.from(str, 'utf8');
69
+ const lenBuf = Buffer.alloc(4);
70
+ lenBuf.writeUInt32LE(strBuf.length);
71
+ return Buffer.concat([lenBuf, strBuf]);
72
+ }
73
+
74
+ bcsEncodeValue(value) {
75
+ if (typeof value === 'number' || typeof value === 'bigint') {
76
+ const buf = Buffer.alloc(8);
77
+ buf.writeBigUInt64LE(BigInt(value));
78
+ return buf;
79
+ }
80
+ if (typeof value === 'string') {
81
+ return this.bcsEncodeString(value);
82
+ }
83
+ if (typeof value === 'boolean') {
84
+ return Buffer.from([value ? 1 : 0]);
85
+ }
86
+ return Buffer.alloc(0);
87
+ }
88
+
89
+ generateModuleAddress(bytecode) {
90
+ const crypto = require('crypto');
91
+ return '0x' + crypto.createHash('sha256').update(bytecode).digest('hex').substring(0, 64);
92
+ }
93
+
94
+ async getResource(address, resourceType) {
95
+ const key = `${address}:${resourceType}`;
96
+ return this.resources.get(key);
97
+ }
98
+
99
+ async setResource(address, resourceType, value) {
100
+ const key = `${address}:${resourceType}`;
101
+ this.resources.set(key, value);
102
+ }
103
+ }
104
+
105
+ /**
106
+ * TON VM Adapter
107
+ */
108
+ class TVMAdapter {
109
+ constructor(vm) {
110
+ this.vm = vm;
111
+ this.contracts = new Map();
112
+ }
113
+
114
+ async deployContract(code, data, options = {}) {
115
+ // TON uses code + data cells
116
+ const stateInit = this.createStateInit(code, data);
117
+ const address = this.calculateAddress(stateInit, options.workchain || 0);
118
+
119
+ return this.vm.deployBytecode(code, 'tvm', {
120
+ ...options,
121
+ address,
122
+ stateInit
123
+ });
124
+ }
125
+
126
+ async sendMessage(address, body, options = {}) {
127
+ return this.vm.execute(address, 'recv_internal', body, options);
128
+ }
129
+
130
+ async sendExternalMessage(address, body, options = {}) {
131
+ return this.vm.execute(address, 'recv_external', body, options);
132
+ }
133
+
134
+ createStateInit(code, data) {
135
+ // Simplified state init
136
+ return {
137
+ code,
138
+ data,
139
+ libraries: null,
140
+ splitDepth: null,
141
+ tickTock: null
142
+ };
143
+ }
144
+
145
+ calculateAddress(stateInit, workchain) {
146
+ const crypto = require('crypto');
147
+ const hash = crypto.createHash('sha256')
148
+ .update(JSON.stringify(stateInit))
149
+ .digest('hex');
150
+
151
+ return `${workchain}:${hash}`;
152
+ }
153
+
154
+ // Cell serialization (simplified)
155
+ createCell(data) {
156
+ return {
157
+ bits: data,
158
+ refs: []
159
+ };
160
+ }
161
+
162
+ parseCell(cell) {
163
+ return cell.bits;
164
+ }
165
+ }
166
+
167
+ /**
168
+ * WASM Adapter (Cosmos, Polkadot, NEAR)
169
+ */
170
+ class WASMAdapter {
171
+ constructor(vm) {
172
+ this.vm = vm;
173
+ this.contracts = new Map();
174
+ }
175
+
176
+ async instantiate(wasmCode, initMsg, options = {}) {
177
+ const codeId = this.storeCode(wasmCode);
178
+ const address = this.generateAddress(codeId, options.salt);
179
+
180
+ // Execute instantiate
181
+ const result = await this.vm.deployBytecode(wasmCode, 'wasm', {
182
+ ...options,
183
+ address,
184
+ initMsg
185
+ });
186
+
187
+ this.contracts.set(address, { codeId, admin: options.admin });
188
+
189
+ return { ...result, address };
190
+ }
191
+
192
+ async execute(address, msg, options = {}) {
193
+ const msgBuffer = Buffer.from(JSON.stringify(msg));
194
+ return this.vm.execute(address, 'execute', msgBuffer, options);
195
+ }
196
+
197
+ async query(address, msg) {
198
+ const msgBuffer = Buffer.from(JSON.stringify(msg));
199
+ const result = await this.vm.execute(address, 'query', msgBuffer, {
200
+ readOnly: true
201
+ });
202
+
203
+ if (result.returnValue) {
204
+ try {
205
+ return JSON.parse(result.returnValue.toString());
206
+ } catch {
207
+ return result.returnValue;
208
+ }
209
+ }
210
+ return null;
211
+ }
212
+
213
+ storeCode(wasmCode) {
214
+ const crypto = require('crypto');
215
+ return crypto.createHash('sha256').update(wasmCode).digest('hex');
216
+ }
217
+
218
+ generateAddress(codeId, salt) {
219
+ const crypto = require('crypto');
220
+ const input = salt ? `${codeId}${salt}` : `${codeId}${Date.now()}`;
221
+ return crypto.createHash('sha256').update(input).digest('hex').substring(0, 40);
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Cairo Adapter (Starknet)
227
+ */
228
+ class CairoAdapter {
229
+ constructor(vm) {
230
+ this.vm = vm;
231
+ this.classes = new Map();
232
+ this.contracts = new Map();
233
+ }
234
+
235
+ async declareClass(sierraCode, casmCode) {
236
+ const crypto = require('crypto');
237
+ const classHash = crypto.createHash('sha256')
238
+ .update(sierraCode)
239
+ .digest('hex');
240
+
241
+ this.classes.set(classHash, { sierra: sierraCode, casm: casmCode });
242
+
243
+ return { classHash };
244
+ }
245
+
246
+ async deploy(classHash, constructorCalldata, options = {}) {
247
+ const classData = this.classes.get(classHash);
248
+ if (!classData) {
249
+ throw new Error(`Class not found: ${classHash}`);
250
+ }
251
+
252
+ const address = this.calculateContractAddress(classHash, constructorCalldata, options.salt);
253
+
254
+ const result = await this.vm.deployBytecode(classData.casm, 'cairo', {
255
+ ...options,
256
+ address,
257
+ constructorCalldata
258
+ });
259
+
260
+ this.contracts.set(address, { classHash });
261
+
262
+ return { ...result, address };
263
+ }
264
+
265
+ async invoke(address, selector, calldata, options = {}) {
266
+ return this.vm.execute(address, selector, calldata, options);
267
+ }
268
+
269
+ async call(address, selector, calldata) {
270
+ return this.vm.execute(address, selector, calldata, { readOnly: true });
271
+ }
272
+
273
+ calculateContractAddress(classHash, constructorCalldata, salt) {
274
+ const crypto = require('crypto');
275
+ const input = `${classHash}${JSON.stringify(constructorCalldata)}${salt || ''}`;
276
+ return '0x' + crypto.createHash('sha256').update(input).digest('hex').substring(0, 64);
277
+ }
278
+
279
+ // Felt252 operations
280
+ feltToString(felt) {
281
+ return BigInt(felt).toString(16);
282
+ }
283
+
284
+ stringToFelt(str) {
285
+ let result = 0n;
286
+ for (let i = 0; i < str.length; i++) {
287
+ result = result * 256n + BigInt(str.charCodeAt(i));
288
+ }
289
+ return result;
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Bitcoin Script Adapter
295
+ */
296
+ class BitcoinAdapter {
297
+ constructor(vm) {
298
+ this.vm = vm;
299
+ this.utxos = new Map();
300
+ }
301
+
302
+ async executeScript(script, stack = []) {
303
+ const result = await this.vm.execute('bitcoin', 'execute', script, {
304
+ stack
305
+ });
306
+
307
+ return {
308
+ success: result.returnValue !== 0,
309
+ stack: result.stateChanges?.stack || []
310
+ };
311
+ }
312
+
313
+ createP2PKH(pubkeyHash) {
314
+ // OP_DUP OP_HASH160 <pubkeyHash> OP_EQUALVERIFY OP_CHECKSIG
315
+ return Buffer.concat([
316
+ Buffer.from([0x76, 0xA9, 0x14]), // OP_DUP OP_HASH160 PUSH20
317
+ pubkeyHash,
318
+ Buffer.from([0x88, 0xAC]) // OP_EQUALVERIFY OP_CHECKSIG
319
+ ]);
320
+ }
321
+
322
+ createP2SH(scriptHash) {
323
+ // OP_HASH160 <scriptHash> OP_EQUAL
324
+ return Buffer.concat([
325
+ Buffer.from([0xA9, 0x14]), // OP_HASH160 PUSH20
326
+ scriptHash,
327
+ Buffer.from([0x87]) // OP_EQUAL
328
+ ]);
329
+ }
330
+
331
+ createP2WPKH(pubkeyHash) {
332
+ // OP_0 <pubkeyHash>
333
+ return Buffer.concat([
334
+ Buffer.from([0x00, 0x14]), // OP_0 PUSH20
335
+ pubkeyHash
336
+ ]);
337
+ }
338
+
339
+ createMultisig(m, pubkeys) {
340
+ // OP_M <pubkeys...> OP_N OP_CHECKMULTISIG
341
+ const n = pubkeys.length;
342
+ const parts = [Buffer.from([0x50 + m])]; // OP_M
343
+
344
+ for (const pubkey of pubkeys) {
345
+ parts.push(Buffer.from([pubkey.length]));
346
+ parts.push(pubkey);
347
+ }
348
+
349
+ parts.push(Buffer.from([0x50 + n])); // OP_N
350
+ parts.push(Buffer.from([0xAE])); // OP_CHECKMULTISIG
351
+
352
+ return Buffer.concat(parts);
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Adapter Factory
358
+ */
359
+ class AdapterFactory {
360
+ constructor(vm) {
361
+ this.vm = vm;
362
+ this.adapters = new Map();
363
+ }
364
+
365
+ getAdapter(vmType) {
366
+ if (this.adapters.has(vmType)) {
367
+ return this.adapters.get(vmType);
368
+ }
369
+
370
+ let adapter;
371
+ switch (vmType) {
372
+ case 'evm':
373
+ adapter = new EVMAdapter(this.vm);
374
+ break;
375
+ case 'svm':
376
+ adapter = new SVMAdapter(this.vm);
377
+ break;
378
+ case 'move':
379
+ adapter = new MoveAdapter(this.vm);
380
+ break;
381
+ case 'tvm':
382
+ adapter = new TVMAdapter(this.vm);
383
+ break;
384
+ case 'wasm':
385
+ adapter = new WASMAdapter(this.vm);
386
+ break;
387
+ case 'cairo':
388
+ adapter = new CairoAdapter(this.vm);
389
+ break;
390
+ case 'bitcoin':
391
+ adapter = new BitcoinAdapter(this.vm);
392
+ break;
393
+ default:
394
+ throw new Error(`Unknown VM type: ${vmType}`);
395
+ }
396
+
397
+ this.adapters.set(vmType, adapter);
398
+ return adapter;
399
+ }
400
+ }
401
+
402
+ module.exports = {
403
+ EVMAdapter,
404
+ SVMAdapter,
405
+ MoveAdapter,
406
+ TVMAdapter,
407
+ WASMAdapter,
408
+ CairoAdapter,
409
+ BitcoinAdapter,
410
+ AdapterFactory
411
+ };