@wallfree-dev/solana 0.13.42

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 (38) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +4 -0
  3. package/index.d.ts +4 -0
  4. package/index.js +24 -0
  5. package/index.js.map +1 -0
  6. package/package.json +64 -0
  7. package/v1/block-explorer/SolanaBlockExplorer.d.ts +9 -0
  8. package/v1/block-explorer/SolanaBlockExplorer.js +72 -0
  9. package/v1/block-explorer/SolanaBlockExplorer.js.map +1 -0
  10. package/v1/module/SolanaModule.d.ts +16 -0
  11. package/v1/module/SolanaModule.js +106 -0
  12. package/v1/module/SolanaModule.js.map +1 -0
  13. package/v1/protocol/SolanaProtocol.d.ts +74 -0
  14. package/v1/protocol/SolanaProtocol.js +694 -0
  15. package/v1/protocol/SolanaProtocol.js.map +1 -0
  16. package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +14 -0
  17. package/v1/serializer/v3/schemas/converter/transaction-converter.js +34 -0
  18. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
  19. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-solana.d.ts +6 -0
  20. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-solana.js +3 -0
  21. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-solana.js.map +1 -0
  22. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-solana.d.ts +3 -0
  23. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-solana.js +3 -0
  24. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-solana.js.map +1 -0
  25. package/v1/serializer/v3/schemas/generated/transaction-sign-request-solana.json +29 -0
  26. package/v1/serializer/v3/schemas/generated/transaction-sign-response-solana.json +19 -0
  27. package/v1/serializer/v3/serializer-companion.d.ts +14 -0
  28. package/v1/serializer/v3/serializer-companion.js +156 -0
  29. package/v1/serializer/v3/serializer-companion.js.map +1 -0
  30. package/v1/serializer/v3/validators/transaction-validator.d.ts +7 -0
  31. package/v1/serializer/v3/validators/transaction-validator.js +84 -0
  32. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -0
  33. package/v1/serializer/v3/validators/validators.d.ts +4 -0
  34. package/v1/serializer/v3/validators/validators.js +69 -0
  35. package/v1/serializer/v3/validators/validators.js.map +1 -0
  36. package/v1/types/protocol.d.ts +16 -0
  37. package/v1/types/protocol.js +3 -0
  38. package/v1/types/protocol.js.map +1 -0
