@swapkit/wallet-hardware 4.8.0 → 4.8.2

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 (38) hide show
  1. package/dist/keepkey/index.cjs.map +2 -2
  2. package/dist/keepkey/index.js.map +2 -2
  3. package/dist/types/keepkey/chains/utxo.d.ts +5 -379
  4. package/dist/types/keepkey/chains/utxo.d.ts.map +1 -1
  5. package/package.json +5 -7
  6. package/src/index.ts +1 -0
  7. package/src/keepkey/chains/cosmos.ts +69 -0
  8. package/src/keepkey/chains/evm.ts +141 -0
  9. package/src/keepkey/chains/mayachain.ts +98 -0
  10. package/src/keepkey/chains/ripple.ts +88 -0
  11. package/src/keepkey/chains/thorchain.ts +93 -0
  12. package/src/keepkey/chains/utxo.ts +364 -0
  13. package/src/keepkey/coins.ts +67 -0
  14. package/src/keepkey/index.ts +174 -0
  15. package/src/ledger/clients/cosmos.ts +84 -0
  16. package/src/ledger/clients/evm.ts +186 -0
  17. package/src/ledger/clients/near.ts +63 -0
  18. package/src/ledger/clients/sui.ts +130 -0
  19. package/src/ledger/clients/thorchain/common.ts +93 -0
  20. package/src/ledger/clients/thorchain/helpers.ts +120 -0
  21. package/src/ledger/clients/thorchain/index.ts +87 -0
  22. package/src/ledger/clients/thorchain/lib.ts +258 -0
  23. package/src/ledger/clients/thorchain/utils.ts +69 -0
  24. package/src/ledger/clients/tron.ts +85 -0
  25. package/src/ledger/clients/utxo-legacy-adapter.ts +71 -0
  26. package/src/ledger/clients/utxo-psbt.ts +145 -0
  27. package/src/ledger/clients/utxo.ts +359 -0
  28. package/src/ledger/clients/xrp.ts +50 -0
  29. package/src/ledger/cosmosTypes.ts +98 -0
  30. package/src/ledger/helpers/getLedgerAddress.ts +76 -0
  31. package/src/ledger/helpers/getLedgerClient.ts +124 -0
  32. package/src/ledger/helpers/getLedgerTransport.ts +102 -0
  33. package/src/ledger/helpers/index.ts +3 -0
  34. package/src/ledger/index.ts +546 -0
  35. package/src/ledger/interfaces/CosmosLedgerInterface.ts +54 -0
  36. package/src/ledger/types.ts +42 -0
  37. package/src/trezor/evmSigner.ts +210 -0
  38. package/src/trezor/index.ts +847 -0
