@uswap/toolboxes 4.3.6 → 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.
Files changed (149) hide show
  1. package/dist/src/cardano/index.cjs +2 -2
  2. package/dist/src/cardano/index.cjs.map +3 -3
  3. package/dist/src/cardano/index.js +2 -2
  4. package/dist/src/cardano/index.js.map +3 -3
  5. package/dist/src/cosmos/index.cjs +2 -2
  6. package/dist/src/cosmos/index.cjs.map +8 -8
  7. package/dist/src/cosmos/index.js +2 -2
  8. package/dist/src/cosmos/index.js.map +8 -8
  9. package/dist/src/evm/index.cjs +2 -2
  10. package/dist/src/evm/index.cjs.map +7 -7
  11. package/dist/src/evm/index.js +2 -2
  12. package/dist/src/evm/index.js.map +7 -7
  13. package/dist/src/index.cjs +3 -3
  14. package/dist/src/index.cjs.map +34 -34
  15. package/dist/src/index.js +3 -3
  16. package/dist/src/index.js.map +34 -34
  17. package/dist/src/near/index.cjs +2 -2
  18. package/dist/src/near/index.cjs.map +5 -5
  19. package/dist/src/near/index.js +2 -2
  20. package/dist/src/near/index.js.map +5 -5
  21. package/dist/src/radix/index.cjs +2 -2
  22. package/dist/src/radix/index.cjs.map +3 -3
  23. package/dist/src/radix/index.js +2 -2
  24. package/dist/src/radix/index.js.map +3 -3
  25. package/dist/src/ripple/index.cjs +2 -2
  26. package/dist/src/ripple/index.cjs.map +3 -3
  27. package/dist/src/ripple/index.js +2 -2
  28. package/dist/src/ripple/index.js.map +3 -3
  29. package/dist/src/solana/index.cjs +2 -2
  30. package/dist/src/solana/index.cjs.map +3 -3
  31. package/dist/src/solana/index.js +2 -2
  32. package/dist/src/solana/index.js.map +3 -3
  33. package/dist/src/substrate/index.cjs +2 -2
  34. package/dist/src/substrate/index.cjs.map +4 -4
  35. package/dist/src/substrate/index.js +2 -2
  36. package/dist/src/substrate/index.js.map +4 -4
  37. package/dist/src/sui/index.cjs +2 -2
  38. package/dist/src/sui/index.cjs.map +3 -3
  39. package/dist/src/sui/index.js +2 -2
  40. package/dist/src/sui/index.js.map +3 -3
  41. package/dist/src/ton/index.cjs +2 -2
  42. package/dist/src/ton/index.cjs.map +3 -3
  43. package/dist/src/ton/index.js +2 -2
  44. package/dist/src/ton/index.js.map +3 -3
  45. package/dist/src/tron/index.cjs +2 -2
  46. package/dist/src/tron/index.cjs.map +4 -4
  47. package/dist/src/tron/index.js +2 -2
  48. package/dist/src/tron/index.js.map +4 -4
  49. package/dist/src/utxo/index.cjs +3 -3
  50. package/dist/src/utxo/index.cjs.map +11 -11
  51. package/dist/src/utxo/index.js +3 -3
  52. package/dist/src/utxo/index.js.map +11 -11
  53. package/dist/types/cardano/toolbox.d.ts +5 -0
  54. package/dist/types/cardano/toolbox.d.ts.map +1 -1
  55. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts +5 -0
  56. package/dist/types/cosmos/thorchainUtils/addressFormat.d.ts.map +1 -1
  57. package/dist/types/cosmos/thorchainUtils/messages.d.ts +5 -0
  58. package/dist/types/cosmos/thorchainUtils/messages.d.ts.map +1 -1
  59. package/dist/types/cosmos/toolbox/cosmos.d.ts +5 -0
  60. package/dist/types/cosmos/toolbox/cosmos.d.ts.map +1 -1
  61. package/dist/types/cosmos/toolbox/index.d.ts +5 -0
  62. package/dist/types/cosmos/toolbox/index.d.ts.map +1 -1
  63. package/dist/types/cosmos/toolbox/thorchain.d.ts +5 -0
  64. package/dist/types/cosmos/toolbox/thorchain.d.ts.map +1 -1
  65. package/dist/types/cosmos/util.d.ts +5 -0
  66. package/dist/types/cosmos/util.d.ts.map +1 -1
  67. package/dist/types/evm/helpers.d.ts +1 -1
  68. package/dist/types/evm/helpers.d.ts.map +1 -1
  69. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +7 -0
  70. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -1
  71. package/dist/types/evm/toolbox/evm.d.ts +38 -38
  72. package/dist/types/evm/toolbox/evm.d.ts.map +1 -1
  73. package/dist/types/evm/toolbox/op.d.ts +7 -14
  74. package/dist/types/evm/toolbox/op.d.ts.map +1 -1
  75. package/dist/types/index.d.ts +5 -0
  76. package/dist/types/index.d.ts.map +1 -1
  77. package/dist/types/near/helpers/core.d.ts +5 -0
  78. package/dist/types/near/helpers/core.d.ts.map +1 -1
  79. package/dist/types/near/toolbox.d.ts +5 -0
  80. package/dist/types/near/toolbox.d.ts.map +1 -1
  81. package/dist/types/radix/index.d.ts +5 -0
  82. package/dist/types/radix/index.d.ts.map +1 -1
  83. package/dist/types/ripple/index.d.ts +5 -0
  84. package/dist/types/ripple/index.d.ts.map +1 -1
  85. package/dist/types/solana/toolbox.d.ts +5 -0
  86. package/dist/types/solana/toolbox.d.ts.map +1 -1
  87. package/dist/types/substrate/substrate.d.ts +5 -0
  88. package/dist/types/substrate/substrate.d.ts.map +1 -1
  89. package/dist/types/sui/toolbox.d.ts +5 -0
  90. package/dist/types/sui/toolbox.d.ts.map +1 -1
  91. package/dist/types/ton/toolbox.d.ts +5 -0
  92. package/dist/types/ton/toolbox.d.ts.map +1 -1
  93. package/dist/types/tron/helpers/trongrid.d.ts +5 -0
  94. package/dist/types/tron/helpers/trongrid.d.ts.map +1 -1
  95. package/dist/types/tron/toolbox.d.ts +5 -0
  96. package/dist/types/tron/toolbox.d.ts.map +1 -1
  97. package/dist/types/utils.d.ts.map +1 -1
  98. package/dist/types/utxo/helpers/api.d.ts +5 -0
  99. package/dist/types/utxo/helpers/api.d.ts.map +1 -1
  100. package/dist/types/utxo/helpers/bchaddrjs.d.ts +5 -0
  101. package/dist/types/utxo/helpers/bchaddrjs.d.ts.map +1 -1
  102. package/dist/types/utxo/helpers/coinselect.d.ts +5 -0
  103. package/dist/types/utxo/helpers/coinselect.d.ts.map +1 -1
  104. package/dist/types/utxo/helpers/txSize.d.ts +5 -0
  105. package/dist/types/utxo/helpers/txSize.d.ts.map +1 -1
  106. package/dist/types/utxo/toolbox/bitcoinCash.d.ts +5 -0
  107. package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -1
  108. package/dist/types/utxo/toolbox/index.d.ts +5 -0
  109. package/dist/types/utxo/toolbox/index.d.ts.map +1 -1
  110. package/dist/types/utxo/toolbox/utxo.d.ts +5 -0
  111. package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -1
  112. package/dist/types/utxo/toolbox/zcash.d.ts +6 -1
  113. package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -1
  114. package/package.json +4 -4
  115. package/src/cardano/toolbox.ts +12 -6
  116. package/src/cosmos/__tests__/toolbox.test.ts +8 -4
  117. package/src/cosmos/thorchainUtils/addressFormat.ts +8 -2
  118. package/src/cosmos/thorchainUtils/messages.ts +8 -2
  119. package/src/cosmos/toolbox/cosmos.ts +11 -5
  120. package/src/cosmos/toolbox/index.ts +8 -2
  121. package/src/cosmos/toolbox/thorchain.ts +9 -3
  122. package/src/cosmos/util.ts +9 -3
  123. package/src/evm/helpers.ts +7 -9
  124. package/src/evm/toolbox/baseEVMToolbox.ts +34 -26
  125. package/src/evm/toolbox/evm.ts +2 -3
  126. package/src/evm/toolbox/op.ts +10 -17
  127. package/src/index.ts +8 -2
  128. package/src/near/helpers/core.ts +8 -2
  129. package/src/near/toolbox.ts +21 -15
  130. package/src/radix/index.ts +15 -3
  131. package/src/ripple/index.ts +15 -12
  132. package/src/solana/toolbox.ts +16 -16
  133. package/src/substrate/balance.ts +2 -2
  134. package/src/substrate/substrate.ts +17 -11
  135. package/src/sui/__tests__/toolbox.test.ts +9 -5
  136. package/src/sui/toolbox.ts +18 -11
  137. package/src/ton/__tests__/toolbox.test.ts +9 -5
  138. package/src/ton/toolbox.ts +14 -8
  139. package/src/tron/helpers/trongrid.ts +8 -2
  140. package/src/tron/toolbox.ts +21 -15
  141. package/src/utils.ts +8 -1
  142. package/src/utxo/helpers/api.ts +15 -9
  143. package/src/utxo/helpers/bchaddrjs.ts +9 -3
  144. package/src/utxo/helpers/coinselect.ts +8 -2
  145. package/src/utxo/helpers/txSize.ts +8 -2
  146. package/src/utxo/toolbox/bitcoinCash.ts +13 -7
  147. package/src/utxo/toolbox/index.ts +8 -2
  148. package/src/utxo/toolbox/utxo.ts +16 -10
  149. package/src/utxo/toolbox/zcash.ts +24 -21