@@ -0,0 +1,694 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ };
45
+ var __generator = (this && this.__generator) || function (thisArg, body) {
46
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
+ function verb(n) { return function (v) { return step([n, v]); }; }
49
+ function step(op) {
50
+ if (f) throw new TypeError("Generator is already executing.");
51
+ while (_) try {
52
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53
+ if (y = 0, t) op = [op[0] & 2, t.value];
54
+ switch (op[0]) {
55
+ case 0: case 1: t = op; break;
56
+ case 4: _.label++; return { value: op[1], done: false };
57
+ case 5: _.label++; y = op[1]; op = [0]; continue;
58
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
+ default:
60
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
+ if (t[2]) _.ops.pop();
65
+ _.trys.pop(); continue;
66
+ }
67
+ op = body.call(thisArg, _);
68
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
+ }
71
+ };
72
+ Object.defineProperty(exports, "__esModule", { value: true });
73
+ exports.createSolanaProtocol = exports.SolanaProtocol = exports.SOLANA_UNITS_METADATA = void 0;
74
+ var module_kit_1 = require("@wallfree-dev/module-kit");
75
+ var solanaWeb3 = __importStar(require("@solana/web3.js"));
76
+ // @ts-ignore
77
+ var bs58 = __importStar(require("bs58"));
78
+ var nacl = __importStar(require("tweetnacl"));
79
+ var bignumber_1 = require("@wallfree-dev/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
80
+ exports.SOLANA_UNITS_METADATA = {
81
+ SOL: {
82
+ symbol: { value: 'SOL', market: 'sol' },
83
+ decimals: 9
84
+ },
85
+ Lamports: {
86
+ symbol: { value: 'Lamports' },
87
+ decimals: 0
88
+ }
89
+ };
90
+ var SolanaProtocol = /** @class */ (function () {
91
+ function SolanaProtocol(options) {
92
+ this.options = options;
93
+ }
94
+ SolanaProtocol.prototype.getMetadata = function () {
95
+ return __awaiter(this, void 0, void 0, function () {
96
+ return __generator(this, function (_a) {
97
+ return [2 /*return*/, {
98
+ identifier: 'solana',
99
+ name: 'Solana',
100
+ units: exports.SOLANA_UNITS_METADATA,
101
+ mainUnit: 'SOL',
102
+ fee: {
103
+ defaults: {
104
+ low: (0, module_kit_1.newAmount)(0.000005, 'SOL'),
105
+ medium: (0, module_kit_1.newAmount)(0.000005, 'SOL'),
106
+ high: (0, module_kit_1.newAmount)(0.000005, 'SOL')
107
+ },
108
+ units: exports.SOLANA_UNITS_METADATA,
109
+ mainUnit: 'SOL'
110
+ },
111
+ account: {
112
+ standardDerivationPath: "m/44'/501'/0'/0'",
113
+ address: {
114
+ isCaseSensitive: true,
115
+ placeholder: '...',
116
+ regex: '^[1-9A-HJ-NP-Za-km-z]{32,44}$'
117
+ }
118
+ },
119
+ transaction: {
120
+ arbitraryData: {
121
+ inner: { name: 'data' }
122
+ }
123
+ }
124
+ }];
125
+ });
126
+ });
127
+ };
128
+ SolanaProtocol.prototype.getPublicKeyBuffer = function (publicKey) {
129
+ if (publicKey.format === 'hex') {
130
+ return Buffer.from(publicKey.value, 'hex');
131
+ }
132
+ else if (publicKey.format === 'encoded') {
133
+ // Assuming Base58 for 'encoded' in Solana context
134
+ return bs58.decode(publicKey.value);
135
+ }
136
+ // Fallback or default assumption
137
+ return Buffer.from(publicKey.value, 'hex');
138
+ };
139
+ SolanaProtocol.prototype.getAddressFromPublicKey = function (publicKey) {
140
+ return __awaiter(this, void 0, void 0, function () {
141
+ var pubKeyBytes;
142
+ return __generator(this, function (_a) {
143
+ pubKeyBytes = this.getPublicKeyBuffer(publicKey);
144
+ return [2 /*return*/, bs58.encode(pubKeyBytes)];
145
+ });
146
+ });
147
+ };
148
+ SolanaProtocol.prototype.getAddressesFromPublicKey = function (publicKey) {
149
+ return __awaiter(this, void 0, void 0, function () {
150
+ var address;
151
+ return __generator(this, function (_a) {
152
+ switch (_a.label) {
153
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
154
+ case 1:
155
+ address = _a.sent();
156
+ return [2 /*return*/, [{
157
+ address: address,
158
+ cursor: { hasNext: false }
159
+ }]];
160
+ }
161
+ });
162
+ });
163
+ };
164
+ SolanaProtocol.prototype.getBalanceOfAddress = function (address) {
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ var pubKey, connection, balance, e_1;
167
+ return __generator(this, function (_a) {
168
+ switch (_a.label) {
169
+ case 0:
170
+ pubKey = new solanaWeb3.PublicKey(address);
171
+ _a.label = 1;
172
+ case 1:
173
+ _a.trys.push([1, 3, , 4]);
174
+ console.info('[solana][balance] rpcUrl=%s address=%s', this.options.network.rpcUrl, address);
175
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
176
+ return [4 /*yield*/, connection.getBalance(pubKey)];
177
+ case 2:
178
+ balance = _a.sent();
179
+ console.info('[solana][balance] success lamports=%s via %s', balance, this.options.network.rpcUrl);
180
+ return [2 /*return*/, {
181
+ total: (0, module_kit_1.newAmount)(balance, 'Lamports')
182
+ }];
183
+ case 3:
184
+ e_1 = _a.sent();
185
+ console.error('[solana][balance][error] rpcUrl=%s', this.options.network.rpcUrl, e_1);
186
+ return [2 /*return*/, { total: (0, module_kit_1.newAmount)(0, 'Lamports') }];
187
+ case 4: return [2 /*return*/];
188
+ }
189
+ });
190
+ });
191
+ };
192
+ SolanaProtocol.prototype.getBalanceOfPublicKey = function (publicKey) {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var address;
195
+ return __generator(this, function (_a) {
196
+ switch (_a.label) {
197
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
198
+ case 1:
199
+ address = _a.sent();
200
+ return [2 /*return*/, this.getBalanceOfAddress(address)];
201
+ }
202
+ });
203
+ });
204
+ };
205
+ SolanaProtocol.prototype.getDetailsFromTransaction = function (transaction, publicKey) {
206
+ var _a;
207
+ return __awaiter(this, void 0, void 0, function () {
208
+ var tx, fallbackFeePayer, _b, _c, _d, feePayer, firstIx, decoded, amountLamports, fromAddress, toAddress, fee, connection, feeInfo, e_2, e_3, fallbackTx;
209
+ return __generator(this, function (_e) {
210
+ switch (_e.label) {
211
+ case 0:
212
+ _e.trys.push([0, 8, , 9]);
213
+ tx = solanaWeb3.Transaction.from(Buffer.from(transaction.serialized, 'hex'));
214
+ if (!(publicKey !== null && publicKey !== undefined)) return [3 /*break*/, 2];
215
+ _d = (_c = solanaWeb3.PublicKey).bind;
216
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
217
+ case 1:
218
+ _b = new (_d.apply(_c, [void 0, _e.sent()]))();
219
+ return [3 /*break*/, 3];
220
+ case 2:
221
+ _b = undefined;
222
+ _e.label = 3;
223
+ case 3:
224
+ fallbackFeePayer = _b;
225
+ feePayer = (tx === null || tx === void 0 ? void 0 : tx.feePayer) || (tx.signatures.length > 0 ? tx.signatures[0].publicKey : fallbackFeePayer);
226
+ if (!feePayer) {
227
+ throw new Error('missing fee payer');
228
+ }
229
+ firstIx = tx.instructions && tx.instructions[0];
230
+ if (!firstIx) {
231
+ throw new Error('missing instruction');
232
+ }
233
+ decoded = solanaWeb3.SystemInstruction.decodeTransfer(firstIx);
234
+ amountLamports = new bignumber_1.BigNumber(decoded.lamports.toString());
235
+ fromAddress = feePayer.toBase58();
236
+ toAddress = decoded.toPubkey.toBase58();
237
+ fee = (0, module_kit_1.newAmount)(5000, 'Lamports');
238
+ _e.label = 4;
239
+ case 4:
240
+ _e.trys.push([4, 6, , 7]);
241
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
242
+ return [4 /*yield*/, connection.getFeeForMessage(tx.compileMessage())];
243
+ case 5:
244
+ feeInfo = _e.sent();
245
+ fee = (0, module_kit_1.newAmount)((_a = feeInfo === null || feeInfo === void 0 ? void 0 : feeInfo.value) !== null && _a !== void 0 ? _a : 5000, 'Lamports');
246
+ return [3 /*break*/, 7];
247
+ case 6:
248
+ e_2 = _e.sent();
249
+ // Network unavailable (e.g., offline vault), use default fee
250
+ console.warn('[solana][getDetailsFromTransaction] Network unavailable, using default fee');
251
+ return [3 /*break*/, 7];
252
+ case 7: return [2 /*return*/, [
253
+ {
254
+ from: [fromAddress],
255
+ to: [toAddress],
256
+ amount: (0, module_kit_1.newAmount)(amountLamports.toString(), 'Lamports'),
257
+ fee: fee,
258
+ isInbound: false,
259
+ network: this.options.network
260
+ }
261
+ ]];
262
+ case 8:
263
+ e_3 = _e.sent();
264
+ console.error('[solana][getDetailsFromTransaction] Failed to parse transaction:', e_3);
265
+ fallbackTx = {
266
+ from: [],
267
+ to: [],
268
+ amount: (0, module_kit_1.newAmount)(0, 'Lamports'),
269
+ fee: (0, module_kit_1.newAmount)(0, 'Lamports'),
270
+ isInbound: false,
271
+ network: this.options.network
272
+ };
273
+ return [2 /*return*/, [fallbackTx]];
274
+ case 9: return [2 /*return*/];
275
+ }
276
+ });
277
+ });
278
+ };
279
+ SolanaProtocol.prototype.getTransactionDetailsFromSigned = function (signedTx) {
280
+ return __awaiter(this, void 0, void 0, function () {
281
+ return __generator(this, function (_a) {
282
+ return [2 /*return*/, this.getDetailsFromTransaction(signedTx.transaction, null)];
283
+ });
284
+ });
285
+ };
286
+ SolanaProtocol.prototype.verifyMessageWithPublicKey = function (message, signature, publicKey) {
287
+ return __awaiter(this, void 0, void 0, function () {
288
+ var messageBytes, signatureBytes, pubKeyBytes;
289
+ return __generator(this, function (_a) {
290
+ try {
291
+ messageBytes = Buffer.from(message);
292
+ signatureBytes = Buffer.from(signature.value, signature.format === 'hex' ? 'hex' : undefined);
293
+ pubKeyBytes = this.getPublicKeyBuffer(publicKey);
294
+ return [2 /*return*/, nacl.sign.detached.verify(messageBytes, signatureBytes, pubKeyBytes)];
295
+ }
296
+ catch (e) {
297
+ return [2 /*return*/, false];
298
+ }
299
+ return [2 /*return*/];
300
+ });
301
+ });
302
+ };
303
+ SolanaProtocol.prototype.encryptAsymmetricWithPublicKey = function (payload, publicKey) {
304
+ return __awaiter(this, void 0, void 0, function () {
305
+ return __generator(this, function (_a) {
306
+ throw new Error('Not implemented');
307
+ });
308
+ });
309
+ };
310
+ SolanaProtocol.prototype.getCryptoConfiguration = function () {
311
+ return __awaiter(this, void 0, void 0, function () {
312
+ return __generator(this, function (_a) {
313
+ return [2 /*return*/, { algorithm: 'ed25519' }];
314
+ });
315
+ });
316
+ };
317
+ SolanaProtocol.prototype.getKeyPairFromDerivative = function (derivative) {
318
+ return __awaiter(this, void 0, void 0, function () {
319
+ var secretKeyBuffer, publicKeyBuffer;
320
+ return __generator(this, function (_a) {
321
+ secretKeyBuffer = Buffer.from(derivative.secretKey, 'hex');
322
+ publicKeyBuffer = Buffer.from(derivative.publicKey, 'hex');
323
+ return [2 /*return*/, {
324
+ publicKey: {
325
+ value: publicKeyBuffer.toString('hex'),
326
+ format: 'hex',
327
+ type: 'pub'
328
+ },
329
+ secretKey: {
330
+ value: secretKeyBuffer.toString('hex'),
331
+ format: 'hex',
332
+ type: 'priv'
333
+ }
334
+ }];
335
+ });
336
+ });
337
+ };
338
+ // Solana does not support Bip32 extended key derivation
339
+ // These methods are intentionally not implemented
340
+ SolanaProtocol.prototype.signTransactionWithSecretKey = function (transaction, secretKey) {
341
+ return __awaiter(this, void 0, void 0, function () {
342
+ var tx, keyHex, secretKeyBytes, keyPair;
343
+ return __generator(this, function (_a) {
344
+ tx = solanaWeb3.Transaction.from(Buffer.from(transaction.serialized, 'hex'));
345
+ keyHex = secretKey.value;
346
+ secretKeyBytes = Buffer.from(keyHex, secretKey.format === 'hex' ? 'hex' : undefined);
347
+ try {
348
+ if (secretKeyBytes.length === 64) {
349
+ keyPair = solanaWeb3.Keypair.fromSecretKey(secretKeyBytes);
350
+ }
351
+ else if (secretKeyBytes.length === 32) {
352
+ keyPair = solanaWeb3.Keypair.fromSeed(secretKeyBytes);
353
+ }
354
+ else {
355
+ throw new Error("Invalid secret key length: ".concat(secretKeyBytes.length));
356
+ }
357
+ }
358
+ catch (e) {
359
+ throw new Error("Could not create keypair from secret key: ".concat(e));
360
+ }
361
+ tx.sign(keyPair);
362
+ return [2 /*return*/, {
363
+ serialized: tx.serialize().toString('hex'),
364
+ type: 'signed'
365
+ }];
366
+ });
367
+ });
368
+ };
369
+ SolanaProtocol.prototype.signMessageWithKeyPair = function (message, keyPair) {
370
+ return __awaiter(this, void 0, void 0, function () {
371
+ var messageBytes, secretKeyBytes, signingKey, signature;
372
+ return __generator(this, function (_a) {
373
+ messageBytes = Buffer.from(message);
374
+ secretKeyBytes = Buffer.from(keyPair.secretKey.value, keyPair.secretKey.format === 'hex' ? 'hex' : undefined);
375
+ if (secretKeyBytes.length === 64) {
376
+ signingKey = secretKeyBytes;
377
+ }
378
+ else if (secretKeyBytes.length === 32) {
379
+ signingKey = nacl.sign.keyPair.fromSeed(secretKeyBytes).secretKey;
380
+ }
381
+ else {
382
+ throw new Error('Invalid secret key length');
383
+ }
384
+ signature = nacl.sign.detached(messageBytes, signingKey);
385
+ return [2 /*return*/, (0, module_kit_1.newSignature)(Buffer.from(signature).toString('hex'), 'hex')];
386
+ });
387
+ });
388
+ };
389
+ SolanaProtocol.prototype.decryptAsymmetricWithKeyPair = function (payload, keyPair) {
390
+ return __awaiter(this, void 0, void 0, function () {
391
+ return __generator(this, function (_a) {
392
+ throw new Error('Not implemented');
393
+ });
394
+ });
395
+ };
396
+ SolanaProtocol.prototype.encryptAESWithSecretKey = function (payload, secretKey) {
397
+ return __awaiter(this, void 0, void 0, function () {
398
+ return __generator(this, function (_a) {
399
+ throw new Error('Not implemented');
400
+ });
401
+ });
402
+ };
403
+ SolanaProtocol.prototype.decryptAESWithSecretKey = function (payload, secretKey) {
404
+ return __awaiter(this, void 0, void 0, function () {
405
+ return __generator(this, function (_a) {
406
+ throw new Error('Not implemented');
407
+ });
408
+ });
409
+ };
410
+ SolanaProtocol.prototype.getNetwork = function () {
411
+ return __awaiter(this, void 0, void 0, function () {
412
+ return __generator(this, function (_a) {
413
+ return [2 /*return*/, this.options.network];
414
+ });
415
+ });
416
+ };
417
+ SolanaProtocol.prototype.getTransactionsForAddress = function (address, limit, cursor) {
418
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
419
+ return __awaiter(this, void 0, void 0, function () {
420
+ var connection, signatures, txs, _i, signatures_1, sigInfo, parsed, amount, fee, from, to, instructions, _m, instructions_1, ix, parsedIx, info, accountKeys, preBalances, postBalances, i, pubkeyStr, diff, sentAmount, blockTime, timestamp, e_4;
421
+ return __generator(this, function (_o) {
422
+ switch (_o.label) {
423
+ case 0:
424
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
425
+ return [4 /*yield*/, connection.getSignaturesForAddress(new solanaWeb3.PublicKey(address), { limit: limit || 10 })];
426
+ case 1:
427
+ signatures = _o.sent();
428
+ txs = [];
429
+ _i = 0, signatures_1 = signatures;
430
+ _o.label = 2;
431
+ case 2:
432
+ if (!(_i < signatures_1.length)) return [3 /*break*/, 7];
433
+ sigInfo = signatures_1[_i];
434
+ if (!sigInfo.signature) {
435
+ return [3 /*break*/, 6];
436
+ }
437
+ _o.label = 3;
438
+ case 3:
439
+ _o.trys.push([3, 5, , 6]);
440
+ return [4 /*yield*/, connection.getParsedTransaction(sigInfo.signature, {
441
+ commitment: 'confirmed',
442
+ maxSupportedTransactionVersion: 0
443
+ })];
444
+ case 4:
445
+ parsed = _o.sent();
446
+ if (!parsed) {
447
+ return [3 /*break*/, 6];
448
+ }
449
+ amount = (0, module_kit_1.newAmount)(0, 'Lamports');
450
+ fee = (0, module_kit_1.newAmount)((_b = (_a = parsed.meta) === null || _a === void 0 ? void 0 : _a.fee) !== null && _b !== void 0 ? _b : 0, 'Lamports');
451
+ from = [];
452
+ to = [];
453
+ instructions = ((_d = (_c = parsed.transaction) === null || _c === void 0 ? void 0 : _c.message) === null || _d === void 0 ? void 0 : _d.instructions) || [];
454
+ for (_m = 0, instructions_1 = instructions; _m < instructions_1.length; _m++) {
455
+ ix = instructions_1[_m];
456
+ // Check if it's a parsed instruction (has 'parsed' property)
457
+ if ('parsed' in ix && ix.parsed) {
458
+ parsedIx = ix;
459
+ if (parsedIx.program === 'system' && ((_e = parsedIx.parsed) === null || _e === void 0 ? void 0 : _e.type) === 'transfer') {
460
+ info = parsedIx.parsed.info;
461
+ if (info) {
462
+ amount = (0, module_kit_1.newAmount)(((_f = info.lamports) === null || _f === void 0 ? void 0 : _f.toString()) || '0', 'Lamports');
463
+ from = info.source ? [info.source] : [];
464
+ to = info.destination ? [info.destination] : [];
465
+ break;
466
+ }
467
+ }
468
+ }
469
+ }
470
+ // Fallback: try to extract from preBalances/postBalances if transfer info not found
471
+ if (from.length === 0 && to.length === 0 && parsed.meta) {
472
+ accountKeys = ((_h = (_g = parsed.transaction) === null || _g === void 0 ? void 0 : _g.message) === null || _h === void 0 ? void 0 : _h.accountKeys) || [];
473
+ preBalances = parsed.meta.preBalances || [];
474
+ postBalances = parsed.meta.postBalances || [];
475
+ for (i = 0; i < accountKeys.length; i++) {
476
+ pubkeyStr = typeof accountKeys[i] === 'string'
477
+ ? accountKeys[i]
478
+ : ((_j = accountKeys[i].pubkey) === null || _j === void 0 ? void 0 : _j.toString()) || accountKeys[i].toString();
479
+ diff = (postBalances[i] || 0) - (preBalances[i] || 0);
480
+ if (diff < 0) {
481
+ // This account sent SOL (balance decreased)
482
+ from.push(pubkeyStr);
483
+ if (amount.value === '0') {
484
+ sentAmount = Math.abs(diff) - (((_k = parsed.meta) === null || _k === void 0 ? void 0 : _k.fee) || 0);
485
+ if (sentAmount > 0) {
486
+ amount = (0, module_kit_1.newAmount)(sentAmount.toString(), 'Lamports');
487
+ }
488
+ }
489
+ }
490
+ else if (diff > 0) {
491
+ // This account received SOL (balance increased)
492
+ to.push(pubkeyStr);
493
+ }
494
+ }
495
+ }
496
+ blockTime = (_l = sigInfo.blockTime) !== null && _l !== void 0 ? _l : parsed.blockTime;
497
+ timestamp = blockTime ? blockTime : Math.floor(Date.now() / 1000);
498
+ txs.push({
499
+ hash: sigInfo.signature,
500
+ timestamp: timestamp,
501
+ status: sigInfo.confirmationStatus || 'processed',
502
+ amount: amount,
503
+ fee: fee,
504
+ from: from,
505
+ to: to,
506
+ isInbound: to.includes(address),
507
+ network: this.options.network
508
+ });
509
+ return [3 /*break*/, 6];
510
+ case 5:
511
+ e_4 = _o.sent();
512
+ console.error('[solana][getTransactionsForAddress] Failed to parse transaction:', sigInfo.signature, e_4);
513
+ return [3 /*break*/, 6];
514
+ case 6:
515
+ _i++;
516
+ return [3 /*break*/, 2];
517
+ case 7: return [2 /*return*/, {
518
+ transactions: txs,
519
+ cursor: { hasNext: false }
520
+ }];
521
+ }
522
+ });
523
+ });
524
+ };
525
+ SolanaProtocol.prototype.getTransactionsForPublicKey = function (publicKey, limit, cursor) {
526
+ return __awaiter(this, void 0, void 0, function () {
527
+ var address;
528
+ return __generator(this, function (_a) {
529
+ switch (_a.label) {
530
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
531
+ case 1:
532
+ address = _a.sent();
533
+ return [2 /*return*/, this.getTransactionsForAddress(address, limit, cursor)];
534
+ }
535
+ });
536
+ });
537
+ };
538
+ SolanaProtocol.prototype.getTransactionStatus = function (transactionIds) {
539
+ var _a;
540
+ return __awaiter(this, void 0, void 0, function () {
541
+ var connection, statuses, result, i, id, status_1, confirmationStatus;
542
+ return __generator(this, function (_b) {
543
+ switch (_b.label) {
544
+ case 0:
545
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
546
+ return [4 /*yield*/, connection.getSignatureStatuses(transactionIds)];
547
+ case 1:
548
+ statuses = (_b.sent()).value;
549
+ result = {};
550
+ for (i = 0; i < transactionIds.length; i++) {
551
+ id = transactionIds[i];
552
+ status_1 = statuses[i];
553
+ confirmationStatus = (_a = status_1 === null || status_1 === void 0 ? void 0 : status_1.confirmationStatus) !== null && _a !== void 0 ? _a : 'unknown';
554
+ result[id] = { status: confirmationStatus };
555
+ }
556
+ return [2 /*return*/, result];
557
+ }
558
+ });
559
+ });
560
+ };
561
+ SolanaProtocol.prototype.getTransactionMaxAmountWithPublicKey = function (publicKey, to, configuration) {
562
+ return __awaiter(this, void 0, void 0, function () {
563
+ var balance, fee, feeLamports, maxAmount, maxString;
564
+ return __generator(this, function (_a) {
565
+ switch (_a.label) {
566
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
567
+ case 1:
568
+ balance = _a.sent();
569
+ return [4 /*yield*/, this.getTransactionFeeWithPublicKey(publicKey, [{ to: to === null || to === void 0 ? void 0 : to[0], amount: (0, module_kit_1.newAmount)(0, 'Lamports') }])];
570
+ case 2:
571
+ fee = _a.sent();
572
+ feeLamports = fee && fee.medium && fee.medium.value !== undefined ? new bignumber_1.BigNumber(fee.medium.value) : new bignumber_1.BigNumber(5000);
573
+ maxAmount = new bignumber_1.BigNumber(balance.total.value).minus(feeLamports);
574
+ maxString = maxAmount.gt(0) ? maxAmount.toFixed(0) : '0';
575
+ return [2 /*return*/, (0, module_kit_1.newAmount)(maxString, 'Lamports')];
576
+ }
577
+ });
578
+ });
579
+ };
580
+ SolanaProtocol.prototype.getTransactionFeeWithPublicKey = function (publicKey, details, configuration) {
581
+ var _a;
582
+ return __awaiter(this, void 0, void 0, function () {
583
+ var connection, fromAddress, fromPubkey, transaction, _i, details_1, detail, latestBlockhash, feeForMessage, lamportsFee;
584
+ return __generator(this, function (_b) {
585
+ switch (_b.label) {
586
+ case 0:
587
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
588
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
589
+ case 1:
590
+ fromAddress = _b.sent();
591
+ fromPubkey = new solanaWeb3.PublicKey(fromAddress);
592
+ transaction = new solanaWeb3.Transaction();
593
+ for (_i = 0, details_1 = details; _i < details_1.length; _i++) {
594
+ detail = details_1[_i];
595
+ transaction.add(solanaWeb3.SystemProgram.transfer({
596
+ fromPubkey: fromPubkey,
597
+ toPubkey: new solanaWeb3.PublicKey(detail.to),
598
+ lamports: new bignumber_1.BigNumber((0, module_kit_1.newAmount)(detail.amount).blockchain(exports.SOLANA_UNITS_METADATA).value).toNumber()
599
+ }));
600
+ }
601
+ transaction.feePayer = fromPubkey;
602
+ return [4 /*yield*/, connection.getLatestBlockhash()];
603
+ case 2:
604
+ latestBlockhash = _b.sent();
605
+ transaction.recentBlockhash = latestBlockhash.blockhash;
606
+ return [4 /*yield*/, connection.getFeeForMessage(transaction.compileMessage())];
607
+ case 3:
608
+ feeForMessage = _b.sent();
609
+ lamportsFee = (_a = feeForMessage === null || feeForMessage === void 0 ? void 0 : feeForMessage.value) !== null && _a !== void 0 ? _a : 5000;
610
+ return [2 /*return*/, {
611
+ low: (0, module_kit_1.newAmount)(lamportsFee, 'Lamports'),
612
+ medium: (0, module_kit_1.newAmount)(lamportsFee, 'Lamports'),
613
+ high: (0, module_kit_1.newAmount)(lamportsFee, 'Lamports')
614
+ }];
615
+ }
616
+ });
617
+ });
618
+ };
619
+ SolanaProtocol.prototype.prepareTransactionWithPublicKey = function (publicKey, details, configuration) {
620
+ return __awaiter(this, void 0, void 0, function () {
621
+ var connection, fromAddress, fromPubkey, transaction, _i, details_2, detail, blockhash, serialized;
622
+ return __generator(this, function (_a) {
623
+ switch (_a.label) {
624
+ case 0:
625
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
626
+ return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
627
+ case 1:
628
+ fromAddress = _a.sent();
629
+ fromPubkey = new solanaWeb3.PublicKey(fromAddress);
630
+ transaction = new solanaWeb3.Transaction();
631
+ for (_i = 0, details_2 = details; _i < details_2.length; _i++) {
632
+ detail = details_2[_i];
633
+ transaction.add(solanaWeb3.SystemProgram.transfer({
634
+ fromPubkey: fromPubkey,
635
+ toPubkey: new solanaWeb3.PublicKey(detail.to),
636
+ lamports: new bignumber_1.BigNumber((0, module_kit_1.newAmount)(detail.amount).blockchain(exports.SOLANA_UNITS_METADATA).value).toNumber()
637
+ }));
638
+ }
639
+ transaction.feePayer = fromPubkey;
640
+ return [4 /*yield*/, connection.getLatestBlockhash()];
641
+ case 2:
642
+ blockhash = (_a.sent()).blockhash;
643
+ transaction.recentBlockhash = blockhash;
644
+ serialized = transaction.serialize({ requireAllSignatures: false }).toString('hex');
645
+ return [2 /*return*/, {
646
+ serialized: serialized,
647
+ type: 'unsigned'
648
+ }];
649
+ }
650
+ });
651
+ });
652
+ };
653
+ SolanaProtocol.prototype.broadcastTransaction = function (transaction) {
654
+ return __awaiter(this, void 0, void 0, function () {
655
+ var connection, txBuffer, signature;
656
+ return __generator(this, function (_a) {
657
+ switch (_a.label) {
658
+ case 0:
659
+ connection = new solanaWeb3.Connection(this.options.network.rpcUrl);
660
+ txBuffer = Buffer.from(transaction.serialized, 'hex');
661
+ return [4 /*yield*/, connection.sendRawTransaction(txBuffer)];
662
+ case 1:
663
+ signature = _a.sent();
664
+ return [2 /*return*/, signature];
665
+ }
666
+ });
667
+ });
668
+ };
669
+ SolanaProtocol.prototype.getTokenBalancesOfPublicKey = function (publicKey, tokens) {
670
+ return __awaiter(this, void 0, void 0, function () {
671
+ return __generator(this, function (_a) {
672
+ return [2 /*return*/, {}];
673
+ });
674
+ });
675
+ };
676
+ return SolanaProtocol;
677
+ }());
678
+ exports.SolanaProtocol = SolanaProtocol;
679
+ function createSolanaProtocol(options) {
680
+ if (options === void 0) { options = {}; }
681
+ var defaultNetwork = {
682
+ name: 'Solana Mainnet',
683
+ type: 'mainnet',
684
+ rpcUrl: 'https://solana.drpc.org',
685
+ blockExplorerUrl: 'https://explorer.solana.com',
686
+ chainId: 101,
687
+ blockExplorerApi: ''
688
+ };
689
+ return new SolanaProtocol({
690
+ network: __assign(__assign({}, defaultNetwork), options.network)
691
+ });
692
+ }
693
+ exports.createSolanaProtocol = createSolanaProtocol;
694
+ //# sourceMappingURL=SolanaProtocol.js.map