@@ -0,0 +1,98 @@
1
+ import type { KeepKeySdk } from "@keepkey/keepkey-sdk";
2
+ import {
3
+ type AssetValue,
4
+ Chain,
5
+ DerivationPath,
6
+ type DerivationPathArray,
7
+ derivationPathToString,
8
+ type GenericTransferParams,
9
+ getRPCUrl,
10
+ MAYAConfig,
11
+ SwapKitError,
12
+ } from "@swapkit/helpers";
13
+ import type { ThorchainDepositParams } from "@swapkit/toolboxes/cosmos";
14
+
15
+ import { bip32ToAddressNList } from "../coins";
16
+
17
+ type SignTransactionParams = { assetValue: AssetValue; recipient?: string; sender: string; memo: string | undefined };
18
+
19
+ export async function mayachainWalletMethods({
20
+ sdk,
21
+ derivationPath,
22
+ }: {
23
+ sdk: KeepKeySdk;
24
+ derivationPath?: DerivationPathArray;
25
+ }): Promise<any> {
26
+ const { createStargateClient, getCosmosToolbox } = await import("@swapkit/toolboxes/cosmos");
27
+
28
+ const toolbox = await getCosmosToolbox(Chain.Maya);
29
+ const derivationPathString = derivationPath ? derivationPathToString(derivationPath) : `${DerivationPath.MAYA}/0`;
30
+
31
+ const { address: fromAddress } = (await sdk.address.mayachainGetAddress({
32
+ address_n: bip32ToAddressNList(derivationPathString),
33
+ })) as { address: string };
34
+
35
+ const signTransaction = async ({ assetValue, recipient, sender, memo }: SignTransactionParams) => {
36
+ const importedAmino = await import("@cosmjs/amino");
37
+ const makeSignDoc = importedAmino.makeSignDoc ?? importedAmino.default?.makeSignDoc;
38
+ const { getDenomWithChain } = await import("@swapkit/toolboxes/cosmos");
39
+
40
+ const account = await toolbox.getAccount(sender);
41
+ if (!account) throw new SwapKitError("wallet_keepkey_account_not_found");
42
+ const { accountNumber, sequence = 0 } = account;
43
+ const amount = assetValue.getBaseValue("string");
44
+
45
+ const isTransfer = recipient && recipient !== "";
46
+
47
+ // TODO check if we can move to toolbox created msg
48
+ const msg = isTransfer
49
+ ? {
50
+ type: "mayachain/MsgSend",
51
+ value: {
52
+ amount: [{ amount, denom: assetValue.symbol.toLowerCase() }],
53
+ from_address: sender,
54
+ to_address: recipient,
55
+ },
56
+ }
57
+ : {
58
+ type: "mayachain/MsgDeposit",
59
+ value: { coins: [{ amount, asset: getDenomWithChain(assetValue) }], memo, signer: sender },
60
+ };
61
+
62
+ const signDoc = makeSignDoc(
63
+ [msg],
64
+ { amount: [], gas: "500000000" },
65
+ MAYAConfig.chainId,
66
+ memo,
67
+ accountNumber?.toString(),
68
+ sequence,
69
+ );
70
+
71
+ const sdkMethod = isTransfer ? sdk.mayachain.mayachainSignAminoTransfer : sdk.mayachain.mayachainSignAminoDeposit;
72
+
73
+ // @ts-expect-error TC
74
+ const signedTx = await sdkMethod({ signDoc, signerAddress: sender });
75
+ const decodedBytes = atob(signedTx.serialized);
76
+ return new Uint8Array(decodedBytes.length).map((_, i) => decodedBytes.charCodeAt(i));
77
+ };
78
+
79
+ const transfer = async ({ assetValue, recipient, memo }: GenericTransferParams) => {
80
+ const rpcUrl = await getRPCUrl(Chain.Maya);
81
+ const stargateClient = await createStargateClient(rpcUrl);
82
+ const signedTransaction = await signTransaction({ assetValue, memo, recipient, sender: fromAddress });
83
+ const { transactionHash } = await stargateClient.broadcastTx(signedTransaction);
84
+
85
+ return transactionHash;
86
+ };
87
+
88
+ const deposit = async ({ assetValue, memo }: ThorchainDepositParams) => {
89
+ const rpcUrl = await getRPCUrl(Chain.Maya);
90
+ const stargateClient = await createStargateClient(rpcUrl);
91
+ const signedTransaction = await signTransaction({ assetValue, memo, sender: fromAddress });
92
+ const { transactionHash } = await stargateClient.broadcastTx(signedTransaction);
93
+
94
+ return transactionHash;
95
+ };
96
+
97
+ return { ...toolbox, address: fromAddress, deposit, transfer };
98
+ }
@@ -0,0 +1,88 @@
1
+ import type { KeepKeySdk } from "@keepkey/keepkey-sdk";
2
+ import {
3
+ Chain,
4
+ DerivationPath,
5
+ type DerivationPathArray,
6
+ derivationPathToString,
7
+ type GenericTransferParams,
8
+ } from "@swapkit/helpers";
9
+ import { getRippleToolbox } from "@swapkit/toolboxes/ripple";
10
+ import { bip32ToAddressNList } from "../coins";
11
+
12
+ export const rippleWalletMethods = async ({
13
+ sdk,
14
+ derivationPath,
15
+ }: {
16
+ sdk: KeepKeySdk;
17
+ derivationPath?: DerivationPathArray;
18
+ }) => {
19
+ // Derivation path handling (default to standard XRP 44'/144'/0'/0/0)
20
+ const derivationPathString = derivationPath
21
+ ? derivationPathToString(derivationPath)
22
+ : `${DerivationPath[Chain.Ripple]}/0`;
23
+
24
+ // Fetch address from KeepKey
25
+ const { address } = await (sdk as any).address.xrpGetAddress({
26
+ address_n: bip32ToAddressNList(derivationPathString),
27
+ });
28
+
29
+ // Inject minimal signer so toolbox's address helpers work
30
+ const signer = {
31
+ getAddress: () => Promise.resolve(address),
32
+ signTransaction: () => {
33
+ throw new Error("signTransaction not supported via toolbox");
34
+ },
35
+ };
36
+
37
+ const toolbox = await getRippleToolbox({ signer });
38
+
39
+ const transfer = async ({ recipient, assetValue, memo }: GenericTransferParams) => {
40
+ // Build XRPL Payment tx using toolbox helper
41
+ const tx = await toolbox.createTransaction({ assetValue, memo, recipient, sender: address });
42
+
43
+ // Convert toolbox Payment tx into KeepKey StdTx wrapper (KeepKey-specific format)
44
+ const stdTx = {
45
+ type: "auth/StdTx",
46
+ value: {
47
+ fee: { amount: [{ amount: "1000", denom: "drop" }], gas: "28000" },
48
+ memo: memo && memo.length > 0 ? memo : "",
49
+ msg: [
50
+ {
51
+ type: "ripple-sdk/MsgSend",
52
+ value: { amount: [{ amount: tx.Amount, denom: "drop" }], from_address: address, to_address: recipient },
53
+ },
54
+ ],
55
+ signatures: null,
56
+ },
57
+ };
58
+
59
+ const unsignedTx = {
60
+ addressNList: bip32ToAddressNList(derivationPathString),
61
+ flags: tx.Flags === 0 ? undefined : tx.Flags,
62
+ lastLedgerSequence: tx.LastLedgerSequence?.toString(),
63
+ payment: {
64
+ amount: tx.Amount,
65
+ destination: tx.Destination,
66
+ destinationTag: (tx.DestinationTag ?? "0").toString(),
67
+ },
68
+ sequence: (tx.Sequence ?? 0).toString(),
69
+ tx: stdTx,
70
+ } as any;
71
+
72
+ // Sign with KeepKey
73
+ const responseSign = JSON.parse(await (sdk as any).xrp.xrpSignTransaction(unsignedTx));
74
+
75
+ // keepkey-sdk may return either { tx_blob } or StdTx with Base64 serializedTx
76
+ const txBlob: string | undefined =
77
+ (responseSign as any).tx_blob ?? (responseSign as any).value?.signatures?.[0]?.serializedTx;
78
+ if (!txBlob) throw new Error("KeepKey XRP sign failed");
79
+
80
+ const buffer = Buffer.from(txBlob, "base64");
81
+ const txBlobHex = buffer.toString("hex");
82
+
83
+ // Broadcast signed tx via toolbox
84
+ return toolbox.broadcastTransaction(txBlobHex);
85
+ };
86
+
87
+ return { ...toolbox, address, getAddress: () => address, transfer };
88
+ };
@@ -0,0 +1,93 @@
1
+ import type { KeepKeySdk, TypesThorchainSignDocDeposit, TypesThorchainSignDocTransfer } from "@keepkey/keepkey-sdk";
2
+ import {
3
+ type AssetValue,
4
+ Chain,
5
+ DerivationPath,
6
+ type DerivationPathArray,
7
+ derivationPathToString,
8
+ type GenericTransferParams,
9
+ getRPCUrl,
10
+ SwapKitError,
11
+ THORConfig,
12
+ } from "@swapkit/helpers";
13
+ import type { ThorchainDepositParams } from "@swapkit/toolboxes/cosmos";
14
+
15
+ import { bip32ToAddressNList } from "../coins";
16
+
17
+ type SignTransactionParams = { assetValue: AssetValue; recipient?: string; sender: string; memo: string | undefined };
18
+
19
+ export async function thorchainWalletMethods({
20
+ sdk,
21
+ derivationPath,
22
+ }: {
23
+ sdk: KeepKeySdk;
24
+ derivationPath?: DerivationPathArray;
25
+ }): Promise<any> {
26
+ const importedAmino = await import("@cosmjs/amino");
27
+ const makeSignDoc = importedAmino.makeSignDoc ?? importedAmino.default?.makeSignDoc;
28
+ const { buildAminoMsg, getDefaultChainFee, createStargateClient, getCosmosToolbox } = await import(
29
+ "@swapkit/toolboxes/cosmos"
30
+ );
31
+
32
+ const toolbox = await getCosmosToolbox(Chain.THORChain);
33
+ const derivationPathString = derivationPath ? derivationPathToString(derivationPath) : `${DerivationPath.THOR}/0`;
34
+
35
+ const { address: fromAddress } = (await sdk.address.thorchainGetAddress({
36
+ address_n: bip32ToAddressNList(derivationPathString),
37
+ })) as { address: string };
38
+
39
+ const signTransaction = async ({ assetValue, recipient, sender, memo }: SignTransactionParams) => {
40
+ const account = await toolbox.getAccount(sender);
41
+ if (!account) throw new SwapKitError("wallet_keepkey_account_not_found");
42
+ const { accountNumber, sequence = 0 } = account;
43
+
44
+ const isTransfer = recipient && recipient !== "";
45
+ const msg = buildAminoMsg({ assetValue, memo, recipient, sender });
46
+
47
+ const signDoc = makeSignDoc(
48
+ [msg],
49
+ getDefaultChainFee(Chain.THORChain),
50
+ THORConfig.chainId,
51
+ memo,
52
+ accountNumber?.toString(),
53
+ sequence,
54
+ );
55
+
56
+ const signedTx = isTransfer
57
+ ? await sdk.thorchain.thorchainSignAminoTransfer({
58
+ signDoc: signDoc as TypesThorchainSignDocTransfer,
59
+ signerAddress: sender,
60
+ })
61
+ : await sdk.thorchain.thorchainSignAminoDeposit({
62
+ signDoc: signDoc as TypesThorchainSignDocDeposit,
63
+ signerAddress: sender,
64
+ });
65
+ const decodedBytes = atob(signedTx.serialized);
66
+ return new Uint8Array(decodedBytes.length).map((_, i) => decodedBytes.charCodeAt(i));
67
+ };
68
+
69
+ const transfer = async ({ assetValue, recipient, memo }: GenericTransferParams) => {
70
+ const rpcUrl = await getRPCUrl(Chain.THORChain);
71
+ const stargateClient = await createStargateClient(rpcUrl);
72
+ const signedTransaction = await signTransaction({ assetValue, memo, recipient, sender: fromAddress });
73
+ const { transactionHash } = await stargateClient.broadcastTx(signedTransaction);
74
+
75
+ return transactionHash;
76
+ };
77
+
78
+ const deposit = async ({ assetValue, memo }: ThorchainDepositParams) => {
79
+ const rpcUrl = await getRPCUrl(Chain.THORChain);
80
+ const stargateClient = await createStargateClient(rpcUrl);
81
+ const signedTransaction = await signTransaction({ assetValue, memo, sender: fromAddress });
82
+ const { transactionHash } = await stargateClient.broadcastTx(signedTransaction);
83
+
84
+ return transactionHash;
85
+ };
86
+
87
+ // const signMessage = async (message: string) => {
88
+ // const stargateClient = await createStargateClient(RPCUrl.THORChain);
89
+ // // return signedTx;
90
+ // };
91
+
92
+ return { ...toolbox, address: fromAddress, deposit, transfer };
93
+ }
@@ -0,0 +1,364 @@
1
+ import type { KeepKeySdk } from "@keepkey/keepkey-sdk";
2
+ import {
3
+ Chain,
4
+ DerivationPath,
5
+ type DerivationPathArray,
6
+ derivationPathToString,
7
+ FeeOption,
8
+ type GenericTransferParams,
9
+ SwapKitError,
10
+ type UTXOChain,
11
+ } from "@swapkit/helpers";
12
+ import {
13
+ assertDerivationIndex,
14
+ createHDWalletHelpers,
15
+ getNetworkForChain,
16
+ getUTXOAccountIndexFromPath,
17
+ getUTXOAccountPath,
18
+ getUTXOAddressPath,
19
+ getUtxoApi,
20
+ stripToCashAddress,
21
+ type UTXOToolboxes,
22
+ } from "@swapkit/toolboxes/utxo";
23
+ import type { Transaction } from "@swapkit/utxo-signer";
24
+ import { bip32ToAddressNList, ChainToKeepKeyName } from "../coins";
25
+
26
+ interface KeepKeyInputObject {
27
+ addressNList: number[];
28
+ scriptType: string;
29
+ amount: string;
30
+ vout: number;
31
+ txid: string;
32
+ hex: string;
33
+ }
34
+
35
+ type KeepKeyUTXOWalletMethods = Record<string, unknown> & { address: string };
36
+
37
+ export async function utxoWalletMethods({
38
+ sdk,
39
+ chain,
40
+ derivationPath,
41
+ }: {
42
+ sdk: KeepKeySdk;
43
+ chain: Exclude<UTXOChain, typeof Chain.Zcash>;
44
+ derivationPath?: DerivationPathArray;
45
+ }): Promise<KeepKeyUTXOWalletMethods> {
46
+ const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
47
+ // This might not work for BCH
48
+ const toolbox = await getUtxoToolbox(chain);
49
+ const scriptType = [Chain.Bitcoin, Chain.Litecoin].includes(chain as typeof Chain.Bitcoin)
50
+ ? ("p2wpkh" as const)
51
+ : ("p2pkh" as const);
52
+
53
+ const derivationPathString = derivationPath ? derivationPathToString(derivationPath) : `${DerivationPath[chain]}/0`;
54
+
55
+ const addressInfo = {
56
+ address_n: bip32ToAddressNList(derivationPathString),
57
+ coin: ChainToKeepKeyName[chain],
58
+ script_type: scriptType,
59
+ };
60
+
61
+ const walletAddress: string = (await sdk.address.utxoGetAddress(addressInfo)).address;
62
+ const network = getNetworkForChain(chain);
63
+
64
+ const signTransaction = async (tx: Transaction, inputs: KeepKeyInputObject[], memo = "") => {
65
+ const outputs: any[] = [];
66
+
67
+ for (let i = 0; i < tx.outputsLength; i++) {
68
+ const output = tx.getOutput(i);
69
+ const address = tx.getOutputAddress(i, network);
70
+ const value = Number(output.amount);
71
+
72
+ if (address === walletAddress) {
73
+ outputs.push({
74
+ addressNList: addressInfo.address_n,
75
+ addressType: "change",
76
+ amount: value,
77
+ isChange: true,
78
+ scriptType,
79
+ });
80
+ } else if (address) {
81
+ const outputAddress = chain === Chain.BitcoinCash ? stripToCashAddress(address) : address;
82
+
83
+ if (outputAddress) {
84
+ outputs.push({ address: outputAddress, addressType: "spend", amount: value });
85
+ }
86
+ }
87
+ }
88
+
89
+ const removeNullAndEmptyObjectsFromArray = (arr: any[]) => {
90
+ return arr.filter((item) => item !== null && typeof item === "object" && Object.keys(item).length > 0);
91
+ };
92
+
93
+ const responseSign = await sdk.utxo.utxoSignTransaction({
94
+ coin: ChainToKeepKeyName[chain],
95
+ inputs,
96
+ opReturnData: memo,
97
+ outputs: removeNullAndEmptyObjectsFromArray(outputs),
98
+ });
99
+
100
+ return responseSign.serializedTx?.toString();
101
+ };
102
+
103
+ const transfer = async ({ recipient, feeOptionKey, feeRate, memo, ...rest }: GenericTransferParams) => {
104
+ if (!walletAddress)
105
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "From address must be provided" });
106
+ if (!recipient)
107
+ throw new SwapKitError("wallet_keepkey_invalid_params", { reason: "Recipient address must be provided" });
108
+
109
+ const createTxMethod =
110
+ chain === Chain.BitcoinCash
111
+ ? (toolbox as UTXOToolboxes["BCH"]).buildTx
112
+ : (toolbox as UTXOToolboxes["BTC"]).createTransaction;
113
+
114
+ const { tx, inputs: rawInputs } = await createTxMethod({
115
+ ...rest,
116
+ feeRate: feeRate || (await toolbox.getFeeRates())[feeOptionKey || FeeOption.Fast],
117
+ fetchTxHex: true,
118
+ memo,
119
+ recipient,
120
+ sender: walletAddress,
121
+ });
122
+
123
+ const inputs = rawInputs.map(({ value, index, hash, txHex }) => ({
124
+ //@TODO don't hardcode master, lookup on blockbook what input this is for and what path that address is!
125
+ addressNList: addressInfo.address_n,
126
+ amount: value.toString(),
127
+ hex: txHex || "",
128
+ scriptType,
129
+ txid: hash,
130
+ vout: index,
131
+ }));
132
+
133
+ const txHex = await signTransaction(tx, inputs, memo);
134
+ return toolbox.broadcastTx(txHex);
135
+ };
136
+
137
+ const signTransactionWithMultipleInputs = async (
138
+ tx: Transaction,
139
+ inputs: Array<{
140
+ hash: string;
141
+ index: number;
142
+ value: number;
143
+ txHex?: string;
144
+ derivationIndex: number;
145
+ isChange: boolean;
146
+ }>,
147
+ memo = "",
148
+ ) => {
149
+ const accountAddressN = bip32ToAddressNList(
150
+ derivationPath
151
+ ? derivationPathToString(derivationPath.slice(0, 3) as DerivationPathArray)
152
+ : DerivationPath[chain],
153
+ );
154
+
155
+ type KeepKeyOutput =
156
+ | { addressNList: number[]; addressType: "change"; amount: number; isChange: true; scriptType: string }
157
+ | { address: string; addressType: "spend"; amount: number };
158
+
159
+ const outputs: KeepKeyOutput[] = [];
160
+ for (let i = 0; i < tx.outputsLength; i++) {
161
+ const output = tx.getOutput(i);
162
+ const outputAddress = tx.getOutputAddress(i, network);
163
+ const value = Number(output.amount);
164
+
165
+ if (outputAddress === walletAddress) {
166
+ outputs.push({
167
+ addressNList: addressInfo.address_n,
168
+ addressType: "change",
169
+ amount: value,
170
+ isChange: true,
171
+ scriptType,
172
+ });
173
+ } else if (outputAddress) {
174
+ const finalAddress = chain === Chain.BitcoinCash ? stripToCashAddress(outputAddress) : outputAddress;
175
+ if (finalAddress) {
176
+ outputs.push({ address: finalAddress, addressType: "spend", amount: value });
177
+ }
178
+ }
179
+ }
180
+
181
+ const keepKeyInputs = inputs.map(({ hash, index: inputIndex, value, txHex, derivationIndex, isChange }) => {
182
+ const changePath = isChange ? 1 : 0;
183
+ const inputAddressN = [...accountAddressN, changePath, derivationIndex];
184
+ return {
185
+ addressNList: inputAddressN,
186
+ amount: value.toString(),
187
+ hex: txHex || "",
188
+ scriptType,
189
+ txid: hash,
190
+ vout: inputIndex,
191
+ };
192
+ });
193
+
194
+ const responseSign = await sdk.utxo.utxoSignTransaction({
195
+ coin: ChainToKeepKeyName[chain],
196
+ inputs: keepKeyInputs,
197
+ opReturnData: memo,
198
+ outputs: outputs.filter((item) => item !== null && typeof item === "object" && Object.keys(item).length > 0),
199
+ });
200
+
201
+ return responseSign.serializedTx?.toString() || "";
202
+ };
203
+
204
+ const transferFromMultipleAddresses = async ({
205
+ utxos,
206
+ recipient,
207
+ assetValue,
208
+ memo,
209
+ feeRate,
210
+ feeOptionKey,
211
+ }: {
212
+ utxos: Array<{
213
+ hash: string;
214
+ index: number;
215
+ value: number;
216
+ txHex?: string;
217
+ derivationIndex: number;
218
+ isChange: boolean;
219
+ address: string;
220
+ }>;
221
+ recipient: string;
222
+ assetValue: { getBaseValue: (unit: string) => number; chain: string };
223
+ memo?: string;
224
+ feeRate?: number;
225
+ feeOptionKey?: (typeof FeeOption)[keyof typeof FeeOption];
226
+ }) => {
227
+ const txFeeRate = feeRate || (await toolbox.getFeeRates())[feeOptionKey || FeeOption.Fast];
228
+
229
+ const createTxMethod =
230
+ chain === Chain.BitcoinCash
231
+ ? (toolbox as UTXOToolboxes["BCH"]).buildTx
232
+ : (toolbox as UTXOToolboxes["BTC"]).createTransaction;
233
+
234
+ const { tx, inputs: selectedInputs } = await createTxMethod({
235
+ assetValue: assetValue as any,
236
+ feeRate: txFeeRate,
237
+ fetchTxHex: true,
238
+ memo,
239
+ recipient,
240
+ sender: walletAddress,
241
+ });
242
+
243
+ const inputsWithDerivation = selectedInputs.map(
244
+ (input: { hash: string; index: number; value: number; txHex?: string }) => {
245
+ const utxoInfo = utxos.find((u) => u.hash === input.hash && u.index === input.index);
246
+ return { ...input, derivationIndex: utxoInfo?.derivationIndex ?? 0, isChange: utxoInfo?.isChange ?? false };
247
+ },
248
+ );
249
+
250
+ const signedTxHex = await signTransactionWithMultipleInputs(tx, inputsWithDerivation, memo);
251
+ return toolbox.broadcastTx(signedTxHex);
252
+ };
253
+
254
+ async function getExtendedPublicKeyInfo({ accountIndex }: { accountIndex?: number } = {}) {
255
+ try {
256
+ const resolvedAccountPath = getUTXOAccountPath({ accountIndex, chain, derivationPath });
257
+ const resolvedAccountPathString = derivationPathToString(resolvedAccountPath);
258
+ const path = {
259
+ address_n: bip32ToAddressNList(resolvedAccountPathString),
260
+ coin: ChainToKeepKeyName[chain],
261
+ script_type: scriptType,
262
+ showDisplay: false,
263
+ symbol: chain.toUpperCase(),
264
+ };
265
+
266
+ const responsePubkey = await sdk.system.info.getPublicKey(path);
267
+
268
+ return {
269
+ accountIndex: getUTXOAccountIndexFromPath(resolvedAccountPath),
270
+ path: resolvedAccountPathString,
271
+ xpub: responsePubkey.xpub,
272
+ };
273
+ } catch (error) {
274
+ throw new SwapKitError("wallet_keepkey_failed_to_get_public_key", {
275
+ chain,
276
+ error: error instanceof Error ? error.message : "Unknown error",
277
+ });
278
+ }
279
+ }
280
+
281
+ function getExtendedPublicKey() {
282
+ return getExtendedPublicKeyInfo();
283
+ }
284
+
285
+ async function deriveAddressAtIndex({
286
+ accountIndex,
287
+ index,
288
+ change = false,
289
+ }: {
290
+ accountIndex?: number;
291
+ index: number;
292
+ change?: boolean;
293
+ }) {
294
+ try {
295
+ assertDerivationIndex("index", index);
296
+ const fullPath = getUTXOAddressPath({ accountIndex, chain, change, derivationPath, index });
297
+ const fullPathString = derivationPathToString(fullPath);
298
+
299
+ const result = await sdk.address.utxoGetAddress({
300
+ address_n: bip32ToAddressNList(fullPathString),
301
+ coin: ChainToKeepKeyName[chain],
302
+ script_type: scriptType,
303
+ });
304
+
305
+ let finalAddress = result.address;
306
+ if (chain === Chain.BitcoinCash) {
307
+ finalAddress = stripToCashAddress(result.address);
308
+ }
309
+
310
+ return {
311
+ accountIndex: getUTXOAccountIndexFromPath(fullPath),
312
+ address: finalAddress,
313
+ change,
314
+ index,
315
+ path: fullPathString,
316
+ pubkey: "",
317
+ };
318
+ } catch {
319
+ return undefined;
320
+ }
321
+ }
322
+
323
+ async function deriveAddresses({
324
+ accountIndex,
325
+ count,
326
+ startIndex = 0,
327
+ change = false,
328
+ }: {
329
+ accountIndex?: number;
330
+ count: number;
331
+ startIndex?: number;
332
+ change?: boolean;
333
+ }) {
334
+ assertDerivationIndex("count", count);
335
+ assertDerivationIndex("startIndex", startIndex);
336
+
337
+ const addresses = await Promise.all(
338
+ Array.from({ length: count }, (_, i) => deriveAddressAtIndex({ accountIndex, change, index: startIndex + i })),
339
+ );
340
+
341
+ return addresses.filter((address) => !!address);
342
+ }
343
+
344
+ const hdHelpers = createHDWalletHelpers({
345
+ chain,
346
+ deriveAddress: deriveAddressAtIndex,
347
+ getBalance: toolbox.getBalance,
348
+ getUtxos: (address: string) => getUtxoApi(chain).getUtxos({ address, fetchTxHex: true }),
349
+ });
350
+
351
+ return {
352
+ ...toolbox,
353
+ ...hdHelpers,
354
+ address: walletAddress,
355
+ deriveAddressAtIndex,
356
+ deriveAddresses,
357
+ getExtendedPublicKey,
358
+ getExtendedPublicKeyInfo,
359
+ signTransaction,
360
+ signTransactionWithMultipleInputs,
361
+ transfer,
362
+ transferFromMultipleAddresses,
363
+ };
364
+ }