@@ -1,4 +1,10 @@
1
- import { SwapKitError } from "@uswap/helpers";
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 SwapKitError("toolbox_utxo_invalid_address", { address });
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 SwapKitError("toolbox_utxo_invalid_address", { address });
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
- import { Chain, SwapKitError, type UTXOChain } from "@uswap/helpers";
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 SwapKitError("toolbox_utxo_not_supported", { chain });
32
+ throw new USwapError("toolbox_utxo_not_supported", { chain });
27
33
  }
28
34
  }
29
35
 
@@ -1,4 +1,10 @@
1
- import { SwapKitError } from "@uswap/helpers";
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 SwapKitError("toolbox_utxo_invalid_address", { address });
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
- SwapKitError,
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 SwapKitError("toolbox_utxo_invalid_address", { address: recipient });
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 SwapKitError("toolbox_utxo_insufficient_balance", { assetValue, sender });
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 SwapKitError("toolbox_utxo_no_signer");
168
+ if (!(signer && from)) throw new USwapError("toolbox_utxo_no_signer");
163
169
  if (!recipient)
164
- throw new SwapKitError("toolbox_utxo_invalid_params", { error: "Recipient address must be provided" });
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 SwapKitError("toolbox_utxo_invalid_address", { address: recipientCashAddress });
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 SwapKitError("toolbox_utxo_insufficient_balance", { assetValue, sender });
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
- import { Chain, type DerivationPathArray, SwapKitError, type UTXOChain } from "@uswap/helpers";
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 SwapKitError("toolbox_utxo_not_supported", { chain });
59
+ throw new USwapError("toolbox_utxo_not_supported", { chain });
54
60
  }
