@exodus/solana-lib 1.5.0 → 1.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/solana-lib",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "Exodus internal Solana low-level library",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -27,5 +27,5 @@
27
27
  "lodash": "^4.17.11",
28
28
  "tweetnacl": "^1.0.3"
29
29
  },
30
- "gitHead": "fe989a47ba9278f5fe797f9b01d8bc93fb53467a"
30
+ "gitHead": "301d6e9222b3c0828c73cea460c94c9bbce878e6"
31
31
  }
package/src/constants.js CHANGED
@@ -20,3 +20,5 @@ export const SEED = 'stake:0'
20
20
  export const LAMPORTS_PER_SOL = 1000000000
21
21
 
22
22
  export const SOL_DECIMAL = Math.log10(LAMPORTS_PER_SOL)
23
+
24
+ export const SUPPORTED_TRANSACTION_VERSIONS = new Set(['legacy', 0])
@@ -0,0 +1,25 @@
1
+ import { Transaction, VersionedTransaction } from '..'
2
+ import { SUPPORTED_TRANSACTION_VERSIONS } from '../constants'
3
+
4
+ const isVersionedTransactionType = (transaction) => {
5
+ // new transaction types have a version field, either a string(legacy) or a number(>=0)
6
+ return transaction.version !== undefined
7
+ }
8
+
9
+ const isVersionedTransactionTypeSupported = (transaction) => {
10
+ return SUPPORTED_TRANSACTION_VERSIONS.has(transaction.version)
11
+ }
12
+
13
+ const getTransactionStrategy = (transaction): VersionedTransaction | Transaction => {
14
+ if (isVersionedTransactionType(transaction)) {
15
+ if (isVersionedTransactionTypeSupported(transaction)) {
16
+ return VersionedTransaction
17
+ }
18
+
19
+ throw new Error(`unsupported transaction version: ${transaction.version}`)
20
+ }
21
+
22
+ return Transaction
23
+ }
24
+
25
+ export default getTransactionStrategy
package/src/index.js CHANGED
@@ -14,3 +14,5 @@ export {
14
14
  } from './vendor'
15
15
  export { default as Transaction } from './transaction'
16
16
  export { U64 } from './helpers/spl-token'
17
+ export { default as getTransactionStrategy } from './helpers/transaction-strategy'
18
+ export { default as VersionedTransaction } from './versioned-transaction'
@@ -38,8 +38,8 @@ export function createUnsignedTx({
38
38
  txData: {
39
39
  from,
40
40
  to,
41
- amount: amount ? amount.toBase().toNumber() : null,
42
- fee: fee ? fee.toBase().toNumber() : null,
41
+ amount: amount ? amount.toBaseNumber() : null,
42
+ fee: fee ? fee.toBaseNumber() : null,
43
43
  recentBlockhash,
44
44
  // Tokens related:
45
45
  tokenMintAddress,
@@ -2,7 +2,7 @@ import { merge } from 'lodash'
2
2
  import assets from '@exodus/assets'
3
3
  import type { UnsignedTransaction, SignedTransaction } from '@exodus/models/lib/types'
4
4
 
5
- import { Transaction } from '../'
5
+ import { Transaction, getTransactionStrategy } from '../'
6
6
 
7
7
  export function signUnsignedTx(
8
8
  unsignedTx: UnsignedTransaction,
@@ -30,9 +30,11 @@ export function signUnsignedTx(
30
30
 
31
31
  // Signs plain tx.
32
32
  const _signTx = ({ tx, privateKey }) => {
33
- Transaction.sign(tx, privateKey)
34
- const rawTx = Transaction.serialize(tx)
35
- const txId = Transaction.getTxId(tx)
33
+ const _Transaction = getTransactionStrategy(tx)
34
+
35
+ _Transaction.sign(tx, privateKey)
36
+ const rawTx = _Transaction.serialize(tx)
37
+ const txId = _Transaction.getTxId(tx)
36
38
 
37
39
  return { txId, rawTx }
38
40
  }
@@ -0,0 +1,45 @@
1
+ import base58 from 'bs58'
2
+ import { getKeyPairFromPrivateKey } from './keypair'
3
+ import { Account } from './vendor'
4
+
5
+ function getFirstSignature(tx) {
6
+ if (tx.signatures.length > 0) {
7
+ return tx.signatures[0]
8
+ }
9
+
10
+ return null
11
+ }
12
+
13
+ class VersionedTx {
14
+ // tx is not supported from the vendored library (solana-lib/src/vendor).
15
+ // We also can't make any guarantees that Account from vendored lib with version X
16
+ // will be compatible with the tx built by an outside library with version Y.
17
+ static sign(tx, privateKey: Buffer | String, extraSigners: Account[] = []) {
18
+ if (!privateKey) {
19
+ throw new Error('Please provide a secretKey')
20
+ }
21
+ const { secretKey } = getKeyPairFromPrivateKey(privateKey)
22
+ const signers = [new Account(secretKey), ...extraSigners]
23
+
24
+ tx.sign(signers)
25
+
26
+ if (!(tx.signatures && tx.signatures.length)) {
27
+ throw new Error('!Signature')
28
+ }
29
+ }
30
+
31
+ static serialize(tx) {
32
+ const serializedTx = tx.serialize()
33
+ return Buffer.from(serializedTx).toString('base64')
34
+ }
35
+
36
+ static getTxId(tx) {
37
+ const signature = getFirstSignature(tx)
38
+ if (signature === null) {
39
+ throw new Error('Cannot get transaction ID of unsigned transaction')
40
+ }
41
+ return base58.encode(signature)
42
+ }
43
+ }
44
+
45
+ export default VersionedTx