@exodus/solana-lib 3.9.5 → 3.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,26 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.10.1](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.10.0...@exodus/solana-lib@3.10.1) (2025-04-04)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+
12
+ * fix(solana): fee payer validation (#5387)
13
+
14
+
15
+
16
+ ## [3.10.0](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.9.5...@exodus/solana-lib@3.10.0) (2025-04-03)
17
+
18
+
19
+ ### Features
20
+
21
+
22
+ * feat(solana): add optional addExternalFeePayerToTransaction (#5345)
23
+
24
+
25
+
6
26
  ## [3.9.5](https://github.com/ExodusMovement/assets/compare/@exodus/solana-lib@3.9.4...@exodus/solana-lib@3.9.5) (2025-03-25)
7
27
 
8
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-lib",
3
- "version": "3.9.5",
3
+ "version": "3.10.1",
4
4
  "description": "Solana utils, such as for cryptography, address encoding/decoding, transaction building, etc.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -29,6 +29,7 @@
29
29
  "bn.js": "^5.2.1",
30
30
  "borsh": "^0.7.0",
31
31
  "bs58": "^4.0.1",
32
+ "lodash": "^4.17.11",
32
33
  "minimalistic-assert": "^1.0.1"
33
34
  },
34
35
  "devDependencies": {
@@ -45,5 +46,5 @@
45
46
  "type": "git",
46
47
  "url": "git+https://github.com/ExodusMovement/assets.git"
47
48
  },
48
- "gitHead": "0908ae0c3bd6a7d5bdd91946f88d389037f7d710"
49
+ "gitHead": "b4ed33112d03cda68ad156c3c455b4168f7abc16"
49
50
  }
package/src/constants.js CHANGED
@@ -9,6 +9,10 @@ export const STAKE_PROGRAM_ID = StakeProgram.programId
9
9
 
10
10
  export const TOKEN_PROGRAM_ID = new PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA')
11
11
 
12
+ export const COMPUTE_BUDGET_PROGRAM_ID = new PublicKey(
13
+ 'ComputeBudget111111111111111111111111111111'
14
+ )
15
+
12
16
  export const TOKEN_2022_PROGRAM_ID = new PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb')
13
17
 
14
18
  export const MEMO_PROGRAM_ID = new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr')
package/src/tx/common.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { VersionedTransaction } from '@exodus/solana-web3.js'
1
2
  import base58 from 'bs58'
2
3
 
