@exodus/solana-lib 1.2.13 → 1.2.14
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/lib/constants.js +19 -0
- package/lib/encode.js +67 -0
- package/lib/fee-data/index.js +15 -0
- package/lib/fee-data/solana.js +14 -0
- package/lib/helpers/spl-token.js +122 -0
- package/lib/helpers/tokenTransfer.js +78 -0
- package/lib/index.js +88 -0
- package/lib/keypair.js +38 -0
- package/lib/tokens.js +21 -0
- package/lib/transaction.js +338 -0
- package/lib/tx/create-and-sign-tx.js +15 -0
- package/{src → lib}/tx/create-unsigned-tx.js +18 -11
- package/lib/tx/index.js +53 -0
- package/lib/tx/parse-unsigned-tx.js +55 -0
- package/lib/tx/sign-unsigned-tx.js +90 -0
- package/lib/vendor/account.js +48 -0
- package/lib/vendor/index.js +113 -0
- package/lib/vendor/instruction.js +48 -0
- package/lib/vendor/message.js +167 -0
- package/lib/vendor/nonce-account.js +56 -0
- package/lib/vendor/publickey.js +211 -0
- package/lib/vendor/stake-program.js +476 -0
- package/lib/vendor/system-program.js +640 -0
- package/lib/vendor/sysvar.js +19 -0
- package/lib/vendor/transaction.js +594 -0
- package/lib/vendor/utils/blockhash.js +1 -0
- package/lib/vendor/utils/fee-calculator.js +25 -0
- package/lib/vendor/utils/layout.js +97 -0
- package/lib/vendor/utils/shortvec-encoding.js +41 -0
- package/lib/vendor/utils/to-buffer.js +18 -0
- package/package.json +4 -5
- package/src/constants.js +0 -12
- package/src/encode.js +0 -57
- package/src/fee-data/index.js +0 -1
- package/src/fee-data/solana.js +0 -9
- package/src/helpers/spl-token.js +0 -108
- package/src/helpers/tokenTransfer.js +0 -72
- package/src/index.js +0 -9
- package/src/keypair.js +0 -32
- package/src/tokens.js +0 -19
- package/src/transaction.js +0 -292
- package/src/tx/create-and-sign-tx.js +0 -8
- package/src/tx/index.js +0 -4
- package/src/tx/parse-unsigned-tx.js +0 -41
- package/src/tx/sign-unsigned-tx.js +0 -78
- package/src/vendor/account.js +0 -38
- package/src/vendor/index.js +0 -9
- package/src/vendor/instruction.js +0 -46
- package/src/vendor/message.js +0 -216
- package/src/vendor/nonce-account.js +0 -46
- package/src/vendor/publickey.js +0 -212
- package/src/vendor/stake-program.js +0 -527
- package/src/vendor/system-program.js +0 -782
- package/src/vendor/sysvar.js +0 -16
- package/src/vendor/transaction.js +0 -594
- package/src/vendor/utils/blockhash.js +0 -6
- package/src/vendor/utils/fee-calculator.js +0 -17
- package/src/vendor/utils/layout.js +0 -80
- package/src/vendor/utils/shortvec-encoding.js +0 -30
- package/src/vendor/utils/to-buffer.js +0 -9
package/src/transaction.js
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import assert from 'assert'
|
|
3
|
-
import bs58 from 'bs58'
|
|
4
|
-
import { get } from 'lodash'
|
|
5
|
-
|
|
6
|
-
import { getKeyPairFromPrivateKey } from './keypair'
|
|
7
|
-
import { findAssociatedTokenAddress, createStakeAddress } from './encode'
|
|
8
|
-
import {
|
|
9
|
-
createAssociatedTokenAccount,
|
|
10
|
-
createTokenTransferInstruction,
|
|
11
|
-
} from './helpers/tokenTransfer'
|
|
12
|
-
import {
|
|
13
|
-
PublicKey,
|
|
14
|
-
Account,
|
|
15
|
-
Transaction,
|
|
16
|
-
SystemProgram,
|
|
17
|
-
StakeProgram,
|
|
18
|
-
StakeInstruction,
|
|
19
|
-
Authorized,
|
|
20
|
-
Lockup,
|
|
21
|
-
} from './vendor'
|
|
22
|
-
import { SEED, STAKE_PROGRAM_ID } from './constants'
|
|
23
|
-
import TOKENS from './tokens'
|
|
24
|
-
|
|
25
|
-
class Tx {
|
|
26
|
-
constructor({
|
|
27
|
-
from,
|
|
28
|
-
to,
|
|
29
|
-
amount,
|
|
30
|
-
recentBlockhash,
|
|
31
|
-
// fee, // (Fee per Signature: 5000 lamports)
|
|
32
|
-
// Tokens related:
|
|
33
|
-
tokenName,
|
|
34
|
-
destinationAddressType,
|
|
35
|
-
isAssociatedTokenAccountActive, // true when recipient balance !== 0
|
|
36
|
-
fromTokenAddresses, // sender token addresses
|
|
37
|
-
} = {}) {
|
|
38
|
-
assert(from, 'from is required')
|
|
39
|
-
assert(to, 'to is required')
|
|
40
|
-
assert(amount, 'amount is required')
|
|
41
|
-
assert(typeof amount === 'number', 'amount must be a number')
|
|
42
|
-
assert(recentBlockhash, 'recentBlockhash is required')
|
|
43
|
-
if (tokenName) {
|
|
44
|
-
assert(
|
|
45
|
-
typeof destinationAddressType !== 'undefined',
|
|
46
|
-
'destinationAddressType is required when sending tokens'
|
|
47
|
-
)
|
|
48
|
-
assert(
|
|
49
|
-
typeof isAssociatedTokenAccountActive !== 'undefined',
|
|
50
|
-
'isAssociatedTokenAccountActive is required when sending tokens'
|
|
51
|
-
) // needed to create the recipient account
|
|
52
|
-
assert(Array.isArray(fromTokenAddresses), 'fromTokenAddresses Array is required')
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const token = TOKENS.find(({ tokenName: name }) => name === tokenName)
|
|
56
|
-
this.txObj = {
|
|
57
|
-
from,
|
|
58
|
-
to,
|
|
59
|
-
amount,
|
|
60
|
-
recentBlockhash,
|
|
61
|
-
token,
|
|
62
|
-
destinationAddressType,
|
|
63
|
-
isAssociatedTokenAccountActive,
|
|
64
|
-
fromTokenAddresses,
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (token) {
|
|
68
|
-
// TOKEN transfer tx
|
|
69
|
-
this.buildTokenTransaction(this.txObj)
|
|
70
|
-
} else {
|
|
71
|
-
// SOL tx
|
|
72
|
-
this.buildSOLtransaction(this.txObj)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
buildSOLtransaction({ from, to, amount, recentBlockhash }) {
|
|
77
|
-
const txInstruction = SystemProgram.transfer({
|
|
78
|
-
fromPubkey: new PublicKey(from),
|
|
79
|
-
toPubkey: new PublicKey(to),
|
|
80
|
-
lamports: amount,
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
this.transaction = new Transaction({
|
|
84
|
-
instructions: [txInstruction],
|
|
85
|
-
recentBlockhash,
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
buildTokenTransaction({
|
|
90
|
-
from,
|
|
91
|
-
to,
|
|
92
|
-
amount,
|
|
93
|
-
recentBlockhash,
|
|
94
|
-
token,
|
|
95
|
-
destinationAddressType,
|
|
96
|
-
isAssociatedTokenAccountActive,
|
|
97
|
-
fromTokenAddresses,
|
|
98
|
-
}) {
|
|
99
|
-
this.transaction = new Transaction({
|
|
100
|
-
recentBlockhash,
|
|
101
|
-
})
|
|
102
|
-
const isUnknown = destinationAddressType === null
|
|
103
|
-
const isSOLaddress = destinationAddressType === 'solana'
|
|
104
|
-
// crete account instruction
|
|
105
|
-
if (isUnknown) throw new Error('Destination SOL balance cannot be zero (address not active)') // cannot initialize without knowing the owner
|
|
106
|
-
if (isSOLaddress && !isAssociatedTokenAccountActive)
|
|
107
|
-
this.transaction.add(createAssociatedTokenAccount(from, token.mintAddress, to))
|
|
108
|
-
|
|
109
|
-
let amountLeft = amount
|
|
110
|
-
let amountToSend
|
|
111
|
-
for (let { ticker, tokenAccountAddress, balance } of fromTokenAddresses) {
|
|
112
|
-
// need to add more of this instruction until we reach the desired balance (amount) to send
|
|
113
|
-
assert(ticker === token.tokenSymbol, `Got unexpected ticker ${ticker}`)
|
|
114
|
-
if (amountLeft === 0) break
|
|
115
|
-
|
|
116
|
-
balance = Number(balance)
|
|
117
|
-
if (balance >= amountLeft) {
|
|
118
|
-
amountToSend = amountLeft
|
|
119
|
-
amountLeft = 0
|
|
120
|
-
} else {
|
|
121
|
-
amountToSend = balance // not enough balance
|
|
122
|
-
amountLeft -= amountToSend
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const dest = isSOLaddress ? findAssociatedTokenAddress(to, token.mintAddress) : to
|
|
126
|
-
// add transfer token instruction
|
|
127
|
-
this.transaction.add(
|
|
128
|
-
createTokenTransferInstruction(from, tokenAccountAddress, dest, amountToSend)
|
|
129
|
-
)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
assert(amountLeft === 0, `Not enough balance to send ${amount} ${token.tokenSymbol}`)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
static createStakeAccountTransaction({ address, amount, seed = SEED, pool, recentBlockhash }) {
|
|
136
|
-
const fromPubkey = new PublicKey(address)
|
|
137
|
-
const stakeAddress = createStakeAddress(address, seed)
|
|
138
|
-
const stakePublicKey = new PublicKey(stakeAddress)
|
|
139
|
-
const poolKey = new PublicKey(pool)
|
|
140
|
-
|
|
141
|
-
const authorized = new Authorized(fromPubkey, fromPubkey) // staker, withdrawer
|
|
142
|
-
const lockup = new Lockup(0, 0, fromPubkey) // no lockup
|
|
143
|
-
|
|
144
|
-
// create account instruction
|
|
145
|
-
const programTx = StakeProgram.createAccountWithSeed({
|
|
146
|
-
fromPubkey,
|
|
147
|
-
stakePubkey: stakePublicKey,
|
|
148
|
-
basePubkey: fromPubkey,
|
|
149
|
-
seed,
|
|
150
|
-
authorized,
|
|
151
|
-
lockup,
|
|
152
|
-
lamports: amount, // number
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
// delegate funds instruction
|
|
156
|
-
const delegateTx = StakeProgram.delegate({
|
|
157
|
-
stakePubkey: stakePublicKey,
|
|
158
|
-
authorizedPubkey: fromPubkey,
|
|
159
|
-
votePubkey: poolKey, // pool vote key
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
const transaction = new Transaction({ recentBlockhash }).add(programTx).add(delegateTx)
|
|
163
|
-
return transaction
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
static undelegate({ address, stakeAddresses, recentBlockhash }) {
|
|
167
|
-
// undelegate all stake addresses
|
|
168
|
-
assert(Array.isArray(stakeAddresses), 'stakeAddresses Array is required')
|
|
169
|
-
|
|
170
|
-
const fromPubkey = new PublicKey(address)
|
|
171
|
-
const transaction = new Transaction({ recentBlockhash })
|
|
172
|
-
|
|
173
|
-
stakeAddresses.forEach((stakeAddress) => {
|
|
174
|
-
const stakePublicKey = new PublicKey(stakeAddress)
|
|
175
|
-
const programTx = StakeProgram.deactivate({
|
|
176
|
-
stakePubkey: stakePublicKey,
|
|
177
|
-
authorizedPubkey: fromPubkey,
|
|
178
|
-
})
|
|
179
|
-
transaction.add(programTx)
|
|
180
|
-
})
|
|
181
|
-
|
|
182
|
-
return transaction
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
static withdraw({ address, stakeAddresses, amount, recentBlockhash }) {
|
|
186
|
-
const fromPubkey = new PublicKey(address)
|
|
187
|
-
const stakeAddress = Array.isArray(stakeAddresses) ? stakeAddresses[0] : stakeAddresses
|
|
188
|
-
const stakePublicKey = new PublicKey(stakeAddress)
|
|
189
|
-
|
|
190
|
-
const transaction = StakeProgram.withdraw({
|
|
191
|
-
stakePubkey: stakePublicKey,
|
|
192
|
-
authorizedPubkey: fromPubkey,
|
|
193
|
-
toPubkey: fromPubkey,
|
|
194
|
-
lamports: amount,
|
|
195
|
-
})
|
|
196
|
-
transaction.recentBlockhash = recentBlockhash
|
|
197
|
-
return transaction
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
static sign(tx, privateKey: Buffer | string) {
|
|
201
|
-
if (!privateKey) throw new Error('Please provide a secretKey')
|
|
202
|
-
const { secretKey } = getKeyPairFromPrivateKey(privateKey)
|
|
203
|
-
const signers = [new Account(secretKey)]
|
|
204
|
-
|
|
205
|
-
let transaction = tx
|
|
206
|
-
if (tx instanceof Tx) transaction = tx.transaction
|
|
207
|
-
|
|
208
|
-
transaction.sign(...signers)
|
|
209
|
-
if (!transaction.signature) {
|
|
210
|
-
throw new Error('!signature') // should never happen
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
serialize() {
|
|
215
|
-
const wireTransaction = this.transaction.serialize()
|
|
216
|
-
return wireTransaction.toString('base64')
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
static serialize(tx) {
|
|
220
|
-
let transaction = tx
|
|
221
|
-
if (tx instanceof Tx) transaction = tx.transaction
|
|
222
|
-
return transaction.serialize().toString('base64')
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
static decodeStakingTx(serialized: string) {
|
|
226
|
-
// as base64
|
|
227
|
-
const wireTransaction = Buffer.from(serialized, 'base64')
|
|
228
|
-
const tx = Transaction.from(wireTransaction) // Transaction instance
|
|
229
|
-
|
|
230
|
-
const txId = bs58.encode(tx.signature)
|
|
231
|
-
|
|
232
|
-
const stakingInstructions = tx.instructions.filter(
|
|
233
|
-
(ix) => ix.programId.toString() === STAKE_PROGRAM_ID.toString()
|
|
234
|
-
)
|
|
235
|
-
const isStakingTx = !!stakingInstructions.length
|
|
236
|
-
|
|
237
|
-
if (!isStakingTx) return null // normal transfer tx
|
|
238
|
-
|
|
239
|
-
const info = {
|
|
240
|
-
txId,
|
|
241
|
-
owner: tx.feePayer.toString(), // SOL sender
|
|
242
|
-
}
|
|
243
|
-
const txDetails = stakingInstructions.reduce((info, ix) => {
|
|
244
|
-
const type = StakeInstruction.decodeInstructionType(ix)
|
|
245
|
-
switch (type) {
|
|
246
|
-
case 'Delegate':
|
|
247
|
-
info.type = 'Delegate'
|
|
248
|
-
info.stakeAddress = get(ix, 'keys[0].pubkey', '').toString()
|
|
249
|
-
info.validator = get(ix, 'keys[1].pubkey', '').toString() // pool
|
|
250
|
-
return info
|
|
251
|
-
|
|
252
|
-
case 'Deactivate': // undelegate
|
|
253
|
-
info.type = 'Deactivate'
|
|
254
|
-
info.stakeAddress = get(ix, 'keys[0].pubkey', '').toString()
|
|
255
|
-
// TODO: could have multiple addresses undelegating
|
|
256
|
-
return info
|
|
257
|
-
|
|
258
|
-
case 'Withdraw':
|
|
259
|
-
info.type = 'Withdraw'
|
|
260
|
-
info.stakeAddress = get(ix, 'keys[0].pubkey', '').toString()
|
|
261
|
-
const { lamports, toPubkey } = StakeInstruction.decodeWithdraw(ix)
|
|
262
|
-
info.to = toPubkey.toString()
|
|
263
|
-
info.lamports = lamports.toString()
|
|
264
|
-
return info
|
|
265
|
-
|
|
266
|
-
default:
|
|
267
|
-
// skip unknown instruction type
|
|
268
|
-
return info
|
|
269
|
-
}
|
|
270
|
-
}, info)
|
|
271
|
-
|
|
272
|
-
return txDetails
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
getTxId() {
|
|
276
|
-
if (!this.transaction.signature) {
|
|
277
|
-
throw new Error('Cannot get txId, tx is not signed')
|
|
278
|
-
}
|
|
279
|
-
return bs58.encode(this.transaction.signature)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
static getTxId(tx) {
|
|
283
|
-
let transaction = tx
|
|
284
|
-
if (tx instanceof Tx) transaction = tx.transaction
|
|
285
|
-
if (!transaction.signature) {
|
|
286
|
-
throw new Error('Cannot get txId, tx is not signed')
|
|
287
|
-
}
|
|
288
|
-
return bs58.encode(transaction.signature)
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
export default Tx
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { createUnsignedTx } from './create-unsigned-tx'
|
|
2
|
-
import { signUnsignedTx } from './sign-unsigned-tx'
|
|
3
|
-
import type { ParsedTransaction, SignedTransaction } from '@exodus/models/lib/types'
|
|
4
|
-
|
|
5
|
-
export function createAndSignTx(input: ParsedTransaction, privateKey: Buffer): SignedTransaction {
|
|
6
|
-
const unsignedTx = createUnsignedTx(input)
|
|
7
|
-
return signUnsignedTx(unsignedTx, privateKey)
|
|
8
|
-
}
|
package/src/tx/index.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
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
|
-
}
|
package/src/vendor/account.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
}
|
package/src/vendor/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
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'
|
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
}
|