@swapkit/toolboxes 1.0.0-beta.0 → 1.0.0-beta.10
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/chunk-0f0249b1.js +3 -0
- package/dist/chunk-0f0249b1.js.map +10 -0
- package/dist/chunk-0h4xdrwz.js +4 -0
- package/dist/chunk-0h4xdrwz.js.map +10 -0
- package/dist/chunk-4yap1fvd.js +3 -0
- package/dist/chunk-4yap1fvd.js.map +10 -0
- package/dist/chunk-fjfxga2v.js +3 -0
- package/dist/chunk-fjfxga2v.js.map +10 -0
- package/dist/{chunk-tvrdndbw.js → chunk-p1kdg37m.js} +2 -2
- package/dist/{chunk-tvrdndbw.js.map → chunk-p1kdg37m.js.map} +1 -1
- package/dist/cosmos/index.cjs +2 -2
- package/dist/cosmos/index.cjs.map +10 -13
- package/dist/cosmos/index.js +2 -2
- package/dist/cosmos/index.js.map +10 -13
- package/dist/evm/index.cjs +2 -2
- package/dist/evm/index.cjs.map +10 -16
- package/dist/evm/index.js +2 -2
- package/dist/evm/index.js.map +10 -16
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +4 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -3
- package/dist/radix/index.cjs +2 -2
- package/dist/radix/index.cjs.map +3 -3
- package/dist/radix/index.js +2 -2
- package/dist/radix/index.js.map +3 -3
- package/dist/ripple/index.cjs +3 -0
- package/dist/ripple/index.cjs.map +10 -0
- package/dist/ripple/index.js +3 -0
- package/dist/ripple/index.js.map +10 -0
- package/dist/solana/index.cjs +2 -2
- package/dist/solana/index.cjs.map +3 -3
- package/dist/solana/index.js +2 -2
- package/dist/solana/index.js.map +3 -3
- package/dist/substrate/index.cjs +2 -2
- package/dist/substrate/index.cjs.map +5 -6
- package/dist/substrate/index.js +2 -2
- package/dist/substrate/index.js.map +5 -6
- package/dist/utxo/index.cjs +2 -2
- package/dist/utxo/index.cjs.map +9 -11
- package/dist/utxo/index.js +2 -2
- package/dist/utxo/index.js.map +9 -11
- package/package.json +27 -23
- package/src/cosmos/index.ts +2 -9
- package/src/cosmos/thorchainUtils/addressFormat.ts +1 -2
- package/src/cosmos/thorchainUtils/index.ts +1 -1
- package/src/cosmos/thorchainUtils/messages.ts +74 -56
- package/src/cosmos/thorchainUtils/registry.ts +19 -26
- package/src/cosmos/thorchainUtils/types/{proto/MsgCompiled.ts → MsgCompiled.ts} +1 -3
- package/src/cosmos/thorchainUtils/types/client-types.ts +16 -23
- package/src/cosmos/toolbox/cosmos.ts +334 -0
- package/src/cosmos/toolbox/index.ts +33 -0
- package/src/cosmos/toolbox/thorchain.ts +125 -137
- package/src/cosmos/types.ts +37 -18
- package/src/cosmos/util.ts +24 -74
- package/src/evm/__tests__/ethereum.test.ts +110 -116
- package/src/evm/api.ts +11 -147
- package/src/evm/helpers.ts +111 -83
- package/src/evm/index.ts +1 -17
- package/src/evm/toolbox/baseEVMToolbox.ts +744 -0
- package/src/evm/toolbox/evm.ts +69 -0
- package/src/evm/toolbox/index.ts +36 -0
- package/src/evm/toolbox/op.ts +97 -143
- package/src/evm/types.ts +50 -28
- package/src/index.ts +235 -0
- package/src/radix/index.ts +18 -19
- package/src/ripple/index.ts +199 -0
- package/src/solana/index.ts +11 -5
- package/src/solana/toolbox.ts +227 -137
- package/src/substrate/index.ts +2 -3
- package/src/substrate/{toolbox/baseSubstrateToolbox.ts → substrate.ts} +104 -72
- package/src/substrate/types.ts +120 -0
- package/src/utils.ts +27 -0
- package/src/utxo/helpers/api.ts +27 -23
- package/src/utxo/helpers/bchaddrjs.ts +21 -21
- package/src/utxo/helpers/index.ts +0 -1
- package/src/utxo/helpers/txSize.ts +3 -4
- package/src/utxo/index.ts +3 -7
- package/src/utxo/toolbox/bitcoinCash.ts +165 -155
- package/src/utxo/toolbox/index.ts +63 -24
- package/src/utxo/toolbox/utxo.ts +376 -229
- package/src/utxo/types.ts +26 -39
- package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.js +0 -2806
- package/src/cosmos/thorchainUtils/util.ts +0 -46
- package/src/cosmos/toolbox/BaseCosmosToolbox.ts +0 -254
- package/src/cosmos/toolbox/gaia.ts +0 -39
- package/src/cosmos/toolbox/getToolboxByChain.ts +0 -29
- package/src/cosmos/toolbox/kujira.ts +0 -61
- package/src/evm/provider.ts +0 -6
- package/src/evm/toolbox/EVMToolbox.ts +0 -662
- package/src/evm/toolbox/arb.ts +0 -61
- package/src/evm/toolbox/avax.ts +0 -36
- package/src/evm/toolbox/base.ts +0 -42
- package/src/evm/toolbox/bsc.ts +0 -34
- package/src/evm/toolbox/eth.ts +0 -44
- package/src/evm/toolbox/getToolboxByChain.ts +0 -42
- package/src/evm/toolbox/matic.ts +0 -42
- package/src/radix/toolbox.ts +0 -693
- package/src/substrate/toolbox/index.ts +0 -40
- package/src/substrate/types/index.ts +0 -2
- package/src/substrate/types/network.ts +0 -42
- package/src/substrate/types/wallet.ts +0 -78
- package/src/utxo/helpers/utils.ts +0 -45
|
@@ -1,82 +1,155 @@
|
|
|
1
|
-
import * as secp256k1 from "@bitcoinerlab/secp256k1";
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
2
|
+
Chain,
|
|
3
|
+
type ChainSigner,
|
|
4
|
+
type DerivationPathArray,
|
|
5
|
+
FeeOption,
|
|
6
|
+
NetworkDerivationPath,
|
|
7
|
+
derivationPathToString,
|
|
8
|
+
updateDerivationPath,
|
|
9
|
+
} from "@swapkit/helpers";
|
|
10
|
+
import type { UtxoToolboxParams } from ".";
|
|
13
11
|
import {
|
|
14
12
|
accumulative,
|
|
15
|
-
|
|
13
|
+
UtxoNetwork as bchNetwork,
|
|
16
14
|
compileMemo,
|
|
17
15
|
detectAddressNetwork,
|
|
18
|
-
getNetwork,
|
|
19
16
|
getUtxoApi,
|
|
17
|
+
getUtxoNetwork,
|
|
20
18
|
isValidAddress,
|
|
21
19
|
toCashAddress,
|
|
22
20
|
toLegacyAddress,
|
|
23
21
|
} from "../helpers";
|
|
24
22
|
import type {
|
|
23
|
+
BchECPair,
|
|
25
24
|
TargetOutput,
|
|
26
25
|
TransactionBuilderType,
|
|
27
26
|
TransactionType,
|
|
28
27
|
UTXOBuildTxParams,
|
|
28
|
+
UTXOTransferParams,
|
|
29
29
|
UTXOType,
|
|
30
|
-
UTXOWalletTransferParams,
|
|
31
30
|
} from "../types";
|
|
31
|
+
import { createUTXOToolbox, getCreateKeysForPath } from "./utxo";
|
|
32
|
+
|
|
33
|
+
const chain = Chain.BitcoinCash;
|
|
34
|
+
|
|
35
|
+
export function stripPrefix(address: string) {
|
|
36
|
+
return address.replace(/(bchtest:|bitcoincash:)/, "");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function bchValidateAddress(address: string) {
|
|
40
|
+
const strippedAddress = stripPrefix(address);
|
|
41
|
+
return (
|
|
42
|
+
isValidAddress(strippedAddress) && detectAddressNetwork(strippedAddress) === bchNetwork.Mainnet
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function stripToCashAddress(address: string) {
|
|
47
|
+
return stripPrefix(toCashAddress(address));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function createSignerWithKeys(keys: BchECPair) {
|
|
51
|
+
async function signTransaction({
|
|
52
|
+
builder,
|
|
53
|
+
utxos,
|
|
54
|
+
}: { builder: TransactionBuilderType; utxos: UTXOType[] }) {
|
|
55
|
+
utxos.forEach((utxo, index) => {
|
|
56
|
+
builder.sign(index, keys, undefined, 0x41, utxo.witnessUtxo?.value);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
return builder.build();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const getAddress = () => {
|
|
63
|
+
const address = keys.getAddress(0);
|
|
64
|
+
return Promise.resolve(stripToCashAddress(address));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
getAddress,
|
|
69
|
+
signTransaction,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function createBCHToolbox<T extends Chain.BitcoinCash>(
|
|
74
|
+
toolboxParams:
|
|
75
|
+
| UtxoToolboxParams[T]
|
|
76
|
+
| {
|
|
77
|
+
phrase?: string;
|
|
78
|
+
derivationPath?: DerivationPathArray;
|
|
79
|
+
index?: number;
|
|
80
|
+
},
|
|
81
|
+
) {
|
|
82
|
+
const phrase = "phrase" in toolboxParams ? toolboxParams.phrase : undefined;
|
|
83
|
+
|
|
84
|
+
const index = "index" in toolboxParams ? toolboxParams.index || 0 : 0;
|
|
85
|
+
|
|
86
|
+
const derivationPath = derivationPathToString(
|
|
87
|
+
"derivationPath" in toolboxParams && toolboxParams.derivationPath
|
|
88
|
+
? toolboxParams.derivationPath
|
|
89
|
+
: updateDerivationPath(NetworkDerivationPath[chain], { index }),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const keys = (await getCreateKeysForPath(chain))({ phrase, derivationPath });
|
|
93
|
+
|
|
94
|
+
const signer = keys
|
|
95
|
+
? await createSignerWithKeys(keys)
|
|
96
|
+
: "signer" in toolboxParams
|
|
97
|
+
? toolboxParams.signer
|
|
98
|
+
: undefined;
|
|
99
|
+
|
|
100
|
+
function getAddress() {
|
|
101
|
+
return Promise.resolve(signer?.getAddress());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const { getBalance, getFeeRates, broadcastTx, ...toolbox } = await createUTXOToolbox({ chain });
|
|
105
|
+
|
|
106
|
+
function handleGetBalance(address: string, _scamFilter = true) {
|
|
107
|
+
return getBalance(stripPrefix(toCashAddress(address)));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
...toolbox,
|
|
112
|
+
getAddress,
|
|
113
|
+
broadcastTx,
|
|
114
|
+
createTransaction,
|
|
115
|
+
buildTx,
|
|
116
|
+
getAddressFromKeys,
|
|
117
|
+
getBalance: handleGetBalance,
|
|
118
|
+
getFeeRates,
|
|
119
|
+
stripPrefix,
|
|
120
|
+
stripToCashAddress,
|
|
121
|
+
validateAddress: bchValidateAddress,
|
|
122
|
+
transfer: transfer({ getFeeRates, broadcastTx, signer }),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
32
125
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// needed because TS can not infer types
|
|
36
|
-
type BCHMethods = {
|
|
37
|
-
stripPrefix: (address: string) => string;
|
|
38
|
-
stripToCashAddress: (address: string) => string;
|
|
39
|
-
validateAddress: (address: string, chain?: UTXOChain) => boolean;
|
|
40
|
-
createKeysForPath: (params: {
|
|
41
|
-
wif?: string;
|
|
42
|
-
phrase?: string;
|
|
43
|
-
derivationPath?: string;
|
|
44
|
-
}) => Promise<{ getAddress: (index?: number) => string }>;
|
|
45
|
-
getAddressFromKeys: (keys: { getAddress: (index?: number) => string }) => string;
|
|
46
|
-
buildBCHTx: (
|
|
47
|
-
params: UTXOBuildTxParams,
|
|
48
|
-
) => Promise<{ builder: TransactionBuilderType; utxos: UTXOType[] }>;
|
|
49
|
-
buildTx: (params: UTXOBuildTxParams) => Promise<{ psbt: Psbt }>;
|
|
50
|
-
transfer: (
|
|
51
|
-
params: UTXOWalletTransferParams<
|
|
52
|
-
{ builder: TransactionBuilderType; utxos: UTXOType[] },
|
|
53
|
-
TransactionType
|
|
54
|
-
>,
|
|
55
|
-
) => Promise<string>;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const chain = Chain.BitcoinCash as UTXOChain;
|
|
59
|
-
|
|
60
|
-
export const stripToCashAddress = (address: string) => stripPrefix(toCashAddress(address));
|
|
61
|
-
|
|
62
|
-
const buildBCHTx: BCHMethods["buildBCHTx"] = async ({
|
|
126
|
+
async function createTransaction({
|
|
63
127
|
assetValue,
|
|
64
128
|
recipient,
|
|
65
129
|
memo,
|
|
66
130
|
feeRate,
|
|
67
131
|
sender,
|
|
68
|
-
})
|
|
69
|
-
|
|
132
|
+
}: UTXOBuildTxParams) {
|
|
133
|
+
const {
|
|
134
|
+
Transaction,
|
|
135
|
+
TransactionBuilder,
|
|
136
|
+
address: bchAddress,
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
} = await import("@psf/bitcoincashjs-lib");
|
|
139
|
+
if (!bchValidateAddress(recipient)) throw new Error("Invalid address");
|
|
70
140
|
const utxos = await getUtxoApi(chain).scanUTXOs({
|
|
71
141
|
address: stripToCashAddress(sender),
|
|
72
142
|
fetchTxHex: true,
|
|
73
143
|
});
|
|
74
144
|
|
|
75
|
-
const compiledMemo = memo ? compileMemo(memo) : null;
|
|
145
|
+
const compiledMemo = memo ? await compileMemo(memo) : null;
|
|
76
146
|
|
|
77
147
|
const targetOutputs: TargetOutput[] = [];
|
|
78
148
|
// output to recipient
|
|
79
|
-
targetOutputs.push({
|
|
149
|
+
targetOutputs.push({
|
|
150
|
+
address: recipient,
|
|
151
|
+
value: assetValue.getBaseValue("number"),
|
|
152
|
+
});
|
|
80
153
|
const { inputs, outputs } = accumulative({
|
|
81
154
|
inputs: utxos,
|
|
82
155
|
outputs: targetOutputs,
|
|
@@ -86,8 +159,8 @@ const buildBCHTx: BCHMethods["buildBCHTx"] = async ({
|
|
|
86
159
|
|
|
87
160
|
// .inputs and .outputs will be undefined if no solution was found
|
|
88
161
|
if (!(inputs && outputs)) throw new Error("Balance insufficient for transaction");
|
|
89
|
-
|
|
90
|
-
const builder = new TransactionBuilder(getNetwork(chain));
|
|
162
|
+
const getNetwork = await getUtxoNetwork();
|
|
163
|
+
const builder = new TransactionBuilder(getNetwork(chain)) as TransactionBuilderType;
|
|
91
164
|
|
|
92
165
|
await Promise.all(
|
|
93
166
|
inputs.map(async (utxo: UTXOType) => {
|
|
@@ -100,6 +173,7 @@ const buildBCHTx: BCHMethods["buildBCHTx"] = async ({
|
|
|
100
173
|
for (const output of outputs) {
|
|
101
174
|
const address =
|
|
102
175
|
"address" in output && output.address ? output.address : toLegacyAddress(sender);
|
|
176
|
+
const getNetwork = await getUtxoNetwork();
|
|
103
177
|
const outputScript = bchAddress.toOutputScript(toLegacyAddress(address), getNetwork(chain));
|
|
104
178
|
|
|
105
179
|
builder.addOutput(outputScript, output.value);
|
|
@@ -111,54 +185,50 @@ const buildBCHTx: BCHMethods["buildBCHTx"] = async ({
|
|
|
111
185
|
}
|
|
112
186
|
|
|
113
187
|
return { builder, utxos: inputs };
|
|
114
|
-
}
|
|
188
|
+
}
|
|
115
189
|
|
|
116
|
-
|
|
117
|
-
signTransaction,
|
|
118
|
-
from,
|
|
119
|
-
recipient,
|
|
120
|
-
assetValue,
|
|
190
|
+
function transfer({
|
|
121
191
|
broadcastTx,
|
|
122
192
|
getFeeRates,
|
|
123
|
-
|
|
124
|
-
}:
|
|
125
|
-
{ builder: TransactionBuilderType; utxos: UTXOType[] },
|
|
126
|
-
TransactionType
|
|
127
|
-
> & {
|
|
193
|
+
signer,
|
|
194
|
+
}: {
|
|
128
195
|
broadcastTx: (txHash: string) => Promise<string>;
|
|
129
196
|
getFeeRates: () => Promise<Record<FeeOption, number>>;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (!signTransaction) throw new Error("signTransaction must be provided");
|
|
134
|
-
|
|
135
|
-
const feeRate = rest.feeRate || (await getFeeRates())[FeeOption.Fast];
|
|
136
|
-
|
|
137
|
-
// try out if psbt tx is faster/better/nicer
|
|
138
|
-
const { builder, utxos } = await buildBCHTx({
|
|
139
|
-
...rest,
|
|
140
|
-
assetValue,
|
|
141
|
-
feeRate,
|
|
197
|
+
signer?: ChainSigner<{ builder: TransactionBuilderType; utxos: UTXOType[] }, TransactionType>;
|
|
198
|
+
}) {
|
|
199
|
+
return async function transfer({
|
|
142
200
|
recipient,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
201
|
+
assetValue,
|
|
202
|
+
feeOptionKey = FeeOption.Fast,
|
|
203
|
+
...rest
|
|
204
|
+
}: UTXOTransferParams) {
|
|
205
|
+
const from = await signer?.getAddress();
|
|
206
|
+
if (!(signer && from)) throw new Error("Signer must provider address");
|
|
207
|
+
if (!recipient) throw new Error("Recipient address must be provided");
|
|
208
|
+
|
|
209
|
+
const feeRate = rest.feeRate || (await getFeeRates())[feeOptionKey];
|
|
210
|
+
|
|
211
|
+
// try out if psbt tx is faster/better/nicer
|
|
212
|
+
const { builder, utxos } = await createTransaction({
|
|
213
|
+
...rest,
|
|
214
|
+
assetValue,
|
|
215
|
+
feeRate,
|
|
216
|
+
recipient,
|
|
217
|
+
sender: from,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const tx = await signer.signTransaction({ builder, utxos });
|
|
221
|
+
const txHex = tx.toHex();
|
|
222
|
+
|
|
223
|
+
return broadcastTx(txHex);
|
|
224
|
+
};
|
|
225
|
+
}
|
|
151
226
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
memo,
|
|
156
|
-
feeRate,
|
|
157
|
-
sender,
|
|
158
|
-
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor
|
|
159
|
-
}: UTXOBuildTxParams) => {
|
|
227
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO: refactor
|
|
228
|
+
async function buildTx({ assetValue, recipient, memo, feeRate, sender }: UTXOBuildTxParams) {
|
|
229
|
+
const { Psbt } = (await import("bitcoinjs-lib")).default;
|
|
160
230
|
const recipientCashAddress = toCashAddress(recipient);
|
|
161
|
-
if (!
|
|
231
|
+
if (!bchValidateAddress(recipientCashAddress)) throw new Error("Invalid address");
|
|
162
232
|
|
|
163
233
|
const utxos = await getUtxoApi(chain).scanUTXOs({
|
|
164
234
|
address: stripToCashAddress(sender),
|
|
@@ -166,7 +236,7 @@ const buildTx = async ({
|
|
|
166
236
|
});
|
|
167
237
|
|
|
168
238
|
const feeRateWhole = Number(feeRate.toFixed(0));
|
|
169
|
-
const compiledMemo = memo ? compileMemo(memo) : null;
|
|
239
|
+
const compiledMemo = memo ? await compileMemo(memo) : null;
|
|
170
240
|
|
|
171
241
|
const targetOutputs = [] as TargetOutput[];
|
|
172
242
|
|
|
@@ -190,6 +260,7 @@ const buildTx = async ({
|
|
|
190
260
|
|
|
191
261
|
// .inputs and .outputs will be undefined if no solution was found
|
|
192
262
|
if (!(inputs && outputs)) throw new Error("Balance insufficient for transaction");
|
|
263
|
+
const getNetwork = await getUtxoNetwork();
|
|
193
264
|
const psbt = new Psbt({ network: getNetwork(chain) }); // Network-specific
|
|
194
265
|
|
|
195
266
|
for (const { hash, index, witnessUtxo } of inputs) {
|
|
@@ -212,70 +283,9 @@ const buildTx = async ({
|
|
|
212
283
|
}
|
|
213
284
|
|
|
214
285
|
return { psbt, utxos, inputs: inputs as UTXOType[] };
|
|
215
|
-
}
|
|
286
|
+
}
|
|
216
287
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
export const validateAddress = (address: string) => {
|
|
220
|
-
const strippedAddress = stripPrefix(address);
|
|
221
|
-
return (
|
|
222
|
-
isValidAddress(strippedAddress) && detectAddressNetwork(strippedAddress) === bchNetwork.Mainnet
|
|
223
|
-
);
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
const createKeysForPath: BCHMethods["createKeysForPath"] = async ({
|
|
227
|
-
phrase,
|
|
228
|
-
derivationPath = `${DerivationPath.BCH}/0`,
|
|
229
|
-
wif,
|
|
230
|
-
}) => {
|
|
231
|
-
const { ECPairFactory } = await import("ecpair");
|
|
232
|
-
|
|
233
|
-
const network = getNetwork(chain);
|
|
234
|
-
|
|
235
|
-
if (wif) {
|
|
236
|
-
return ECPairFactory(secp256k1).fromWIF(wif, network);
|
|
237
|
-
}
|
|
238
|
-
if (!phrase) throw new Error("No phrase provided");
|
|
239
|
-
|
|
240
|
-
const masterHDNode = HDNode.fromSeedBuffer(Buffer.from(mnemonicToSeedSync(phrase)), network);
|
|
241
|
-
const keyPair = masterHDNode.derivePath(derivationPath).keyPair;
|
|
242
|
-
// TODO: Figure out same pattern as in BTC
|
|
243
|
-
// const testWif = keyPair.toWIF();
|
|
244
|
-
// const k = ECPairFactory(secp256k1).fromWIF(testWif, network);
|
|
245
|
-
// const a = payments.p2pkh({ pubkey: k.publicKey, network });
|
|
246
|
-
|
|
247
|
-
return keyPair;
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
const getAddressFromKeys = (keys: { getAddress: (index?: number) => string }) => {
|
|
288
|
+
function getAddressFromKeys(keys: { getAddress: (index?: number) => string }) {
|
|
251
289
|
const address = keys.getAddress(0);
|
|
252
290
|
return stripToCashAddress(address);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export const createBCHToolbox = (): Omit<
|
|
256
|
-
ReturnType<typeof BaseUTXOToolbox>,
|
|
257
|
-
"getAddressFromKeys" | "transfer" | "createKeysForPath"
|
|
258
|
-
> &
|
|
259
|
-
BCHMethods => {
|
|
260
|
-
const { getBalance, ...toolbox } = BaseUTXOToolbox(Chain.BitcoinCash);
|
|
261
|
-
|
|
262
|
-
return {
|
|
263
|
-
...toolbox,
|
|
264
|
-
stripPrefix,
|
|
265
|
-
stripToCashAddress,
|
|
266
|
-
validateAddress,
|
|
267
|
-
createKeysForPath,
|
|
268
|
-
getAddressFromKeys,
|
|
269
|
-
buildBCHTx,
|
|
270
|
-
buildTx,
|
|
271
|
-
getBalance: (address: string, _potentialScamFilter?: boolean) =>
|
|
272
|
-
getBalance(stripPrefix(toCashAddress(address))),
|
|
273
|
-
transfer: (
|
|
274
|
-
params: UTXOWalletTransferParams<
|
|
275
|
-
{ builder: TransactionBuilderType; utxos: UTXOType[] },
|
|
276
|
-
TransactionType
|
|
277
|
-
>,
|
|
278
|
-
) =>
|
|
279
|
-
transfer({ ...params, getFeeRates: toolbox.getFeeRates, broadcastTx: toolbox.broadcastTx }),
|
|
280
|
-
};
|
|
281
|
-
};
|
|
291
|
+
}
|
|
@@ -1,37 +1,76 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
Chain,
|
|
3
|
+
type ChainSigner,
|
|
4
|
+
type DerivationPathArray,
|
|
5
|
+
type UTXOChain,
|
|
6
|
+
} from "@swapkit/helpers";
|
|
7
|
+
import type { Psbt } from "bitcoinjs-lib";
|
|
2
8
|
|
|
9
|
+
import type { TransactionBuilderType, TransactionType, UTXOType } from "../types";
|
|
3
10
|
import { createBCHToolbox } from "./bitcoinCash";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
import { createUTXOToolbox } from "./utxo";
|
|
12
|
+
|
|
13
|
+
type BCHToolbox = Awaited<ReturnType<typeof createBCHToolbox>>;
|
|
14
|
+
type CommonUTXOToolbox = Awaited<
|
|
15
|
+
ReturnType<typeof createUTXOToolbox<Exclude<UTXOChain, Chain.BitcoinCash>>>
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
export type UTXOToolboxes = {
|
|
19
|
+
[Chain.BitcoinCash]: BCHToolbox;
|
|
20
|
+
[Chain.Bitcoin]: CommonUTXOToolbox;
|
|
21
|
+
[Chain.Dogecoin]: CommonUTXOToolbox;
|
|
22
|
+
[Chain.Litecoin]: CommonUTXOToolbox;
|
|
23
|
+
[Chain.Dash]: CommonUTXOToolbox;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type UTXOWallets = {
|
|
27
|
+
[key in keyof UTXOToolboxes]: UTXOToolboxes[key];
|
|
12
28
|
};
|
|
13
29
|
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
30
|
+
export type UtxoToolboxParams = {
|
|
31
|
+
[Chain.BitcoinCash]: {
|
|
32
|
+
signer: ChainSigner<{ builder: TransactionBuilderType; utxos: UTXOType[] }, TransactionType>;
|
|
33
|
+
};
|
|
34
|
+
[Chain.Bitcoin]: { signer: ChainSigner<Psbt, Psbt> };
|
|
35
|
+
[Chain.Dogecoin]: { signer: ChainSigner<Psbt, Psbt> };
|
|
36
|
+
[Chain.Litecoin]: { signer: ChainSigner<Psbt, Psbt> };
|
|
37
|
+
[Chain.Dash]: { signer: ChainSigner<Psbt, Psbt> };
|
|
38
|
+
};
|
|
19
39
|
|
|
20
|
-
export
|
|
40
|
+
export async function getUtxoToolbox<T extends keyof UTXOToolboxes>(
|
|
41
|
+
chain: T,
|
|
42
|
+
params?:
|
|
43
|
+
| UtxoToolboxParams[T]
|
|
44
|
+
| {
|
|
45
|
+
phrase?: string;
|
|
46
|
+
derivationPath?: DerivationPathArray;
|
|
47
|
+
index?: number;
|
|
48
|
+
},
|
|
49
|
+
): Promise<UTXOToolboxes[T]> {
|
|
21
50
|
switch (chain) {
|
|
22
|
-
case Chain.BitcoinCash:
|
|
23
|
-
|
|
51
|
+
case Chain.BitcoinCash: {
|
|
52
|
+
const toolbox = await createBCHToolbox(params as UtxoToolboxParams[Chain.BitcoinCash]);
|
|
53
|
+
return toolbox as UTXOToolboxes[T];
|
|
54
|
+
}
|
|
55
|
+
|
|
24
56
|
case Chain.Bitcoin:
|
|
25
|
-
return BTCToolbox as ToolboxType[T];
|
|
26
57
|
case Chain.Dogecoin:
|
|
27
|
-
return DOGEToolbox as ToolboxType[T];
|
|
28
58
|
case Chain.Litecoin:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
59
|
+
case Chain.Dash: {
|
|
60
|
+
const toolbox = await createUTXOToolbox({
|
|
61
|
+
chain,
|
|
62
|
+
...(params as UtxoToolboxParams[Exclude<T, Chain.BitcoinCash>]),
|
|
63
|
+
});
|
|
64
|
+
return toolbox as UTXOToolboxes[Exclude<T, Chain.BitcoinCash>];
|
|
65
|
+
}
|
|
66
|
+
|
|
32
67
|
default:
|
|
33
68
|
throw new Error(`Chain ${chain} is not supported`);
|
|
34
69
|
}
|
|
35
|
-
}
|
|
70
|
+
}
|
|
36
71
|
|
|
37
|
-
export {
|
|
72
|
+
export {
|
|
73
|
+
stripToCashAddress,
|
|
74
|
+
stripPrefix,
|
|
75
|
+
bchValidateAddress,
|
|
76
|
+
} from "./bitcoinCash";
|