3
4
  export function isVersionedTransaction(tx) {
@@ -12,6 +13,10 @@ export function transactionToBase58(tx) {
12
13
  return base58.encode(tx.serialize())
13
14
  }
14
15
 
16
+ export function deserializeTransaction(tx) {
17
+ return VersionedTransaction.deserialize(tx)
18
+ }
19
+
15
20
  export function getTxId(tx) {
16
21
  const signature = getFirstSignature(tx)
17
22
  if (signature === null) {
@@ -6,6 +6,7 @@ export function createUnsignedTx({
6
6
  fee,
7
7
  feeData,
8
8
  recentBlockhash,
9
+ useFeePayer,
9
10
  // Tokens related:
10
11
  tokenMintAddress,
11
12
  destinationAddressType,
@@ -73,6 +74,7 @@ export function createUnsignedTx({
73
74
  },
74
75
  txMeta: {
75
76
  assetName: asset.name,
77
+ useFeePayer,
76
78
  },
77
79
  }
78
80
  }
package/src/tx/index.js CHANGED
@@ -7,4 +7,6 @@ export * from './decode-tx-instructions.js'
7
7
  export * from './build-raw-transaction.js'
8
8
  export * from './sign-hardware.js'
9
9
  export * from './prepare-for-signing.js'
10
- export { transactionToBase58 } from './common.js'
10
+ export * from './verify-only-fee-payer-changed.js'
11
+ export * from './parse-tx-buffer.js'
12
+ export { transactionToBase58, deserializeTransaction } from './common.js'
@@ -0,0 +1,136 @@
1
+ import BN from 'bn.js'
2
+ import bs58 from 'bs58'
3
+
4
+ import { COMPUTE_BUDGET_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_PROGRAM_ID } from '../constants.js'
5
+ import { deserializeTransaction } from './common.js'
6
+
7
+ function decodeSPLTransferData(base58Data) {
8
+ const buffer = bs58.decode(base58Data)
9
+
10
+ // 1 byte
11
+ if (buffer[0] !== 3) {
12
+ throw new Error(`Unsupported instruction type: ${buffer[0]}`)
13
+ }
14
+
15
+ const amountBytes = buffer.slice(1, 9)
16
+ const amountBN = new BN(amountBytes, 'le')
17
+
18
+ return { amount: amountBN, method: 'transfer' }
19
+ }
20
+
21
+ function decodeSystemTransferData(base58Data) {
22
+ const buffer = bs58.decode(base58Data)
23
+
24
+ // 4 bytes
25
+ if (buffer.readUInt32LE(0) !== 2) {
26
+ throw new Error(`Unsupported instruction type for SystemProgram: ${buffer.readUInt32LE(0)}`)
27
+ }
28
+
29
+ const amountBytes = buffer.slice(4, 12)
30
+ const amountBN = new BN(amountBytes, 'le')
31
+
32
+ return {
33
+ amount: amountBN,
34
+ method: 'systemTransfer',
35
+ }
36
+ }
37
+
38
+ // TODO support more tx types/options
39
+ export function isTokenTransfer(tx) {
40
+ const { message } = tx
41
+ const { accountKeys, instructions } = message
42
+
43
+ if (!Array.isArray(accountKeys) || accountKeys.length !== 6) return false
44
+ if (!Array.isArray(instructions) || instructions.length !== 3) return false
45
+
46
+ const [ix1, ix2, ix3] = instructions
47
+ if (
48
+ !accountKeys[ix1.programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID) ||
49
+ !accountKeys[ix2.programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID)
50
+ ) {
51
+ return false
52
+ }
53
+
54
+ if (!accountKeys[ix3.programIdIndex].equals(TOKEN_PROGRAM_ID)) return false
55
+
56
+ if (!Array.isArray(ix3.accounts) || ix3.accounts.length !== 3) return false
57
+
58
+ try {
59
+ const data = bs58.decode(ix3.data)
60
+ if (data[0] !== 0x03) return false
61
+ } catch {
62
+ return false
63
+ }
64
+
65
+ return true
66
+ }
67
+
68
+ export function isSolanaTransfer(tx) {
69
+ const { message } = tx
70
+ const { accountKeys, instructions } = message
71
+
72
+ if (!Array.isArray(accountKeys) || accountKeys.length !== 5) return false
73
+ if (!Array.isArray(instructions) || instructions.length !== 3) return false
74
+
75
+ if (
76
+ !accountKeys[instructions[0].programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID) ||
77
+ !accountKeys[instructions[1].programIdIndex].equals(COMPUTE_BUDGET_PROGRAM_ID)
78
+ ) {
79
+ return false
80
+ }
81
+
82
+ const ix = instructions[2]
83
+ if (!accountKeys[ix.programIdIndex].equals(SYSTEM_PROGRAM_ID)) return false
84
+
85
+ if (!Array.isArray(ix.accounts) || ix.accounts.length !== 2) return false
86
+
87
+ try {
88
+ const data = bs58.decode(ix.data)
89
+ if (data[0] !== 0x02) return false
90
+ } catch {
91
+ return false
92
+ }
93
+
94
+ return true
95
+ }
96
+
97
+ // TODO: Unify with parseTransaction in solana-api and use there as well?
98
+ // TODO: support more tx types.
99
+ export async function parseTxBuffer(buffer, api) {
100
+ const transaction = deserializeTransaction(buffer)
101
+
102
+ try {
103
+ if (isTokenTransfer(transaction)) {
104
+ const mainInstruction = transaction.message.instructions[2]
105
+ const { amount, method } = decodeSPLTransferData(mainInstruction.data)
106
+
107
+ const fromTokenAddress =
108
+ transaction.message.accountKeys[mainInstruction.accounts[0]].toBase58()
109
+ const toTokenAddress = transaction.message.accountKeys[mainInstruction.accounts[1]].toBase58()
110
+ const from = await api.getTokenAddressOwner(fromTokenAddress)
111
+ const to = await api.getTokenAddressOwner(toTokenAddress)
112
+ return {
113
+ method,
114
+ from,
115
+ to,
116
+ amount,
117
+ }
118
+ }
119
+
120
+ if (isSolanaTransfer(transaction)) {
121
+ const mainInstruction = transaction.message.instructions[2]
122
+ const { amount, method } = decodeSystemTransferData(mainInstruction.data)
123
+
124
+ return {
125
+ method,
126
+ from: transaction.message.accountKeys[mainInstruction.accounts[0]],
127
+ to: transaction.message.accountKeys[mainInstruction.accounts[1]],
128
+ amount,
129
+ }
130
+ }
131
+ } catch (error) {
132
+ console.log('transaction check error', error)
133
+ }
134
+
135
+ throw new Error('Transaction not supported for buffer parsing')
136
+ }
@@ -0,0 +1,104 @@
1
+ import lodash from 'lodash'
2
+ import assert from 'minimalistic-assert'
3
+
4
+ export function verifyOnlyFeePayerChanged(beforeTx, afterTx) {
5
+ assert(
6
+ beforeTx.signatures.length + 1 === afterTx.signatures.length &&
7
+ beforeTx.message.header.numRequiredSignatures + 1 ===
8
+ afterTx.message.header.numRequiredSignatures,
9
+ 'A signature was not added for the new signer'
10
+ )
11
+ beforeTx.signatures.forEach((signature, index) => {
12
+ assert(
13
+ lodash.isEqual(signature, afterTx.signatures[index + 1]),
14
+ 'Existing signatures do not match'
15
+ )
16
+ })
17
+ assert(
18
+ beforeTx.message.accountKeys.length + 1 === afterTx.message.accountKeys.length,
19
+ 'Fee payer account key was not added'
20
+ )
21
+ beforeTx.message.accountKeys.forEach((accountKey, index) => {
22
+ assert(
23
+ lodash.isEqual(accountKey, afterTx.message.accountKeys[index + 1]),
24
+ 'Existing account keys do not match'
25
+ )
26
+ })
27
+
28
+ assert(
29
+ beforeTx.message.instructions.length === afterTx.message.instructions.length,
30
+ 'No new instructions are allowed'
31
+ )
32
+
33
+ beforeTx.message.instructions.forEach(({ programIdIndex }, index) => {
34
+ assert(
35
+ programIdIndex + 1 === afterTx.message.instructions[index]?.programIdIndex,
36
+ 'Instructions program ids were not updated'
37
+ )
38
+ })
39
+
40
+ beforeTx.message.instructions.forEach(({ accounts }, index) => {
41
+ assert(
42
+ lodash.isEqual(
43
+ accounts.map((id) => id + 1),
44
+ afterTx.message.instructions[index].accounts
45
+ ),
46
+ 'Instructions account key indexes were not updated'
47
+ )
48
+ })
49
+
50
+ beforeTx.message.instructions.forEach((instruction, index) => {
51
+ assert(
52
+ lodash.isEqual(
53
+ { ...instruction, accounts: null, programIdIndex: null },
54
+ {
55
+ ...afterTx.message.instructions[index],
56
+ accounts: null,
57
+ programIdIndex: null,
58
+ }
59
+ ),
60
+ 'Instructions do not match in some attributes'
61
+ )
62
+ })
63
+
64
+ afterTx.message.indexToProgramIds.forEach((value, key) => {
65
+ assert(afterTx.message.accountKeys[key] === value, 'IndexToProgramIds do not match accountKeys')
66
+ })
67
+
68
+ assert(
69
+ lodash.isEqual(
70
+ {
71
+ ...beforeTx,
72
+ signatures: null,
73
+ message: null,
74
+ },
75
+ {
76
+ ...afterTx,
77
+
78
+ signatures: null,
79
+ message: null,
80
+ }
81
+ ),
82
+ 'Transactions do not match in some attributes'
83
+ )
84
+
85
+ assert(
86
+ lodash.isEqual(
87
+ {
88
+ ...beforeTx.message,
89
+ header: { ...beforeTx.message.header, numRequiredSignatures: null },
90
+ accountKeys: null,
91
+ instructions: null,
92
+ indexToProgramIds: null,
93
+ },
94
+ {
95
+ ...afterTx.message,
96
+ header: { ...afterTx.message.header, numRequiredSignatures: null },
97
+ accountKeys: null,
98
+ instructions: null,
99
+ indexToProgramIds: null,
100
+ }
101
+ ),
102
+ 'Transactions do not match in some attributes'
103
+ )
104
+ }