@bitpay-labs/crypto-wallet-core 11.5.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 +13 -0
- package/package.json +58 -0
- package/src/constants/address.ts +7 -0
- package/src/constants/algorithms.ts +9 -0
- package/src/constants/chains.ts +64 -0
- package/src/constants/feeMinimums.ts +20 -0
- package/src/constants/index.ts +37 -0
- package/src/constants/tokens.ts +224 -0
- package/src/constants/units.ts +289 -0
- package/src/derivation/arb/index.ts +3 -0
- package/src/derivation/base/index.ts +3 -0
- package/src/derivation/bch/index.ts +11 -0
- package/src/derivation/btc/index.ts +39 -0
- package/src/derivation/doge/index.ts +6 -0
- package/src/derivation/eth/index.ts +58 -0
- package/src/derivation/index.ts +124 -0
- package/src/derivation/ltc/index.ts +6 -0
- package/src/derivation/matic/index.ts +3 -0
- package/src/derivation/op/index.ts +3 -0
- package/src/derivation/paths.ts +42 -0
- package/src/derivation/sol/index.ts +57 -0
- package/src/derivation/xrp/index.ts +38 -0
- package/src/index.ts +48 -0
- package/src/message/btc.ts +28 -0
- package/src/message/eth.ts +47 -0
- package/src/message/index.ts +70 -0
- package/src/message/sol.ts +3 -0
- package/src/message/xrp.ts +4 -0
- package/src/transactions/arb/index.ts +14 -0
- package/src/transactions/base/index.ts +13 -0
- package/src/transactions/bch/index.ts +25 -0
- package/src/transactions/btc/index.ts +227 -0
- package/src/transactions/doge/index.ts +31 -0
- package/src/transactions/erc20/abi.ts +418 -0
- package/src/transactions/erc20/index.ts +57 -0
- package/src/transactions/eth/index.ts +183 -0
- package/src/transactions/eth-multisig/abi.ts +724 -0
- package/src/transactions/eth-multisig/index.ts +172 -0
- package/src/transactions/index.ts +83 -0
- package/src/transactions/ltc/index.ts +31 -0
- package/src/transactions/matic/index.ts +14 -0
- package/src/transactions/matic-multisig/index.ts +172 -0
- package/src/transactions/op/index.ts +14 -0
- package/src/transactions/sol/index.ts +292 -0
- package/src/transactions/spl/index.ts +192 -0
- package/src/transactions/xrp/index.ts +129 -0
- package/src/types/derivation.ts +17 -0
- package/src/types/index.ts +3 -0
- package/src/types/message.ts +15 -0
- package/src/types/utils.ts +1 -0
- package/src/types/validation.ts +4 -0
- package/src/utils/bigint.ts +238 -0
- package/src/utils/index.ts +94 -0
- package/src/validation/arb/index.ts +8 -0
- package/src/validation/base/index.ts +8 -0
- package/src/validation/bch/index.ts +17 -0
- package/src/validation/btc/index.ts +17 -0
- package/src/validation/doge/index.ts +16 -0
- package/src/validation/eth/index.ts +29 -0
- package/src/validation/index.ts +43 -0
- package/src/validation/ltc/index.ts +16 -0
- package/src/validation/matic/index.ts +8 -0
- package/src/validation/op/index.ts +8 -0
- package/src/validation/sol/index.ts +34 -0
- package/src/validation/xrp/index.ts +50 -0
- package/test/address.test.ts +178 -0
- package/test/bigint.test.ts +373 -0
- package/test/transactions.test.ts +1777 -0
- package/test/utils.test.ts +121 -0
- package/test/validation.test.ts +261 -0
- package/ts_build/src/constants/address.d.ts +8 -0
- package/ts_build/src/constants/address.d.ts.map +1 -0
- package/ts_build/src/constants/address.js +11 -0
- package/ts_build/src/constants/address.js.map +1 -0
- package/ts_build/src/constants/algorithms.d.ts +9 -0
- package/ts_build/src/constants/algorithms.d.ts.map +1 -0
- package/ts_build/src/constants/algorithms.js +12 -0
- package/ts_build/src/constants/algorithms.js.map +1 -0
- package/ts_build/src/constants/chains.d.ts +42 -0
- package/ts_build/src/constants/chains.d.ts.map +1 -0
- package/ts_build/src/constants/chains.js +54 -0
- package/ts_build/src/constants/chains.js.map +1 -0
- package/ts_build/src/constants/feeMinimums.d.ts +21 -0
- package/ts_build/src/constants/feeMinimums.d.ts.map +1 -0
- package/ts_build/src/constants/feeMinimums.js +24 -0
- package/ts_build/src/constants/feeMinimums.js.map +1 -0
- package/ts_build/src/constants/index.d.ts +569 -0
- package/ts_build/src/constants/index.d.ts.map +1 -0
- package/ts_build/src/constants/index.js +31 -0
- package/ts_build/src/constants/index.js.map +1 -0
- package/ts_build/src/constants/tokens.d.ts +423 -0
- package/ts_build/src/constants/tokens.d.ts.map +1 -0
- package/ts_build/src/constants/tokens.js +222 -0
- package/ts_build/src/constants/tokens.js.map +1 -0
- package/ts_build/src/constants/units.d.ts +289 -0
- package/ts_build/src/constants/units.d.ts.map +1 -0
- package/ts_build/src/constants/units.js +292 -0
- package/ts_build/src/constants/units.js.map +1 -0
- package/ts_build/src/derivation/arb/index.d.ts +4 -0
- package/ts_build/src/derivation/arb/index.d.ts.map +1 -0
- package/ts_build/src/derivation/arb/index.js +8 -0
- package/ts_build/src/derivation/arb/index.js.map +1 -0
- package/ts_build/src/derivation/base/index.d.ts +4 -0
- package/ts_build/src/derivation/base/index.d.ts.map +1 -0
- package/ts_build/src/derivation/base/index.js +8 -0
- package/ts_build/src/derivation/base/index.js.map +1 -0
- package/ts_build/src/derivation/bch/index.d.ts +6 -0
- package/ts_build/src/derivation/bch/index.d.ts.map +1 -0
- package/ts_build/src/derivation/bch/index.js +17 -0
- package/ts_build/src/derivation/bch/index.js.map +1 -0
- package/ts_build/src/derivation/btc/index.d.ts +23 -0
- package/ts_build/src/derivation/btc/index.d.ts.map +1 -0
- package/ts_build/src/derivation/btc/index.js +40 -0
- package/ts_build/src/derivation/btc/index.js.map +1 -0
- package/ts_build/src/derivation/doge/index.d.ts +5 -0
- package/ts_build/src/derivation/doge/index.d.ts.map +1 -0
- package/ts_build/src/derivation/doge/index.js +13 -0
- package/ts_build/src/derivation/doge/index.js.map +1 -0
- package/ts_build/src/derivation/eth/index.d.ts +21 -0
- package/ts_build/src/derivation/eth/index.d.ts.map +1 -0
- package/ts_build/src/derivation/eth/index.js +58 -0
- package/ts_build/src/derivation/eth/index.js.map +1 -0
- package/ts_build/src/derivation/index.d.ts +12 -0
- package/ts_build/src/derivation/index.d.ts.map +1 -0
- package/ts_build/src/derivation/index.js +68 -0
- package/ts_build/src/derivation/index.js.map +1 -0
- package/ts_build/src/derivation/ltc/index.d.ts +5 -0
- package/ts_build/src/derivation/ltc/index.d.ts.map +1 -0
- package/ts_build/src/derivation/ltc/index.js +13 -0
- package/ts_build/src/derivation/ltc/index.js.map +1 -0
- package/ts_build/src/derivation/matic/index.d.ts +4 -0
- package/ts_build/src/derivation/matic/index.d.ts.map +1 -0
- package/ts_build/src/derivation/matic/index.js +8 -0
- package/ts_build/src/derivation/matic/index.js.map +1 -0
- package/ts_build/src/derivation/op/index.d.ts +4 -0
- package/ts_build/src/derivation/op/index.d.ts.map +1 -0
- package/ts_build/src/derivation/op/index.js +8 -0
- package/ts_build/src/derivation/op/index.js.map +1 -0
- package/ts_build/src/derivation/paths.d.ts +43 -0
- package/ts_build/src/derivation/paths.d.ts.map +1 -0
- package/ts_build/src/derivation/paths.js +46 -0
- package/ts_build/src/derivation/paths.js.map +1 -0
- package/ts_build/src/derivation/sol/index.d.ts +17 -0
- package/ts_build/src/derivation/sol/index.d.ts.map +1 -0
- package/ts_build/src/derivation/sol/index.js +85 -0
- package/ts_build/src/derivation/sol/index.js.map +1 -0
- package/ts_build/src/derivation/xrp/index.d.ts +19 -0
- package/ts_build/src/derivation/xrp/index.d.ts.map +1 -0
- package/ts_build/src/derivation/xrp/index.js +40 -0
- package/ts_build/src/derivation/xrp/index.js.map +1 -0
- package/ts_build/src/index.d.ts +30 -0
- package/ts_build/src/index.d.ts.map +1 -0
- package/ts_build/src/index.js +81 -0
- package/ts_build/src/index.js.map +1 -0
- package/ts_build/src/message/btc.d.ts +19 -0
- package/ts_build/src/message/btc.d.ts.map +1 -0
- package/ts_build/src/message/btc.js +32 -0
- package/ts_build/src/message/btc.js.map +1 -0
- package/ts_build/src/message/eth.d.ts +19 -0
- package/ts_build/src/message/eth.d.ts.map +1 -0
- package/ts_build/src/message/eth.js +39 -0
- package/ts_build/src/message/eth.js.map +1 -0
- package/ts_build/src/message/index.d.ts +25 -0
- package/ts_build/src/message/index.d.ts.map +1 -0
- package/ts_build/src/message/index.js +50 -0
- package/ts_build/src/message/index.js.map +1 -0
- package/ts_build/src/message/sol.d.ts +4 -0
- package/ts_build/src/message/sol.d.ts.map +1 -0
- package/ts_build/src/message/sol.js +9 -0
- package/ts_build/src/message/sol.js.map +1 -0
- package/ts_build/src/message/xrp.d.ts +4 -0
- package/ts_build/src/message/xrp.d.ts.map +1 -0
- package/ts_build/src/message/xrp.js +9 -0
- package/ts_build/src/message/xrp.js.map +1 -0
- package/ts_build/src/transactions/arb/index.d.ts +9 -0
- package/ts_build/src/transactions/arb/index.d.ts.map +1 -0
- package/ts_build/src/transactions/arb/index.js +18 -0
- package/ts_build/src/transactions/arb/index.js.map +1 -0
- package/ts_build/src/transactions/base/index.d.ts +8 -0
- package/ts_build/src/transactions/base/index.d.ts.map +1 -0
- package/ts_build/src/transactions/base/index.js +17 -0
- package/ts_build/src/transactions/base/index.js.map +1 -0
- package/ts_build/src/transactions/bch/index.d.ts +12 -0
- package/ts_build/src/transactions/bch/index.d.ts.map +1 -0
- package/ts_build/src/transactions/bch/index.js +32 -0
- package/ts_build/src/transactions/bch/index.js.map +1 -0
- package/ts_build/src/transactions/btc/index.d.ts +84 -0
- package/ts_build/src/transactions/btc/index.d.ts.map +1 -0
- package/ts_build/src/transactions/btc/index.js +184 -0
- package/ts_build/src/transactions/btc/index.js.map +1 -0
- package/ts_build/src/transactions/doge/index.d.ts +12 -0
- package/ts_build/src/transactions/doge/index.d.ts.map +1 -0
- package/ts_build/src/transactions/doge/index.js +38 -0
- package/ts_build/src/transactions/doge/index.js.map +1 -0
- package/ts_build/src/transactions/erc20/abi.d.ts +78 -0
- package/ts_build/src/transactions/erc20/abi.d.ts.map +1 -0
- package/ts_build/src/transactions/erc20/abi.js +421 -0
- package/ts_build/src/transactions/erc20/abi.js.map +1 -0
- package/ts_build/src/transactions/erc20/index.d.ts +29 -0
- package/ts_build/src/transactions/erc20/index.d.ts.map +1 -0
- package/ts_build/src/transactions/erc20/index.js +46 -0
- package/ts_build/src/transactions/erc20/index.js.map +1 -0
- package/ts_build/src/transactions/eth/index.d.ts +53 -0
- package/ts_build/src/transactions/eth/index.d.ts.map +1 -0
- package/ts_build/src/transactions/eth/index.js +156 -0
- package/ts_build/src/transactions/eth/index.js.map +1 -0
- package/ts_build/src/transactions/eth-multisig/abi.d.ts +51 -0
- package/ts_build/src/transactions/eth-multisig/abi.d.ts.map +1 -0
- package/ts_build/src/transactions/eth-multisig/abi.js +728 -0
- package/ts_build/src/transactions/eth-multisig/abi.js.map +1 -0
- package/ts_build/src/transactions/eth-multisig/index.d.ts +65 -0
- package/ts_build/src/transactions/eth-multisig/index.d.ts.map +1 -0
- package/ts_build/src/transactions/eth-multisig/index.js +143 -0
- package/ts_build/src/transactions/eth-multisig/index.js.map +1 -0
- package/ts_build/src/transactions/index.d.ts +15 -0
- package/ts_build/src/transactions/index.d.ts.map +1 -0
- package/ts_build/src/transactions/index.js +69 -0
- package/ts_build/src/transactions/index.js.map +1 -0
- package/ts_build/src/transactions/ltc/index.d.ts +12 -0
- package/ts_build/src/transactions/ltc/index.d.ts.map +1 -0
- package/ts_build/src/transactions/ltc/index.js +38 -0
- package/ts_build/src/transactions/ltc/index.js.map +1 -0
- package/ts_build/src/transactions/matic/index.d.ts +9 -0
- package/ts_build/src/transactions/matic/index.d.ts.map +1 -0
- package/ts_build/src/transactions/matic/index.js +18 -0
- package/ts_build/src/transactions/matic/index.js.map +1 -0
- package/ts_build/src/transactions/matic-multisig/index.d.ts +65 -0
- package/ts_build/src/transactions/matic-multisig/index.d.ts.map +1 -0
- package/ts_build/src/transactions/matic-multisig/index.js +143 -0
- package/ts_build/src/transactions/matic-multisig/index.js.map +1 -0
- package/ts_build/src/transactions/op/index.d.ts +9 -0
- package/ts_build/src/transactions/op/index.d.ts.map +1 -0
- package/ts_build/src/transactions/op/index.js +18 -0
- package/ts_build/src/transactions/op/index.js.map +1 -0
- package/ts_build/src/transactions/sol/index.d.ts +56 -0
- package/ts_build/src/transactions/sol/index.d.ts.map +1 -0
- package/ts_build/src/transactions/sol/index.js +261 -0
- package/ts_build/src/transactions/sol/index.js.map +1 -0
- package/ts_build/src/transactions/spl/index.d.ts +57 -0
- package/ts_build/src/transactions/spl/index.d.ts.map +1 -0
- package/ts_build/src/transactions/spl/index.js +129 -0
- package/ts_build/src/transactions/spl/index.js.map +1 -0
- package/ts_build/src/transactions/xrp/index.d.ts +50 -0
- package/ts_build/src/transactions/xrp/index.d.ts.map +1 -0
- package/ts_build/src/transactions/xrp/index.js +146 -0
- package/ts_build/src/transactions/xrp/index.js.map +1 -0
- package/ts_build/src/types/derivation.d.ts +13 -0
- package/ts_build/src/types/derivation.d.ts.map +1 -0
- package/ts_build/src/types/derivation.js +3 -0
- package/ts_build/src/types/derivation.js.map +1 -0
- package/ts_build/src/types/index.d.ts +4 -0
- package/ts_build/src/types/index.d.ts.map +1 -0
- package/ts_build/src/types/index.js +40 -0
- package/ts_build/src/types/index.js.map +1 -0
- package/ts_build/src/types/message.d.ts +26 -0
- package/ts_build/src/types/message.d.ts.map +1 -0
- package/ts_build/src/types/message.js +3 -0
- package/ts_build/src/types/message.js.map +1 -0
- package/ts_build/src/types/utils.d.ts +2 -0
- package/ts_build/src/types/utils.d.ts.map +1 -0
- package/ts_build/src/types/utils.js +3 -0
- package/ts_build/src/types/utils.js.map +1 -0
- package/ts_build/src/types/validation.d.ts +5 -0
- package/ts_build/src/types/validation.d.ts.map +1 -0
- package/ts_build/src/types/validation.js +3 -0
- package/ts_build/src/types/validation.js.map +1 -0
- package/ts_build/src/utils/bigint.d.ts +14 -0
- package/ts_build/src/utils/bigint.d.ts.map +1 -0
- package/ts_build/src/utils/bigint.js +165 -0
- package/ts_build/src/utils/bigint.js.map +1 -0
- package/ts_build/src/utils/index.d.ts +12 -0
- package/ts_build/src/utils/index.d.ts.map +1 -0
- package/ts_build/src/utils/index.js +118 -0
- package/ts_build/src/utils/index.js.map +1 -0
- package/ts_build/src/validation/arb/index.d.ts +5 -0
- package/ts_build/src/validation/arb/index.d.ts.map +1 -0
- package/ts_build/src/validation/arb/index.js +12 -0
- package/ts_build/src/validation/arb/index.js.map +1 -0
- package/ts_build/src/validation/base/index.d.ts +5 -0
- package/ts_build/src/validation/base/index.d.ts.map +1 -0
- package/ts_build/src/validation/base/index.js +12 -0
- package/ts_build/src/validation/base/index.js.map +1 -0
- package/ts_build/src/validation/bch/index.d.ts +6 -0
- package/ts_build/src/validation/bch/index.d.ts.map +1 -0
- package/ts_build/src/validation/bch/index.js +19 -0
- package/ts_build/src/validation/bch/index.js.map +1 -0
- package/ts_build/src/validation/btc/index.d.ts +6 -0
- package/ts_build/src/validation/btc/index.d.ts.map +1 -0
- package/ts_build/src/validation/btc/index.js +19 -0
- package/ts_build/src/validation/btc/index.js.map +1 -0
- package/ts_build/src/validation/doge/index.d.ts +6 -0
- package/ts_build/src/validation/doge/index.d.ts.map +1 -0
- package/ts_build/src/validation/doge/index.js +19 -0
- package/ts_build/src/validation/doge/index.js.map +1 -0
- package/ts_build/src/validation/eth/index.d.ts +9 -0
- package/ts_build/src/validation/eth/index.d.ts.map +1 -0
- package/ts_build/src/validation/eth/index.js +28 -0
- package/ts_build/src/validation/eth/index.js.map +1 -0
- package/ts_build/src/validation/index.d.ts +9 -0
- package/ts_build/src/validation/index.d.ts.map +1 -0
- package/ts_build/src/validation/index.js +42 -0
- package/ts_build/src/validation/index.js.map +1 -0
- package/ts_build/src/validation/ltc/index.d.ts +6 -0
- package/ts_build/src/validation/ltc/index.d.ts.map +1 -0
- package/ts_build/src/validation/ltc/index.js +19 -0
- package/ts_build/src/validation/ltc/index.js.map +1 -0
- package/ts_build/src/validation/matic/index.d.ts +5 -0
- package/ts_build/src/validation/matic/index.d.ts.map +1 -0
- package/ts_build/src/validation/matic/index.js +12 -0
- package/ts_build/src/validation/matic/index.js.map +1 -0
- package/ts_build/src/validation/op/index.d.ts +5 -0
- package/ts_build/src/validation/op/index.d.ts.map +1 -0
- package/ts_build/src/validation/op/index.js +12 -0
- package/ts_build/src/validation/op/index.js.map +1 -0
- package/ts_build/src/validation/sol/index.d.ts +9 -0
- package/ts_build/src/validation/sol/index.d.ts.map +1 -0
- package/ts_build/src/validation/sol/index.js +34 -0
- package/ts_build/src/validation/sol/index.js.map +1 -0
- package/ts_build/src/validation/xrp/index.d.ts +7 -0
- package/ts_build/src/validation/xrp/index.d.ts.map +1 -0
- package/ts_build/src/validation/xrp/index.js +50 -0
- package/ts_build/src/validation/xrp/index.js.map +1 -0
- package/ts_build/test/address.test.d.ts +2 -0
- package/ts_build/test/address.test.d.ts.map +1 -0
- package/ts_build/test/address.test.js +139 -0
- package/ts_build/test/address.test.js.map +1 -0
- package/ts_build/test/bigint.test.d.ts +2 -0
- package/ts_build/test/bigint.test.d.ts.map +1 -0
- package/ts_build/test/bigint.test.js +298 -0
- package/ts_build/test/bigint.test.js.map +1 -0
- package/ts_build/test/transactions.test.d.ts +2 -0
- package/ts_build/test/transactions.test.d.ts.map +1 -0
- package/ts_build/test/transactions.test.js +1549 -0
- package/ts_build/test/transactions.test.js.map +1 -0
- package/ts_build/test/utils.test.d.ts +2 -0
- package/ts_build/test/utils.test.d.ts.map +1 -0
- package/ts_build/test/utils.test.js +137 -0
- package/ts_build/test/utils.test.js.map +1 -0
- package/ts_build/test/validation.test.d.ts +2 -0
- package/ts_build/test/validation.test.d.ts.map +1 -0
- package/ts_build/test/validation.test.js +210 -0
- package/ts_build/test/validation.test.js.map +1 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import * as SolKit from '@solana/kit';
|
|
2
|
+
import * as SolComputeBudget from '@solana-program/compute-budget';
|
|
3
|
+
import * as SolComputeMemo from '@solana-program/memo';
|
|
4
|
+
import * as SolSystem from '@solana-program/system';
|
|
5
|
+
import * as SolToken from '@solana-program/token';
|
|
6
|
+
import type { Key } from '../../types/derivation';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export class SOLTxProvider {
|
|
10
|
+
|
|
11
|
+
MAX_TRANSFERS = 12;
|
|
12
|
+
MINIMUM_PRIORITY_FEE = 1000;
|
|
13
|
+
|
|
14
|
+
create(params: {
|
|
15
|
+
recipients: Array<{ address: string; amount: string; addressKeyPair?: SolKit.KeyPairSigner }>;
|
|
16
|
+
from: string;
|
|
17
|
+
fee?: number;
|
|
18
|
+
feeRate: number;
|
|
19
|
+
txType?: 'legacy' | '0'; // legacy, version 0
|
|
20
|
+
category?: 'transfer' | 'createAccount'; // transfer, create account
|
|
21
|
+
nonce?: string; // nonce is represented as a transaction id
|
|
22
|
+
nonceAddress?: string;
|
|
23
|
+
blockHash?: string;
|
|
24
|
+
blockHeight?: number;
|
|
25
|
+
priorityFee?: number;
|
|
26
|
+
computeUnits?: number;
|
|
27
|
+
memo?: string;
|
|
28
|
+
txInstructions?: Array<SolKit.BaseTransactionMessage['instructions'][number]>;
|
|
29
|
+
// account creation fields
|
|
30
|
+
fromKeyPair?: any;
|
|
31
|
+
space?: number; // amount of space to reserve a new account in bytes
|
|
32
|
+
mint?: string; // mint address for createATA
|
|
33
|
+
ataAddress?: any; // ATA address for createATA
|
|
34
|
+
}) {
|
|
35
|
+
const { recipients, from, nonce, nonceAddress, category, space, blockHash, blockHeight, priorityFee, txInstructions, computeUnits, fromKeyPair, memo } = params;
|
|
36
|
+
const fromAddress = SolKit.address(from);
|
|
37
|
+
const txType: SolKit.TransactionVersion = ['0', 0].includes(params?.txType) ? 0 : 'legacy';
|
|
38
|
+
let lifetimeConstrainedTx;
|
|
39
|
+
switch (category?.toLowerCase()) {
|
|
40
|
+
case 'transfer':
|
|
41
|
+
default:
|
|
42
|
+
if (recipients.length > this.MAX_TRANSFERS) {
|
|
43
|
+
throw new Error('Too many recipients');
|
|
44
|
+
}
|
|
45
|
+
const transactionMessage = SolKit.pipe(
|
|
46
|
+
SolKit.createTransactionMessage({ version: txType }),
|
|
47
|
+
tx => SolKit.setTransactionMessageFeePayer(fromAddress, tx),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
if (nonce) {
|
|
51
|
+
const nonceAccountAddress = SolKit.address(nonceAddress);
|
|
52
|
+
const nonceAuthorityAddress = fromAddress;
|
|
53
|
+
lifetimeConstrainedTx = SolKit.setTransactionMessageLifetimeUsingDurableNonce({
|
|
54
|
+
nonce: nonce as SolKit.Nonce,
|
|
55
|
+
nonceAccountAddress,
|
|
56
|
+
nonceAuthorityAddress
|
|
57
|
+
}, transactionMessage);
|
|
58
|
+
} else {
|
|
59
|
+
const recentBlockhash = {
|
|
60
|
+
blockhash: blockHash as SolKit.Blockhash,
|
|
61
|
+
lastValidBlockHeight: BigInt(blockHeight)
|
|
62
|
+
};
|
|
63
|
+
lifetimeConstrainedTx = SolKit.setTransactionMessageLifetimeUsingBlockhash(
|
|
64
|
+
recentBlockhash,
|
|
65
|
+
transactionMessage,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
const transferInstructions = txInstructions || [];
|
|
69
|
+
if (!transferInstructions.length) {
|
|
70
|
+
for (const recipient of recipients) {
|
|
71
|
+
const { address: recipientAddress, amount: recipientAmount } = recipient;
|
|
72
|
+
transferInstructions.push(SolSystem.getTransferSolInstruction({
|
|
73
|
+
amount: BigInt(recipientAmount),
|
|
74
|
+
destination: SolKit.address(recipientAddress),
|
|
75
|
+
source: {
|
|
76
|
+
address: fromAddress,
|
|
77
|
+
signTransactions: async () => []
|
|
78
|
+
} as SolKit.TransactionPartialSigner
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (priorityFee) {
|
|
83
|
+
const maxPriorityFee = Math.max(this.MINIMUM_PRIORITY_FEE, priorityFee);
|
|
84
|
+
transferInstructions.push(SolComputeBudget.getSetComputeUnitPriceInstruction({ microLamports: maxPriorityFee }));
|
|
85
|
+
}
|
|
86
|
+
if (computeUnits) {
|
|
87
|
+
transferInstructions.push(SolComputeBudget.getSetComputeUnitLimitInstruction({ units: computeUnits }));
|
|
88
|
+
}
|
|
89
|
+
if (memo) {
|
|
90
|
+
const memoInstruction = SolComputeMemo.getAddMemoInstruction({
|
|
91
|
+
memo
|
|
92
|
+
});
|
|
93
|
+
transferInstructions.push(memoInstruction);
|
|
94
|
+
}
|
|
95
|
+
const transferTxMessage = SolKit.appendTransactionMessageInstructions(transferInstructions, lifetimeConstrainedTx);
|
|
96
|
+
const compiledTx = SolKit.compileTransaction(transferTxMessage);
|
|
97
|
+
return SolKit.getBase64EncodedWireTransaction(compiledTx);
|
|
98
|
+
case 'createaccount':
|
|
99
|
+
const { amount, addressKeyPair } = recipients[0];
|
|
100
|
+
const _space = space || 200;
|
|
101
|
+
const _amount = Number(amount);
|
|
102
|
+
|
|
103
|
+
if (!addressKeyPair) {
|
|
104
|
+
throw new Error('New address keypair is required to create an account.');
|
|
105
|
+
}
|
|
106
|
+
const recentBlockhash = {
|
|
107
|
+
blockhash: blockHash as SolKit.Blockhash,
|
|
108
|
+
lastValidBlockHeight: BigInt(blockHeight)
|
|
109
|
+
};
|
|
110
|
+
const createAccountInstructions = [];
|
|
111
|
+
createAccountInstructions.push(SolSystem.getCreateAccountInstruction({
|
|
112
|
+
payer: fromKeyPair,
|
|
113
|
+
newAccount: addressKeyPair,
|
|
114
|
+
lamports: _amount,
|
|
115
|
+
space: _space,
|
|
116
|
+
programAddress: SolSystem.SYSTEM_PROGRAM_ADDRESS
|
|
117
|
+
}));
|
|
118
|
+
const txMessage = SolKit.pipe(
|
|
119
|
+
SolKit.createTransactionMessage({ version: txType }),
|
|
120
|
+
(tx) => SolKit.setTransactionMessageFeePayerSigner(fromKeyPair, tx),
|
|
121
|
+
);
|
|
122
|
+
lifetimeConstrainedTx = SolKit.setTransactionMessageLifetimeUsingBlockhash(
|
|
123
|
+
recentBlockhash,
|
|
124
|
+
txMessage,
|
|
125
|
+
);
|
|
126
|
+
if (priorityFee) {
|
|
127
|
+
const maxPriorityFee = Math.max(this.MINIMUM_PRIORITY_FEE, priorityFee);
|
|
128
|
+
createAccountInstructions.push(SolComputeBudget.getSetComputeUnitPriceInstruction({ microLamports: maxPriorityFee }));
|
|
129
|
+
}
|
|
130
|
+
if (computeUnits) {
|
|
131
|
+
createAccountInstructions.push(SolComputeBudget.getSetComputeUnitLimitInstruction({ units: computeUnits }));
|
|
132
|
+
}
|
|
133
|
+
const completeMessage = SolKit.appendTransactionMessageInstructions(
|
|
134
|
+
createAccountInstructions,
|
|
135
|
+
lifetimeConstrainedTx
|
|
136
|
+
);
|
|
137
|
+
const compiled = SolKit.compileTransaction(completeMessage);
|
|
138
|
+
return SolKit.getBase64EncodedWireTransaction(compiled);
|
|
139
|
+
case 'createata':
|
|
140
|
+
const { mint, ataAddress } = params;
|
|
141
|
+
const createAssociatedTokenIdempotentInstruction = SolToken.getCreateAssociatedTokenIdempotentInstruction({
|
|
142
|
+
payer: fromKeyPair,
|
|
143
|
+
owner: fromAddress,
|
|
144
|
+
mint: SolKit.address(mint),
|
|
145
|
+
ata: ataAddress
|
|
146
|
+
});
|
|
147
|
+
const ataTxMessage = SolKit.pipe(
|
|
148
|
+
SolKit.createTransactionMessage({ version: 0 }),
|
|
149
|
+
(tx) => SolKit.setTransactionMessageFeePayerSigner(fromKeyPair, tx),
|
|
150
|
+
(tx) => SolKit.setTransactionMessageLifetimeUsingBlockhash({ blockhash: blockHash as SolKit.Blockhash, lastValidBlockHeight: BigInt(blockHeight) }, tx),
|
|
151
|
+
(tx) => SolKit.appendTransactionMessageInstructions(
|
|
152
|
+
[createAssociatedTokenIdempotentInstruction],
|
|
153
|
+
tx
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
const compiledAtaTx = SolKit.compileTransaction(ataTxMessage);
|
|
157
|
+
return SolKit.getBase64EncodedWireTransaction(compiledAtaTx);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
decodeRawTransaction({ rawTx, decodeTransactionMessage = true }) {
|
|
162
|
+
if (typeof rawTx !== 'string') {
|
|
163
|
+
throw new Error(`Raw transaction expected to be a string. Found ${typeof rawTx} instead.`);
|
|
164
|
+
}
|
|
165
|
+
const uint8ArrayTx = SolKit.getBase64Encoder().encode(rawTx);
|
|
166
|
+
const decodedTx: any = SolKit.getTransactionDecoder().decode(uint8ArrayTx);
|
|
167
|
+
|
|
168
|
+
// decoding and decompiling the transaction message allows extraction of key data such as lifetimeConstraint
|
|
169
|
+
// certain advance transactions such as dex swaps, lookup table txs, may need additional data to perform decompilation and will throw errors if not provided
|
|
170
|
+
if (decodeTransactionMessage) {
|
|
171
|
+
const decompiledTransactionMessage = this.decodeTransactionMessage(decodedTx?.messageBytes);
|
|
172
|
+
decodedTx.lifetimeConstraint = decompiledTransactionMessage.lifetimeConstraint;
|
|
173
|
+
}
|
|
174
|
+
return decodedTx;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
decodeTransactionMessage(bytes) {
|
|
178
|
+
// TODO support lookup tables
|
|
179
|
+
const compiledTransactionMessage = SolKit.getCompiledTransactionMessageDecoder().decode(bytes);
|
|
180
|
+
return SolKit.decompileTransactionMessage(compiledTransactionMessage);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async sign(params: { tx: string; key: Key }): Promise<string> {
|
|
184
|
+
const { tx, key } = params;
|
|
185
|
+
const decodedTx = this.decodeRawTransaction({ rawTx: tx, decodeTransactionMessage: false });
|
|
186
|
+
const privKeyBytes = SolKit.getBase58Encoder().encode(key.privKey);
|
|
187
|
+
const keypair = await SolKit.createKeyPairFromPrivateKeyBytes(privKeyBytes);
|
|
188
|
+
const signedTransaciton = await SolKit.signTransaction([keypair], decodedTx);
|
|
189
|
+
return SolKit.getBase64EncodedWireTransaction(signedTransaciton);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async signPartially(params) {
|
|
193
|
+
const { tx, key } = params;
|
|
194
|
+
const decodedTx = this.decodeRawTransaction({ rawTx: tx, decodeTransactionMessage: false });
|
|
195
|
+
const privKeyBytes = SolKit.getBase58Encoder().encode(key.privKey);
|
|
196
|
+
const keypair = await SolKit.createKeyPairFromPrivateKeyBytes(privKeyBytes);
|
|
197
|
+
const signedTransaciton = await SolKit.partiallySignTransaction([keypair], decodedTx);
|
|
198
|
+
return SolKit.getBase64EncodedWireTransaction(signedTransaciton);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async signMessage(params) {
|
|
202
|
+
const { key, messageBytes } = params;
|
|
203
|
+
const privKeyBytes = SolKit.getBase58Encoder().encode(key.privKey);
|
|
204
|
+
const keypair = await SolKit.createKeyPairFromPrivateKeyBytes(privKeyBytes);
|
|
205
|
+
const signedBytes = await SolKit.signBytes(keypair.privateKey, messageBytes);
|
|
206
|
+
return SolKit.getBase58Decoder().decode(signedBytes);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async getSignature(params: { tx: string; keys: Array<Key> }) {
|
|
210
|
+
const { tx, keys } = params;
|
|
211
|
+
const key = keys[0];
|
|
212
|
+
const signedTx = await this.sign({ tx, key });
|
|
213
|
+
const decodedTx = this.decodeRawTransaction({ rawTx: signedTx, decodeTransactionMessage: false });
|
|
214
|
+
const sigEncoding = decodedTx.signatures[key.address];
|
|
215
|
+
return SolKit.getBase58Decoder().decode(sigEncoding);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
applySignature(params: { tx: string; signature: string }): string {
|
|
219
|
+
const { tx, signature } = params;
|
|
220
|
+
const signatures = [SolKit.getBase58Encoder().encode(signature)];
|
|
221
|
+
const transaction = SolKit.getBase64Encoder().encode(tx);
|
|
222
|
+
const transformWithNewSignatures = (_tx) => {
|
|
223
|
+
const { messageBytes } = _tx;
|
|
224
|
+
const signerAddressesDecoder = SolKit.getTupleDecoder([
|
|
225
|
+
// read transaction version
|
|
226
|
+
SolKit.getTransactionVersionDecoder(),
|
|
227
|
+
// read first byte of header, `numSignerAccounts`
|
|
228
|
+
// padRight to skip the next 2 bytes, `numReadOnlySignedAccounts` and `numReadOnlyUnsignedAccounts` which we don't need
|
|
229
|
+
SolKit.padRightDecoder(SolKit.getU8Decoder(), 2),
|
|
230
|
+
// read static addresses
|
|
231
|
+
SolKit.getArrayDecoder(SolKit.getAddressDecoder(), { size: SolKit.getShortU16Decoder() })
|
|
232
|
+
]);
|
|
233
|
+
const [, numRequiredSignatures, staticAddresses] = signerAddressesDecoder.decode(messageBytes);
|
|
234
|
+
const signerAddresses = staticAddresses.slice(0, numRequiredSignatures);
|
|
235
|
+
if (signerAddresses.length !== signatures.length) {
|
|
236
|
+
throw new Error(`The transaction message expected the transaction to have ${signerAddresses.length} signatures, got ${signatures.length}.`);
|
|
237
|
+
}
|
|
238
|
+
const signaturesMap = {};
|
|
239
|
+
for (let index = 0; index < signerAddresses.length; index++) {
|
|
240
|
+
const address = signerAddresses[index];
|
|
241
|
+
const signatureForAddress = signatures[index];
|
|
242
|
+
|
|
243
|
+
if (signatureForAddress.every((b) => b === 0)) {
|
|
244
|
+
signaturesMap[address] = null;
|
|
245
|
+
} else {
|
|
246
|
+
signaturesMap[address] = signatureForAddress;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
messageBytes,
|
|
251
|
+
signatures: Object.freeze(signaturesMap)
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
const decoderTransform = SolKit.transformDecoder(
|
|
255
|
+
SolKit.getStructDecoder([
|
|
256
|
+
['signatures', SolKit.getArrayDecoder(SolKit.fixDecoderSize(SolKit.getBytesDecoder(), 64), { size: SolKit.getShortU16Decoder() })],
|
|
257
|
+
['messageBytes', SolKit.getBytesDecoder()]
|
|
258
|
+
]),
|
|
259
|
+
transformWithNewSignatures
|
|
260
|
+
);
|
|
261
|
+
const signedTx = decoderTransform.decode(transaction);
|
|
262
|
+
return SolKit.getBase64EncodedWireTransaction(signedTx);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
getHash(params: { tx: string }): string {
|
|
266
|
+
const { tx } = params;
|
|
267
|
+
const decodedTx = this.decodeRawTransaction({ rawTx: tx, decodeTransactionMessage: false });
|
|
268
|
+
const pubKeys = Object.keys(decodedTx.signatures);
|
|
269
|
+
let signature;
|
|
270
|
+
|
|
271
|
+
if (pubKeys.length == 1) {
|
|
272
|
+
signature = decodedTx.signatures[pubKeys[0]];
|
|
273
|
+
} else if (pubKeys.length > 1) {
|
|
274
|
+
try {
|
|
275
|
+
const compiledTransactionMessage = SolKit.getCompiledTransactionMessageDecoder().decode(decodedTx.messageBytes);
|
|
276
|
+
const feePayerAddress = compiledTransactionMessage.staticAccounts[0];
|
|
277
|
+
signature = decodedTx.signatures[feePayerAddress];
|
|
278
|
+
} catch (err) {
|
|
279
|
+
throw new Error('unable to get fee payer signature %o', err.stack || err.message || err);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (!signature) {
|
|
283
|
+
throw new Error('tx is unsigned by fee payer');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return SolKit.getBase58Decoder().decode(signature);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
getSighash(_params: { tx: string }): string {
|
|
290
|
+
return null; // TODO
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import * as SolKit from '@solana/kit';
|
|
2
|
+
import * as SolToken from '@solana-program/token';
|
|
3
|
+
import { SOLTxProvider } from '../sol';
|
|
4
|
+
|
|
5
|
+
export class SPLTxProvider extends SOLTxProvider {
|
|
6
|
+
|
|
7
|
+
create(params: CreateParams) {
|
|
8
|
+
// Reuse exposed TransactionProxy API (Create)
|
|
9
|
+
// @ts-expect-error - so public api is minimally changed
|
|
10
|
+
if (params.category === 'recoverNestedAssociatedToken') {
|
|
11
|
+
return SPLTxProvider.createRecoverNestedAssociatedTokenTransaction(params as unknown as CreateRecoverNestedAssociatedTokenParams);
|
|
12
|
+
}
|
|
13
|
+
// @ts-expect-error - so public api is minimally changed
|
|
14
|
+
if (params.category === 'closeTokenAccount') {
|
|
15
|
+
return SPLTxProvider.createCloseTokenAccountTransaction(params as unknown as CreateCloseTokenAccountParams);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const { recipients, from, fromAta, tokenAddress, decimals, instructions = [] } = params;
|
|
19
|
+
// Start with custom instructions
|
|
20
|
+
const allInstructions = [...instructions];
|
|
21
|
+
if (recipients.length > 0 && !instructions?.length) {
|
|
22
|
+
// Add SPL token transfer instructions for each recipient
|
|
23
|
+
for (const recipient of recipients) {
|
|
24
|
+
const { address: recipientAddress, amount: recipientAmount } = recipient;
|
|
25
|
+
allInstructions.push(SolToken.getTransferCheckedInstruction({
|
|
26
|
+
source: SolKit.address(fromAta), // ATA address
|
|
27
|
+
authority: SolKit.address(from),
|
|
28
|
+
mint: SolKit.address(tokenAddress),
|
|
29
|
+
destination: SolKit.address(recipientAddress), // ATA address
|
|
30
|
+
amount: BigInt(recipientAmount),
|
|
31
|
+
decimals
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return super.create({ ...params, txInstructions: allInstructions });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static createAtokenInstructions(instructionType: InstructionType, params: any) {
|
|
40
|
+
try {
|
|
41
|
+
switch (instructionType) {
|
|
42
|
+
case 'createAssociatedToken':
|
|
43
|
+
return SolToken.getCreateAssociatedTokenInstruction(params);
|
|
44
|
+
case 'createAssociatedTokenIdempotent':
|
|
45
|
+
return SolToken.getCreateAssociatedTokenIdempotentInstruction(params);
|
|
46
|
+
case 'recoverNestedAssociatedToken':
|
|
47
|
+
return SolToken.getRecoverNestedAssociatedTokenInstruction(params);
|
|
48
|
+
default:
|
|
49
|
+
throw new Error(`Unsupported AToken instruction type: ${instructionType}`);
|
|
50
|
+
}
|
|
51
|
+
} catch (error) {
|
|
52
|
+
throw new Error(`Failed to create AToken instruction: ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static createRecoverNestedAssociatedTokenTransaction(params: CreateRecoverNestedAssociatedTokenParams) {
|
|
57
|
+
const {
|
|
58
|
+
fromKeyPair,
|
|
59
|
+
blockHash,
|
|
60
|
+
blockHeight,
|
|
61
|
+
nestedAssociatedAccountAddress,
|
|
62
|
+
nestedTokenMintAddress,
|
|
63
|
+
destinationAssociatedAccountAddress,
|
|
64
|
+
ownerAssociatedAccountAddress,
|
|
65
|
+
ownerTokenMintAddress
|
|
66
|
+
} = params;
|
|
67
|
+
// Validate
|
|
68
|
+
if (!SolKit.isKeyPairSigner(fromKeyPair)) {
|
|
69
|
+
throw new Error('fromKeyPair required to implement KeyPairSigner');
|
|
70
|
+
}
|
|
71
|
+
if (!(blockHash && blockHeight)) {
|
|
72
|
+
throw new Error('blockHash and blockHeight required');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const instruction = SolToken.getRecoverNestedAssociatedTokenInstruction({
|
|
76
|
+
nestedAssociatedAccountAddress: SolKit.address(nestedAssociatedAccountAddress),
|
|
77
|
+
nestedTokenMintAddress: SolKit.address(nestedTokenMintAddress),
|
|
78
|
+
destinationAssociatedAccountAddress: SolKit.address(destinationAssociatedAccountAddress),
|
|
79
|
+
ownerAssociatedAccountAddress: SolKit.address(ownerAssociatedAccountAddress),
|
|
80
|
+
ownerTokenMintAddress: SolKit.address(ownerTokenMintAddress),
|
|
81
|
+
walletAddress: params.fromKeyPair
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const recentBlockhash = {
|
|
85
|
+
blockhash: blockHash as SolKit.Blockhash,
|
|
86
|
+
lastValidBlockHeight: BigInt(blockHeight)
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// Create transaction
|
|
90
|
+
const transactionMessage = SolKit.pipe(
|
|
91
|
+
SolKit.createTransactionMessage({ version: 'legacy' }),
|
|
92
|
+
(tx) => SolKit.setTransactionMessageFeePayer(fromKeyPair.address, tx),
|
|
93
|
+
(tx) => SolKit.setTransactionMessageLifetimeUsingBlockhash(recentBlockhash, tx),
|
|
94
|
+
(tx) => SolKit.appendTransactionMessageInstructions(
|
|
95
|
+
[instruction], tx
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const compiledTx = SolKit.compileTransaction(transactionMessage);
|
|
100
|
+
return SolKit.getBase64EncodedWireTransaction(compiledTx);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static createCloseTokenAccountTransaction(params: CreateCloseTokenAccountParams) {
|
|
104
|
+
const {
|
|
105
|
+
fromKeyPair,
|
|
106
|
+
blockHash,
|
|
107
|
+
blockHeight,
|
|
108
|
+
ataAddressToClose,
|
|
109
|
+
solRentReturnAddress,
|
|
110
|
+
ataAddressToCloseOwnerSolAddress
|
|
111
|
+
} = params;
|
|
112
|
+
// Validate
|
|
113
|
+
if (!SolKit.isKeyPairSigner(fromKeyPair)) {
|
|
114
|
+
throw new Error('fromKeyPair required to implement KeyPairSigner');
|
|
115
|
+
}
|
|
116
|
+
if (!(blockHash && blockHeight)) {
|
|
117
|
+
throw new Error('blockHash and blockHeight required');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const instruction = SolToken.getCloseAccountInstruction({
|
|
121
|
+
account: SolKit.address(ataAddressToClose),
|
|
122
|
+
destination: SolKit.address(solRentReturnAddress),
|
|
123
|
+
owner: SolKit.address(ataAddressToCloseOwnerSolAddress)
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const recentBlockhash = {
|
|
127
|
+
blockhash: blockHash as SolKit.Blockhash,
|
|
128
|
+
lastValidBlockHeight: BigInt(blockHeight)
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// Create transaction
|
|
132
|
+
const transactionMessage = SolKit.pipe(
|
|
133
|
+
SolKit.createTransactionMessage({ version: 'legacy' }),
|
|
134
|
+
(tx) => SolKit.setTransactionMessageFeePayer(fromKeyPair.address, tx),
|
|
135
|
+
(tx) => SolKit.setTransactionMessageLifetimeUsingBlockhash(recentBlockhash, tx),
|
|
136
|
+
(tx) => SolKit.appendTransactionMessageInstructions(
|
|
137
|
+
[instruction], tx
|
|
138
|
+
)
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const compiledTx = SolKit.compileTransaction(transactionMessage);
|
|
142
|
+
return SolKit.getBase64EncodedWireTransaction(compiledTx);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
interface CreateParams {
|
|
147
|
+
recipients: Array<{ address: string; amount: string; addressKeyPair?: SolKit.KeyPairSigner }>;
|
|
148
|
+
from: string;
|
|
149
|
+
fee?: number;
|
|
150
|
+
feeRate: number;
|
|
151
|
+
txType?: 'legacy' | '0'; // legacy, version 0
|
|
152
|
+
category?: 'transfer' | 'createAccount';
|
|
153
|
+
nonce?: string; // nonce is represented as a transaction id
|
|
154
|
+
nonceAddress?: string;
|
|
155
|
+
blockHash?: string;
|
|
156
|
+
blockHeight?: number;
|
|
157
|
+
priorityFee?: number;
|
|
158
|
+
computeUnits?: number;
|
|
159
|
+
memo?: string;
|
|
160
|
+
// account creation fields
|
|
161
|
+
fromKeyPair?: SolKit.KeyPairSigner;
|
|
162
|
+
space?: number; // amount of space to reserve a new account in bytes
|
|
163
|
+
instructions?: Array<SolKit.BaseTransactionMessage['instructions'][number]>;
|
|
164
|
+
// SPL token transfer fields (required for token transfers)
|
|
165
|
+
tokenAddress: string; // mint address
|
|
166
|
+
fromAta?: string;
|
|
167
|
+
decimals?: number;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
interface CreateRecoverNestedAssociatedTokenParams {
|
|
171
|
+
category: 'recoverNestedAssociatedToken';
|
|
172
|
+
blockHash: string;
|
|
173
|
+
blockHeight: number;
|
|
174
|
+
fromKeyPair: SolKit.KeyPairSigner;
|
|
175
|
+
nestedAssociatedAccountAddress: string;
|
|
176
|
+
nestedTokenMintAddress: string;
|
|
177
|
+
destinationAssociatedAccountAddress: string;
|
|
178
|
+
ownerAssociatedAccountAddress: string;
|
|
179
|
+
ownerTokenMintAddress: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface CreateCloseTokenAccountParams {
|
|
183
|
+
category: 'closeTokenAccount';
|
|
184
|
+
blockHash: string;
|
|
185
|
+
blockHeight: number;
|
|
186
|
+
fromKeyPair: SolKit.KeyPairSigner;
|
|
187
|
+
ataAddressToClose: string;
|
|
188
|
+
solRentReturnAddress: string;
|
|
189
|
+
ataAddressToCloseOwnerSolAddress: string;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
type InstructionType = 'createAssociatedToken' | 'createAssociatedTokenIdempotent' | 'recoverNestedAssociatedToken';
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import * as RBC from 'ripple-binary-codec';
|
|
3
|
+
import * as binary from 'ripple-binary-codec/dist/binary';
|
|
4
|
+
import { HashPrefix } from 'ripple-binary-codec/dist/hash-prefixes';
|
|
5
|
+
import * as xrpl from 'xrpl';
|
|
6
|
+
import { BTCTxProvider } from '../btc';
|
|
7
|
+
import type { Key } from '../../types/derivation';
|
|
8
|
+
|
|
9
|
+
export class XRPTxProvider {
|
|
10
|
+
create(params: {
|
|
11
|
+
recipients: Array<{ address: string; amount: string; tag?: number }>;
|
|
12
|
+
tag?: number;
|
|
13
|
+
from: string;
|
|
14
|
+
invoiceID?: string;
|
|
15
|
+
fee: number;
|
|
16
|
+
feeRate: number;
|
|
17
|
+
nonce: number;
|
|
18
|
+
type?: string;
|
|
19
|
+
flags?: number;
|
|
20
|
+
}) {
|
|
21
|
+
const { recipients, tag, from, invoiceID, fee, nonce, type, flags } = params;
|
|
22
|
+
|
|
23
|
+
switch (type?.toLowerCase()) {
|
|
24
|
+
case 'payment':
|
|
25
|
+
default:
|
|
26
|
+
const { address, amount } = recipients[0];
|
|
27
|
+
const _tag = recipients[0]?.tag || tag;
|
|
28
|
+
const paymentTx: xrpl.Payment = {
|
|
29
|
+
TransactionType: 'Payment',
|
|
30
|
+
Account: from,
|
|
31
|
+
Destination: address,
|
|
32
|
+
Amount: amount.toString(),
|
|
33
|
+
Fee: fee.toString(),
|
|
34
|
+
Sequence: nonce,
|
|
35
|
+
Flags: 2147483648 // tfFullyCanonicalSig - DEPRECATED but still here for backward compatibility
|
|
36
|
+
};
|
|
37
|
+
if (flags != null) {
|
|
38
|
+
paymentTx.Flags = flags;
|
|
39
|
+
}
|
|
40
|
+
if (invoiceID) {
|
|
41
|
+
paymentTx.InvoiceID = invoiceID;
|
|
42
|
+
}
|
|
43
|
+
if (_tag) {
|
|
44
|
+
paymentTx.DestinationTag = _tag;
|
|
45
|
+
}
|
|
46
|
+
return xrpl.encode(paymentTx);
|
|
47
|
+
case 'accountset':
|
|
48
|
+
if (!xrpl.AccountSetTfFlags[flags]) {
|
|
49
|
+
throw new Error('Invalid tfAccountSet flag');
|
|
50
|
+
}
|
|
51
|
+
const accountSetTx: xrpl.AccountSet = {
|
|
52
|
+
TransactionType: 'AccountSet',
|
|
53
|
+
Account: from,
|
|
54
|
+
Flags: (isNaN(flags) ? xrpl.AccountSetTfFlags[flags] : flags) as number, // in testing, only the number values take effect.
|
|
55
|
+
Fee: fee.toString(),
|
|
56
|
+
Sequence: nonce
|
|
57
|
+
};
|
|
58
|
+
return xrpl.encode(accountSetTx);
|
|
59
|
+
case 'accountdelete':
|
|
60
|
+
const accountDeleteTx: xrpl.AccountDelete = {
|
|
61
|
+
TransactionType: 'AccountDelete',
|
|
62
|
+
Account: from,
|
|
63
|
+
Destination: recipients[0].address,
|
|
64
|
+
DestinationTag: recipients[0].tag,
|
|
65
|
+
Fee: fee.toString(),
|
|
66
|
+
Sequence: nonce
|
|
67
|
+
};
|
|
68
|
+
return xrpl.encode(accountDeleteTx);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getSignatureObject(params: { tx: string; key: Key }) {
|
|
73
|
+
const { tx, key } = params;
|
|
74
|
+
const txJSON = (xrpl.decode(tx) as unknown) as xrpl.Payment;
|
|
75
|
+
const signedTx = new xrpl.Wallet(key.pubKey.toUpperCase(), key.privKey.toUpperCase()).sign(txJSON);
|
|
76
|
+
return { signedTransaction: signedTx.tx_blob, hash: signedTx.hash };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getSignature(params: { tx: string; key: Key }): string {
|
|
80
|
+
const { signedTransaction } = this.getSignatureObject(params);
|
|
81
|
+
const decoded = (xrpl.decode(signedTransaction) as any) as xrpl.Transaction;
|
|
82
|
+
return decoded.TxnSignature;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getHash(params: { tx: string }): string {
|
|
86
|
+
const { tx } = params;
|
|
87
|
+
const prefix = HashPrefix.transactionID.toString('hex').toUpperCase();
|
|
88
|
+
return this.sha512Half(prefix + tx);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
applySignature(params: { tx: string; signature: string; pubKey: string }): string {
|
|
92
|
+
const { tx, signature, pubKey } = params;
|
|
93
|
+
const txJSON = (xrpl.decode(tx) as any) as xrpl.Transaction;
|
|
94
|
+
txJSON.TxnSignature = signature;
|
|
95
|
+
txJSON.SigningPubKey = pubKey;
|
|
96
|
+
const signedTx = xrpl.encode(txJSON);
|
|
97
|
+
return signedTx;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
sign(params: { tx: string; key: Key }): string {
|
|
101
|
+
const { tx, key } = params;
|
|
102
|
+
const signature = this.getSignature({ tx, key });
|
|
103
|
+
return this.applySignature({ tx, signature, pubKey: key.pubKey });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
sha512Half(hex: string): string {
|
|
107
|
+
return createHash('sha512')
|
|
108
|
+
.update(Buffer.from(hex, 'hex'))
|
|
109
|
+
.digest('hex')
|
|
110
|
+
.toUpperCase()
|
|
111
|
+
.slice(0, 64);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
transformSignatureObject(params: { obj: any }) {
|
|
115
|
+
const { obj } = params;
|
|
116
|
+
return new BTCTxProvider().transformSignatureObject({ obj });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getSighash(params: { tx: string; pubKey: string }) {
|
|
120
|
+
const { tx, pubKey } = params;
|
|
121
|
+
const decoded = RBC.decode(tx);
|
|
122
|
+
decoded.SigningPubKey = pubKey;
|
|
123
|
+
const encoded = binary.serializeObject(decoded, {
|
|
124
|
+
prefix: HashPrefix.transactionSig,
|
|
125
|
+
signingFieldsOnly: true
|
|
126
|
+
}).toString('hex');
|
|
127
|
+
return this.sha512Half(encoded);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface Key {
|
|
2
|
+
address: string;
|
|
3
|
+
privKey?: string;
|
|
4
|
+
pubKey?: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface IDeriver {
|
|
8
|
+
deriveAddress(network: string, xPub: string, addressIndex: number, isChange: boolean, addressType?: string): string;
|
|
9
|
+
|
|
10
|
+
derivePrivateKey(network: string, xPriv: string, addressIndex: number, isChange: boolean, addressType?: string): Key;
|
|
11
|
+
|
|
12
|
+
deriveAddressWithPath(network: string, xpubKey: string, path: string, addressType: string): string;
|
|
13
|
+
|
|
14
|
+
derivePrivateKeyWithPath(network: string, xprivKey: string, path: string, addressType: string): Key;
|
|
15
|
+
|
|
16
|
+
getAddress(network: string, pubKey, addressType: string): string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type BitcoreLib from '@bitpay-labs/bitcore-lib';
|
|
2
|
+
|
|
3
|
+
export type KeyType = string | BitcoreLib.PrivateKey;
|
|
4
|
+
export type HDKeyType = string | BitcoreLib.HDPrivateKey;
|
|
5
|
+
export type Encoding = BufferEncoding | 'base58';
|
|
6
|
+
export interface ISignedMessage<T = Buffer | string> {
|
|
7
|
+
signature: T;
|
|
8
|
+
publicKey: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface IMessageClass {
|
|
12
|
+
getMessageHash(args: { message: string; encoding?: Encoding }): Buffer | string;
|
|
13
|
+
signMessage(args: { message: string; privateKey: KeyType; encoding?: Encoding }): ISignedMessage;
|
|
14
|
+
signMessageWithPath(args: { message: string; hdPrivateKey: HDKeyType; derivationPath: string; encoding?: Encoding }): ISignedMessage;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type BigIntLike = bigint | number | string;
|