jaelis-node 1.3.2 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +97 -8
  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 +310 -4
  162. package/package.json +2 -2
@@ -0,0 +1,457 @@
1
+ /**
2
+ * SOLANA VM (SVM) ADAPTER
3
+ *
4
+ * Provides Solana-compatible interface for JAELIS Universal VM
5
+ *
6
+ * Features:
7
+ * - Anchor IDL support
8
+ * - Account model emulation
9
+ * - BPF program execution
10
+ * - Cross-Program Invocation (CPI)
11
+ * - Program Derived Addresses (PDA)
12
+ *
13
+ * @version 0.1.0
14
+ * @author Mario Papaleo - JAELIS Foundation
15
+ */
16
+
17
+ const crypto = require('crypto');
18
+
19
+ // Solana system program addresses
20
+ const SYSTEM_PROGRAMS = {
21
+ SYSTEM_PROGRAM: '11111111111111111111111111111111',
22
+ TOKEN_PROGRAM: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
23
+ TOKEN_2022_PROGRAM: 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb',
24
+ ASSOCIATED_TOKEN_PROGRAM: 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
25
+ RENT_PROGRAM: 'SysvarRent111111111111111111111111111111111',
26
+ CLOCK_PROGRAM: 'SysvarC1ock11111111111111111111111111111111'
27
+ };
28
+
29
+ // Borsh serialization types
30
+ const BORSH_TYPES = {
31
+ u8: { size: 1 },
32
+ u16: { size: 2 },
33
+ u32: { size: 4 },
34
+ u64: { size: 8 },
35
+ u128: { size: 16 },
36
+ i8: { size: 1 },
37
+ i16: { size: 2 },
38
+ i32: { size: 4 },
39
+ i64: { size: 8 },
40
+ i128: { size: 16 },
41
+ bool: { size: 1 },
42
+ pubkey: { size: 32 },
43
+ string: { size: null, dynamic: true },
44
+ vec: { size: null, dynamic: true }
45
+ };
46
+
47
+ class SVMAdapter {
48
+ constructor(vm) {
49
+ this.vm = vm;
50
+
51
+ // Program accounts
52
+ this.programs = new Map();
53
+
54
+ // Accounts (Solana's data model)
55
+ this.accounts = new Map();
56
+
57
+ // PDAs cache
58
+ this.pdaCache = new Map();
59
+ }
60
+
61
+ /**
62
+ * Deploy Solana program
63
+ */
64
+ async deployProgram(bytecode, options = {}) {
65
+ // Generate program ID
66
+ const programId = options.programId || this.generateProgramId(bytecode);
67
+
68
+ // Create program account
69
+ const programAccount = {
70
+ lamports: 1_000_000_000n, // 1 SOL rent exempt
71
+ data: bytecode,
72
+ owner: SYSTEM_PROGRAMS.SYSTEM_PROGRAM,
73
+ executable: true,
74
+ rentEpoch: 0
75
+ };
76
+
77
+ this.programs.set(programId, {
78
+ bytecode,
79
+ idl: options.idl || null
80
+ });
81
+
82
+ // Store in VM
83
+ const result = await this.vm.deployBytecode(bytecode, 'svm', {
84
+ ...options,
85
+ deployer: programId
86
+ });
87
+
88
+ return {
89
+ ...result,
90
+ programId
91
+ };
92
+ }
93
+
94
+ /**
95
+ * Execute instruction
96
+ */
97
+ async executeInstruction(programId, instruction, accounts, signers = []) {
98
+ // Build transaction message
99
+ const message = {
100
+ header: {
101
+ numRequiredSignatures: signers.length,
102
+ numReadonlySignedAccounts: 0,
103
+ numReadonlyUnsignedAccounts: 0
104
+ },
105
+ accountKeys: accounts.map(a => a.pubkey),
106
+ recentBlockhash: this.getRecentBlockhash(),
107
+ instructions: [{
108
+ programIdIndex: accounts.findIndex(a => a.pubkey === programId),
109
+ accounts: accounts.map((_, i) => i),
110
+ data: instruction
111
+ }]
112
+ };
113
+
114
+ // Prepare account infos
115
+ const accountInfos = accounts.map(acc => ({
116
+ pubkey: acc.pubkey,
117
+ isSigner: acc.isSigner || false,
118
+ isWritable: acc.isWritable || false,
119
+ lamports: this.getAccount(acc.pubkey)?.lamports || 0n,
120
+ data: this.getAccount(acc.pubkey)?.data || Buffer.alloc(0),
121
+ owner: this.getAccount(acc.pubkey)?.owner || SYSTEM_PROGRAMS.SYSTEM_PROGRAM,
122
+ executable: this.getAccount(acc.pubkey)?.executable || false,
123
+ rentEpoch: 0
124
+ }));
125
+
126
+ // Execute on VM
127
+ const result = await this.vm.execute(programId, 'process_instruction', instruction, {
128
+ accounts: accountInfos,
129
+ signers
130
+ });
131
+
132
+ // Apply state changes
133
+ if (result.success && result.stateChanges) {
134
+ for (const change of result.stateChanges) {
135
+ if (change.type === 'account') {
136
+ this.accounts.set(change.pubkey, change.data);
137
+ }
138
+ }
139
+ }
140
+
141
+ return result;
142
+ }
143
+
144
+ /**
145
+ * Anchor-style instruction execution
146
+ */
147
+ async anchorExecute(programId, methodName, args, accounts, options = {}) {
148
+ const program = this.programs.get(programId);
149
+
150
+ if (!program?.idl) {
151
+ throw new Error('Program IDL not found');
152
+ }
153
+
154
+ // Find instruction in IDL
155
+ const instruction = program.idl.instructions?.find(i => i.name === methodName);
156
+ if (!instruction) {
157
+ throw new Error(`Instruction not found: ${methodName}`);
158
+ }
159
+
160
+ // Encode instruction data using Anchor format
161
+ // First 8 bytes: instruction discriminator (sha256 of "global:method_name")[0:8]
162
+ const discriminator = this.getInstructionDiscriminator(methodName);
163
+
164
+ // Serialize arguments using Borsh
165
+ const serializedArgs = this.borshSerialize(instruction.args, args);
166
+
167
+ const instructionData = Buffer.concat([discriminator, serializedArgs]);
168
+
169
+ return this.executeInstruction(programId, instructionData, accounts, options.signers);
170
+ }
171
+
172
+ /**
173
+ * Create account
174
+ */
175
+ async createAccount(payer, newAccount, space, programOwner) {
176
+ const lamports = this.calculateRent(space);
177
+
178
+ this.accounts.set(newAccount, {
179
+ lamports,
180
+ data: Buffer.alloc(space),
181
+ owner: programOwner,
182
+ executable: false,
183
+ rentEpoch: 0
184
+ });
185
+
186
+ // Deduct from payer
187
+ const payerAccount = this.getAccount(payer);
188
+ if (payerAccount) {
189
+ payerAccount.lamports -= lamports;
190
+ }
191
+
192
+ return { success: true };
193
+ }
194
+
195
+ /**
196
+ * Transfer SOL
197
+ */
198
+ async transfer(from, to, amount) {
199
+ const fromAccount = this.getAccount(from);
200
+ const toAccount = this.getAccount(to) || {
201
+ lamports: 0n,
202
+ data: Buffer.alloc(0),
203
+ owner: SYSTEM_PROGRAMS.SYSTEM_PROGRAM,
204
+ executable: false,
205
+ rentEpoch: 0
206
+ };
207
+
208
+ if (!fromAccount || fromAccount.lamports < BigInt(amount)) {
209
+ throw new Error('Insufficient balance');
210
+ }
211
+
212
+ fromAccount.lamports -= BigInt(amount);
213
+ toAccount.lamports += BigInt(amount);
214
+
215
+ this.accounts.set(to, toAccount);
216
+
217
+ return { success: true };
218
+ }
219
+
220
+ /**
221
+ * Find Program Derived Address (PDA)
222
+ */
223
+ findProgramAddress(seeds, programId) {
224
+ const cacheKey = `${programId}:${seeds.map(s => s.toString('hex')).join(':')}`;
225
+
226
+ if (this.pdaCache.has(cacheKey)) {
227
+ return this.pdaCache.get(cacheKey);
228
+ }
229
+
230
+ // Try bump seeds from 255 down to 0
231
+ for (let bump = 255; bump >= 0; bump--) {
232
+ const seedsWithBump = [...seeds, Buffer.from([bump])];
233
+ const address = this.createProgramAddress(seedsWithBump, programId);
234
+
235
+ if (address && !this.isOnCurve(address)) {
236
+ const result = { address, bump };
237
+ this.pdaCache.set(cacheKey, result);
238
+ return result;
239
+ }
240
+ }
241
+
242
+ throw new Error('Unable to find PDA');
243
+ }
244
+
245
+ /**
246
+ * Create program address
247
+ */
248
+ createProgramAddress(seeds, programId) {
249
+ // SHA256(seeds || programId || "ProgramDerivedAddress")
250
+ const hash = crypto.createHash('sha256');
251
+
252
+ for (const seed of seeds) {
253
+ if (seed.length > 32) {
254
+ throw new Error('Seed too long');
255
+ }
256
+ hash.update(seed);
257
+ }
258
+
259
+ hash.update(Buffer.from(programId.replace('0x', ''), 'hex'));
260
+ hash.update(Buffer.from('ProgramDerivedAddress'));
261
+
262
+ return hash.digest('hex');
263
+ }
264
+
265
+ /**
266
+ * Check if public key is on ed25519 curve
267
+ */
268
+ isOnCurve(pubkey) {
269
+ // Simplified check - real implementation would verify ed25519
270
+ // PDAs should NOT be on the curve
271
+ const bytes = Buffer.from(pubkey, 'hex');
272
+ return bytes[0] !== 0 && bytes[31] < 0x80;
273
+ }
274
+
275
+ /**
276
+ * Get Associated Token Address
277
+ */
278
+ getAssociatedTokenAddress(mint, owner) {
279
+ const { address } = this.findProgramAddress(
280
+ [
281
+ Buffer.from(owner.replace('0x', ''), 'hex'),
282
+ Buffer.from(SYSTEM_PROGRAMS.TOKEN_PROGRAM, 'hex'),
283
+ Buffer.from(mint.replace('0x', ''), 'hex')
284
+ ],
285
+ SYSTEM_PROGRAMS.ASSOCIATED_TOKEN_PROGRAM
286
+ );
287
+ return address;
288
+ }
289
+
290
+ // ═══════════════════════════════════════════════════════════════
291
+ // BORSH SERIALIZATION
292
+ // ═══════════════════════════════════════════════════════════════
293
+
294
+ borshSerialize(schema, data) {
295
+ const buffers = [];
296
+
297
+ for (const field of schema) {
298
+ const value = data[field.name];
299
+ buffers.push(this.borshSerializeValue(field.type, value));
300
+ }
301
+
302
+ return Buffer.concat(buffers);
303
+ }
304
+
305
+ borshSerializeValue(type, value) {
306
+ if (typeof type === 'string') {
307
+ switch (type) {
308
+ case 'u8':
309
+ const u8Buf = Buffer.alloc(1);
310
+ u8Buf.writeUInt8(value);
311
+ return u8Buf;
312
+
313
+ case 'u16':
314
+ const u16Buf = Buffer.alloc(2);
315
+ u16Buf.writeUInt16LE(value);
316
+ return u16Buf;
317
+
318
+ case 'u32':
319
+ const u32Buf = Buffer.alloc(4);
320
+ u32Buf.writeUInt32LE(value);
321
+ return u32Buf;
322
+
323
+ case 'u64':
324
+ const u64Buf = Buffer.alloc(8);
325
+ u64Buf.writeBigUInt64LE(BigInt(value));
326
+ return u64Buf;
327
+
328
+ case 'i64':
329
+ const i64Buf = Buffer.alloc(8);
330
+ i64Buf.writeBigInt64LE(BigInt(value));
331
+ return i64Buf;
332
+
333
+ case 'bool':
334
+ return Buffer.from([value ? 1 : 0]);
335
+
336
+ case 'pubkey':
337
+ const hex = value.startsWith('0x') ? value.slice(2) : value;
338
+ return Buffer.from(hex.padStart(64, '0'), 'hex');
339
+
340
+ case 'string':
341
+ const strBuf = Buffer.from(value, 'utf8');
342
+ const lenBuf = Buffer.alloc(4);
343
+ lenBuf.writeUInt32LE(strBuf.length);
344
+ return Buffer.concat([lenBuf, strBuf]);
345
+
346
+ default:
347
+ throw new Error(`Unknown Borsh type: ${type}`);
348
+ }
349
+ }
350
+
351
+ // Handle complex types
352
+ if (type.vec) {
353
+ const lenBuf = Buffer.alloc(4);
354
+ lenBuf.writeUInt32LE(value.length);
355
+ const items = value.map(v => this.borshSerializeValue(type.vec, v));
356
+ return Buffer.concat([lenBuf, ...items]);
357
+ }
358
+
359
+ if (type.option) {
360
+ if (value === null || value === undefined) {
361
+ return Buffer.from([0]);
362
+ }
363
+ return Buffer.concat([Buffer.from([1]), this.borshSerializeValue(type.option, value)]);
364
+ }
365
+
366
+ throw new Error(`Unsupported Borsh type: ${JSON.stringify(type)}`);
367
+ }
368
+
369
+ borshDeserialize(schema, data) {
370
+ const result = {};
371
+ let offset = 0;
372
+
373
+ for (const field of schema) {
374
+ const { value, bytesRead } = this.borshDeserializeValue(field.type, data, offset);
375
+ result[field.name] = value;
376
+ offset += bytesRead;
377
+ }
378
+
379
+ return result;
380
+ }
381
+
382
+ borshDeserializeValue(type, data, offset) {
383
+ if (typeof type === 'string') {
384
+ switch (type) {
385
+ case 'u8':
386
+ return { value: data.readUInt8(offset), bytesRead: 1 };
387
+
388
+ case 'u16':
389
+ return { value: data.readUInt16LE(offset), bytesRead: 2 };
390
+
391
+ case 'u32':
392
+ return { value: data.readUInt32LE(offset), bytesRead: 4 };
393
+
394
+ case 'u64':
395
+ return { value: data.readBigUInt64LE(offset), bytesRead: 8 };
396
+
397
+ case 'i64':
398
+ return { value: data.readBigInt64LE(offset), bytesRead: 8 };
399
+
400
+ case 'bool':
401
+ return { value: data[offset] !== 0, bytesRead: 1 };
402
+
403
+ case 'pubkey':
404
+ return { value: data.slice(offset, offset + 32).toString('hex'), bytesRead: 32 };
405
+
406
+ case 'string':
407
+ const len = data.readUInt32LE(offset);
408
+ const str = data.slice(offset + 4, offset + 4 + len).toString('utf8');
409
+ return { value: str, bytesRead: 4 + len };
410
+
411
+ default:
412
+ throw new Error(`Unknown Borsh type: ${type}`);
413
+ }
414
+ }
415
+
416
+ throw new Error(`Unsupported Borsh type: ${JSON.stringify(type)}`);
417
+ }
418
+
419
+ // ═══════════════════════════════════════════════════════════════
420
+ // HELPERS
421
+ // ═══════════════════════════════════════════════════════════════
422
+
423
+ getInstructionDiscriminator(methodName) {
424
+ const hash = crypto.createHash('sha256')
425
+ .update(`global:${methodName}`)
426
+ .digest();
427
+ return hash.slice(0, 8);
428
+ }
429
+
430
+ generateProgramId(bytecode) {
431
+ const hash = crypto.createHash('sha256').update(bytecode).digest('hex');
432
+ return hash;
433
+ }
434
+
435
+ getRecentBlockhash() {
436
+ // Generate pseudo-random blockhash
437
+ return crypto.randomBytes(32).toString('hex');
438
+ }
439
+
440
+ calculateRent(space) {
441
+ // Solana rent calculation
442
+ // Approximate: 19.055441478439427 lamports per byte per year
443
+ // Rent-exempt minimum: 2 years
444
+ const lamportsPerByteYear = 19055441478n;
445
+ return (BigInt(space) * lamportsPerByteYear * 2n) / 1000000000n;
446
+ }
447
+
448
+ getAccount(pubkey) {
449
+ return this.accounts.get(pubkey);
450
+ }
451
+
452
+ setAccount(pubkey, account) {
453
+ this.accounts.set(pubkey, account);
454
+ }
455
+ }
456
+
457
+ module.exports = SVMAdapter;