@exodus/solana-lib 1.2.14 → 1.2.20
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 +6 -5
- package/src/constants.js +12 -0
- package/src/encode.js +62 -0
- package/src/fee-data/index.js +1 -0
- package/src/fee-data/solana.js +9 -0
- package/src/helpers/spl-token.js +108 -0
- package/src/helpers/tokenTransfer.js +72 -0
- package/src/index.js +9 -0
- package/src/keypair.js +32 -0
- package/src/tokens.js +12 -0
- package/src/transaction.js +292 -0
- package/src/tx/create-and-sign-tx.js +8 -0
- package/{lib → src}/tx/create-unsigned-tx.js +11 -18
- package/src/tx/index.js +4 -0
- package/src/tx/parse-unsigned-tx.js +41 -0
- package/src/tx/sign-unsigned-tx.js +78 -0
- package/src/vendor/account.js +38 -0
- package/src/vendor/index.js +9 -0
- package/src/vendor/instruction.js +46 -0
- package/src/vendor/message.js +216 -0
- package/src/vendor/nonce-account.js +46 -0
- package/src/vendor/publickey.js +212 -0
- package/src/vendor/stake-program.js +527 -0
- package/src/vendor/system-program.js +782 -0
- package/src/vendor/sysvar.js +16 -0
- package/src/vendor/transaction.js +594 -0
- package/src/vendor/utils/blockhash.js +6 -0
- package/src/vendor/utils/fee-calculator.js +17 -0
- package/src/vendor/utils/layout.js +80 -0
- package/src/vendor/utils/shortvec-encoding.js +30 -0
- package/src/vendor/utils/to-buffer.js +9 -0
- package/lib/constants.js +0 -19
- package/lib/encode.js +0 -67
- package/lib/fee-data/index.js +0 -15
- package/lib/fee-data/solana.js +0 -14
- package/lib/helpers/spl-token.js +0 -122
- package/lib/helpers/tokenTransfer.js +0 -78
- package/lib/index.js +0 -88
- package/lib/keypair.js +0 -38
- package/lib/tokens.js +0 -21
- package/lib/transaction.js +0 -338
- package/lib/tx/create-and-sign-tx.js +0 -15
- package/lib/tx/index.js +0 -53
- package/lib/tx/parse-unsigned-tx.js +0 -55
- package/lib/tx/sign-unsigned-tx.js +0 -90
- package/lib/vendor/account.js +0 -48
- package/lib/vendor/index.js +0 -113
- package/lib/vendor/instruction.js +0 -48
- package/lib/vendor/message.js +0 -167
- package/lib/vendor/nonce-account.js +0 -56
- package/lib/vendor/publickey.js +0 -211
- package/lib/vendor/stake-program.js +0 -476
- package/lib/vendor/system-program.js +0 -640
- package/lib/vendor/sysvar.js +0 -19
- package/lib/vendor/transaction.js +0 -594
- package/lib/vendor/utils/blockhash.js +0 -1
- package/lib/vendor/utils/fee-calculator.js +0 -25
- package/lib/vendor/utils/layout.js +0 -97
- package/lib/vendor/utils/shortvec-encoding.js +0 -41
- package/lib/vendor/utils/to-buffer.js +0 -18
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import type { UnsignedTransaction, ParsedTransaction } from '@exodus/models/lib/types'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createUnsignedTx = createUnsignedTx;
|
|
7
|
-
|
|
8
|
-
function createUnsignedTx({
|
|
3
|
+
export function createUnsignedTx({
|
|
9
4
|
asset,
|
|
10
5
|
from,
|
|
11
6
|
to,
|
|
@@ -15,16 +10,14 @@ function createUnsignedTx({
|
|
|
15
10
|
// Tokens related:
|
|
16
11
|
tokenName,
|
|
17
12
|
destinationAddressType,
|
|
18
|
-
isAssociatedTokenAccountActive,
|
|
19
|
-
//
|
|
20
|
-
fromTokenAddresses,
|
|
21
|
-
// sender token addresses
|
|
13
|
+
isAssociatedTokenAccountActive, // true when recipient balance !== 0
|
|
14
|
+
fromTokenAddresses, // sender token addresses
|
|
22
15
|
// Staking related:
|
|
23
16
|
method,
|
|
24
17
|
stakeAddresses,
|
|
25
18
|
seed,
|
|
26
|
-
pool
|
|
27
|
-
}) {
|
|
19
|
+
pool,
|
|
20
|
+
}: ParsedTransaction): UnsignedTransaction {
|
|
28
21
|
return {
|
|
29
22
|
txData: {
|
|
30
23
|
from,
|
|
@@ -41,10 +34,10 @@ function createUnsignedTx({
|
|
|
41
34
|
method,
|
|
42
35
|
stakeAddresses,
|
|
43
36
|
seed,
|
|
44
|
-
pool
|
|
37
|
+
pool,
|
|
45
38
|
},
|
|
46
39
|
txMeta: {
|
|
47
|
-
assetName: asset.name
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
40
|
+
assetName: asset.name,
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/tx/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import assets from '@exodus/assets'
|
|
2
|
+
import type { UnsignedTransaction, ParsedTransaction } from '@exodus/models/lib/types'
|
|
3
|
+
|
|
4
|
+
export function parseUnsignedTx(unsignedTx: UnsignedTransaction): ParsedTransaction {
|
|
5
|
+
const asset = assets.solana
|
|
6
|
+
const {
|
|
7
|
+
from,
|
|
8
|
+
to,
|
|
9
|
+
recentBlockhash,
|
|
10
|
+
tokenName,
|
|
11
|
+
destinationAddressType,
|
|
12
|
+
isAssociatedTokenAccountActive,
|
|
13
|
+
fromTokenAddresses,
|
|
14
|
+
method,
|
|
15
|
+
stakeAddresses,
|
|
16
|
+
seed,
|
|
17
|
+
pool,
|
|
18
|
+
...txData
|
|
19
|
+
} = unsignedTx.txData
|
|
20
|
+
|
|
21
|
+
const amount = asset.currency.baseUnit(txData.amount)
|
|
22
|
+
const fee = asset.currency.baseUnit(txData.fee)
|
|
23
|
+
return {
|
|
24
|
+
asset,
|
|
25
|
+
from: [from],
|
|
26
|
+
to,
|
|
27
|
+
amount,
|
|
28
|
+
fee,
|
|
29
|
+
recentBlockhash,
|
|
30
|
+
// token related
|
|
31
|
+
tokenName,
|
|
32
|
+
destinationAddressType,
|
|
33
|
+
isAssociatedTokenAccountActive,
|
|
34
|
+
fromTokenAddresses,
|
|
35
|
+
// staking related
|
|
36
|
+
method,
|
|
37
|
+
stakeAddresses,
|
|
38
|
+
seed,
|
|
39
|
+
pool,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import assets from '@exodus/assets'
|
|
2
|
+
import type { UnsignedTransaction, SignedTransaction } from '@exodus/models/lib/types'
|
|
3
|
+
import { Transaction } from '../'
|
|
4
|
+
|
|
5
|
+
export function signUnsignedTx(
|
|
6
|
+
unsignedTx: UnsignedTransaction,
|
|
7
|
+
privateKey: Buffer
|
|
8
|
+
): SignedTransaction {
|
|
9
|
+
const {
|
|
10
|
+
from,
|
|
11
|
+
to,
|
|
12
|
+
amount: unitAmount,
|
|
13
|
+
recentBlockhash,
|
|
14
|
+
// tokens related
|
|
15
|
+
tokenName,
|
|
16
|
+
destinationAddressType,
|
|
17
|
+
isAssociatedTokenAccountActive,
|
|
18
|
+
fromTokenAddresses,
|
|
19
|
+
// staking related
|
|
20
|
+
stakeAddresses,
|
|
21
|
+
method,
|
|
22
|
+
seed,
|
|
23
|
+
pool,
|
|
24
|
+
} = unsignedTx.txData
|
|
25
|
+
|
|
26
|
+
const asset = assets.solana
|
|
27
|
+
|
|
28
|
+
const address = from
|
|
29
|
+
const amount = unitAmount ? asset.currency.baseUnit(unitAmount).toNumber() : unitAmount
|
|
30
|
+
|
|
31
|
+
let tx
|
|
32
|
+
switch (method) {
|
|
33
|
+
case 'delegate':
|
|
34
|
+
tx = Transaction.createStakeAccountTransaction({
|
|
35
|
+
address,
|
|
36
|
+
amount,
|
|
37
|
+
recentBlockhash,
|
|
38
|
+
seed,
|
|
39
|
+
pool,
|
|
40
|
+
})
|
|
41
|
+
break
|
|
42
|
+
case 'undelegate':
|
|
43
|
+
tx = Transaction.undelegate({
|
|
44
|
+
address,
|
|
45
|
+
stakeAddresses,
|
|
46
|
+
recentBlockhash,
|
|
47
|
+
})
|
|
48
|
+
break
|
|
49
|
+
case 'withdraw':
|
|
50
|
+
tx = Transaction.withdraw({
|
|
51
|
+
address,
|
|
52
|
+
stakeAddresses,
|
|
53
|
+
amount,
|
|
54
|
+
recentBlockhash,
|
|
55
|
+
})
|
|
56
|
+
break
|
|
57
|
+
default:
|
|
58
|
+
// SOL and Token tx
|
|
59
|
+
tx = new Transaction({
|
|
60
|
+
from,
|
|
61
|
+
to,
|
|
62
|
+
amount,
|
|
63
|
+
recentBlockhash,
|
|
64
|
+
tokenName,
|
|
65
|
+
destinationAddressType,
|
|
66
|
+
isAssociatedTokenAccountActive,
|
|
67
|
+
fromTokenAddresses,
|
|
68
|
+
})
|
|
69
|
+
break
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// sign plain tx
|
|
73
|
+
Transaction.sign(tx, privateKey)
|
|
74
|
+
const rawTx = Transaction.serialize(tx)
|
|
75
|
+
const txId = Transaction.getTxId(tx)
|
|
76
|
+
|
|
77
|
+
return { txId, rawTx }
|
|
78
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type { KeyPair } from 'tweetnacl'
|
|
3
|
+
import { PublicKey } from './publickey'
|
|
4
|
+
import { getKeyPairFromPrivateKey } from '../keypair'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* An account key pair (public and secret keys).
|
|
8
|
+
*/
|
|
9
|
+
export class Account {
|
|
10
|
+
_keypair: KeyPair
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create a new Account object
|
|
14
|
+
*
|
|
15
|
+
* @param secretKey Secret key for the account
|
|
16
|
+
*/
|
|
17
|
+
constructor(secretKey: Buffer | Uint8Array | Array<number>) {
|
|
18
|
+
if (secretKey) {
|
|
19
|
+
this._keypair = getKeyPairFromPrivateKey(Buffer.from(secretKey, 'hex'))
|
|
20
|
+
} else {
|
|
21
|
+
throw new Error('Please provide a secretKey')
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The public key for this account
|
|
27
|
+
*/
|
|
28
|
+
get publicKey(): PublicKey {
|
|
29
|
+
return new PublicKey(this._keypair.publicKey)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The **unencrypted** secret key for this account
|
|
34
|
+
*/
|
|
35
|
+
get secretKey(): Buffer {
|
|
36
|
+
return this._keypair.secretKey
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './account'
|
|
2
|
+
export * from './instruction'
|
|
3
|
+
export * from './message'
|
|
4
|
+
export * from './nonce-account'
|
|
5
|
+
export * from './publickey'
|
|
6
|
+
export * from './system-program'
|
|
7
|
+
export * from './stake-program'
|
|
8
|
+
export * from './sysvar'
|
|
9
|
+
export * from './transaction'
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import * as BufferLayout from '@exodus/buffer-layout'
|
|
4
|
+
|
|
5
|
+
import * as Layout from './utils/layout'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} InstructionType
|
|
9
|
+
* @property (index} The Instruction index (from solana upstream program)
|
|
10
|
+
* @property (BufferLayout} The BufferLayout to use to build data
|
|
11
|
+
*/
|
|
12
|
+
export type InstructionType = {|
|
|
13
|
+
index: number,
|
|
14
|
+
layout: typeof BufferLayout,
|
|
15
|
+
|}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Populate a buffer of instruction data using an InstructionType
|
|
19
|
+
*/
|
|
20
|
+
export function encodeData(type: InstructionType, fields: Object): Buffer {
|
|
21
|
+
const allocLength = type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields)
|
|
22
|
+
const data = Buffer.alloc(allocLength)
|
|
23
|
+
const layoutFields = Object.assign({ instruction: type.index }, fields)
|
|
24
|
+
type.layout.encode(layoutFields, data)
|
|
25
|
+
return data
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Decode instruction data buffer using an InstructionType
|
|
30
|
+
*/
|
|
31
|
+
export function decodeData(type: InstructionType, buffer: Buffer): Object {
|
|
32
|
+
let data
|
|
33
|
+
try {
|
|
34
|
+
data = type.layout.decode(buffer)
|
|
35
|
+
} catch (err) {
|
|
36
|
+
throw new Error('invalid instruction; ' + err)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (data.instruction !== type.index) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`invalid instruction; instruction index mismatch ${data.instruction} != ${type.index}`
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return data
|
|
46
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import bs58 from 'bs58'
|
|
4
|
+
import * as BufferLayout from '@exodus/buffer-layout'
|
|
5
|
+
|
|
6
|
+
import { PublicKey } from './publickey'
|
|
7
|
+
import type { Blockhash } from './utils/blockhash'
|
|
8
|
+
import * as Layout from './utils/layout'
|
|
9
|
+
import { PACKET_DATA_SIZE } from './transaction'
|
|
10
|
+
import * as shortvec from './utils/shortvec-encoding'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The message header, identifying signed and read-only account
|
|
14
|
+
*
|
|
15
|
+
* @typedef {Object} MessageHeader
|
|
16
|
+
* @property {number} numRequiredSignatures The number of signatures required for this message to be considered valid. The
|
|
17
|
+
* signatures must match the first `numRequiredSignatures` of `accountKeys`.
|
|
18
|
+
* @property {number} numReadonlySignedAccounts: The last `numReadonlySignedAccounts` of the signed keys are read-only accounts
|
|
19
|
+
* @property {number} numReadonlyUnsignedAccounts The last `numReadonlySignedAccounts` of the unsigned keys are read-only accounts
|
|
20
|
+
*/
|
|
21
|
+
export type MessageHeader = {
|
|
22
|
+
numRequiredSignatures: number,
|
|
23
|
+
numReadonlySignedAccounts: number,
|
|
24
|
+
numReadonlyUnsignedAccounts: number,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* An instruction to execute by a program
|
|
29
|
+
*
|
|
30
|
+
* @typedef {Object} CompiledInstruction
|
|
31
|
+
* @property {number} programIdIndex Index into the transaction keys array indicating the program account that executes this instruction
|
|
32
|
+
* @property {number[]} accounts Ordered indices into the transaction keys array indicating which accounts to pass to the program
|
|
33
|
+
* @property {string} data The program input data encoded as base 58
|
|
34
|
+
*/
|
|
35
|
+
export type CompiledInstruction = {
|
|
36
|
+
programIdIndex: number,
|
|
37
|
+
accounts: number[],
|
|
38
|
+
data: string,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Message constructor arguments
|
|
43
|
+
*
|
|
44
|
+
* @typedef {Object} MessageArgs
|
|
45
|
+
* @property {MessageHeader} header The message header, identifying signed and read-only `accountKeys`
|
|
46
|
+
* @property {string[]} accounts All the account keys used by this transaction
|
|
47
|
+
* @property {Blockhash} recentBlockhash The hash of a recent ledger block
|
|
48
|
+
* @property {CompiledInstruction[]} instructions Instructions that will be executed in sequence and committed in one atomic transaction if all succeed.
|
|
49
|
+
*/
|
|
50
|
+
type MessageArgs = {
|
|
51
|
+
header: MessageHeader,
|
|
52
|
+
accountKeys: string[],
|
|
53
|
+
recentBlockhash: Blockhash,
|
|
54
|
+
instructions: CompiledInstruction[],
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const PUBKEY_LENGTH = 32
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* List of instructions to be processed atomically
|
|
61
|
+
*/
|
|
62
|
+
export class Message {
|
|
63
|
+
header: MessageHeader
|
|
64
|
+
accountKeys: PublicKey[]
|
|
65
|
+
recentBlockhash: Blockhash
|
|
66
|
+
instructions: CompiledInstruction[]
|
|
67
|
+
|
|
68
|
+
constructor(args: MessageArgs) {
|
|
69
|
+
this.header = args.header
|
|
70
|
+
this.accountKeys = args.accountKeys.map((account) => new PublicKey(account))
|
|
71
|
+
this.recentBlockhash = args.recentBlockhash
|
|
72
|
+
this.instructions = args.instructions
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
isAccountWritable(index: number): boolean {
|
|
76
|
+
return (
|
|
77
|
+
index < this.header.numRequiredSignatures - this.header.numReadonlySignedAccounts ||
|
|
78
|
+
(index >= this.header.numRequiredSignatures &&
|
|
79
|
+
index < this.accountKeys.length - this.header.numReadonlyUnsignedAccounts)
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
findSignerIndex(signer: PublicKey): number {
|
|
84
|
+
const index = this.accountKeys.findIndex((accountKey) => {
|
|
85
|
+
return accountKey.equals(signer)
|
|
86
|
+
})
|
|
87
|
+
if (index < 0) {
|
|
88
|
+
throw new Error(`unknown signer: ${signer.toString()}`)
|
|
89
|
+
}
|
|
90
|
+
return index
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
serialize(): Buffer {
|
|
94
|
+
const numKeys = this.accountKeys.length
|
|
95
|
+
|
|
96
|
+
let keyCount = []
|
|
97
|
+
shortvec.encodeLength(keyCount, numKeys)
|
|
98
|
+
|
|
99
|
+
const instructions = this.instructions.map((instruction) => {
|
|
100
|
+
const { accounts, programIdIndex } = instruction
|
|
101
|
+
const data = bs58.decode(instruction.data)
|
|
102
|
+
|
|
103
|
+
let keyIndicesCount = []
|
|
104
|
+
shortvec.encodeLength(keyIndicesCount, accounts.length)
|
|
105
|
+
|
|
106
|
+
let dataCount = []
|
|
107
|
+
shortvec.encodeLength(dataCount, data.length)
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
programIdIndex,
|
|
111
|
+
keyIndicesCount: Buffer.from(keyIndicesCount),
|
|
112
|
+
keyIndices: Buffer.from(accounts),
|
|
113
|
+
dataLength: Buffer.from(dataCount),
|
|
114
|
+
data,
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
let instructionCount = []
|
|
119
|
+
shortvec.encodeLength(instructionCount, instructions.length)
|
|
120
|
+
let instructionBuffer = Buffer.alloc(PACKET_DATA_SIZE)
|
|
121
|
+
Buffer.from(instructionCount).copy(instructionBuffer)
|
|
122
|
+
let instructionBufferLength = instructionCount.length
|
|
123
|
+
|
|
124
|
+
instructions.forEach((instruction) => {
|
|
125
|
+
const instructionLayout = BufferLayout.struct([
|
|
126
|
+
BufferLayout.u8('programIdIndex'),
|
|
127
|
+
|
|
128
|
+
BufferLayout.blob(instruction.keyIndicesCount.length, 'keyIndicesCount'),
|
|
129
|
+
BufferLayout.seq(BufferLayout.u8('keyIndex'), instruction.keyIndices.length, 'keyIndices'),
|
|
130
|
+
BufferLayout.blob(instruction.dataLength.length, 'dataLength'),
|
|
131
|
+
BufferLayout.seq(BufferLayout.u8('userdatum'), instruction.data.length, 'data'),
|
|
132
|
+
])
|
|
133
|
+
const length = instructionLayout.encode(
|
|
134
|
+
instruction,
|
|
135
|
+
instructionBuffer,
|
|
136
|
+
instructionBufferLength
|
|
137
|
+
)
|
|
138
|
+
instructionBufferLength += length
|
|
139
|
+
})
|
|
140
|
+
instructionBuffer = instructionBuffer.slice(0, instructionBufferLength)
|
|
141
|
+
|
|
142
|
+
const signDataLayout = BufferLayout.struct([
|
|
143
|
+
BufferLayout.blob(1, 'numRequiredSignatures'),
|
|
144
|
+
BufferLayout.blob(1, 'numReadonlySignedAccounts'),
|
|
145
|
+
BufferLayout.blob(1, 'numReadonlyUnsignedAccounts'),
|
|
146
|
+
BufferLayout.blob(keyCount.length, 'keyCount'),
|
|
147
|
+
BufferLayout.seq(Layout.publicKey('key'), numKeys, 'keys'),
|
|
148
|
+
Layout.publicKey('recentBlockhash'),
|
|
149
|
+
])
|
|
150
|
+
|
|
151
|
+
const transaction = {
|
|
152
|
+
numRequiredSignatures: Buffer.from([this.header.numRequiredSignatures]),
|
|
153
|
+
numReadonlySignedAccounts: Buffer.from([this.header.numReadonlySignedAccounts]),
|
|
154
|
+
numReadonlyUnsignedAccounts: Buffer.from([this.header.numReadonlyUnsignedAccounts]),
|
|
155
|
+
keyCount: Buffer.from(keyCount),
|
|
156
|
+
keys: this.accountKeys.map((key) => key.toBuffer()),
|
|
157
|
+
recentBlockhash: bs58.decode(this.recentBlockhash),
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let signData = Buffer.alloc(2048)
|
|
161
|
+
const length = signDataLayout.encode(transaction, signData)
|
|
162
|
+
instructionBuffer.copy(signData, length)
|
|
163
|
+
return signData.slice(0, length + instructionBuffer.length)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Decode a compiled message into a Message object.
|
|
168
|
+
*/
|
|
169
|
+
static from(buffer: Buffer | Uint8Array | Array<number>): Message {
|
|
170
|
+
// Slice up wire data
|
|
171
|
+
let byteArray = [...buffer]
|
|
172
|
+
|
|
173
|
+
const numRequiredSignatures = byteArray.shift()
|
|
174
|
+
const numReadonlySignedAccounts = byteArray.shift()
|
|
175
|
+
const numReadonlyUnsignedAccounts = byteArray.shift()
|
|
176
|
+
|
|
177
|
+
const accountCount = shortvec.decodeLength(byteArray)
|
|
178
|
+
let accountKeys = []
|
|
179
|
+
for (let i = 0; i < accountCount; i++) {
|
|
180
|
+
const account = byteArray.slice(0, PUBKEY_LENGTH)
|
|
181
|
+
byteArray = byteArray.slice(PUBKEY_LENGTH)
|
|
182
|
+
accountKeys.push(bs58.encode(Buffer.from(account)))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const recentBlockhash = byteArray.slice(0, PUBKEY_LENGTH)
|
|
186
|
+
byteArray = byteArray.slice(PUBKEY_LENGTH)
|
|
187
|
+
|
|
188
|
+
const instructionCount = shortvec.decodeLength(byteArray)
|
|
189
|
+
let instructions = []
|
|
190
|
+
for (let i = 0; i < instructionCount; i++) {
|
|
191
|
+
let instruction = {}
|
|
192
|
+
instruction.programIdIndex = byteArray.shift()
|
|
193
|
+
const accountCount = shortvec.decodeLength(byteArray)
|
|
194
|
+
instruction.accounts = byteArray.slice(0, accountCount)
|
|
195
|
+
byteArray = byteArray.slice(accountCount)
|
|
196
|
+
const dataLength = shortvec.decodeLength(byteArray)
|
|
197
|
+
const data = byteArray.slice(0, dataLength)
|
|
198
|
+
instruction.data = bs58.encode(Buffer.from(data))
|
|
199
|
+
byteArray = byteArray.slice(dataLength)
|
|
200
|
+
instructions.push(instruction)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const messageArgs = {
|
|
204
|
+
header: {
|
|
205
|
+
numRequiredSignatures,
|
|
206
|
+
numReadonlySignedAccounts,
|
|
207
|
+
numReadonlyUnsignedAccounts,
|
|
208
|
+
},
|
|
209
|
+
recentBlockhash: bs58.encode(Buffer.from(recentBlockhash)),
|
|
210
|
+
accountKeys,
|
|
211
|
+
instructions,
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return new Message(messageArgs)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import * as BufferLayout from '@exodus/buffer-layout'
|
|
3
|
+
|
|
4
|
+
import { PublicKey } from './publickey'
|
|
5
|
+
import type { Blockhash } from './utils/blockhash'
|
|
6
|
+
import * as Layout from './utils/layout'
|
|
7
|
+
import type { FeeCalculator } from './utils/fee-calculator'
|
|
8
|
+
import { FeeCalculatorLayout } from './utils/fee-calculator'
|
|
9
|
+
import { toBuffer } from './utils/to-buffer'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* See https://github.com/solana-labs/solana/blob/0ea2843ec9cdc517572b8e62c959f41b55cf4453/sdk/src/nonce_state.rs#L29-L32
|
|
13
|
+
*
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
const NonceAccountLayout = BufferLayout.struct([
|
|
17
|
+
BufferLayout.u32('version'),
|
|
18
|
+
BufferLayout.u32('state'),
|
|
19
|
+
Layout.publicKey('authorizedPubkey'),
|
|
20
|
+
Layout.publicKey('nonce'),
|
|
21
|
+
BufferLayout.struct([FeeCalculatorLayout], 'feeCalculator'),
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
export const NONCE_ACCOUNT_LENGTH = NonceAccountLayout.span
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* NonceAccount class
|
|
28
|
+
*/
|
|
29
|
+
export class NonceAccount {
|
|
30
|
+
authorizedPubkey: PublicKey
|
|
31
|
+
nonce: Blockhash
|
|
32
|
+
feeCalculator: FeeCalculator
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Deserialize NonceAccount from the account data.
|
|
36
|
+
*
|
|
37
|
+
* @param buffer account data
|
|
38
|
+
* @return NonceAccount
|
|
39
|
+
*/
|
|
40
|
+
static fromAccountData(buffer: Buffer | Uint8Array | Array<number>): NonceAccount {
|
|
41
|
+
const nonceAccount = NonceAccountLayout.decode(toBuffer(buffer), 0)
|
|
42
|
+
nonceAccount.authorizedPubkey = new PublicKey(nonceAccount.authorizedPubkey)
|
|
43
|
+
nonceAccount.nonce = new PublicKey(nonceAccount.nonce).toString()
|
|
44
|
+
return nonceAccount
|
|
45
|
+
}
|
|
46
|
+
}
|