@exodus/solana-lib 1.0.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/README.md +8 -0
- package/package.json +26 -0
- package/src/encode.js +29 -0
- package/src/fee-data/index.js +1 -0
- package/src/fee-data/solana.js +9 -0
- package/src/helpers/account.js +38 -0
- package/src/helpers/blockhash.js +6 -0
- package/src/helpers/fee-calculator.js +17 -0
- package/src/helpers/instruction.js +46 -0
- package/src/helpers/layout.js +80 -0
- package/src/helpers/message.js +216 -0
- package/src/helpers/nonce-account.js +46 -0
- package/src/helpers/publickey.js +209 -0
- package/src/helpers/shortvec-encoding.js +30 -0
- package/src/helpers/system-program.js +782 -0
- package/src/helpers/sysvar.js +16 -0
- package/src/helpers/transaction.js +593 -0
- package/src/helpers/util/to-buffer.js +9 -0
- package/src/index.js +6 -0
- package/src/keypair.js +32 -0
- package/src/transaction.js +64 -0
- package/src/tx/create-and-sign-tx.js +8 -0
- package/src/tx/create-unsigned-tx.js +21 -0
- package/src/tx/index.js +4 -0
- package/src/tx/parse-unsigned-tx.js +16 -0
- package/src/tx/sign-unsigned-tx.js +25 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import assert from 'assert'
|
|
3
|
+
|
|
4
|
+
import { Account } from './helpers/account'
|
|
5
|
+
import { SystemProgram } from './helpers/system-program'
|
|
6
|
+
import { Transaction } from './helpers/transaction'
|
|
7
|
+
import { PublicKey } from './helpers/publickey'
|
|
8
|
+
import bs58 from 'bs58'
|
|
9
|
+
|
|
10
|
+
class Tx {
|
|
11
|
+
constructor({
|
|
12
|
+
from,
|
|
13
|
+
to,
|
|
14
|
+
amount,
|
|
15
|
+
// fee, // (Fee per Signature: 5000 lamports)
|
|
16
|
+
recentBlockhash,
|
|
17
|
+
} = {}) {
|
|
18
|
+
assert(from, 'from is required')
|
|
19
|
+
assert(to, 'to is required')
|
|
20
|
+
assert(amount, 'amount is required')
|
|
21
|
+
assert(typeof amount === 'number', 'amount must be a number')
|
|
22
|
+
assert(recentBlockhash, 'recentBlockhash is required')
|
|
23
|
+
this.txObj = {
|
|
24
|
+
from,
|
|
25
|
+
to,
|
|
26
|
+
amount,
|
|
27
|
+
recentBlockhash,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const txInstruction = SystemProgram.transfer({
|
|
31
|
+
fromPubkey: new PublicKey(from),
|
|
32
|
+
toPubkey: new PublicKey(to),
|
|
33
|
+
lamports: amount,
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
this.transaction = new Transaction({
|
|
37
|
+
instructions: [txInstruction],
|
|
38
|
+
recentBlockhash,
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static sign(tx: Tx, privateKey: Buffer | string) {
|
|
43
|
+
const signers = [new Account(Buffer.from(privateKey, 'hex'))]
|
|
44
|
+
|
|
45
|
+
tx.transaction.sign(...signers)
|
|
46
|
+
if (!tx.transaction.signature) {
|
|
47
|
+
throw new Error('!signature') // should never happen
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
serialize() {
|
|
52
|
+
const wireTransaction = this.transaction.serialize()
|
|
53
|
+
return bs58.encode(wireTransaction)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getTxId() {
|
|
57
|
+
if (!this.transaction.signature) {
|
|
58
|
+
throw new Error('Cannot get txId, tx is not signed')
|
|
59
|
+
}
|
|
60
|
+
return bs58.encode(this.transaction.signature)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default Tx
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { UnsignedTransaction, ParsedTransaction } from '@exodus/models/lib/types'
|
|
2
|
+
|
|
3
|
+
export function createUnsignedTx({
|
|
4
|
+
asset,
|
|
5
|
+
from,
|
|
6
|
+
to,
|
|
7
|
+
amount,
|
|
8
|
+
recentBlockhash,
|
|
9
|
+
}: ParsedTransaction): UnsignedTransaction {
|
|
10
|
+
return {
|
|
11
|
+
txData: {
|
|
12
|
+
from,
|
|
13
|
+
to,
|
|
14
|
+
amount: amount.toBase().toNumber(),
|
|
15
|
+
recentBlockhash,
|
|
16
|
+
},
|
|
17
|
+
txMeta: {
|
|
18
|
+
assetName: asset.name,
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/tx/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
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 { from, to, recentBlockhash, ...txData } = unsignedTx.txData
|
|
7
|
+
|
|
8
|
+
const amount = asset.currency.baseUnit(txData.amount)
|
|
9
|
+
return {
|
|
10
|
+
asset,
|
|
11
|
+
from: [from],
|
|
12
|
+
to,
|
|
13
|
+
amount,
|
|
14
|
+
recentBlockhash,
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 { from, to, amount, recentBlockhash } = unsignedTx.txData
|
|
10
|
+
|
|
11
|
+
const asset = assets.solana
|
|
12
|
+
const tx = new Transaction({
|
|
13
|
+
from,
|
|
14
|
+
to,
|
|
15
|
+
amount: asset.currency.baseUnit(amount).toNumber(),
|
|
16
|
+
recentBlockhash,
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
// sign plain tx
|
|
20
|
+
Transaction.sign(tx, privateKey)
|
|
21
|
+
const rawTx = tx.serialize()
|
|
22
|
+
const txId = tx.getTxId()
|
|
23
|
+
|
|
24
|
+
return { txId, rawTx }
|
|
25
|
+
}
|