@swapkit/toolboxes 4.0.0-beta.50 → 4.0.0-beta.52
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/dist/src/cosmos/index.cjs +2 -2
- package/dist/src/cosmos/index.cjs.map +10 -6
- package/dist/src/cosmos/index.js +2 -2
- package/dist/src/cosmos/index.js.map +10 -6
- package/dist/src/evm/index.cjs +2 -2
- package/dist/src/evm/index.cjs.map +10 -8
- package/dist/src/evm/index.js +2 -2
- package/dist/src/evm/index.js.map +10 -8
- package/dist/src/index.cjs +4 -2
- package/dist/src/index.cjs.map +54 -4
- package/dist/src/index.js +4 -2
- package/dist/src/index.js.map +54 -4
- package/dist/src/near/index.cjs +2 -2
- package/dist/src/near/index.cjs.map +7 -5
- package/dist/src/near/index.js +2 -2
- package/dist/src/near/index.js.map +7 -5
- package/dist/src/radix/index.cjs +2 -2
- package/dist/src/radix/index.cjs.map +2 -2
- package/dist/src/radix/index.js +2 -2
- package/dist/src/radix/index.js.map +2 -2
- package/dist/src/ripple/index.cjs +2 -2
- package/dist/src/ripple/index.cjs.map +2 -2
- package/dist/src/ripple/index.js +2 -2
- package/dist/src/ripple/index.js.map +2 -2
- package/dist/src/solana/index.cjs +2 -2
- package/dist/src/solana/index.cjs.map +5 -4
- package/dist/src/solana/index.js +2 -2
- package/dist/src/solana/index.js.map +5 -4
- package/dist/src/substrate/index.cjs +2 -2
- package/dist/src/substrate/index.cjs.map +6 -5
- package/dist/src/substrate/index.js +2 -2
- package/dist/src/substrate/index.js.map +6 -5
- package/dist/src/tron/index.cjs +2 -2
- package/dist/src/tron/index.cjs.map +6 -5
- package/dist/src/tron/index.js +2 -2
- package/dist/src/tron/index.js.map +6 -5
- package/dist/src/utxo/index.cjs +4 -4
- package/dist/src/utxo/index.cjs.map +10 -7
- package/dist/src/utxo/index.js +4 -4
- package/dist/src/utxo/index.js.map +10 -7
- package/{src/cosmos/index.ts → dist/types/cosmos/index.d.ts} +1 -0
- package/dist/types/cosmos/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -0
- package/{src/cosmos/thorchainUtils/index.ts → dist/types/cosmos/thorchainUtils/index.d.ts} +1 -0
- package/dist/types/cosmos/thorchainUtils/index.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts +208 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts +4 -0
- package/dist/types/cosmos/thorchainUtils/registry.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/MsgCompiled.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts +66 -0
- package/dist/types/cosmos/thorchainUtils/types/client-types.d.ts.map +1 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts +2 -0
- package/dist/types/cosmos/thorchainUtils/types/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts +93 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/index.d.ts +14 -0
- package/dist/types/cosmos/toolbox/index.d.ts.map +1 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts +158 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -0
- package/{src/cosmos/types.ts → dist/types/cosmos/types.d.ts} +24 -29
- package/dist/types/cosmos/types.d.ts.map +1 -0
- package/dist/types/cosmos/util.d.ts +68 -0
- package/dist/types/cosmos/util.d.ts.map +1 -0
- package/dist/types/evm/api.d.ts +8 -0
- package/dist/types/evm/api.d.ts.map +1 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts +36 -0
- package/dist/types/evm/contracts/eth/multicall.d.ts.map +1 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts +40 -0
- package/dist/types/evm/contracts/op/gasOracle.d.ts.map +1 -0
- package/dist/types/evm/helpers.d.ts +20 -0
- package/dist/types/evm/helpers.d.ts.map +1 -0
- package/{src/evm/index.ts → dist/types/evm/index.d.ts} +1 -0
- package/dist/types/evm/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +82 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -0
- package/dist/types/evm/toolbox/evm.d.ts +367 -0
- package/dist/types/evm/toolbox/evm.d.ts.map +1 -0
- package/dist/types/evm/toolbox/index.d.ts +89 -0
- package/dist/types/evm/toolbox/index.d.ts.map +1 -0
- package/dist/types/evm/toolbox/op.d.ts +62 -0
- package/dist/types/evm/toolbox/op.d.ts.map +1 -0
- package/dist/types/evm/types.d.ts +98 -0
- package/dist/types/evm/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +63 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/near/helpers/core.d.ts +15 -0
- package/dist/types/near/helpers/core.d.ts.map +1 -0
- package/dist/types/near/helpers/gasEstimation.d.ts +41 -0
- package/dist/types/near/helpers/gasEstimation.d.ts.map +1 -0
- package/dist/types/near/helpers/nep141.d.ts +72 -0
- package/dist/types/near/helpers/nep141.d.ts.map +1 -0
- package/dist/types/near/index.d.ts +10 -0
- package/dist/types/near/index.d.ts.map +1 -0
- package/dist/types/near/toolbox.d.ts +4 -0
- package/dist/types/near/toolbox.d.ts.map +1 -0
- package/dist/types/near/types/contract.d.ts +38 -0
- package/dist/types/near/types/contract.d.ts.map +1 -0
- package/dist/types/near/types/nep141.d.ts +56 -0
- package/dist/types/near/types/nep141.d.ts.map +1 -0
- package/dist/types/near/types/toolbox.d.ts +57 -0
- package/dist/types/near/types/toolbox.d.ts.map +1 -0
- package/dist/types/near/types.d.ts +44 -0
- package/dist/types/near/types.d.ts.map +1 -0
- package/dist/types/radix/index.d.ts +14 -0
- package/dist/types/radix/index.d.ts.map +1 -0
- package/dist/types/ripple/index.d.ts +43 -0
- package/dist/types/ripple/index.d.ts.map +1 -0
- package/dist/types/solana/index.d.ts +37 -0
- package/dist/types/solana/index.d.ts.map +1 -0
- package/dist/types/solana/toolbox.d.ts +41 -0
- package/dist/types/solana/toolbox.d.ts.map +1 -0
- package/dist/types/substrate/balance.d.ts +17 -0
- package/dist/types/substrate/balance.d.ts.map +1 -0
- package/{src/substrate/index.ts → dist/types/substrate/index.d.ts} +1 -0
- package/dist/types/substrate/index.d.ts.map +1 -0
- package/dist/types/substrate/substrate.d.ts +148 -0
- package/dist/types/substrate/substrate.d.ts.map +1 -0
- package/dist/types/substrate/types.d.ts +100 -0
- package/dist/types/substrate/types.d.ts.map +1 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts +156 -0
- package/dist/types/tron/helpers/trc20.abi.d.ts.map +1 -0
- package/dist/types/tron/helpers/trongrid.d.ts +8 -0
- package/dist/types/tron/helpers/trongrid.d.ts.map +1 -0
- package/dist/types/tron/index.d.ts +6 -0
- package/dist/types/tron/index.d.ts.map +1 -0
- package/dist/types/tron/toolbox.d.ts +26 -0
- package/dist/types/tron/toolbox.d.ts.map +1 -0
- package/dist/types/tron/types.d.ts +101 -0
- package/dist/types/tron/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +18 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils.d.ts +4 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utxo/helpers/api.d.ts +133 -0
- package/dist/types/utxo/helpers/api.d.ts.map +1 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts +10 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -0
- package/dist/types/utxo/helpers/coinselect.d.ts +16 -0
- package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -0
- package/{src/utxo/helpers/index.ts → dist/types/utxo/helpers/index.d.ts} +1 -0
- package/dist/types/utxo/helpers/index.d.ts.map +1 -0
- package/dist/types/utxo/helpers/txSize.d.ts +21 -0
- package/dist/types/utxo/helpers/txSize.d.ts.map +1 -0
- package/{src/utxo/index.ts → dist/types/utxo/index.d.ts} +1 -0
- package/dist/types/utxo/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts +104 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/index.d.ts +50 -0
- package/dist/types/utxo/toolbox/index.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/utxo.d.ts +102 -0
- package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -0
- package/dist/types/utxo/toolbox/zcash.d.ts +83 -0
- package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -0
- package/dist/types/utxo/types.d.ts +46 -0
- package/dist/types/utxo/types.d.ts.map +1 -0
- package/package.json +15 -18
- package/dist/chunk-0h4xdrwz.js +0 -5
- package/dist/chunk-0h4xdrwz.js.map +0 -10
- package/dist/chunk-4yap1fvd.js +0 -4
- package/dist/chunk-4yap1fvd.js.map +0 -10
- package/dist/chunk-9bqegm61.js +0 -4
- package/dist/chunk-9bqegm61.js.map +0 -10
- package/dist/chunk-fazw0jvt.js +0 -4
- package/dist/chunk-fazw0jvt.js.map +0 -9
- package/dist/chunk-fjfxga2v.js +0 -4
- package/dist/chunk-fjfxga2v.js.map +0 -10
- package/dist/chunk-s47y8512.js +0 -5
- package/dist/chunk-s47y8512.js.map +0 -9
- package/dist/chunk-vtd17cje.js +0 -4
- package/dist/chunk-vtd17cje.js.map +0 -10
- package/dist/chunk-zcdeg6h9.js +0 -5
- package/dist/chunk-zcdeg6h9.js.map +0 -10
- package/src/cosmos/thorchainUtils/addressFormat.ts +0 -26
- package/src/cosmos/thorchainUtils/messages.ts +0 -262
- package/src/cosmos/thorchainUtils/registry.ts +0 -44
- package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +0 -2800
- package/src/cosmos/thorchainUtils/types/client-types.ts +0 -73
- package/src/cosmos/thorchainUtils/types/index.ts +0 -1
- package/src/cosmos/toolbox/cosmos.ts +0 -375
- package/src/cosmos/toolbox/index.ts +0 -33
- package/src/cosmos/toolbox/thorchain.ts +0 -313
- package/src/cosmos/util.ts +0 -266
- package/src/evm/__tests__/address-validation.test.ts +0 -86
- package/src/evm/__tests__/ethereum.test.ts +0 -141
- package/src/evm/api.ts +0 -21
- package/src/evm/contracts/eth/multicall.ts +0 -165
- package/src/evm/contracts/op/gasOracle.ts +0 -151
- package/src/evm/helpers.ts +0 -194
- package/src/evm/toolbox/baseEVMToolbox.ts +0 -762
- package/src/evm/toolbox/evm.ts +0 -66
- package/src/evm/toolbox/index.ts +0 -52
- package/src/evm/toolbox/op.ts +0 -131
- package/src/evm/types.ts +0 -146
- package/src/index.ts +0 -263
- package/src/near/__tests__/core.test.ts +0 -80
- package/src/near/helpers/contractFactory.ts +0 -22
- package/src/near/helpers/core.ts +0 -91
- package/src/near/helpers/gasEstimation.ts +0 -110
- package/src/near/helpers/index.ts +0 -5
- package/src/near/helpers/nep141.ts +0 -110
- package/src/near/index.ts +0 -24
- package/src/near/toolbox.ts +0 -509
- package/src/near/types/contract.ts +0 -48
- package/src/near/types/nep141.ts +0 -66
- package/src/near/types.ts +0 -57
- package/src/radix/index.ts +0 -156
- package/src/ripple/index.ts +0 -192
- package/src/solana/index.ts +0 -55
- package/src/solana/toolbox.ts +0 -433
- package/src/substrate/balance.ts +0 -92
- package/src/substrate/substrate.ts +0 -320
- package/src/substrate/types.ts +0 -120
- package/src/tron/__tests__/toolbox.test.ts +0 -147
- package/src/tron/helpers/trc20.abi.ts +0 -107
- package/src/tron/helpers/trongrid.ts +0 -54
- package/src/tron/index.ts +0 -17
- package/src/tron/toolbox.ts +0 -650
- package/src/tron/types.ts +0 -120
- package/src/utils.ts +0 -27
- package/src/utxo/__tests__/zcash-integration.test.ts +0 -114
- package/src/utxo/helpers/api.ts +0 -560
- package/src/utxo/helpers/bchaddrjs.ts +0 -183
- package/src/utxo/helpers/coinselect.ts +0 -98
- package/src/utxo/helpers/txSize.ts +0 -104
- package/src/utxo/toolbox/bitcoinCash.ts +0 -320
- package/src/utxo/toolbox/index.ts +0 -90
- package/src/utxo/toolbox/utxo.ts +0 -525
- package/src/utxo/toolbox/zcash.ts +0 -208
- package/src/utxo/types.ts +0 -57
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Chain,
|
|
3
|
-
type ChainSigner,
|
|
4
|
-
type DerivationPathArray,
|
|
5
|
-
SwapKitError,
|
|
6
|
-
type UTXOChain,
|
|
7
|
-
} from "@swapkit/helpers";
|
|
8
|
-
import type { Psbt } from "bitcoinjs-lib";
|
|
9
|
-
|
|
10
|
-
import type { TransactionBuilderType, TransactionType, UTXOType } from "../types";
|
|
11
|
-
import { createBCHToolbox } from "./bitcoinCash";
|
|
12
|
-
import { createUTXOToolbox } from "./utxo";
|
|
13
|
-
import { createZcashToolbox } from "./zcash";
|
|
14
|
-
|
|
15
|
-
type BCHToolbox = Awaited<ReturnType<typeof createBCHToolbox>>;
|
|
16
|
-
type CommonUTXOToolbox = Awaited<
|
|
17
|
-
ReturnType<typeof createUTXOToolbox<Exclude<UTXOChain, Chain.BitcoinCash | Chain.Zcash>>>
|
|
18
|
-
>;
|
|
19
|
-
type ZcashToolbox = Awaited<ReturnType<typeof createZcashToolbox>>;
|
|
20
|
-
|
|
21
|
-
export type UTXOToolboxes = {
|
|
22
|
-
[Chain.BitcoinCash]: BCHToolbox;
|
|
23
|
-
[Chain.Bitcoin]: CommonUTXOToolbox;
|
|
24
|
-
[Chain.Dogecoin]: CommonUTXOToolbox;
|
|
25
|
-
[Chain.Litecoin]: CommonUTXOToolbox;
|
|
26
|
-
[Chain.Dash]: CommonUTXOToolbox;
|
|
27
|
-
[Chain.Zcash]: ZcashToolbox;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
export type UTXOWallets = {
|
|
31
|
-
[key in keyof UTXOToolboxes]: UTXOToolboxes[key];
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type UtxoToolboxParams = {
|
|
35
|
-
[Chain.BitcoinCash]: {
|
|
36
|
-
signer: ChainSigner<{ builder: TransactionBuilderType; utxos: UTXOType[] }, TransactionType>;
|
|
37
|
-
};
|
|
38
|
-
[Chain.Bitcoin]: { signer: ChainSigner<Psbt, Psbt> };
|
|
39
|
-
[Chain.Dogecoin]: { signer: ChainSigner<Psbt, Psbt> };
|
|
40
|
-
[Chain.Litecoin]: { signer: ChainSigner<Psbt, Psbt> };
|
|
41
|
-
[Chain.Dash]: { signer: ChainSigner<Psbt, Psbt> };
|
|
42
|
-
[Chain.Zcash]: {
|
|
43
|
-
signer?: ChainSigner<Psbt, Psbt>;
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export async function getUtxoToolbox<T extends keyof UTXOToolboxes>(
|
|
48
|
-
chain: T,
|
|
49
|
-
params?:
|
|
50
|
-
| UtxoToolboxParams[T]
|
|
51
|
-
| {
|
|
52
|
-
phrase?: string;
|
|
53
|
-
derivationPath?: DerivationPathArray;
|
|
54
|
-
index?: number;
|
|
55
|
-
},
|
|
56
|
-
): Promise<UTXOToolboxes[T]> {
|
|
57
|
-
switch (chain) {
|
|
58
|
-
case Chain.BitcoinCash: {
|
|
59
|
-
const toolbox = await createBCHToolbox(
|
|
60
|
-
(params as UtxoToolboxParams[Chain.BitcoinCash]) || {},
|
|
61
|
-
);
|
|
62
|
-
return toolbox as UTXOToolboxes[T];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
case Chain.Zcash: {
|
|
66
|
-
const toolbox = await createZcashToolbox(params as UtxoToolboxParams[Chain.Zcash]);
|
|
67
|
-
return toolbox as UTXOToolboxes[T];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
case Chain.Bitcoin:
|
|
71
|
-
case Chain.Dogecoin:
|
|
72
|
-
case Chain.Litecoin:
|
|
73
|
-
case Chain.Dash: {
|
|
74
|
-
const toolbox = await createUTXOToolbox({
|
|
75
|
-
chain,
|
|
76
|
-
...(params as UtxoToolboxParams[Exclude<T, Chain.BitcoinCash | Chain.Zcash>]),
|
|
77
|
-
});
|
|
78
|
-
return toolbox as UTXOToolboxes[Exclude<T, Chain.BitcoinCash | Chain.Zcash>];
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
default:
|
|
82
|
-
throw new SwapKitError("toolbox_utxo_not_supported", { chain });
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export {
|
|
87
|
-
stripToCashAddress,
|
|
88
|
-
stripPrefix,
|
|
89
|
-
bchValidateAddress,
|
|
90
|
-
} from "./bitcoinCash";
|
package/src/utxo/toolbox/utxo.ts
DELETED
|
@@ -1,525 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AssetValue,
|
|
3
|
-
Chain,
|
|
4
|
-
type ChainSigner,
|
|
5
|
-
DerivationPath,
|
|
6
|
-
type DerivationPathArray,
|
|
7
|
-
FeeOption,
|
|
8
|
-
NetworkDerivationPath,
|
|
9
|
-
SwapKitError,
|
|
10
|
-
SwapKitNumber,
|
|
11
|
-
type UTXOChain,
|
|
12
|
-
applyFeeMultiplier,
|
|
13
|
-
derivationPathToString,
|
|
14
|
-
updateDerivationPath,
|
|
15
|
-
} from "@swapkit/helpers";
|
|
16
|
-
import { Psbt, address as btcLibAddress, initEccLib, payments } from "bitcoinjs-lib";
|
|
17
|
-
import type { ECPairInterface } from "ecpair";
|
|
18
|
-
import { ECPairFactory } from "ecpair";
|
|
19
|
-
import type { UtxoToolboxParams } from ".";
|
|
20
|
-
import { getBalance } from "../../utils";
|
|
21
|
-
import {
|
|
22
|
-
UTXOScriptType,
|
|
23
|
-
accumulative,
|
|
24
|
-
calculateTxSize,
|
|
25
|
-
compileMemo,
|
|
26
|
-
getDustThreshold,
|
|
27
|
-
getInputSize,
|
|
28
|
-
getUtxoApi,
|
|
29
|
-
getUtxoNetwork,
|
|
30
|
-
} from "../helpers";
|
|
31
|
-
import type {
|
|
32
|
-
BchECPair,
|
|
33
|
-
TargetOutput,
|
|
34
|
-
UTXOBuildTxParams,
|
|
35
|
-
UTXOTransferParams,
|
|
36
|
-
UTXOType,
|
|
37
|
-
} from "../types";
|
|
38
|
-
import { bchValidateAddress } from "./bitcoinCash";
|
|
39
|
-
|
|
40
|
-
import secp256k1 from "@bitcoinerlab/secp256k1";
|
|
41
|
-
// @ts-ignore
|
|
42
|
-
import { ECPair, HDNode } from "@psf/bitcoincashjs-lib";
|
|
43
|
-
import { HDKey } from "@scure/bip32";
|
|
44
|
-
import { mnemonicToSeedSync } from "@scure/bip39";
|
|
45
|
-
import { validateZcashAddress } from "./zcash";
|
|
46
|
-
|
|
47
|
-
export const nonSegwitChains = [Chain.Dash, Chain.Dogecoin];
|
|
48
|
-
|
|
49
|
-
function addInputsAndOutputs({
|
|
50
|
-
inputs,
|
|
51
|
-
outputs,
|
|
52
|
-
chain,
|
|
53
|
-
psbt,
|
|
54
|
-
sender,
|
|
55
|
-
compiledMemo,
|
|
56
|
-
}: {
|
|
57
|
-
inputs: UTXOType[];
|
|
58
|
-
outputs: TargetOutput[];
|
|
59
|
-
chain: UTXOChain;
|
|
60
|
-
psbt: Psbt;
|
|
61
|
-
sender: string;
|
|
62
|
-
compiledMemo: Buffer<ArrayBufferLike> | null;
|
|
63
|
-
}) {
|
|
64
|
-
for (const utxo of inputs) {
|
|
65
|
-
const witnessInfo = !!utxo.witnessUtxo &&
|
|
66
|
-
!nonSegwitChains.includes(chain) && { witnessUtxo: utxo.witnessUtxo };
|
|
67
|
-
|
|
68
|
-
const nonWitnessInfo = nonSegwitChains.includes(chain) && {
|
|
69
|
-
nonWitnessUtxo: utxo.txHex ? Buffer.from(utxo.txHex, "hex") : undefined,
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
psbt.addInput({ hash: utxo.hash, index: utxo.index, ...witnessInfo, ...nonWitnessInfo });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
for (const output of outputs) {
|
|
76
|
-
const address = "address" in output && output.address ? output.address : sender;
|
|
77
|
-
const hasOutputScript = output.script;
|
|
78
|
-
|
|
79
|
-
if (hasOutputScript && !compiledMemo) {
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const mappedOutput = hasOutputScript
|
|
84
|
-
? {
|
|
85
|
-
script: compiledMemo as Buffer<ArrayBufferLike>,
|
|
86
|
-
value: 0,
|
|
87
|
-
}
|
|
88
|
-
: {
|
|
89
|
-
address,
|
|
90
|
-
value: output.value,
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
initEccLib(secp256k1);
|
|
94
|
-
psbt.addOutput(mappedOutput);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return { psbt, inputs };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async function createTransaction({
|
|
101
|
-
assetValue,
|
|
102
|
-
recipient,
|
|
103
|
-
memo,
|
|
104
|
-
feeRate,
|
|
105
|
-
sender,
|
|
106
|
-
fetchTxHex = false,
|
|
107
|
-
}: UTXOBuildTxParams): Promise<{
|
|
108
|
-
psbt: Psbt;
|
|
109
|
-
utxos: UTXOType[];
|
|
110
|
-
inputs: UTXOType[];
|
|
111
|
-
}> {
|
|
112
|
-
const chain = assetValue.chain as UTXOChain;
|
|
113
|
-
const compiledMemo = memo ? await compileMemo(memo) : null;
|
|
114
|
-
|
|
115
|
-
const inputsAndOutputs = await getInputsAndTargetOutputs({
|
|
116
|
-
assetValue,
|
|
117
|
-
recipient,
|
|
118
|
-
memo,
|
|
119
|
-
sender,
|
|
120
|
-
fetchTxHex,
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
const { inputs, outputs } = accumulative({ ...inputsAndOutputs, feeRate, chain });
|
|
124
|
-
|
|
125
|
-
// .inputs and .outputs will be undefined if no solution was found
|
|
126
|
-
if (!(inputs && outputs))
|
|
127
|
-
throw new SwapKitError("toolbox_utxo_insufficient_balance", { sender, assetValue });
|
|
128
|
-
const getNetwork = await getUtxoNetwork();
|
|
129
|
-
const psbt = new Psbt({ network: getNetwork(chain) });
|
|
130
|
-
|
|
131
|
-
if (chain === Chain.Dogecoin) psbt.setMaximumFeeRate(650000000);
|
|
132
|
-
|
|
133
|
-
const { psbt: mappedPsbt, inputs: mappedInputs } = await addInputsAndOutputs({
|
|
134
|
-
inputs,
|
|
135
|
-
outputs,
|
|
136
|
-
chain,
|
|
137
|
-
psbt,
|
|
138
|
-
sender,
|
|
139
|
-
compiledMemo,
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
psbt: mappedPsbt,
|
|
144
|
-
utxos: inputsAndOutputs.inputs,
|
|
145
|
-
inputs: mappedInputs,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export async function getUTXOAddressValidator() {
|
|
150
|
-
const getNetwork = await getUtxoNetwork();
|
|
151
|
-
|
|
152
|
-
return function validateAddress({ address, chain }: { address: string; chain: UTXOChain }) {
|
|
153
|
-
if (chain === Chain.BitcoinCash) {
|
|
154
|
-
return bchValidateAddress(address);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (chain === Chain.Zcash) {
|
|
158
|
-
return validateZcashAddress(address);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
initEccLib(secp256k1);
|
|
163
|
-
btcLibAddress.toOutputScript(address, getNetwork(chain));
|
|
164
|
-
return true;
|
|
165
|
-
} catch (_error) {
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async function createSignerWithKeys({
|
|
172
|
-
chain,
|
|
173
|
-
phrase,
|
|
174
|
-
derivationPath,
|
|
175
|
-
}: { chain: UTXOChain; phrase: string; derivationPath: string }) {
|
|
176
|
-
const keyPair = (await getCreateKeysForPath(chain as Chain.Bitcoin))({ phrase, derivationPath });
|
|
177
|
-
|
|
178
|
-
async function signTransaction(psbt: Psbt) {
|
|
179
|
-
await psbt.signAllInputs(keyPair);
|
|
180
|
-
return psbt;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async function getAddress() {
|
|
184
|
-
const addressGetter = await addressFromKeysGetter(chain);
|
|
185
|
-
return addressGetter(keyPair);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return {
|
|
189
|
-
getAddress,
|
|
190
|
-
signTransaction,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export async function createUTXOToolbox<T extends UTXOChain>({
|
|
195
|
-
chain,
|
|
196
|
-
...toolboxParams
|
|
197
|
-
}: (
|
|
198
|
-
| UtxoToolboxParams[T]
|
|
199
|
-
| {
|
|
200
|
-
phrase?: string;
|
|
201
|
-
derivationPath?: DerivationPathArray;
|
|
202
|
-
index?: number;
|
|
203
|
-
}
|
|
204
|
-
) & { chain: T }) {
|
|
205
|
-
const phrase = "phrase" in toolboxParams ? toolboxParams.phrase : undefined;
|
|
206
|
-
|
|
207
|
-
const index = "index" in toolboxParams ? toolboxParams.index || 0 : 0;
|
|
208
|
-
|
|
209
|
-
const derivationPath = derivationPathToString(
|
|
210
|
-
"derivationPath" in toolboxParams && toolboxParams.derivationPath
|
|
211
|
-
? toolboxParams.derivationPath
|
|
212
|
-
: updateDerivationPath(NetworkDerivationPath[chain], { index }),
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
const signer = phrase
|
|
216
|
-
? await createSignerWithKeys({ chain, phrase, derivationPath })
|
|
217
|
-
: "signer" in toolboxParams
|
|
218
|
-
? toolboxParams.signer
|
|
219
|
-
: undefined;
|
|
220
|
-
|
|
221
|
-
function getAddress() {
|
|
222
|
-
return Promise.resolve(signer?.getAddress());
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// const { signer } = params || {};
|
|
226
|
-
const getAddressFromKeys = await addressFromKeysGetter(chain);
|
|
227
|
-
const validateAddress = await getUTXOAddressValidator();
|
|
228
|
-
const createKeysForPath = await getCreateKeysForPath(chain);
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
accumulative,
|
|
232
|
-
calculateTxSize,
|
|
233
|
-
getAddressFromKeys,
|
|
234
|
-
getAddress,
|
|
235
|
-
validateAddress: (address: string) => validateAddress({ address, chain }),
|
|
236
|
-
broadcastTx: (txHash: string) => getUtxoApi(chain).broadcastTx(txHash),
|
|
237
|
-
createTransaction,
|
|
238
|
-
createKeysForPath,
|
|
239
|
-
getFeeRates: () => getFeeRates(chain),
|
|
240
|
-
getInputsOutputsFee,
|
|
241
|
-
transfer: transfer(signer as UtxoToolboxParams["BTC"]["signer"]),
|
|
242
|
-
getPrivateKeyFromMnemonic: (params: { phrase: string; derivationPath: string }) => {
|
|
243
|
-
const keys = createKeysForPath(params);
|
|
244
|
-
return keys.toWIF();
|
|
245
|
-
},
|
|
246
|
-
|
|
247
|
-
getBalance: getBalance(chain),
|
|
248
|
-
estimateTransactionFee: estimateTransactionFee(chain),
|
|
249
|
-
estimateMaxSendableAmount: estimateMaxSendableAmount(chain),
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
async function getInputsOutputsFee({
|
|
254
|
-
assetValue,
|
|
255
|
-
feeOptionKey = FeeOption.Fast,
|
|
256
|
-
feeRate,
|
|
257
|
-
memo,
|
|
258
|
-
sender,
|
|
259
|
-
recipient,
|
|
260
|
-
}: Omit<UTXOBuildTxParams, "feeRate"> & {
|
|
261
|
-
feeOptionKey?: FeeOption;
|
|
262
|
-
feeRate?: number;
|
|
263
|
-
}) {
|
|
264
|
-
const chain = assetValue.chain as UTXOChain;
|
|
265
|
-
|
|
266
|
-
const inputsAndOutputs = await getInputsAndTargetOutputs({
|
|
267
|
-
assetValue,
|
|
268
|
-
sender,
|
|
269
|
-
memo,
|
|
270
|
-
recipient,
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
const feeRateWhole = feeRate ? Math.floor(feeRate) : (await getFeeRates(chain))[feeOptionKey];
|
|
274
|
-
|
|
275
|
-
return accumulative({ ...inputsAndOutputs, feeRate: feeRateWhole, chain });
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function estimateMaxSendableAmount(chain: UTXOChain) {
|
|
279
|
-
return async function estimateMaxSendableAmount({
|
|
280
|
-
from,
|
|
281
|
-
memo,
|
|
282
|
-
feeRate,
|
|
283
|
-
feeOptionKey = FeeOption.Fast,
|
|
284
|
-
recipients = 1,
|
|
285
|
-
}: {
|
|
286
|
-
from: string;
|
|
287
|
-
memo?: string;
|
|
288
|
-
feeRate?: number;
|
|
289
|
-
feeOptionKey?: FeeOption;
|
|
290
|
-
recipients?: number | TargetOutput[];
|
|
291
|
-
}) {
|
|
292
|
-
const addressData = await getUtxoApi(chain).getAddressData(from);
|
|
293
|
-
const feeRateWhole = feeRate ? Math.ceil(feeRate) : (await getFeeRates(chain))[feeOptionKey];
|
|
294
|
-
|
|
295
|
-
const inputs = addressData?.utxo
|
|
296
|
-
.map((utxo) => ({
|
|
297
|
-
...utxo,
|
|
298
|
-
// type: utxo.witnessUtxo ? UTXOScriptType.P2WPKH : UTXOScriptType.P2PKH,
|
|
299
|
-
type: UTXOScriptType.P2PKH,
|
|
300
|
-
hash: "",
|
|
301
|
-
}))
|
|
302
|
-
.filter(
|
|
303
|
-
(utxo) => utxo.value > Math.max(getDustThreshold(chain), getInputSize(utxo) * feeRateWhole),
|
|
304
|
-
);
|
|
305
|
-
|
|
306
|
-
if (!inputs?.length) return AssetValue.from({ chain });
|
|
307
|
-
|
|
308
|
-
const balance = AssetValue.from({
|
|
309
|
-
chain,
|
|
310
|
-
value: inputs.reduce((sum, utxo) => sum + utxo.value, 0),
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
const outputs =
|
|
314
|
-
typeof recipients === "number"
|
|
315
|
-
? (Array.from({ length: recipients }, () => ({
|
|
316
|
-
address: from,
|
|
317
|
-
value: 0,
|
|
318
|
-
})) as TargetOutput[])
|
|
319
|
-
: recipients;
|
|
320
|
-
|
|
321
|
-
if (memo) {
|
|
322
|
-
const script = await compileMemo(memo);
|
|
323
|
-
outputs.push({ address: from, script, value: 0 });
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
const txSize = calculateTxSize({ inputs, outputs, feeRate: feeRateWhole });
|
|
327
|
-
|
|
328
|
-
const fee = txSize * feeRateWhole;
|
|
329
|
-
|
|
330
|
-
return balance.sub(fee);
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function estimateTransactionFee(chain: UTXOChain) {
|
|
335
|
-
return async (params: {
|
|
336
|
-
assetValue: AssetValue;
|
|
337
|
-
recipient: string;
|
|
338
|
-
sender: string;
|
|
339
|
-
memo?: string;
|
|
340
|
-
feeOptionKey?: FeeOption;
|
|
341
|
-
feeRate?: number;
|
|
342
|
-
fetchTxHex?: boolean;
|
|
343
|
-
}) => {
|
|
344
|
-
const inputFees = await getInputsOutputsFee(params);
|
|
345
|
-
|
|
346
|
-
return AssetValue.from({
|
|
347
|
-
chain,
|
|
348
|
-
value: SwapKitNumber.fromBigInt(BigInt(inputFees.fee), 8).getValue("string"),
|
|
349
|
-
});
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
type CreateKeysForPathReturnType = {
|
|
354
|
-
[Chain.BitcoinCash]: BchECPair;
|
|
355
|
-
[Chain.Bitcoin]: ECPairInterface;
|
|
356
|
-
[Chain.Dash]: ECPairInterface;
|
|
357
|
-
[Chain.Dogecoin]: ECPairInterface;
|
|
358
|
-
[Chain.Litecoin]: ECPairInterface;
|
|
359
|
-
[Chain.Zcash]: ECPairInterface;
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
export async function getCreateKeysForPath<T extends keyof CreateKeysForPathReturnType>(
|
|
363
|
-
chain: T,
|
|
364
|
-
): Promise<
|
|
365
|
-
(params: {
|
|
366
|
-
wif?: string;
|
|
367
|
-
phrase?: string;
|
|
368
|
-
derivationPath?: string;
|
|
369
|
-
}) => CreateKeysForPathReturnType[T]
|
|
370
|
-
> {
|
|
371
|
-
const getNetwork = await getUtxoNetwork();
|
|
372
|
-
|
|
373
|
-
switch (chain) {
|
|
374
|
-
case Chain.BitcoinCash: {
|
|
375
|
-
return function createKeysForPath({
|
|
376
|
-
phrase,
|
|
377
|
-
derivationPath = `${DerivationPath.BCH}/0`,
|
|
378
|
-
wif,
|
|
379
|
-
}: { wif?: string; phrase?: string; derivationPath?: string }) {
|
|
380
|
-
const network = getNetwork(chain);
|
|
381
|
-
|
|
382
|
-
if (wif) {
|
|
383
|
-
return ECPair.fromWIF(wif, network) as BchECPair;
|
|
384
|
-
}
|
|
385
|
-
if (!phrase)
|
|
386
|
-
throw new SwapKitError("toolbox_utxo_invalid_params", { error: "No phrase provided" });
|
|
387
|
-
|
|
388
|
-
const masterHDNode = HDNode.fromSeedBuffer(
|
|
389
|
-
Buffer.from(mnemonicToSeedSync(phrase)),
|
|
390
|
-
network,
|
|
391
|
-
);
|
|
392
|
-
const keyPair = masterHDNode.derivePath(derivationPath).keyPair;
|
|
393
|
-
|
|
394
|
-
return keyPair as BchECPair;
|
|
395
|
-
} as (params: {
|
|
396
|
-
wif?: string;
|
|
397
|
-
phrase?: string;
|
|
398
|
-
derivationPath?: string;
|
|
399
|
-
}) => CreateKeysForPathReturnType[T];
|
|
400
|
-
}
|
|
401
|
-
case Chain.Bitcoin:
|
|
402
|
-
case Chain.Dogecoin:
|
|
403
|
-
case Chain.Litecoin:
|
|
404
|
-
case Chain.Zcash:
|
|
405
|
-
case Chain.Dash: {
|
|
406
|
-
return function createKeysForPath({
|
|
407
|
-
phrase,
|
|
408
|
-
wif,
|
|
409
|
-
derivationPath,
|
|
410
|
-
}: { phrase?: string; wif?: string; derivationPath: string }) {
|
|
411
|
-
if (!(wif || phrase))
|
|
412
|
-
throw new SwapKitError("toolbox_utxo_invalid_params", {
|
|
413
|
-
error: "Either phrase or wif must be provided",
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
const factory = ECPairFactory(secp256k1);
|
|
417
|
-
const network = getNetwork(chain);
|
|
418
|
-
|
|
419
|
-
if (wif) return factory.fromWIF(wif, network);
|
|
420
|
-
|
|
421
|
-
const seed = mnemonicToSeedSync(phrase as string);
|
|
422
|
-
const master = HDKey.fromMasterSeed(seed, network).derive(derivationPath);
|
|
423
|
-
if (!master.privateKey)
|
|
424
|
-
throw new SwapKitError("toolbox_utxo_invalid_params", {
|
|
425
|
-
error: "Could not get private key from phrase",
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
return factory.fromPrivateKey(Buffer.from(master.privateKey), { network });
|
|
429
|
-
} as (params: {
|
|
430
|
-
wif?: string;
|
|
431
|
-
phrase?: string;
|
|
432
|
-
derivationPath?: string;
|
|
433
|
-
}) => CreateKeysForPathReturnType[T];
|
|
434
|
-
}
|
|
435
|
-
default:
|
|
436
|
-
throw new SwapKitError("toolbox_utxo_not_supported", { chain });
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
export async function addressFromKeysGetter(chain: UTXOChain) {
|
|
441
|
-
const getNetwork = await getUtxoNetwork();
|
|
442
|
-
|
|
443
|
-
return function getAddressFromKeys(keys: ECPairInterface | BchECPair) {
|
|
444
|
-
if (!keys)
|
|
445
|
-
throw new SwapKitError("toolbox_utxo_invalid_params", { error: "Keys must be provided" });
|
|
446
|
-
|
|
447
|
-
const method = nonSegwitChains.includes(chain) ? payments.p2pkh : payments.p2wpkh;
|
|
448
|
-
const { address } = method({ pubkey: keys.publicKey as Buffer, network: getNetwork(chain) });
|
|
449
|
-
if (!address)
|
|
450
|
-
throw new SwapKitError("toolbox_utxo_invalid_address", { error: "Address not defined" });
|
|
451
|
-
|
|
452
|
-
return address;
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
function transfer(signer?: ChainSigner<Psbt, Psbt>) {
|
|
457
|
-
return async function transfer({
|
|
458
|
-
memo,
|
|
459
|
-
recipient,
|
|
460
|
-
feeOptionKey,
|
|
461
|
-
feeRate,
|
|
462
|
-
assetValue,
|
|
463
|
-
}: UTXOTransferParams) {
|
|
464
|
-
const from = await signer?.getAddress();
|
|
465
|
-
|
|
466
|
-
const chain = assetValue.chain as UTXOChain;
|
|
467
|
-
|
|
468
|
-
if (!(signer && from)) throw new SwapKitError("toolbox_utxo_no_signer");
|
|
469
|
-
if (!recipient)
|
|
470
|
-
throw new SwapKitError("toolbox_utxo_invalid_params", {
|
|
471
|
-
error: "Recipient address must be provided",
|
|
472
|
-
});
|
|
473
|
-
const txFeeRate = feeRate || (await getFeeRates(chain))[feeOptionKey || FeeOption.Fast];
|
|
474
|
-
|
|
475
|
-
const { psbt } = await createTransaction({
|
|
476
|
-
recipient,
|
|
477
|
-
feeRate: txFeeRate,
|
|
478
|
-
sender: from,
|
|
479
|
-
assetValue,
|
|
480
|
-
memo,
|
|
481
|
-
});
|
|
482
|
-
const signedPsbt = await signer.signTransaction(psbt);
|
|
483
|
-
signedPsbt.finalizeAllInputs(); // Finalise inputs
|
|
484
|
-
// TX extracted and formatted to hex
|
|
485
|
-
return getUtxoApi(chain).broadcastTx(signedPsbt.extractTransaction().toHex());
|
|
486
|
-
};
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
async function getFeeRates(chain: UTXOChain) {
|
|
490
|
-
const suggestedFeeRate = await getUtxoApi(chain).getSuggestedTxFee();
|
|
491
|
-
|
|
492
|
-
return {
|
|
493
|
-
[FeeOption.Average]: suggestedFeeRate,
|
|
494
|
-
[FeeOption.Fast]: applyFeeMultiplier(suggestedFeeRate, FeeOption.Fast),
|
|
495
|
-
[FeeOption.Fastest]: applyFeeMultiplier(suggestedFeeRate, FeeOption.Fastest),
|
|
496
|
-
};
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
async function getInputsAndTargetOutputs({
|
|
500
|
-
assetValue,
|
|
501
|
-
recipient,
|
|
502
|
-
memo,
|
|
503
|
-
sender,
|
|
504
|
-
fetchTxHex: fetchTxOverwrite = false,
|
|
505
|
-
}: Omit<UTXOBuildTxParams, "feeRate">) {
|
|
506
|
-
const chain = assetValue.chain as UTXOChain;
|
|
507
|
-
const feeRate = (await getFeeRates(chain))[FeeOption.Fastest];
|
|
508
|
-
|
|
509
|
-
const fetchTxHex = fetchTxOverwrite || nonSegwitChains.includes(chain);
|
|
510
|
-
|
|
511
|
-
const amountToSend = assetValue.getBaseValue("number");
|
|
512
|
-
|
|
513
|
-
// Overestimate by 5000 byte * highest feeRate to ensure we have enough UTXOs for fees and change
|
|
514
|
-
const targetValue = Math.ceil(amountToSend + feeRate * 5000);
|
|
515
|
-
|
|
516
|
-
const inputs = await getUtxoApi(chain).getUtxos({ address: sender, fetchTxHex, targetValue });
|
|
517
|
-
|
|
518
|
-
return {
|
|
519
|
-
inputs,
|
|
520
|
-
outputs: [
|
|
521
|
-
{ address: recipient, value: amountToSend },
|
|
522
|
-
...(memo ? [{ address: "", script: await compileMemo(memo), value: 0 }] : []),
|
|
523
|
-
],
|
|
524
|
-
};
|
|
525
|
-
}
|