@uswap/toolboxes 4.3.7 → 4.3.9
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/cardano/index.cjs +2 -2
- package/dist/src/cardano/index.cjs.map +3 -3
- package/dist/src/cardano/index.js +2 -2
- package/dist/src/cardano/index.js.map +3 -3
- package/dist/src/cosmos/index.cjs +2 -2
- package/dist/src/cosmos/index.cjs.map +8 -8
- package/dist/src/cosmos/index.js +2 -2
- package/dist/src/cosmos/index.js.map +8 -8
- package/dist/src/evm/index.cjs +2 -2
- package/dist/src/evm/index.cjs.map +7 -7
- package/dist/src/evm/index.js +2 -2
- package/dist/src/evm/index.js.map +7 -7
- package/dist/src/index.cjs +3 -3
- package/dist/src/index.cjs.map +34 -34
- package/dist/src/index.js +3 -3
- package/dist/src/index.js.map +34 -34
- package/dist/src/near/index.cjs +2 -2
- package/dist/src/near/index.cjs.map +5 -5
- package/dist/src/near/index.js +2 -2
- package/dist/src/near/index.js.map +5 -5
- package/dist/src/radix/index.cjs +2 -2
- package/dist/src/radix/index.cjs.map +3 -3
- package/dist/src/radix/index.js +2 -2
- package/dist/src/radix/index.js.map +3 -3
- package/dist/src/ripple/index.cjs +2 -2
- package/dist/src/ripple/index.cjs.map +3 -3
- package/dist/src/ripple/index.js +2 -2
- package/dist/src/ripple/index.js.map +3 -3
- package/dist/src/solana/index.cjs +2 -2
- package/dist/src/solana/index.cjs.map +3 -3
- package/dist/src/solana/index.js +2 -2
- package/dist/src/solana/index.js.map +3 -3
- package/dist/src/substrate/index.cjs +2 -2
- package/dist/src/substrate/index.cjs.map +4 -4
- package/dist/src/substrate/index.js +2 -2
- package/dist/src/substrate/index.js.map +4 -4
- package/dist/src/sui/index.cjs +2 -2
- package/dist/src/sui/index.cjs.map +3 -3
- package/dist/src/sui/index.js +2 -2
- package/dist/src/sui/index.js.map +3 -3
- package/dist/src/ton/index.cjs +2 -2
- package/dist/src/ton/index.cjs.map +3 -3
- package/dist/src/ton/index.js +2 -2
- package/dist/src/ton/index.js.map +3 -3
- package/dist/src/tron/index.cjs +2 -2
- package/dist/src/tron/index.cjs.map +4 -4
- package/dist/src/tron/index.js +2 -2
- package/dist/src/tron/index.js.map +4 -4
- package/dist/src/utxo/index.cjs +3 -3
- package/dist/src/utxo/index.cjs.map +11 -11
- package/dist/src/utxo/index.js +3 -3
- package/dist/src/utxo/index.js.map +11 -11
- package/dist/types/cardano/toolbox.d.ts +5 -0
- package/dist/types/cardano/toolbox.d.ts.map +1 -1
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -1
- package/dist/types/cosmos/thorchainUtils/messages.d.ts +5 -0
- package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -1
- package/dist/types/cosmos/toolbox/cosmos.d.ts +5 -0
- package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -1
- package/dist/types/cosmos/toolbox/index.d.ts +5 -0
- package/dist/types/cosmos/toolbox/index.d.ts.map +1 -1
- package/dist/types/cosmos/toolbox/thorchain.d.ts +5 -0
- package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -1
- package/dist/types/cosmos/util.d.ts +5 -0
- package/dist/types/cosmos/util.d.ts.map +1 -1
- package/dist/types/evm/helpers.d.ts +1 -1
- package/dist/types/evm/helpers.d.ts.map +1 -1
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +7 -0
- package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -1
- package/dist/types/evm/toolbox/evm.d.ts +38 -38
- package/dist/types/evm/toolbox/evm.d.ts.map +1 -1
- package/dist/types/evm/toolbox/op.d.ts +7 -14
- package/dist/types/evm/toolbox/op.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/near/helpers/core.d.ts +5 -0
- package/dist/types/near/helpers/core.d.ts.map +1 -1
- package/dist/types/near/toolbox.d.ts +5 -0
- package/dist/types/near/toolbox.d.ts.map +1 -1
- package/dist/types/radix/index.d.ts +5 -0
- package/dist/types/radix/index.d.ts.map +1 -1
- package/dist/types/ripple/index.d.ts +5 -0
- package/dist/types/ripple/index.d.ts.map +1 -1
- package/dist/types/solana/toolbox.d.ts +5 -0
- package/dist/types/solana/toolbox.d.ts.map +1 -1
- package/dist/types/substrate/substrate.d.ts +5 -0
- package/dist/types/substrate/substrate.d.ts.map +1 -1
- package/dist/types/sui/toolbox.d.ts +5 -0
- package/dist/types/sui/toolbox.d.ts.map +1 -1
- package/dist/types/ton/toolbox.d.ts +5 -0
- package/dist/types/ton/toolbox.d.ts.map +1 -1
- package/dist/types/tron/helpers/trongrid.d.ts +5 -0
- package/dist/types/tron/helpers/trongrid.d.ts.map +1 -1
- package/dist/types/tron/toolbox.d.ts +5 -0
- package/dist/types/tron/toolbox.d.ts.map +1 -1
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/types/utxo/helpers/api.d.ts +5 -0
- package/dist/types/utxo/helpers/api.d.ts.map +1 -1
- package/dist/types/utxo/helpers/bchaddrjs.d.ts +5 -0
- package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -1
- package/dist/types/utxo/helpers/coinselect.d.ts +5 -0
- package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -1
- package/dist/types/utxo/helpers/txSize.d.ts +5 -0
- package/dist/types/utxo/helpers/txSize.d.ts.map +1 -1
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts +5 -0
- package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -1
- package/dist/types/utxo/toolbox/index.d.ts +5 -0
- package/dist/types/utxo/toolbox/index.d.ts.map +1 -1
- package/dist/types/utxo/toolbox/utxo.d.ts +5 -0
- package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -1
- package/dist/types/utxo/toolbox/zcash.d.ts +6 -1
- package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/cardano/toolbox.ts +12 -6
- package/src/cosmos/__tests__/toolbox.test.ts +8 -4
- package/src/cosmos/thorchainUtils/addressFormat.ts +8 -2
- package/src/cosmos/thorchainUtils/messages.ts +8 -2
- package/src/cosmos/toolbox/cosmos.ts +11 -5
- package/src/cosmos/toolbox/index.ts +8 -2
- package/src/cosmos/toolbox/thorchain.ts +9 -3
- package/src/cosmos/util.ts +9 -3
- package/src/evm/helpers.ts +7 -9
- package/src/evm/toolbox/baseEVMToolbox.ts +34 -26
- package/src/evm/toolbox/evm.ts +2 -3
- package/src/evm/toolbox/op.ts +10 -17
- package/src/index.ts +8 -2
- package/src/near/helpers/core.ts +8 -2
- package/src/near/toolbox.ts +21 -15
- package/src/radix/index.ts +15 -3
- package/src/ripple/index.ts +15 -12
- package/src/solana/toolbox.ts +16 -16
- package/src/substrate/balance.ts +2 -2
- package/src/substrate/substrate.ts +17 -11
- package/src/sui/__tests__/toolbox.test.ts +9 -5
- package/src/sui/toolbox.ts +18 -11
- package/src/ton/__tests__/toolbox.test.ts +9 -5
- package/src/ton/toolbox.ts +14 -8
- package/src/tron/helpers/trongrid.ts +8 -2
- package/src/tron/toolbox.ts +21 -15
- package/src/utils.ts +8 -1
- package/src/utxo/helpers/api.ts +15 -9
- package/src/utxo/helpers/bchaddrjs.ts +9 -3
- package/src/utxo/helpers/coinselect.ts +8 -2
- package/src/utxo/helpers/txSize.ts +8 -2
- package/src/utxo/toolbox/bitcoinCash.ts +13 -7
- package/src/utxo/toolbox/index.ts +8 -2
- package/src/utxo/toolbox/utxo.ts +16 -10
- package/src/utxo/toolbox/zcash.ts +24 -21
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { USwapError } from "@uswap/helpers";
|
|
2
8
|
import base58check from "bs58check";
|
|
3
9
|
// @ts-expect-error
|
|
4
10
|
import cashaddr from "cashaddrjs";
|
|
@@ -73,7 +79,7 @@ function decodeAddress(address: string) {
|
|
|
73
79
|
} catch {
|
|
74
80
|
// Try to decode as bitpay if cashaddr decoding fails.
|
|
75
81
|
}
|
|
76
|
-
throw new
|
|
82
|
+
throw new USwapError("toolbox_utxo_invalid_address", { address });
|
|
77
83
|
}
|
|
78
84
|
|
|
79
85
|
function decodeBase58Address(address: string) {
|
|
@@ -81,7 +87,7 @@ function decodeBase58Address(address: string) {
|
|
|
81
87
|
const payload = base58check.decode(address);
|
|
82
88
|
|
|
83
89
|
// BASE_58_CHECK_PAYLOAD_LENGTH
|
|
84
|
-
if (payload.length !== 21) throw new
|
|
90
|
+
if (payload.length !== 21) throw new USwapError("toolbox_utxo_invalid_address", { address });
|
|
85
91
|
const versionByte = payload[0];
|
|
86
92
|
const hash = Array.prototype.slice.call(payload, 1);
|
|
87
93
|
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Chain, USwapError, type UTXOChain } from "@uswap/helpers";
|
|
2
8
|
|
|
3
9
|
import {
|
|
4
10
|
calculateTxSize,
|
|
@@ -23,7 +29,7 @@ export function getDustThreshold(chain: UTXOChain) {
|
|
|
23
29
|
case Chain.Zcash:
|
|
24
30
|
return 546;
|
|
25
31
|
default:
|
|
26
|
-
throw new
|
|
32
|
+
throw new USwapError("toolbox_utxo_not_supported", { chain });
|
|
27
33
|
}
|
|
28
34
|
}
|
|
29
35
|
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { USwapError } from "@uswap/helpers";
|
|
2
8
|
import { opcodes, script } from "bitcoinjs-lib";
|
|
3
9
|
import type { TargetOutput, UTXOCalculateTxSizeParams, UTXOInputWithScriptType, UTXOType } from "../types";
|
|
4
10
|
|
|
@@ -99,7 +105,7 @@ export const getScriptTypeForAddress = (address: string) => {
|
|
|
99
105
|
return UTXOScriptType.P2PKH;
|
|
100
106
|
}
|
|
101
107
|
|
|
102
|
-
throw new
|
|
108
|
+
throw new USwapError("toolbox_utxo_invalid_address", { address });
|
|
103
109
|
};
|
|
104
110
|
|
|
105
111
|
export const calculateTxSize = ({ inputs, outputs, feeRate }: UTXOCalculateTxSizeParams) => {
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import {
|
|
2
8
|
address as bchAddress,
|
|
3
9
|
Transaction,
|
|
@@ -11,7 +17,7 @@ import {
|
|
|
11
17
|
derivationPathToString,
|
|
12
18
|
FeeOption,
|
|
13
19
|
NetworkDerivationPath,
|
|
14
|
-
|
|
20
|
+
USwapError,
|
|
15
21
|
updateDerivationPath,
|
|
16
22
|
} from "@uswap/helpers";
|
|
17
23
|
import { Psbt } from "bitcoinjs-lib";
|
|
@@ -96,7 +102,7 @@ export async function createBCHToolbox<T extends typeof Chain.BitcoinCash>(
|
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
async function createTransaction({ assetValue, recipient, memo, feeRate, sender }: UTXOBuildTxParams) {
|
|
99
|
-
if (!bchValidateAddress(recipient)) throw new
|
|
105
|
+
if (!bchValidateAddress(recipient)) throw new USwapError("toolbox_utxo_invalid_address", { address: recipient });
|
|
100
106
|
|
|
101
107
|
// Overestimate by 7500 byte * feeRate to ensure we have enough UTXOs for fees and change
|
|
102
108
|
const targetValue = Math.ceil(assetValue.getBaseValue("number") + feeRate * 7500);
|
|
@@ -115,7 +121,7 @@ async function createTransaction({ assetValue, recipient, memo, feeRate, sender
|
|
|
115
121
|
const { inputs, outputs } = accumulative({ chain, feeRate, inputs: utxos, outputs: targetOutputs });
|
|
116
122
|
|
|
117
123
|
// .inputs and .outputs will be undefined if no solution was found
|
|
118
|
-
if (!(inputs && outputs)) throw new
|
|
124
|
+
if (!(inputs && outputs)) throw new USwapError("toolbox_utxo_insufficient_balance", { assetValue, sender });
|
|
119
125
|
const getNetwork = await getUtxoNetwork();
|
|
120
126
|
const builder = new TransactionBuilder(getNetwork(chain)) as TransactionBuilderType;
|
|
121
127
|
|
|
@@ -159,9 +165,9 @@ function transfer({
|
|
|
159
165
|
...rest
|
|
160
166
|
}: UTXOTransferParams) {
|
|
161
167
|
const from = await signer?.getAddress();
|
|
162
|
-
if (!(signer && from)) throw new
|
|
168
|
+
if (!(signer && from)) throw new USwapError("toolbox_utxo_no_signer");
|
|
163
169
|
if (!recipient)
|
|
164
|
-
throw new
|
|
170
|
+
throw new USwapError("toolbox_utxo_invalid_params", { error: "Recipient address must be provided" });
|
|
165
171
|
|
|
166
172
|
const feeRate = rest.feeRate || (await getFeeRates())[feeOptionKey];
|
|
167
173
|
|
|
@@ -185,7 +191,7 @@ async function buildTx({
|
|
|
185
191
|
}: UTXOBuildTxParams & { setSigHashType?: boolean }) {
|
|
186
192
|
const recipientCashAddress = toCashAddress(recipient);
|
|
187
193
|
if (!bchValidateAddress(recipientCashAddress))
|
|
188
|
-
throw new
|
|
194
|
+
throw new USwapError("toolbox_utxo_invalid_address", { address: recipientCashAddress });
|
|
189
195
|
|
|
190
196
|
// Overestimate by 7500 byte * feeRate to ensure we have enough UTXOs for fees and change
|
|
191
197
|
const targetValue = Math.ceil(assetValue.getBaseValue("number") + feeRate * 7500);
|
|
@@ -212,7 +218,7 @@ async function buildTx({
|
|
|
212
218
|
const { inputs, outputs } = accumulative({ chain, feeRate: feeRateWhole, inputs: utxos, outputs: targetOutputs });
|
|
213
219
|
|
|
214
220
|
// .inputs and .outputs will be undefined if no solution was found
|
|
215
|
-
if (!(inputs && outputs)) throw new
|
|
221
|
+
if (!(inputs && outputs)) throw new USwapError("toolbox_utxo_insufficient_balance", { assetValue, sender });
|
|
216
222
|
const getNetwork = await getUtxoNetwork();
|
|
217
223
|
const psbt = new Psbt({ network: getNetwork(chain) }); // Network-specific
|
|
218
224
|
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Chain, type DerivationPathArray, USwapError, type UTXOChain } from "@uswap/helpers";
|
|
2
8
|
import { createBCHToolbox } from "./bitcoinCash";
|
|
3
9
|
import type { UtxoToolboxParams } from "./params";
|
|
4
10
|
import { createUTXOToolbox } from "./utxo";
|
|
@@ -50,7 +56,7 @@ export async function getUtxoToolbox<T extends keyof UTXOToolboxes>(
|
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
default:
|
|
53
|
-
throw new
|
|
59
|
+
throw new USwapError("toolbox_utxo_not_supported", { chain });
|
|
54
60
|
}
|
|
55
61
|
}
|
|
56
62
|
|
package/src/utxo/toolbox/utxo.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import secp256k1 from "@bitcoinerlab/secp256k1";
|
|
2
8
|
// @ts-expect-error
|
|
3
9
|
import { ECPair, HDNode } from "@psf/bitcoincashjs-lib";
|
|
@@ -13,8 +19,8 @@ import {
|
|
|
13
19
|
derivationPathToString,
|
|
14
20
|
FeeOption,
|
|
15
21
|
NetworkDerivationPath,
|
|
16
|
-
SwapKitError,
|
|
17
22
|
SwapKitNumber,
|
|
23
|
+
USwapError,
|
|
18
24
|
type UTXOChain,
|
|
19
25
|
updateDerivationPath,
|
|
20
26
|
} from "@uswap/helpers";
|
|
@@ -98,7 +104,7 @@ async function createTransaction({
|
|
|
98
104
|
const { inputs, outputs } = accumulative({ ...inputsAndOutputs, chain, feeRate });
|
|
99
105
|
|
|
100
106
|
// .inputs and .outputs will be undefined if no solution was found
|
|
101
|
-
if (!(inputs && outputs)) throw new
|
|
107
|
+
if (!(inputs && outputs)) throw new USwapError("toolbox_utxo_insufficient_balance", { assetValue, sender });
|
|
102
108
|
const getNetwork = await getUtxoNetwork();
|
|
103
109
|
const psbt = new Psbt({ network: getNetwork(chain) });
|
|
104
110
|
|
|
@@ -324,7 +330,7 @@ export async function getCreateKeysForPath<T extends keyof CreateKeysForPathRetu
|
|
|
324
330
|
if (wif) {
|
|
325
331
|
return ECPair.fromWIF(wif, network) as BchECPair;
|
|
326
332
|
}
|
|
327
|
-
if (!phrase) throw new
|
|
333
|
+
if (!phrase) throw new USwapError("toolbox_utxo_invalid_params", { error: "No phrase provided" });
|
|
328
334
|
|
|
329
335
|
const masterHDNode = HDNode.fromSeedBuffer(Buffer.from(mnemonicToSeedSync(phrase)), network);
|
|
330
336
|
const keyPair = masterHDNode.derivePath(derivationPath).keyPair;
|
|
@@ -347,7 +353,7 @@ export async function getCreateKeysForPath<T extends keyof CreateKeysForPathRetu
|
|
|
347
353
|
derivationPath: string;
|
|
348
354
|
}) {
|
|
349
355
|
if (!(wif || phrase))
|
|
350
|
-
throw new
|
|
356
|
+
throw new USwapError("toolbox_utxo_invalid_params", { error: "Either phrase or wif must be provided" });
|
|
351
357
|
|
|
352
358
|
const factory = ECPairFactory(secp256k1);
|
|
353
359
|
const network = getNetwork(chain);
|
|
@@ -357,13 +363,13 @@ export async function getCreateKeysForPath<T extends keyof CreateKeysForPathRetu
|
|
|
357
363
|
const seed = mnemonicToSeedSync(phrase as string);
|
|
358
364
|
const master = HDKey.fromMasterSeed(seed, network).derive(derivationPath);
|
|
359
365
|
if (!master.privateKey)
|
|
360
|
-
throw new
|
|
366
|
+
throw new USwapError("toolbox_utxo_invalid_params", { error: "Could not get private key from phrase" });
|
|
361
367
|
|
|
362
368
|
return factory.fromPrivateKey(Buffer.from(master.privateKey), { network });
|
|
363
369
|
} as (params: { wif?: string; phrase?: string; derivationPath?: string }) => CreateKeysForPathReturnType[T];
|
|
364
370
|
}
|
|
365
371
|
default:
|
|
366
|
-
throw new
|
|
372
|
+
throw new USwapError("toolbox_utxo_not_supported", { chain });
|
|
367
373
|
}
|
|
368
374
|
}
|
|
369
375
|
|
|
@@ -371,11 +377,11 @@ export function addressFromKeysGetter(chain: UTXOChain) {
|
|
|
371
377
|
const getNetwork = getUtxoNetwork();
|
|
372
378
|
|
|
373
379
|
return function getAddressFromKeys(keys: ECPairInterface | BchECPair) {
|
|
374
|
-
if (!keys) throw new
|
|
380
|
+
if (!keys) throw new USwapError("toolbox_utxo_invalid_params", { error: "Keys must be provided" });
|
|
375
381
|
|
|
376
382
|
const method = nonSegwitChains.includes(chain) ? payments.p2pkh : payments.p2wpkh;
|
|
377
383
|
const { address } = method({ network: getNetwork(chain), pubkey: keys.publicKey as Buffer });
|
|
378
|
-
if (!address) throw new
|
|
384
|
+
if (!address) throw new USwapError("toolbox_utxo_invalid_address", { error: "Address not defined" });
|
|
379
385
|
|
|
380
386
|
return address;
|
|
381
387
|
};
|
|
@@ -387,9 +393,9 @@ function transfer(signer?: ChainSigner<Psbt, Psbt>) {
|
|
|
387
393
|
|
|
388
394
|
const chain = assetValue.chain as UTXOChain;
|
|
389
395
|
|
|
390
|
-
if (!(signer && from)) throw new
|
|
396
|
+
if (!(signer && from)) throw new USwapError("toolbox_utxo_no_signer");
|
|
391
397
|
if (!recipient)
|
|
392
|
-
throw new
|
|
398
|
+
throw new USwapError("toolbox_utxo_invalid_params", { error: "Recipient address must be provided" });
|
|
393
399
|
const txFeeRate = feeRate || (await getFeeRates(chain))[feeOptionKey || FeeOption.Fast];
|
|
394
400
|
|
|
395
401
|
const { psbt } = await createTransaction({ assetValue, feeRate: txFeeRate, memo, recipient, sender: from });
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on code from SwapKit (https://github.com/swapkit/SwapKit),
|
|
3
|
+
* licensed under the Apache License 2.0.
|
|
4
|
+
* Modifications © 2025 Horizontal Systems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import { bitgo, crypto, ECPair, networks, address as zcashAddress } from "@bitgo/utxo-lib";
|
|
2
|
-
import type
|
|
8
|
+
import { type ZcashPsbt, ZcashTransaction } from "@bitgo/utxo-lib/dist/src/bitgo";
|
|
3
9
|
import { HDKey } from "@scure/bip32";
|
|
4
10
|
import { mnemonicToSeedSync } from "@scure/bip39";
|
|
5
11
|
import {
|
|
@@ -10,7 +16,7 @@ import {
|
|
|
10
16
|
FeeOption,
|
|
11
17
|
NetworkDerivationPath,
|
|
12
18
|
SKConfig,
|
|
13
|
-
|
|
19
|
+
USwapError,
|
|
14
20
|
updateDerivationPath,
|
|
15
21
|
} from "@uswap/helpers";
|
|
16
22
|
import bs58check from "bs58check";
|
|
@@ -49,7 +55,7 @@ function createZcashSignerFromPhrase({
|
|
|
49
55
|
const node = root.derive(derivationPath);
|
|
50
56
|
|
|
51
57
|
if (!node.privateKey) {
|
|
52
|
-
throw new
|
|
58
|
+
throw new USwapError("toolbox_utxo_invalid_params");
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
// Create key pair using BitGo's ECPair with ECPair-compatible network
|
|
@@ -140,23 +146,13 @@ async function createTransaction(buildTxParams: UTXOBuildTxParams) {
|
|
|
140
146
|
});
|
|
141
147
|
|
|
142
148
|
if (!(inputs && outputs)) {
|
|
143
|
-
throw new
|
|
149
|
+
throw new USwapError("toolbox_utxo_insufficient_balance", { assetValue, sender });
|
|
144
150
|
}
|
|
145
151
|
|
|
146
|
-
const psbt = bitgo.createPsbtForNetwork(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
// const branchId = tipHeight >= 2726400 ? NU6 : tipHeight >= 1687104 ? NU5 : NU5;
|
|
151
|
-
const branchId = NU5;
|
|
152
|
-
|
|
153
|
-
const CONSENSUS_BRANCH_ID_KEY = Buffer.concat([Buffer.of(0xfc), Buffer.of(0x05), Buffer.from("BITGO"), Buffer.of(0)]);
|
|
154
|
-
|
|
155
|
-
// PSBT value must be 4-byte little-endian
|
|
156
|
-
const value = Buffer.allocUnsafe(4);
|
|
157
|
-
value.writeUInt32LE(branchId >>> 0, 0);
|
|
158
|
-
|
|
159
|
-
psbt.addUnknownKeyValToGlobal({ key: CONSENSUS_BRANCH_ID_KEY, value });
|
|
152
|
+
const psbt = bitgo.createPsbtForNetwork(
|
|
153
|
+
{ network: getZcashNetwork() },
|
|
154
|
+
{ version: ZcashTransaction.VERSION4_BRANCH_NU6_1 },
|
|
155
|
+
) as ZcashPsbt;
|
|
160
156
|
|
|
161
157
|
const { psbt: mappedPsbt, inputs: mappedInputs } = await addInputsAndOutputs({
|
|
162
158
|
compiledMemo,
|
|
@@ -166,6 +162,8 @@ async function createTransaction(buildTxParams: UTXOBuildTxParams) {
|
|
|
166
162
|
sender,
|
|
167
163
|
});
|
|
168
164
|
|
|
165
|
+
mappedPsbt.setDefaultsForVersion(getZcashNetwork(), ZcashTransaction.VERSION4_BRANCH_NU6_1);
|
|
166
|
+
|
|
169
167
|
return { inputs: mappedInputs, outputs, psbt: mappedPsbt };
|
|
170
168
|
}
|
|
171
169
|
|
|
@@ -188,7 +186,7 @@ export async function createZcashToolbox(
|
|
|
188
186
|
async function transfer({ recipient, assetValue, feeOptionKey = FeeOption.Fast, ...rest }: UTXOTransferParams) {
|
|
189
187
|
const from = await signer?.getAddress();
|
|
190
188
|
if (!(signer && from)) {
|
|
191
|
-
throw new
|
|
189
|
+
throw new USwapError("toolbox_utxo_no_signer");
|
|
192
190
|
}
|
|
193
191
|
|
|
194
192
|
const feeRate = rest.feeRate || (await baseToolbox.getFeeRates())[feeOptionKey];
|
|
@@ -197,9 +195,14 @@ export async function createZcashToolbox(
|
|
|
197
195
|
|
|
198
196
|
const signedPsbt = await signer.signTransaction(psbt);
|
|
199
197
|
|
|
198
|
+
signedPsbt.validateSignaturesOfAllInputs();
|
|
199
|
+
|
|
200
200
|
signedPsbt.finalizeAllInputs();
|
|
201
201
|
|
|
202
|
-
|
|
202
|
+
const finalTx = signedPsbt.extractTransaction();
|
|
203
|
+
const txHex = finalTx.toHex();
|
|
204
|
+
|
|
205
|
+
return baseToolbox.broadcastTx(txHex);
|
|
203
206
|
}
|
|
204
207
|
|
|
205
208
|
function createKeysForPath({
|
|
@@ -214,7 +217,7 @@ export async function createZcashToolbox(
|
|
|
214
217
|
const node = root.derive(derivationPath);
|
|
215
218
|
|
|
216
219
|
if (!node.privateKey) {
|
|
217
|
-
throw new
|
|
220
|
+
throw new USwapError("toolbox_utxo_invalid_params");
|
|
218
221
|
}
|
|
219
222
|
|
|
220
223
|
const ecpairNetwork = getECPairNetwork();
|