jaelis-node 1.10.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/README.md +146 -445
  2. package/bin/jaelis-node.js +79 -504
  3. package/lib/index.js +31 -2840
  4. package/lib/node.js +271 -0
  5. package/lib/rpc.js +315 -0
  6. package/lib/storage.js +198 -0
  7. package/lib/sync.js +366 -0
  8. package/package.json +19 -53
  9. package/config/default.json +0 -74
  10. package/config/mainnet.json +0 -30
  11. package/config/testnet.json +0 -26
  12. package/lib/JAELIS-VM/lib/adapters/evm-adapter.js +0 -454
  13. package/lib/JAELIS-VM/lib/adapters/index.js +0 -411
  14. package/lib/JAELIS-VM/lib/adapters/svm-adapter.js +0 -457
  15. package/lib/JAELIS-VM/lib/compiler/jir-compiler.js +0 -1097
  16. package/lib/JAELIS-VM/lib/execution/engine.js +0 -1183
  17. package/lib/JAELIS-VM/lib/index.js +0 -440
  18. package/lib/JAELIS-VM/lib/integration/jaelis-integration.js +0 -543
  19. package/lib/JAELIS-VM/lib/serialization/serializer.js +0 -819
  20. package/lib/JAELIS-VM/lib/state/state-manager.js +0 -1116
  21. package/lib/JAELIS-VM/lib/translator/bytecode-translator.js +0 -1222
  22. package/lib/JAELIS-VM/lib/unified/cross-chain-deploy.js +0 -1678
  23. package/lib/JAELIS-VM/lib/unified/cross-chain-state.js +0 -836
  24. package/lib/JAELIS-VM/lib/unified/dynamic-contracts.js +0 -1127
  25. package/lib/JAELIS-VM/lib/unified/index.js +0 -456
  26. package/lib/JAELIS-VM/lib/unified/jaelis-abi.js +0 -1150
  27. package/lib/JAELIS-VM/lib/unified/unified-compiler.js +0 -1350
  28. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds +0 -12
  29. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.cmd +0 -17
  30. package/lib/JAELIS-VM/node_modules/.bin/download-cbor-prebuilds.ps1 +0 -28
  31. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds +0 -12
  32. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.cmd +0 -17
  33. package/lib/JAELIS-VM/node_modules/.bin/download-msgpackr-prebuilds.ps1 +0 -28
  34. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  35. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  36. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  37. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  38. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  39. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  40. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  41. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  42. package/lib/JAELIS-VM/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  43. package/lib/JAELIS-VM/node_modules/.package-lock.json +0 -127
  44. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/README.md +0 -1
  45. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/index.js +0 -0
  46. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.abi115.node +0 -0
  47. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/node.napi.node +0 -0
  48. package/lib/JAELIS-VM/node_modules/@cbor-extract/cbor-extract-win32-x64/package.json +0 -17
  49. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/README.md +0 -1
  50. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/index.js +0 -0
  51. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.abi115.node +0 -0
  52. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/node.napi.node +0 -0
  53. package/lib/JAELIS-VM/node_modules/@msgpackr-extract/msgpackr-extract-win32-x64/package.json +0 -17
  54. package/lib/JAELIS-VM/node_modules/cbor-extract/LICENSE +0 -21
  55. package/lib/JAELIS-VM/node_modules/cbor-extract/README.md +0 -5
  56. package/lib/JAELIS-VM/node_modules/cbor-extract/bin/download-prebuilds.js +0 -11
  57. package/lib/JAELIS-VM/node_modules/cbor-extract/binding.gyp +0 -60
  58. package/lib/JAELIS-VM/node_modules/cbor-extract/index.js +0 -1
  59. package/lib/JAELIS-VM/node_modules/cbor-extract/package.json +0 -50
  60. package/lib/JAELIS-VM/node_modules/cbor-extract/src/extract.cpp +0 -198
  61. package/lib/JAELIS-VM/node_modules/cbor-x/LICENSE +0 -21
  62. package/lib/JAELIS-VM/node_modules/cbor-x/README.md +0 -380
  63. package/lib/JAELIS-VM/node_modules/cbor-x/SECURITY.md +0 -11
  64. package/lib/JAELIS-VM/node_modules/cbor-x/benchmark.md +0 -73
  65. package/lib/JAELIS-VM/node_modules/cbor-x/browser.js +0 -11
  66. package/lib/JAELIS-VM/node_modules/cbor-x/decode.d.ts +0 -2
  67. package/lib/JAELIS-VM/node_modules/cbor-x/decode.js +0 -1300
  68. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs +0 -1244
  69. package/lib/JAELIS-VM/node_modules/cbor-x/dist/decode-no-eval.cjs.map +0 -1
  70. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs +0 -2509
  71. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.cjs.map +0 -1
  72. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js +0 -2
  73. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index-no-eval.min.js.map +0 -1
  74. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js +0 -2508
  75. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.js.map +0 -1
  76. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js +0 -2
  77. package/lib/JAELIS-VM/node_modules/cbor-x/dist/index.min.js.map +0 -1
  78. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs +0 -2629
  79. package/lib/JAELIS-VM/node_modules/cbor-x/dist/node.cjs.map +0 -1
  80. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js +0 -3343
  81. package/lib/JAELIS-VM/node_modules/cbor-x/dist/test.js.map +0 -1
  82. package/lib/JAELIS-VM/node_modules/cbor-x/encode.d.ts +0 -1
  83. package/lib/JAELIS-VM/node_modules/cbor-x/encode.js +0 -1231
  84. package/lib/JAELIS-VM/node_modules/cbor-x/index.d.ts +0 -79
  85. package/lib/JAELIS-VM/node_modules/cbor-x/index.js +0 -3
  86. package/lib/JAELIS-VM/node_modules/cbor-x/iterators.js +0 -85
  87. package/lib/JAELIS-VM/node_modules/cbor-x/node-index.js +0 -24
  88. package/lib/JAELIS-VM/node_modules/cbor-x/package.json +0 -94
  89. package/lib/JAELIS-VM/node_modules/cbor-x/rollup.config.js +0 -88
  90. package/lib/JAELIS-VM/node_modules/cbor-x/stream.js +0 -61
  91. package/lib/JAELIS-VM/node_modules/cbor-x/webpack.config.js +0 -19
  92. package/lib/JAELIS-VM/node_modules/detect-libc/LICENSE +0 -201
  93. package/lib/JAELIS-VM/node_modules/detect-libc/README.md +0 -163
  94. package/lib/JAELIS-VM/node_modules/detect-libc/index.d.ts +0 -14
  95. package/lib/JAELIS-VM/node_modules/detect-libc/lib/detect-libc.js +0 -313
  96. package/lib/JAELIS-VM/node_modules/detect-libc/lib/elf.js +0 -39
  97. package/lib/JAELIS-VM/node_modules/detect-libc/lib/filesystem.js +0 -51
  98. package/lib/JAELIS-VM/node_modules/detect-libc/lib/process.js +0 -24
  99. package/lib/JAELIS-VM/node_modules/detect-libc/package.json +0 -44
  100. package/lib/JAELIS-VM/node_modules/msgpackr/LICENSE +0 -21
  101. package/lib/JAELIS-VM/node_modules/msgpackr/README.md +0 -372
  102. package/lib/JAELIS-VM/node_modules/msgpackr/SECURITY.md +0 -11
  103. package/lib/JAELIS-VM/node_modules/msgpackr/benchmark.md +0 -67
  104. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs +0 -2407
  105. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.cjs.map +0 -1
  106. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js +0 -2
  107. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index-no-eval.min.js.map +0 -1
  108. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js +0 -2406
  109. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.js.map +0 -1
  110. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js +0 -2
  111. package/lib/JAELIS-VM/node_modules/msgpackr/dist/index.min.js.map +0 -1
  112. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs +0 -3320
  113. package/lib/JAELIS-VM/node_modules/msgpackr/dist/node.cjs.map +0 -1
  114. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js +0 -4540
  115. package/lib/JAELIS-VM/node_modules/msgpackr/dist/test.js.map +0 -1
  116. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs +0 -1250
  117. package/lib/JAELIS-VM/node_modules/msgpackr/dist/unpack-no-eval.cjs.map +0 -1
  118. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.cts +0 -91
  119. package/lib/JAELIS-VM/node_modules/msgpackr/index.d.ts +0 -91
  120. package/lib/JAELIS-VM/node_modules/msgpackr/index.js +0 -5
  121. package/lib/JAELIS-VM/node_modules/msgpackr/iterators.js +0 -87
  122. package/lib/JAELIS-VM/node_modules/msgpackr/node-index.js +0 -25
  123. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.cts +0 -1
  124. package/lib/JAELIS-VM/node_modules/msgpackr/pack.d.ts +0 -1
  125. package/lib/JAELIS-VM/node_modules/msgpackr/pack.js +0 -1141
  126. package/lib/JAELIS-VM/node_modules/msgpackr/package.json +0 -104
  127. package/lib/JAELIS-VM/node_modules/msgpackr/rollup.config.js +0 -88
  128. package/lib/JAELIS-VM/node_modules/msgpackr/stream.js +0 -57
  129. package/lib/JAELIS-VM/node_modules/msgpackr/struct.js +0 -815
  130. package/lib/JAELIS-VM/node_modules/msgpackr/test-worker.js +0 -3
  131. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.cts +0 -2
  132. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.d.ts +0 -2
  133. package/lib/JAELIS-VM/node_modules/msgpackr/unpack.js +0 -1221
  134. package/lib/JAELIS-VM/node_modules/msgpackr-extract/LICENSE +0 -21
  135. package/lib/JAELIS-VM/node_modules/msgpackr-extract/README.md +0 -5
  136. package/lib/JAELIS-VM/node_modules/msgpackr-extract/bin/download-prebuilds.js +0 -13
  137. package/lib/JAELIS-VM/node_modules/msgpackr-extract/binding.gyp +0 -63
  138. package/lib/JAELIS-VM/node_modules/msgpackr-extract/index.js +0 -1
  139. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages +0 -12
  140. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional +0 -12
  141. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.cmd +0 -17
  142. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-optional.ps1 +0 -28
  143. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test +0 -12
  144. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.cmd +0 -17
  145. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages-test.ps1 +0 -28
  146. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.cmd +0 -17
  147. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/.bin/node-gyp-build-optional-packages.ps1 +0 -28
  148. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  149. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  150. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/bin.js +0 -82
  151. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  152. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/index.js +0 -6
  153. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/node-gyp-build.js +0 -236
  154. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  155. package/lib/JAELIS-VM/node_modules/msgpackr-extract/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  156. package/lib/JAELIS-VM/node_modules/msgpackr-extract/package.json +0 -50
  157. package/lib/JAELIS-VM/node_modules/msgpackr-extract/src/extract.cpp +0 -274
  158. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/LICENSE +0 -21
  159. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/README.md +0 -58
  160. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/bin.js +0 -77
  161. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/build-test.js +0 -19
  162. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/index.js +0 -224
  163. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/optional.js +0 -7
  164. package/lib/JAELIS-VM/node_modules/node-gyp-build-optional-packages/package.json +0 -32
  165. package/lib/JAELIS-VM/package-lock.json +0 -284
  166. package/lib/JAELIS-VM/package.json +0 -38
  167. package/lib/JAELIS-VM/test/comprehensive.test.js +0 -267
  168. package/lib/JAELIS-VM/test/cross-chain-test.js +0 -470
  169. package/lib/JAELIS-VM/test/unified-vm-test.js +0 -459
  170. package/lib/JAELIS-VM/test/unified.test.js +0 -166
  171. package/lib/JAELIS-VM/test/vm.test.js +0 -599
  172. package/lib/settlement-server.js +0 -999
  173. package/lib/vm/index.js +0 -397
@@ -1,457 +0,0 @@
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;