@dynamic-labs/embedded-wallet-solana 3.0.0-alpha.10

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 (35) hide show
  1. package/CHANGELOG.md +3504 -0
  2. package/LICENSE +21 -0
  3. package/README.md +11 -0
  4. package/_virtual/_tslib.cjs +49 -0
  5. package/_virtual/_tslib.js +44 -0
  6. package/package.json +47 -0
  7. package/src/TurnkeySolanaWalletConnectors.cjs +19 -0
  8. package/src/TurnkeySolanaWalletConnectors.d.ts +2 -0
  9. package/src/TurnkeySolanaWalletConnectors.js +15 -0
  10. package/src/index.cjs +10 -0
  11. package/src/index.d.ts +2 -0
  12. package/src/index.js +2 -0
  13. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaSigner.cjs +60 -0
  14. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaSigner.d.ts +45 -0
  15. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaSigner.js +56 -0
  16. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaWalletConnector.cjs +336 -0
  17. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaWalletConnector.d.ts +55 -0
  18. package/src/lib/TurnkeySolanaWalletConnector/TurnkeySolanaWalletConnector.js +332 -0
  19. package/src/lib/TurnkeySolanaWalletConnector/index.d.ts +1 -0
  20. package/src/lib/constants.cjs +8 -0
  21. package/src/lib/constants.d.ts +1 -0
  22. package/src/lib/constants.js +4 -0
  23. package/src/lib/utils/createSolanaConnection/createSolanaConnection.cjs +14 -0
  24. package/src/lib/utils/createSolanaConnection/createSolanaConnection.d.ts +2 -0
  25. package/src/lib/utils/createSolanaConnection/createSolanaConnection.js +10 -0
  26. package/src/lib/utils/createSolanaConnection/index.d.ts +1 -0
  27. package/src/lib/utils/getGenesisHashLSKey/getGenesisHashLSKey.cjs +10 -0
  28. package/src/lib/utils/getGenesisHashLSKey/getGenesisHashLSKey.d.ts +1 -0
  29. package/src/lib/utils/getGenesisHashLSKey/getGenesisHashLSKey.js +6 -0
  30. package/src/lib/utils/getGenesisHashLSKey/index.d.ts +1 -0
  31. package/src/lib/utils/index.d.ts +3 -0
  32. package/src/lib/utils/transactionDecoder/index.d.ts +1 -0
  33. package/src/lib/utils/transactionDecoder/transactionDecoder.cjs +140 -0
  34. package/src/lib/utils/transactionDecoder/transactionDecoder.d.ts +7 -0
  35. package/src/lib/utils/transactionDecoder/transactionDecoder.js +134 -0