55
61
  }
56
62
 
@@ -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 SwapKitError("toolbox_utxo_insufficient_balance", { assetValue, sender });
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 SwapKitError("toolbox_utxo_invalid_params", { error: "No phrase provided" });
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 SwapKitError("toolbox_utxo_invalid_params", { error: "Either phrase or wif must be provided" });
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 SwapKitError("toolbox_utxo_invalid_params", { error: "Could not get private key from phrase" });
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 SwapKitError("toolbox_utxo_not_supported", { chain });
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 SwapKitError("toolbox_utxo_invalid_params", { error: "Keys must be provided" });
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 SwapKitError("toolbox_utxo_invalid_address", { error: "Address not defined" });
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 SwapKitError("toolbox_utxo_no_signer");
396
+ if (!(signer && from)) throw new USwapError("toolbox_utxo_no_signer");
391
397
  if (!recipient)
392
- throw new SwapKitError("toolbox_utxo_invalid_params", { error: "Recipient address must be provided" });
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 { ZcashPsbt } from "@bitgo/utxo-lib/dist/src/bitgo";
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
- SwapKitError,
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 SwapKitError("toolbox_utxo_invalid_params");
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 SwapKitError("toolbox_utxo_insufficient_balance", { assetValue, sender });
149
+ throw new USwapError("toolbox_utxo_insufficient_balance", { assetValue, sender });
144
150
  }
145
151
 
146
- const psbt = bitgo.createPsbtForNetwork({ network: getZcashNetwork() }, { version: 455 }) as ZcashPsbt;
147
-
148
- // const NU6 = 0xc8e71055;
149
- const NU5 = 0xc2d6d0b4;
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 SwapKitError("toolbox_utxo_no_signer");
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
- return baseToolbox.broadcastTx(signedPsbt.extractTransaction().toHex());
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 SwapKitError("toolbox_utxo_invalid_params");
220
+ throw new USwapError("toolbox_utxo_invalid_params");
218
221
  }
219
222
 
220
223
  const ecpairNetwork = getECPairNetwork();