@swapkit/toolboxes 4.2.1 → 4.3.3

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 (134) 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/evm/index.cjs +2 -2
  6. package/dist/src/evm/index.cjs.map +8 -8
  7. package/dist/src/evm/index.js +2 -2
  8. package/dist/src/evm/index.js.map +8 -8
  9. package/dist/src/index.cjs +3 -3
  10. package/dist/src/index.cjs.map +15 -15
  11. package/dist/src/index.js +3 -3
  12. package/dist/src/index.js.map +15 -15
  13. package/dist/src/substrate/index.cjs +2 -2
  14. package/dist/src/substrate/index.cjs.map +3 -3
  15. package/dist/src/substrate/index.js +2 -2
  16. package/dist/src/substrate/index.js.map +3 -3
  17. package/dist/src/tron/index.cjs +2 -2
  18. package/dist/src/tron/index.cjs.map +3 -3
  19. package/dist/src/tron/index.js +2 -2
  20. package/dist/src/tron/index.js.map +3 -3
  21. package/dist/src/utxo/index.cjs +3 -3
  22. package/dist/src/utxo/index.cjs.map +6 -6
  23. package/dist/src/utxo/index.js +3 -3
  24. package/dist/src/utxo/index.js.map +6 -6
  25. package/dist/types/cardano/toolbox.d.ts +1 -1
  26. package/dist/types/cardano/toolbox.d.ts.map +1 -1
  27. package/dist/types/evm/api.d.ts +1 -1
  28. package/dist/types/evm/api.d.ts.map +1 -1
  29. package/dist/types/evm/helpers.d.ts.map +1 -1
  30. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts +1 -1
  31. package/dist/types/evm/toolbox/baseEVMToolbox.d.ts.map +1 -1
  32. package/dist/types/evm/toolbox/evm.d.ts +97 -17
  33. package/dist/types/evm/toolbox/evm.d.ts.map +1 -1
  34. package/dist/types/evm/toolbox/index.d.ts.map +1 -1
  35. package/dist/types/evm/toolbox/op.d.ts +1 -1
  36. package/dist/types/evm/toolbox/op.d.ts.map +1 -1
  37. package/dist/types/evm/types.d.ts +4 -2
  38. package/dist/types/evm/types.d.ts.map +1 -1
  39. package/dist/types/substrate/balance.d.ts.map +1 -1
  40. package/dist/types/tron/toolbox.d.ts.map +1 -1
  41. package/dist/types/utxo/helpers/api.d.ts +2 -34
  42. package/dist/types/utxo/helpers/api.d.ts.map +1 -1
  43. package/dist/types/utxo/toolbox/bitcoinCash.d.ts +1 -1
  44. package/dist/types/utxo/toolbox/bitcoinCash.d.ts.map +1 -1
  45. package/dist/types/utxo/toolbox/index.d.ts +3 -27
  46. package/dist/types/utxo/toolbox/index.d.ts.map +1 -1
  47. package/dist/types/utxo/toolbox/params.d.ts +32 -0
  48. package/dist/types/utxo/toolbox/params.d.ts.map +1 -0
  49. package/dist/types/utxo/toolbox/utxo.d.ts +1 -1
  50. package/dist/types/utxo/toolbox/utxo.d.ts.map +1 -1
  51. package/dist/types/utxo/toolbox/zcash.d.ts.map +1 -1
  52. package/package.json +86 -58
  53. package/src/__tests__/address-validation-all-chains.test.ts +162 -0
  54. package/src/__tests__/addressValidator.test.ts +162 -0
  55. package/src/cardano/__tests__/toolbox.test.ts +48 -0
  56. package/src/cardano/index.ts +2 -0
  57. package/src/cardano/toolbox.ts +168 -0
  58. package/src/cardano/types.ts +10 -0
  59. package/src/cosmos/__tests__/toolbox.test.ts +91 -0
  60. package/src/cosmos/index.ts +4 -0
  61. package/src/cosmos/thorchainUtils/addressFormat.ts +22 -0
  62. package/src/cosmos/thorchainUtils/index.ts +4 -0
  63. package/src/cosmos/thorchainUtils/messages.ts +212 -0
  64. package/src/cosmos/thorchainUtils/registry.ts +43 -0
  65. package/src/cosmos/thorchainUtils/types/MsgCompiled.ts +2800 -0
  66. package/src/cosmos/thorchainUtils/types/client-types.ts +54 -0
  67. package/src/cosmos/thorchainUtils/types/index.ts +1 -0
  68. package/src/cosmos/toolbox/cosmos.ts +345 -0
  69. package/src/cosmos/toolbox/index.ts +35 -0
  70. package/src/cosmos/toolbox/thorchain.ts +249 -0
  71. package/src/cosmos/types.ts +48 -0
  72. package/src/cosmos/util.ts +214 -0
  73. package/src/evm/__tests__/address-validation.test.ts +84 -0
  74. package/src/evm/__tests__/ethereum.test.ts +137 -0
  75. package/src/evm/__tests__/signMessage.test.ts +60 -0
  76. package/src/evm/api.ts +10 -0
  77. package/src/evm/contracts/eth/multicall.ts +165 -0
  78. package/src/evm/contracts/op/gasOracle.ts +145 -0
  79. package/src/evm/helpers.ts +73 -0
  80. package/src/evm/index.ts +4 -0
  81. package/src/evm/toolbox/baseEVMToolbox.ts +695 -0
  82. package/src/evm/toolbox/evm.ts +67 -0
  83. package/src/evm/toolbox/index.ts +44 -0
  84. package/src/evm/toolbox/op.ts +163 -0
  85. package/src/evm/types.ts +146 -0
  86. package/src/index.ts +260 -0
  87. package/src/near/__tests__/core.test.ts +70 -0
  88. package/src/near/helpers/core.ts +85 -0
  89. package/src/near/helpers/gasEstimation.ts +96 -0
  90. package/src/near/helpers/nep141.ts +50 -0
  91. package/src/near/index.ts +21 -0
  92. package/src/near/toolbox.ts +421 -0
  93. package/src/near/types/contract.ts +32 -0
  94. package/src/near/types/nep141.ts +34 -0
  95. package/src/near/types/toolbox.ts +55 -0
  96. package/src/near/types.ts +44 -0
  97. package/src/radix/index.ts +132 -0
  98. package/src/ripple/index.ts +179 -0
  99. package/src/solana/index.ts +36 -0
  100. package/src/solana/toolbox.ts +415 -0
  101. package/src/substrate/balance.ts +88 -0
  102. package/src/substrate/index.ts +2 -0
  103. package/src/substrate/substrate.ts +281 -0
  104. package/src/substrate/types.ts +115 -0
  105. package/src/sui/__tests__/toolbox.test.ts +82 -0
  106. package/src/sui/index.ts +2 -0
  107. package/src/sui/toolbox.ts +165 -0
  108. package/src/sui/types.ts +11 -0
  109. package/src/ton/__tests__/toolbox.test.ts +63 -0
  110. package/src/ton/index.ts +2 -0
  111. package/src/ton/toolbox.ts +136 -0
  112. package/src/ton/types.ts +13 -0
  113. package/src/tron/__tests__/toolbox.test.ts +221 -0
  114. package/src/tron/helpers/trc20.abi.ts +107 -0
  115. package/src/tron/helpers/trongrid.ts +53 -0
  116. package/src/tron/index.ts +21 -0
  117. package/src/tron/toolbox.ts +585 -0
  118. package/src/tron/types.ts +83 -0
  119. package/src/types.ts +28 -0
  120. package/src/utils.ts +27 -0
  121. package/src/utxo/__tests__/zcash-integration.test.ts +97 -0
  122. package/src/utxo/helpers/api.ts +471 -0
  123. package/src/utxo/helpers/bchaddrjs.ts +166 -0
  124. package/src/utxo/helpers/coinselect.ts +92 -0
  125. package/src/utxo/helpers/index.ts +4 -0
  126. package/src/utxo/helpers/txSize.ts +137 -0
  127. package/src/utxo/index.ts +6 -0
  128. package/src/utxo/toolbox/bitcoinCash.ts +243 -0
  129. package/src/utxo/toolbox/index.ts +59 -0
  130. package/src/utxo/toolbox/params.ts +18 -0
  131. package/src/utxo/toolbox/utxo.ts +439 -0
  132. package/src/utxo/toolbox/validators.ts +36 -0
  133. package/src/utxo/toolbox/zcash.ts +245 -0
  134. package/src/utxo/types.ts +39 -0