@@ -0,0 +1,134 @@
1
+ 'use client'
2
+ import { __awaiter } from '../../../../_virtual/_tslib.js';
3
+ import { PublicKey, TransactionMessage, Transaction, SystemProgram, SystemInstruction } from '@solana/web3.js';
4
+ import { TOKEN_PROGRAM_ID, decodeTransferInstructionUnchecked } from '@solana/spl-token';
5
+ import { logger } from '@dynamic-labs/wallet-connector-core';
6
+ import { DynamicError } from '@dynamic-labs/utils';
7
+
8
+ const getTotalSolanaSpend = (transaction, connection, thisAddress) => __awaiter(void 0, void 0, void 0, function* () {
9
+ var _a, _b, _c, _d, _e, _f;
10
+ let simulation;
11
+ if ('version' in transaction) {
12
+ simulation = yield connection.simulateTransaction(transaction, {
13
+ accounts: { addresses: [thisAddress], encoding: 'base64' },
14
+ replaceRecentBlockhash: true,
15
+ });
16
+ }
17
+ else {
18
+ simulation = yield connection.simulateTransaction(transaction, undefined, [new PublicKey(thisAddress)]);
19
+ }
20
+ const previousBalance = yield connection.getBalance(new PublicKey(thisAddress));
21
+ if (!((_c = (_b = (_a = simulation === null || simulation === void 0 ? void 0 : simulation.value) === null || _a === void 0 ? void 0 : _a.accounts) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.lamports)) {
22
+ logger.debug('Transaction simulation failed', simulation);
23
+ const instructionError = JSON.stringify({
24
+ InstructionError: [
25
+ 0,
26
+ {
27
+ Custom: 1,
28
+ },
29
+ ],
30
+ });
31
+ const insufficientFundsForRent = JSON.stringify({
32
+ InsufficientFundsForRent: {
33
+ account_index: 0,
34
+ },
35
+ });
36
+ if (JSON.stringify(simulation === null || simulation === void 0 ? void 0 : simulation.value.err) === instructionError ||
37
+ JSON.stringify(simulation === null || simulation === void 0 ? void 0 : simulation.value.err) === insufficientFundsForRent) {
38
+ throw new Error('Insufficient funds');
39
+ }
40
+ return undefined;
41
+ }
42
+ const totalSolTransfer = previousBalance - ((_f = (_e = (_d = simulation === null || simulation === void 0 ? void 0 : simulation.value) === null || _d === void 0 ? void 0 : _d.accounts) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.lamports);
43
+ return BigInt(totalSolTransfer);
44
+ });
45
+ const decodeTransaction = (transaction, connection, thisAddress) => __awaiter(void 0, void 0, void 0, function* () {
46
+ var _g;
47
+ if (!transaction) {
48
+ throw new DynamicError('Transaction is required');
49
+ }
50
+ let decodedInstructions = [];
51
+ if ('version' in transaction) {
52
+ const lookupTableAddresses = transaction.message.addressTableLookups.map((lookup) => new PublicKey(lookup.accountKey));
53
+ // for NON simple sol transfers we need to fetch the lookup table accounts
54
+ if (lookupTableAddresses.length > 0) {
55
+ const lookupTables = yield Promise.all(lookupTableAddresses.map((address) => connection.getAddressLookupTable(address)));
56
+ const lookupTableAccounts = lookupTables
57
+ .filter((result) => result !== null)
58
+ .map((result) => result.value);
59
+ if (lookupTableAccounts.length > 0) {
60
+ decodedInstructions = TransactionMessage.decompile(transaction.message, {
61
+ addressLookupTableAccounts: lookupTableAccounts,
62
+ }).instructions;
63
+ }
64
+ }
65
+ else {
66
+ decodedInstructions = TransactionMessage.decompile(transaction.message).instructions;
67
+ }
68
+ }
69
+ else if (!transaction.instructions) {
70
+ decodedInstructions = (_g = Transaction.from(Buffer.from(transaction.serialize()))) === null || _g === void 0 ? void 0 : _g.instructions;
71
+ }
72
+ else {
73
+ decodedInstructions = transaction.instructions;
74
+ }
75
+ if (!(decodedInstructions === null || decodedInstructions === void 0 ? void 0 : decodedInstructions.length)) {
76
+ throw new DynamicError('Bad formatted instruction');
77
+ }
78
+ const solTransfers = decodedInstructions.filter((instruction) => instruction.programId.equals(SystemProgram.programId));
79
+ // non SPL transfers, just SOL
80
+ if (solTransfers.length > 0) {
81
+ return solTransfers.map((decodedInstruction) => {
82
+ const decodedTransferInstruction = SystemInstruction.decodeTransfer(decodedInstruction);
83
+ return {
84
+ from: decodedTransferInstruction === null || decodedTransferInstruction === void 0 ? void 0 : decodedTransferInstruction.fromPubkey.toBase58(),
85
+ to: decodedTransferInstruction === null || decodedTransferInstruction === void 0 ? void 0 : decodedTransferInstruction.toPubkey.toBase58(),
86
+ };
87
+ });
88
+ }
89
+ // SPL transfers
90
+ return Promise.all(decodedInstructions.map((instruction) => __awaiter(void 0, void 0, void 0, function* () {
91
+ var _h, _j, _k, _l, _m, _o, _p;
92
+ if (instruction.programId.equals(TOKEN_PROGRAM_ID)) {
93
+ const decodedTokenInstruction = decodeTransferInstructionUnchecked(instruction);
94
+ const { source, destination } = decodedTokenInstruction.keys;
95
+ if (destination) {
96
+ // for contract interactions, ex. swaps, the destination is the turnkey address, flip sender and receiver
97
+ if ((destination === null || destination === void 0 ? void 0 : destination.pubkey.toBase58()) === thisAddress) {
98
+ return {
99
+ from: thisAddress,
100
+ to: source === null || source === void 0 ? void 0 : source.pubkey.toBase58(),
101
+ };
102
+ }
103
+ // Pure SPL transfers, get the address from the destination token account to display to user
104
+ const destinationAccountInfo = yield connection.getParsedAccountInfo(destination.pubkey);
105
+ const isTokenAccount = ((_k = (_j = (_h = destinationAccountInfo.value) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.parsed) === null || _k === void 0 ? void 0 : _k.type) === 'account';
106
+ const destinationOwner = isTokenAccount
107
+ ? (_p = (_o = (_m = (_l = destinationAccountInfo.value) === null || _l === void 0 ? void 0 : _l.data) === null || _m === void 0 ? void 0 : _m.parsed) === null || _o === void 0 ? void 0 : _o.info) === null || _p === void 0 ? void 0 : _p.owner
108
+ : null;
109
+ const toAddress = destinationOwner
110
+ ? destinationOwner
111
+ : destination === null || destination === void 0 ? void 0 : destination.pubkey.toBase58();
112
+ return {
113
+ from: thisAddress,
114
+ to: toAddress,
115
+ };
116
+ }
117
+ }
118
+ return null;
119
+ })));
120
+ });
121
+ const summarizeTransactionDecodedData = (transactionsData) => {
122
+ const recipients = new Set();
123
+ transactionsData.forEach((transactionData) => {
124
+ if (transactionData && transactionData.to) {
125
+ recipients.add(transactionData.to);
126
+ }
127
+ });
128
+ const to = recipients.size === 1
129
+ ? recipients.values().next().value
130
+ : 'dyn_send_transaction.multiple_recipients';
131
+ return to;
132
+ };
133
+
134
+ export { decodeTransaction, getTotalSolanaSpend, summarizeTransactionDecodedData };