@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.
- package/dist/keepkey/index.cjs.map +2 -2
- package/dist/keepkey/index.js.map +2 -2
- package/dist/types/keepkey/chains/utxo.d.ts +5 -379
- package/dist/types/keepkey/chains/utxo.d.ts.map +1 -1
- package/package.json +5 -7
- package/src/index.ts +1 -0
- package/src/keepkey/chains/cosmos.ts +69 -0
- package/src/keepkey/chains/evm.ts +141 -0
- package/src/keepkey/chains/mayachain.ts +98 -0
- package/src/keepkey/chains/ripple.ts +88 -0
- package/src/keepkey/chains/thorchain.ts +93 -0
- package/src/keepkey/chains/utxo.ts +364 -0
- package/src/keepkey/coins.ts +67 -0
- package/src/keepkey/index.ts +174 -0
- package/src/ledger/clients/cosmos.ts +84 -0
- package/src/ledger/clients/evm.ts +186 -0
- package/src/ledger/clients/near.ts +63 -0
- package/src/ledger/clients/sui.ts +130 -0
- package/src/ledger/clients/thorchain/common.ts +93 -0
- package/src/ledger/clients/thorchain/helpers.ts +120 -0
- package/src/ledger/clients/thorchain/index.ts +87 -0
- package/src/ledger/clients/thorchain/lib.ts +258 -0
- package/src/ledger/clients/thorchain/utils.ts +69 -0
- package/src/ledger/clients/tron.ts +85 -0
- package/src/ledger/clients/utxo-legacy-adapter.ts +71 -0
- package/src/ledger/clients/utxo-psbt.ts +145 -0
- package/src/ledger/clients/utxo.ts +359 -0
- package/src/ledger/clients/xrp.ts +50 -0
- package/src/ledger/cosmosTypes.ts +98 -0
- package/src/ledger/helpers/getLedgerAddress.ts +76 -0
- package/src/ledger/helpers/getLedgerClient.ts +124 -0
- package/src/ledger/helpers/getLedgerTransport.ts +102 -0
- package/src/ledger/helpers/index.ts +3 -0
- package/src/ledger/index.ts +546 -0
- package/src/ledger/interfaces/CosmosLedgerInterface.ts +54 -0
- package/src/ledger/types.ts +42 -0
- package/src/trezor/evmSigner.ts +210 -0
- package/src/trezor/index.ts +847 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import type BitcoinApp from "@ledgerhq/hw-app-btc";
|
|
2
|
+
import type { CreateTransactionArg } from "@ledgerhq/hw-app-btc/lib-es/createTransaction";
|
|
3
|
+
import { hex } from "@scure/base";
|
|
4
|
+
import { type DerivationPathArray, derivationPathToString, getWalletFormatFor, SwapKitError } from "@swapkit/helpers";
|
|
5
|
+
import type { UTXOType } from "@swapkit/toolboxes/utxo";
|
|
6
|
+
import type { PCZT, Transaction } from "@swapkit/utxo-signer";
|
|
7
|
+
|
|
8
|
+
import { getLedgerTransport } from "../helpers/getLedgerTransport";
|
|
9
|
+
|
|
10
|
+
const nonSegwitLedgerChains = ["bitcoin-cash", "dash", "dogecoin", "zcash"];
|
|
11
|
+
|
|
12
|
+
type Params = {
|
|
13
|
+
tx: Transaction;
|
|
14
|
+
inputUtxos: UTXOType[];
|
|
15
|
+
btcApp: BitcoinApp;
|
|
16
|
+
derivationPath: string;
|
|
17
|
+
chain: "bitcoin-cash" | "bitcoin" | "litecoin" | "dogecoin" | "dash" | "zcash";
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type MultiPathParams = Omit<Params, "derivationPath"> & {
|
|
21
|
+
/** Derivation paths for each input - one per input */
|
|
22
|
+
derivationPaths: string[];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const signUTXOTransaction = (
|
|
26
|
+
{ tx, inputUtxos, btcApp, derivationPath, chain }: Params,
|
|
27
|
+
options?: Partial<CreateTransactionArg>,
|
|
28
|
+
) => {
|
|
29
|
+
const inputs = inputUtxos.map((item) => {
|
|
30
|
+
const splitTx = btcApp.splitTransaction(
|
|
31
|
+
item.txHex || "",
|
|
32
|
+
!nonSegwitLedgerChains.includes(chain),
|
|
33
|
+
chain === "zcash",
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return [splitTx, item.index, undefined as string | null | undefined, undefined as number | null | undefined] as any;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const newTxHex = hex.encode(tx.unsignedTx);
|
|
40
|
+
|
|
41
|
+
const splitNewTx = btcApp.splitTransaction(newTxHex, true);
|
|
42
|
+
const outputScriptHex = btcApp.serializeTransactionOutputs(splitNewTx).toString("hex");
|
|
43
|
+
|
|
44
|
+
const params: CreateTransactionArg = {
|
|
45
|
+
additionals: ["bech32"],
|
|
46
|
+
associatedKeysets: inputs.map(() => derivationPath),
|
|
47
|
+
inputs,
|
|
48
|
+
outputScriptHex,
|
|
49
|
+
segwit: true,
|
|
50
|
+
useTrustedInputForSegwit: true,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return btcApp.createPaymentTransaction({ ...params, ...options });
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Sign a UTXO transaction with multiple derivation paths.
|
|
58
|
+
* Each input can have its own derivation path for HD wallet multi-address support.
|
|
59
|
+
*/
|
|
60
|
+
const signUTXOTransactionWithMultiplePaths = (
|
|
61
|
+
{ tx, inputUtxos, btcApp, derivationPaths, chain }: MultiPathParams,
|
|
62
|
+
options?: Partial<CreateTransactionArg>,
|
|
63
|
+
) => {
|
|
64
|
+
if (derivationPaths.length !== inputUtxos.length) {
|
|
65
|
+
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
66
|
+
message: `Derivation paths count (${derivationPaths.length}) must match inputs count (${inputUtxos.length})`,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const inputs = inputUtxos.map((item) => {
|
|
71
|
+
const splitTx = btcApp.splitTransaction(
|
|
72
|
+
item.txHex || "",
|
|
73
|
+
!nonSegwitLedgerChains.includes(chain),
|
|
74
|
+
chain === "zcash",
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return [splitTx, item.index, undefined as string | null | undefined, undefined as number | null | undefined] as any;
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const newTxHex = hex.encode(tx.unsignedTx);
|
|
81
|
+
|
|
82
|
+
const splitNewTx = btcApp.splitTransaction(newTxHex, true);
|
|
83
|
+
const outputScriptHex = btcApp.serializeTransactionOutputs(splitNewTx).toString("hex");
|
|
84
|
+
|
|
85
|
+
const params: CreateTransactionArg = {
|
|
86
|
+
additionals: ["bech32"],
|
|
87
|
+
associatedKeysets: derivationPaths,
|
|
88
|
+
inputs,
|
|
89
|
+
outputScriptHex,
|
|
90
|
+
segwit: true,
|
|
91
|
+
useTrustedInputForSegwit: true,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
return btcApp.createPaymentTransaction({ ...params, ...options });
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const BaseLedgerUTXO = ({
|
|
98
|
+
chain,
|
|
99
|
+
additionalSignParams,
|
|
100
|
+
}: {
|
|
101
|
+
chain: "bitcoin-cash" | "bitcoin" | "litecoin" | "dogecoin" | "dash" | "zcash";
|
|
102
|
+
additionalSignParams?: Partial<CreateTransactionArg>;
|
|
103
|
+
}) => {
|
|
104
|
+
let btcApp: InstanceType<typeof BitcoinApp>;
|
|
105
|
+
let transport: any = null;
|
|
106
|
+
|
|
107
|
+
async function checkBtcAppAndCreateTransportWebUSB(checkBtcApp = true) {
|
|
108
|
+
if (checkBtcApp && !btcApp) {
|
|
109
|
+
new SwapKitError("wallet_ledger_connection_error", {
|
|
110
|
+
message: `Ledger connection failed:\n${JSON.stringify({ btcApp, checkBtcApp })}`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
transport ||= await getLedgerTransport();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function createTransportWebUSB() {
|
|
118
|
+
transport = await getLedgerTransport();
|
|
119
|
+
const BitcoinApp = (await import("@ledgerhq/hw-app-btc")).default;
|
|
120
|
+
|
|
121
|
+
btcApp = new BitcoinApp({ currency: chain, transport });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return (derivationPathArray?: DerivationPathArray | string) => {
|
|
125
|
+
const derivationPath =
|
|
126
|
+
typeof derivationPathArray === "string"
|
|
127
|
+
? derivationPathArray
|
|
128
|
+
: derivationPathToString(derivationPathArray as DerivationPathArray);
|
|
129
|
+
|
|
130
|
+
const format = getWalletFormatFor(derivationPath);
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
connect: async () => {
|
|
134
|
+
await checkBtcAppAndCreateTransportWebUSB(false);
|
|
135
|
+
const BitcoinApp = (await import("@ledgerhq/hw-app-btc")).default;
|
|
136
|
+
|
|
137
|
+
btcApp = new BitcoinApp({ currency: chain, transport });
|
|
138
|
+
},
|
|
139
|
+
getAddress: async () => {
|
|
140
|
+
const { toCashAddress } = await import("@swapkit/toolboxes/utxo");
|
|
141
|
+
|
|
142
|
+
await checkBtcAppAndCreateTransportWebUSB(false);
|
|
143
|
+
|
|
144
|
+
const { bitcoinAddress: address } = await btcApp.getWalletPublicKey(derivationPath, { format });
|
|
145
|
+
|
|
146
|
+
if (!address) {
|
|
147
|
+
throw new SwapKitError("wallet_ledger_get_address_error", {
|
|
148
|
+
message: `Cannot get ${chain} address from ledger derivation path: ${derivationPath}`,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return chain === "bitcoin-cash" && format === "legacy"
|
|
153
|
+
? toCashAddress(address).replace(/(bchtest:|bitcoincash:)/, "")
|
|
154
|
+
: address;
|
|
155
|
+
},
|
|
156
|
+
getExtendedPublicKey: async (path = "84'/0'/0'", xpubVersion = 76067358) => {
|
|
157
|
+
await checkBtcAppAndCreateTransportWebUSB(false);
|
|
158
|
+
|
|
159
|
+
return btcApp.getWalletXpub({ path, xpubVersion });
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
signPCZT: async (pczt: PCZT): Promise<PCZT> => {
|
|
163
|
+
if (chain !== "zcash") {
|
|
164
|
+
throw new SwapKitError("wallet_ledger_chain_not_supported", {
|
|
165
|
+
message: "PCZT signing is only supported for Zcash",
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
await createTransportWebUSB();
|
|
170
|
+
|
|
171
|
+
const { ZcashTransaction, Script } = await import("@swapkit/utxo-signer");
|
|
172
|
+
|
|
173
|
+
const global = pczt.getGlobal();
|
|
174
|
+
|
|
175
|
+
const unsignedTx = new ZcashTransaction({
|
|
176
|
+
consensusBranchId: global.consensusBranchId,
|
|
177
|
+
expiryHeight: global.expiryHeight,
|
|
178
|
+
lockTime: global.lockTime,
|
|
179
|
+
version: global.txVersion,
|
|
180
|
+
versionGroupId: global.versionGroupId,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const inputUtxos: UTXOType[] = [];
|
|
184
|
+
|
|
185
|
+
for (let i = 0; i < pczt.inputsLength; i++) {
|
|
186
|
+
const input = pczt.getInput(i);
|
|
187
|
+
|
|
188
|
+
unsignedTx.addInput({
|
|
189
|
+
index: input.index,
|
|
190
|
+
script: new Uint8Array(),
|
|
191
|
+
sequence: input.sequence ?? 0xffffffff,
|
|
192
|
+
txid: input.txid,
|
|
193
|
+
value: input.value,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
inputUtxos.push({
|
|
197
|
+
hash: hex.encode(new Uint8Array([...input.txid].reverse())),
|
|
198
|
+
index: input.index,
|
|
199
|
+
txHex: buildMinimalPrevTxHex(input, global),
|
|
200
|
+
value: Number(input.value),
|
|
201
|
+
witnessUtxo: { script: input.scriptPubkey, value: Number(input.value) },
|
|
202
|
+
} as UTXOType);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (let i = 0; i < pczt.outputsLength; i++) {
|
|
206
|
+
const output = pczt.getOutput(i);
|
|
207
|
+
unsignedTx.addOutput({ amount: output.value, script: output.scriptPubkey });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const signedTxHex = await signUTXOTransaction(
|
|
211
|
+
{ btcApp, chain, derivationPath, inputUtxos, tx: unsignedTx as unknown as Transaction },
|
|
212
|
+
{
|
|
213
|
+
...additionalSignParams,
|
|
214
|
+
expiryHeight: (() => {
|
|
215
|
+
const buf = Buffer.alloc(4);
|
|
216
|
+
buf.writeUInt32LE(global.expiryHeight);
|
|
217
|
+
return buf;
|
|
218
|
+
})(),
|
|
219
|
+
lockTime: global.lockTime,
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
const signedTx = ZcashTransaction.fromHex(signedTxHex, { allowUnknownOutputs: true });
|
|
224
|
+
const signedPczt = pczt.clone();
|
|
225
|
+
|
|
226
|
+
for (let i = 0; i < signedTx.inputsLength; i++) {
|
|
227
|
+
const signedInput = signedTx.getInput(i);
|
|
228
|
+
if (signedInput.script && signedInput.script.length > 0) {
|
|
229
|
+
const scriptParts = Script.decode(signedInput.script);
|
|
230
|
+
if (scriptParts.length >= 2) {
|
|
231
|
+
signedPczt.addSignature(i, scriptParts[1] as Uint8Array, scriptParts[0] as Uint8Array);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return signedPczt;
|
|
237
|
+
},
|
|
238
|
+
signTransaction: async (tx: Transaction, inputUtxos: UTXOType[]) => {
|
|
239
|
+
await createTransportWebUSB();
|
|
240
|
+
|
|
241
|
+
return signUTXOTransaction({ btcApp, chain, derivationPath, inputUtxos, tx }, additionalSignParams);
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Sign a transaction with multiple derivation paths for HD wallet multi-address support.
|
|
246
|
+
* Each input can be signed with its own derivation path.
|
|
247
|
+
*/
|
|
248
|
+
signTransactionWithMultiplePaths: async (tx: Transaction, inputUtxos: UTXOType[], derivationPaths: string[]) => {
|
|
249
|
+
await createTransportWebUSB();
|
|
250
|
+
|
|
251
|
+
return signUTXOTransactionWithMultiplePaths(
|
|
252
|
+
{ btcApp, chain, derivationPaths, inputUtxos, tx },
|
|
253
|
+
additionalSignParams,
|
|
254
|
+
);
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
function buildMinimalPrevTxHex(
|
|
261
|
+
input: { txid: Uint8Array; index: number; scriptPubkey: Uint8Array; value: bigint },
|
|
262
|
+
global: { txVersion: number; versionGroupId: number; expiryHeight: number; lockTime: number },
|
|
263
|
+
): string {
|
|
264
|
+
const parts: number[] = [];
|
|
265
|
+
|
|
266
|
+
const version = (global.txVersion | 0x80000000) >>> 0;
|
|
267
|
+
parts.push(version & 0xff, (version >> 8) & 0xff, (version >> 16) & 0xff, (version >> 24) & 0xff);
|
|
268
|
+
|
|
269
|
+
const vgid = global.versionGroupId;
|
|
270
|
+
parts.push(vgid & 0xff, (vgid >> 8) & 0xff, (vgid >> 16) & 0xff, (vgid >> 24) & 0xff);
|
|
271
|
+
|
|
272
|
+
parts.push(0);
|
|
273
|
+
|
|
274
|
+
const outputCount = input.index + 1;
|
|
275
|
+
if (outputCount < 0xfd) {
|
|
276
|
+
parts.push(outputCount);
|
|
277
|
+
} else {
|
|
278
|
+
parts.push(0xfd, outputCount & 0xff, (outputCount >> 8) & 0xff);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
for (let i = 0; i < input.index; i++) {
|
|
282
|
+
parts.push(0, 0, 0, 0, 0, 0, 0, 0);
|
|
283
|
+
parts.push(0);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const value = input.value;
|
|
287
|
+
parts.push(
|
|
288
|
+
Number(value & 0xffn),
|
|
289
|
+
Number((value >> 8n) & 0xffn),
|
|
290
|
+
Number((value >> 16n) & 0xffn),
|
|
291
|
+
Number((value >> 24n) & 0xffn),
|
|
292
|
+
Number((value >> 32n) & 0xffn),
|
|
293
|
+
Number((value >> 40n) & 0xffn),
|
|
294
|
+
Number((value >> 48n) & 0xffn),
|
|
295
|
+
Number((value >> 56n) & 0xffn),
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const script = input.scriptPubkey;
|
|
299
|
+
if (script.length < 0xfd) {
|
|
300
|
+
parts.push(script.length);
|
|
301
|
+
} else {
|
|
302
|
+
parts.push(0xfd, script.length & 0xff, (script.length >> 8) & 0xff);
|
|
303
|
+
}
|
|
304
|
+
for (const byte of script) {
|
|
305
|
+
parts.push(byte);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
parts.push(
|
|
309
|
+
global.lockTime & 0xff,
|
|
310
|
+
(global.lockTime >> 8) & 0xff,
|
|
311
|
+
(global.lockTime >> 16) & 0xff,
|
|
312
|
+
(global.lockTime >> 24) & 0xff,
|
|
313
|
+
);
|
|
314
|
+
parts.push(
|
|
315
|
+
global.expiryHeight & 0xff,
|
|
316
|
+
(global.expiryHeight >> 8) & 0xff,
|
|
317
|
+
(global.expiryHeight >> 16) & 0xff,
|
|
318
|
+
(global.expiryHeight >> 24) & 0xff,
|
|
319
|
+
);
|
|
320
|
+
parts.push(0, 0, 0, 0, 0, 0, 0, 0); // value balance
|
|
321
|
+
parts.push(0); // empty sapling spends
|
|
322
|
+
parts.push(0); // empty sapling outputs
|
|
323
|
+
parts.push(0); // empty joinsplits
|
|
324
|
+
|
|
325
|
+
return hex.encode(new Uint8Array(parts));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export const BitcoinLedger = BaseLedgerUTXO({ chain: "bitcoin" });
|
|
329
|
+
export const LitecoinLedger = BaseLedgerUTXO({ chain: "litecoin" });
|
|
330
|
+
|
|
331
|
+
export const BitcoinCashLedger = BaseLedgerUTXO({
|
|
332
|
+
additionalSignParams: { additionals: ["abc"], segwit: false, sigHashType: 0x41 },
|
|
333
|
+
chain: "bitcoin-cash",
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
export const DogecoinLedger = BaseLedgerUTXO({
|
|
337
|
+
additionalSignParams: { additionals: [], segwit: false, useTrustedInputForSegwit: false },
|
|
338
|
+
chain: "dogecoin",
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
export const DashLedger = BaseLedgerUTXO({
|
|
342
|
+
additionalSignParams: { additionals: [], segwit: false, useTrustedInputForSegwit: false },
|
|
343
|
+
chain: "dash",
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
export const ZcashLedger = BaseLedgerUTXO({
|
|
347
|
+
additionalSignParams: {
|
|
348
|
+
additionals: ["zcash", "sapling"],
|
|
349
|
+
expiryHeight: (() => {
|
|
350
|
+
const buf = Buffer.alloc(4);
|
|
351
|
+
buf.writeUInt32LE(0);
|
|
352
|
+
return buf;
|
|
353
|
+
})(),
|
|
354
|
+
lockTime: 0,
|
|
355
|
+
segwit: false,
|
|
356
|
+
useTrustedInputForSegwit: false,
|
|
357
|
+
},
|
|
358
|
+
chain: "zcash",
|
|
359
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Xrp from "@ledgerhq/hw-app-xrp";
|
|
2
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
3
|
+
import { Chain, type DerivationPathArray, derivationPathToString, NetworkDerivationPath } from "@swapkit/helpers";
|
|
4
|
+
import type { RippleTransaction } from "@swapkit/toolboxes/ripple";
|
|
5
|
+
import { encode } from "ripple-binary-codec";
|
|
6
|
+
import type { Payment } from "xrpl";
|
|
7
|
+
import { getLedgerTransport } from "../helpers/getLedgerTransport";
|
|
8
|
+
|
|
9
|
+
const TF_FULLY_CANONICAL_SIG = 2147483648;
|
|
10
|
+
|
|
11
|
+
function cleanTransactionObject(obj: Record<string, any>) {
|
|
12
|
+
const cleaned: Record<string, any> = {};
|
|
13
|
+
for (const key in obj) {
|
|
14
|
+
if (obj[key] !== null && obj[key] !== undefined) {
|
|
15
|
+
cleaned[key] = obj[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return cleaned;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function establishConnection(transport: Transport) {
|
|
22
|
+
return new Xrp(transport);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const XRPLedger = async (derivationPath?: DerivationPathArray) => {
|
|
26
|
+
const path = derivationPathToString(derivationPath || NetworkDerivationPath[Chain.Ripple]);
|
|
27
|
+
const transport = await getLedgerTransport();
|
|
28
|
+
const xrpInstance = establishConnection(transport);
|
|
29
|
+
|
|
30
|
+
const { address, publicKey } = await xrpInstance.getAddress(path);
|
|
31
|
+
|
|
32
|
+
async function signTransaction(transaction: Payment | RippleTransaction) {
|
|
33
|
+
const { hashes } = await import("@swapkit/toolboxes/ripple");
|
|
34
|
+
const cleanedTxWithPubKey = cleanTransactionObject(transaction);
|
|
35
|
+
const transactionJSON = {
|
|
36
|
+
...cleanedTxWithPubKey,
|
|
37
|
+
Flags: transaction.Flags || TF_FULLY_CANONICAL_SIG,
|
|
38
|
+
SigningPubKey: publicKey.toUpperCase(),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const transactionToSignOnLedger = encode(transactionJSON);
|
|
42
|
+
const txnSignature = await xrpInstance.signTransaction(path, transactionToSignOnLedger);
|
|
43
|
+
const tx_blob = encode({ ...transactionJSON, TxnSignature: txnSignature });
|
|
44
|
+
const hash = hashes.hashSignedTx(tx_blob);
|
|
45
|
+
|
|
46
|
+
return { hash, tx_blob };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return { getAddress: () => address, signTransaction };
|
|
50
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { SwapKitError } from "@swapkit/helpers";
|
|
2
|
+
|
|
3
|
+
export interface Coin {
|
|
4
|
+
readonly denom: string;
|
|
5
|
+
readonly amount: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface EncodeObject {
|
|
9
|
+
readonly typeUrl: string;
|
|
10
|
+
readonly value: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AminoMsg {
|
|
14
|
+
readonly type: string;
|
|
15
|
+
readonly value: any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface AminoMsgSend extends AminoMsg {
|
|
19
|
+
readonly type: "cosmos-sdk/MsgSend";
|
|
20
|
+
readonly value: {
|
|
21
|
+
/** Bech32 account address */
|
|
22
|
+
readonly from_address: string;
|
|
23
|
+
/** Bech32 account address */
|
|
24
|
+
readonly to_address: string;
|
|
25
|
+
readonly amount: readonly Coin[];
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface AminoConverter {
|
|
30
|
+
readonly aminoType: string;
|
|
31
|
+
readonly toAmino: (value: any) => any;
|
|
32
|
+
readonly fromAmino: (value: any) => any;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A map from protobuf type URL to the AminoConverter implementation if supported on chain */
|
|
36
|
+
export type AminoConverters = Record<string, AminoConverter | "not_supported_by_chain">;
|
|
37
|
+
|
|
38
|
+
function isAminoConverter(
|
|
39
|
+
converter: [string, AminoConverter | "not_supported_by_chain"],
|
|
40
|
+
): converter is [string, AminoConverter] {
|
|
41
|
+
return typeof converter[1] !== "string";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A map from Stargate message types as used in the messages's `Any` type
|
|
46
|
+
* to Amino types.
|
|
47
|
+
*/
|
|
48
|
+
export class AminoTypes {
|
|
49
|
+
// The map type here ensures uniqueness of the protobuf type URL in the key.
|
|
50
|
+
// There is no uniqueness guarantee of the Amino type identifier in the type
|
|
51
|
+
// system or constructor. Instead it's the user's responsibility to ensure
|
|
52
|
+
// there is no overlap when fromAmino is called.
|
|
53
|
+
private readonly register: Record<string, AminoConverter | "not_supported_by_chain">;
|
|
54
|
+
|
|
55
|
+
constructor(types: AminoConverters) {
|
|
56
|
+
this.register = types;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
toAmino({ typeUrl, value }: EncodeObject): AminoMsg {
|
|
60
|
+
const converter = this.register[typeUrl];
|
|
61
|
+
if (converter === "not_supported_by_chain") {
|
|
62
|
+
throw new SwapKitError("wallet_ledger_chain_not_supported", {
|
|
63
|
+
reason: `The message type '${typeUrl}' cannot be signed using the Amino JSON sign mode because this is not supported by chain.`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (!converter) {
|
|
67
|
+
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
68
|
+
reason: `Type URL '${typeUrl}' does not exist in the Amino message type register.`,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return { type: converter.aminoType, value: converter.toAmino(value) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fromAmino({ type, value }: AminoMsg): EncodeObject {
|
|
75
|
+
const matches = Object.entries(this.register)
|
|
76
|
+
.filter(isAminoConverter)
|
|
77
|
+
.filter(([_typeUrl, { aminoType }]) => aminoType === type);
|
|
78
|
+
|
|
79
|
+
if (matches.length === 0) {
|
|
80
|
+
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
81
|
+
reason: `Amino type identifier '${type}' does not exist in the Amino message type register.`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (matches.length > 1) {
|
|
86
|
+
throw new SwapKitError("wallet_ledger_invalid_params", {
|
|
87
|
+
reason: `Multiple types are registered with Amino type identifier '${type}': '${matches
|
|
88
|
+
.map(([key, _value]) => key)
|
|
89
|
+
.sort()
|
|
90
|
+
.join("', '")}'. Thus fromAmino cannot be performed.`,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const [typeUrl, converter] = matches[0] as [string, AminoConverter];
|
|
95
|
+
|
|
96
|
+
return { typeUrl, value: converter.fromAmino(value) };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Chain, SwapKitError, WalletOption } from "@swapkit/helpers";
|
|
2
|
+
|
|
3
|
+
import type { getNearLedgerClient } from "../clients/near";
|
|
4
|
+
import type { SuiLedger } from "../clients/sui";
|
|
5
|
+
import type { TronLedger } from "../clients/tron";
|
|
6
|
+
import type { XRPLedger } from "../clients/xrp";
|
|
7
|
+
import type { LEDGER_SUPPORTED_CHAINS } from "../index";
|
|
8
|
+
import type { CosmosLedgerClients, EVMLedgerClients, UTXOLedgerClients } from "../types";
|
|
9
|
+
import type { getLedgerClient } from "./getLedgerClient";
|
|
10
|
+
|
|
11
|
+
export const getLedgerAddress = async <
|
|
12
|
+
T extends (typeof LEDGER_SUPPORTED_CHAINS)[number],
|
|
13
|
+
L extends Awaited<ReturnType<typeof getLedgerClient<T>>>,
|
|
14
|
+
>({
|
|
15
|
+
chain,
|
|
16
|
+
ledgerClient,
|
|
17
|
+
}: {
|
|
18
|
+
chain: T;
|
|
19
|
+
ledgerClient: L;
|
|
20
|
+
}) => {
|
|
21
|
+
if (!ledgerClient) return "";
|
|
22
|
+
|
|
23
|
+
switch (chain) {
|
|
24
|
+
case Chain.Cosmos:
|
|
25
|
+
case Chain.THORChain: {
|
|
26
|
+
return (ledgerClient as CosmosLedgerClients).connect();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
case Chain.Ethereum:
|
|
30
|
+
case Chain.BinanceSmartChain:
|
|
31
|
+
case Chain.Avalanche:
|
|
32
|
+
case Chain.Polygon:
|
|
33
|
+
case Chain.Arbitrum:
|
|
34
|
+
case Chain.Berachain:
|
|
35
|
+
case Chain.Optimism:
|
|
36
|
+
case Chain.Base:
|
|
37
|
+
case Chain.Aurora:
|
|
38
|
+
case Chain.Gnosis:
|
|
39
|
+
case Chain.Monad:
|
|
40
|
+
case Chain.XLayer: {
|
|
41
|
+
return (ledgerClient as EVMLedgerClients).getAddress();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
case Chain.Bitcoin:
|
|
45
|
+
case Chain.BitcoinCash:
|
|
46
|
+
case Chain.Dash:
|
|
47
|
+
case Chain.Dogecoin:
|
|
48
|
+
case Chain.Litecoin:
|
|
49
|
+
case Chain.Zcash: {
|
|
50
|
+
const ledger = ledgerClient as UTXOLedgerClients;
|
|
51
|
+
await ledger.connect();
|
|
52
|
+
const address = await ledger.getAddress();
|
|
53
|
+
|
|
54
|
+
return chain === Chain.BitcoinCash ? address.replace("bitcoincash:", "") : address;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
case Chain.Near: {
|
|
58
|
+
return await (ledgerClient as Awaited<ReturnType<typeof getNearLedgerClient>>).getAddress();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
case Chain.Ripple: {
|
|
62
|
+
return (ledgerClient as Awaited<ReturnType<typeof XRPLedger>>).getAddress();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
case Chain.Tron: {
|
|
66
|
+
return (ledgerClient as Awaited<ReturnType<typeof TronLedger>>).getAddress();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
case Chain.Sui: {
|
|
70
|
+
return (ledgerClient as Awaited<ReturnType<typeof SuiLedger>>).connect();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
default:
|
|
74
|
+
throw new SwapKitError("wallet_chain_not_supported", { chain, wallet: WalletOption.LEDGER });
|
|
75
|
+
}
|
|
76
|
+
};
|