@@ -0,0 +1,245 @@
1
+ import { bitgo, crypto, ECPair, networks, address as zcashAddress } from "@bitgo/utxo-lib";
2
+ import type { ZcashPsbt } from "@bitgo/utxo-lib/dist/src/bitgo";
3
+ import { HDKey } from "@scure/bip32";
4
+ import { mnemonicToSeedSync } from "@scure/bip39";
5
+ import {
6
+ Chain,
7
+ type ChainSigner,
8
+ type DerivationPathArray,
9
+ derivationPathToString,
10
+ FeeOption,
11
+ NetworkDerivationPath,
12
+ SKConfig,
13
+ SwapKitError,
14
+ updateDerivationPath,
15
+ } from "@swapkit/helpers";
16
+ import bs58check from "bs58check";
17
+ import { match, P } from "ts-pattern";
18
+ import { accumulative, compileMemo, getUtxoApi } from "../helpers";
19
+ import type { TargetOutput, UTXOBuildTxParams, UTXOTransferParams, UTXOType } from "../types";
20
+ import { createUTXOToolbox } from "./utxo";
21
+ import { validateZcashAddress } from "./validators";
22
+
23
+ function getZcashNetwork() {
24
+ return networks.zcash;
25
+ }
26
+
27
+ function getECPairNetwork() {
28
+ return {
29
+ bech32: undefined,
30
+ bip32: { private: 0x0488ade4, public: 0x0488b21e },
31
+ messagePrefix: "\x18ZCash Signed Message:\n",
32
+ pubKeyHash: 0x1c,
33
+ scriptHash: 0x1c,
34
+ wif: 0x80,
35
+ };
36
+ }
37
+
38
+ type ZcashSigner = ChainSigner<ZcashPsbt, ZcashPsbt>;
39
+
40
+ function createZcashSignerFromPhrase({
41
+ phrase,
42
+ derivationPath,
43
+ }: {
44
+ phrase: string;
45
+ derivationPath: string;
46
+ }): ZcashSigner {
47
+ const seed = mnemonicToSeedSync(phrase);
48
+ const root = HDKey.fromMasterSeed(seed);
49
+ const node = root.derive(derivationPath);
50
+
51
+ if (!node.privateKey) {
52
+ throw new SwapKitError("toolbox_utxo_invalid_params");
53
+ }
54
+
55
+ // Create key pair using BitGo's ECPair with ECPair-compatible network
56
+ const ecpairNetwork = getECPairNetwork();
57
+ const keyPair = ECPair.fromPrivateKey(Buffer.from(node.privateKey), { network: ecpairNetwork });
58
+
59
+ const pubKeyHash = crypto.hash160(keyPair.publicKey);
60
+ const { isStagenet } = SKConfig.get("envs");
61
+
62
+ const prefix = isStagenet
63
+ ? Buffer.from([0x1d, 0x25]) // testnet prefix (results in tm... addresses)
64
+ : Buffer.from([0x1c, 0xb8]); // mainnet prefix (results in t1... addresses)
65
+
66
+ const payload = Buffer.concat([prefix, pubKeyHash]);
67
+
68
+ const address = bs58check.encode(payload);
69
+
70
+ return {
71
+ getAddress: () => Promise.resolve(address),
72
+
73
+ signTransaction: (psbt) => {
74
+ const signedPsbt = psbt.signAllInputs(keyPair);
75
+
76
+ return Promise.resolve(signedPsbt);
77
+ },
78
+ };
79
+ }
80
+
81
+ function addInputsAndOutputs({
82
+ inputs,
83
+ outputs,
84
+ psbt,
85
+ sender,
86
+ compiledMemo,
87
+ }: {
88
+ inputs: UTXOType[];
89
+ outputs: TargetOutput[];
90
+ psbt: ZcashPsbt;
91
+ sender: string;
92
+ compiledMemo: Buffer<ArrayBufferLike> | null;
93
+ }) {
94
+ for (const utxo of inputs) {
95
+ const witnessInfo = !!utxo.witnessUtxo && { witnessUtxo: { ...utxo.witnessUtxo, value: BigInt(utxo.value) } };
96
+
97
+ const nonWitnessInfo = !utxo.witnessUtxo && {
98
+ nonWitnessUtxo: utxo.txHex ? Buffer.from(utxo.txHex, "hex") : undefined,
99
+ };
100
+
101
+ psbt.addInput({ hash: utxo.hash, index: utxo.index, ...witnessInfo, ...nonWitnessInfo });
102
+ }
103
+
104
+ for (const output of outputs) {
105
+ const address = "address" in output && output.address ? output.address : sender;
106
+ const hasOutputScript = output.script;
107
+
108
+ if (hasOutputScript && !compiledMemo) {
109
+ continue;
110
+ }
111
+
112
+ const mappedOutput = hasOutputScript
113
+ ? { script: compiledMemo as Buffer<ArrayBufferLike>, value: 0n }
114
+ : { script: zcashAddress.toOutputScript(address, getZcashNetwork()), value: BigInt(output.value) };
115
+
116
+ psbt.addOutput(mappedOutput);
117
+ }
118
+
119
+ return { inputs, psbt };
120
+ }
121
+
122
+ async function createTransaction(buildTxParams: UTXOBuildTxParams) {
123
+ const { assetValue, recipient, memo, feeRate, sender, fetchTxHex } = buildTxParams;
124
+
125
+ const compiledMemo = memo ? compileMemo(memo) : null;
126
+
127
+ const utxos = await getUtxoApi(Chain.Zcash).getUtxos({ address: sender, fetchTxHex: fetchTxHex !== false });
128
+
129
+ const targetOutputs = [
130
+ { address: recipient, value: Number(assetValue.getBaseValue("string")) },
131
+ ...(compiledMemo ? [{ script: compiledMemo, value: 0 }] : []),
132
+ ];
133
+
134
+ const { inputs, outputs } = accumulative({
135
+ chain: Chain.Zcash,
136
+ changeAddress: sender, // Overwrite change address to sender
137
+ feeRate,
138
+ inputs: utxos,
139
+ outputs: targetOutputs,
140
+ });
141
+
142
+ if (!(inputs && outputs)) {
143
+ throw new SwapKitError("toolbox_utxo_insufficient_balance", { assetValue, sender });
144
+ }
145
+
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 });
160
+
161
+ const { psbt: mappedPsbt, inputs: mappedInputs } = await addInputsAndOutputs({
162
+ compiledMemo,
163
+ inputs,
164
+ outputs,
165
+ psbt,
166
+ sender,
167
+ });
168
+
169
+ return { inputs: mappedInputs, outputs, psbt: mappedPsbt };
170
+ }
171
+
172
+ export async function createZcashToolbox(
173
+ toolboxParams: { signer?: ZcashSigner } | { phrase?: string; derivationPath?: DerivationPathArray; index?: number },
174
+ ) {
175
+ const signer = await match(toolboxParams)
176
+ .with({ signer: P.not(P.nullish) }, ({ signer }) => Promise.resolve(signer))
177
+ .with({ phrase: P.string }, ({ phrase, derivationPath, index = 0 }) => {
178
+ const baseDerivationPath = derivationPath || NetworkDerivationPath[Chain.Zcash] || [44, 133, 0, 0, 0];
179
+ const updatedPath = updateDerivationPath(baseDerivationPath, { index });
180
+ const pathString = derivationPathToString(updatedPath);
181
+
182
+ return createZcashSignerFromPhrase({ derivationPath: pathString, phrase });
183
+ })
184
+ .otherwise(() => Promise.resolve(undefined));
185
+
186
+ const baseToolbox = await createUTXOToolbox({ chain: Chain.Zcash, signer });
187
+
188
+ async function transfer({ recipient, assetValue, feeOptionKey = FeeOption.Fast, ...rest }: UTXOTransferParams) {
189
+ const from = await signer?.getAddress();
190
+ if (!(signer && from)) {
191
+ throw new SwapKitError("toolbox_utxo_no_signer");
192
+ }
193
+
194
+ const feeRate = rest.feeRate || (await baseToolbox.getFeeRates())[feeOptionKey];
195
+
196
+ const { psbt } = await createTransaction({ ...rest, assetValue, feeRate, recipient, sender: from });
197
+
198
+ const signedPsbt = await signer.signTransaction(psbt);
199
+
200
+ signedPsbt.finalizeAllInputs();
201
+
202
+ return baseToolbox.broadcastTx(signedPsbt.extractTransaction().toHex());
203
+ }
204
+
205
+ function createKeysForPath({
206
+ phrase,
207
+ derivationPath = "m/44'/133'/0'/0/0",
208
+ }: {
209
+ phrase: string;
210
+ derivationPath?: string;
211
+ }) {
212
+ const seed = mnemonicToSeedSync(phrase);
213
+ const root = HDKey.fromMasterSeed(seed);
214
+ const node = root.derive(derivationPath);
215
+
216
+ if (!node.privateKey) {
217
+ throw new SwapKitError("toolbox_utxo_invalid_params");
218
+ }
219
+
220
+ const ecpairNetwork = getECPairNetwork();
221
+ const keyPair = ECPair.fromPrivateKey(Buffer.from(node.privateKey), { network: ecpairNetwork });
222
+
223
+ return keyPair;
224
+ }
225
+
226
+ function getPrivateKeyFromMnemonic({
227
+ phrase,
228
+ derivationPath = "m/44'/133'/0'/0/0",
229
+ }: {
230
+ phrase: string;
231
+ derivationPath: string;
232
+ }) {
233
+ const keys = createKeysForPath({ derivationPath, phrase });
234
+ return keys.toWIF();
235
+ }
236
+
237
+ return {
238
+ ...baseToolbox,
239
+ createKeysForPath,
240
+ createTransaction,
241
+ getPrivateKeyFromMnemonic,
242
+ transfer,
243
+ validateAddress: validateZcashAddress,
244
+ };
245
+ }
@@ -0,0 +1,39 @@
1
+ import type { GenericCreateTransactionParams, GenericTransferParams, Witness } from "@swapkit/helpers";
2
+
3
+ import type { UTXOScriptType } from "./helpers";
4
+
5
+ export type TransactionType = { toHex(): string };
6
+
7
+ export type TargetOutput = { address: string; script?: Buffer; value: number } | { script: Buffer; value: number };
8
+
9
+ export type UTXOType = { hash: string; index: number; value: number; txHex?: string; witnessUtxo?: Witness };
10
+
11
+ export type UTXOInputWithScriptType = UTXOType & { type: UTXOScriptType; address: string };
12
+
13
+ export type UTXOCalculateTxSizeParams = {
14
+ inputs: (UTXOInputWithScriptType | UTXOType)[];
15
+ outputs?: TargetOutput[];
16
+ feeRate: number;
17
+ };
18
+
19
+ export type UTXOBuildTxParams = GenericCreateTransactionParams & { fetchTxHex?: boolean };
20
+
21
+ export type UTXOTransferParams = GenericTransferParams;
22
+
23
+ export type BchECPair = { getAddress: (index?: number) => string; publicKey: Buffer; toWIF: () => string };
24
+
25
+ export type TransactionBuilderType = {
26
+ inputs: any[];
27
+ sign(
28
+ vin: number,
29
+ keyPair: BchECPair,
30
+ redeemScript?: Buffer,
31
+ hashType?: number,
32
+ witnessValue?: number,
33
+ witnessScript?: Buffer,
34
+ signatureAlgorithm?: string,
35
+ ): void;
36
+ build(): TransactionType;
37
+ addOutput(addressOrScriptBuffer: string | Buffer, value: number): void;
38
+ addInput(txHash: string | Buffer, vout: number, sequence?: number, prevOutScript?: Buffer): void;
39
+ };