@swapkit/wallet-hardware 4.8.1 → 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/package.json +2 -2
- 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,258 @@
|
|
|
1
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
2
|
+
import { SwapKitError } from "@swapkit/helpers";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
CHUNK_SIZE,
|
|
6
|
+
CLA,
|
|
7
|
+
ERROR_CODE,
|
|
8
|
+
errorCodeToString,
|
|
9
|
+
getVersion,
|
|
10
|
+
INS,
|
|
11
|
+
P1_VALUES,
|
|
12
|
+
P2_VALUES,
|
|
13
|
+
processErrorResponse,
|
|
14
|
+
} from "./common";
|
|
15
|
+
import {
|
|
16
|
+
publicKeyv1,
|
|
17
|
+
publicKeyv2,
|
|
18
|
+
serializePathv1,
|
|
19
|
+
serializePathv2,
|
|
20
|
+
signSendChunkv1,
|
|
21
|
+
signSendChunkv2,
|
|
22
|
+
} from "./helpers";
|
|
23
|
+
|
|
24
|
+
export class THORChainApp {
|
|
25
|
+
transport: Transport;
|
|
26
|
+
versionResponse: any;
|
|
27
|
+
|
|
28
|
+
constructor(transport: any) {
|
|
29
|
+
if (!transport) {
|
|
30
|
+
throw new SwapKitError("wallet_ledger_transport_not_defined");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.transport = transport;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static serializeHRP(hrp: string) {
|
|
37
|
+
if (hrp == null || hrp.length < 3 || hrp.length > 83) {
|
|
38
|
+
throw new SwapKitError("wallet_ledger_invalid_params", { reason: "Invalid HRP" });
|
|
39
|
+
}
|
|
40
|
+
const buf = Buffer.alloc(1 + hrp.length);
|
|
41
|
+
buf.writeUInt8(hrp.length, 0);
|
|
42
|
+
buf.write(hrp, 1);
|
|
43
|
+
return buf;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async serializePath(path: number[]) {
|
|
47
|
+
this.versionResponse = await getVersion(this.transport);
|
|
48
|
+
|
|
49
|
+
if (this.versionResponse.return_code !== ERROR_CODE.NoError) {
|
|
50
|
+
throw this.versionResponse;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
switch (this.versionResponse.major) {
|
|
54
|
+
case 1:
|
|
55
|
+
return serializePathv1(path);
|
|
56
|
+
case 2:
|
|
57
|
+
return serializePathv2(path);
|
|
58
|
+
default:
|
|
59
|
+
return Buffer.alloc(0);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async signGetChunks(path: number[], buffer: Buffer) {
|
|
64
|
+
const serializedPath = await this.serializePath(path);
|
|
65
|
+
|
|
66
|
+
const chunks = [];
|
|
67
|
+
chunks.push(serializedPath);
|
|
68
|
+
|
|
69
|
+
for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
|
|
70
|
+
let end = i + CHUNK_SIZE;
|
|
71
|
+
if (i > buffer.length) {
|
|
72
|
+
end = buffer.length;
|
|
73
|
+
}
|
|
74
|
+
chunks.push(buffer.slice(i, end));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return chunks;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async getVersion() {
|
|
81
|
+
try {
|
|
82
|
+
this.versionResponse = await getVersion(this.transport);
|
|
83
|
+
return this.versionResponse;
|
|
84
|
+
} catch (e) {
|
|
85
|
+
return processErrorResponse(e);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
appInfo() {
|
|
90
|
+
return this.transport.send(0xb0, 0x01, 0, 0).then((response: any) => {
|
|
91
|
+
const returnCode = response.readUInt16BE(response.length - 2);
|
|
92
|
+
|
|
93
|
+
let appName = "";
|
|
94
|
+
let appVersion = "";
|
|
95
|
+
let flagLen = 0;
|
|
96
|
+
let flagsValue = 0;
|
|
97
|
+
|
|
98
|
+
if (response[0] !== 1) {
|
|
99
|
+
// Ledger responds with format ID 1. There is no spec for any format != 1
|
|
100
|
+
return { error_message: "response format ID not recognized", return_code: 0x9001 };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const appNameLen = response[1];
|
|
104
|
+
appName = response.slice(2, 2 + appNameLen).toString("ascii");
|
|
105
|
+
let idx = 2 + appNameLen;
|
|
106
|
+
const appVersionLen = response[idx];
|
|
107
|
+
idx += 1;
|
|
108
|
+
appVersion = response.slice(idx, idx + appVersionLen).toString("ascii");
|
|
109
|
+
idx += appVersionLen;
|
|
110
|
+
const appFlagsLen = response[idx];
|
|
111
|
+
idx += 1;
|
|
112
|
+
flagLen = appFlagsLen;
|
|
113
|
+
flagsValue = response[idx];
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
appName,
|
|
117
|
+
appVersion,
|
|
118
|
+
error_message: errorCodeToString(returnCode),
|
|
119
|
+
flag_onboarded: (flagsValue & 4) !== 0,
|
|
120
|
+
flag_pin_validated: (flagsValue & 128) !== 0,
|
|
121
|
+
flag_recovery: (flagsValue & 1) !== 0,
|
|
122
|
+
flag_signed_mcu_code: (flagsValue & 2) !== 0,
|
|
123
|
+
flagLen,
|
|
124
|
+
flagsValue,
|
|
125
|
+
return_code: returnCode,
|
|
126
|
+
};
|
|
127
|
+
}, processErrorResponse);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
deviceInfo() {
|
|
131
|
+
return this.transport
|
|
132
|
+
.send(0xe0, 0x01, 0, 0, Buffer.from([]), [ERROR_CODE.NoError, 0x6e00])
|
|
133
|
+
.then((response: any) => {
|
|
134
|
+
const errorCodeData = response.slice(-2);
|
|
135
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
136
|
+
|
|
137
|
+
if (returnCode === 0x6e00) {
|
|
138
|
+
return { error_message: "This command is only available in the Dashboard", return_code: returnCode };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const targetId = response.slice(0, 4).toString("hex");
|
|
142
|
+
|
|
143
|
+
let pos = 4;
|
|
144
|
+
const secureElementVersionLen = response[pos];
|
|
145
|
+
pos += 1;
|
|
146
|
+
const seVersion = response.slice(pos, pos + secureElementVersionLen).toString();
|
|
147
|
+
pos += secureElementVersionLen;
|
|
148
|
+
|
|
149
|
+
const flagsLen = response[pos];
|
|
150
|
+
pos += 1;
|
|
151
|
+
const flag = response.slice(pos, pos + flagsLen).toString("hex");
|
|
152
|
+
pos += flagsLen;
|
|
153
|
+
|
|
154
|
+
const mcuVersionLen = response[pos];
|
|
155
|
+
pos += 1;
|
|
156
|
+
// Patch issue in mcu version
|
|
157
|
+
let tmp = response.slice(pos, pos + mcuVersionLen);
|
|
158
|
+
if (tmp[mcuVersionLen - 1] === 0) {
|
|
159
|
+
tmp = response.slice(pos, pos + mcuVersionLen - 1);
|
|
160
|
+
}
|
|
161
|
+
const mcuVersion = tmp.toString();
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
error_message: errorCodeToString(returnCode),
|
|
165
|
+
flag,
|
|
166
|
+
mcuVersion,
|
|
167
|
+
return_code: returnCode,
|
|
168
|
+
seVersion,
|
|
169
|
+
// //
|
|
170
|
+
targetId,
|
|
171
|
+
};
|
|
172
|
+
}, processErrorResponse);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async publicKey(path: number[]) {
|
|
176
|
+
try {
|
|
177
|
+
const serializedPath = await this.serializePath(path);
|
|
178
|
+
|
|
179
|
+
switch (this.versionResponse.major) {
|
|
180
|
+
case 1:
|
|
181
|
+
return publicKeyv1(this, serializedPath);
|
|
182
|
+
case 2: {
|
|
183
|
+
const data = Buffer.concat([THORChainApp.serializeHRP("thor"), serializedPath]);
|
|
184
|
+
return publicKeyv2(this, data);
|
|
185
|
+
}
|
|
186
|
+
default:
|
|
187
|
+
return { error_message: "App Version is not supported", return_code: 0x6400 };
|
|
188
|
+
}
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return processErrorResponse(e);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async getAddressAndPubKey(path: number[], hrp: string, showInDevice = false) {
|
|
195
|
+
try {
|
|
196
|
+
const serializedPath = await this.serializePath(path);
|
|
197
|
+
const data = Buffer.concat([THORChainApp.serializeHRP(hrp), serializedPath]);
|
|
198
|
+
const response = await this.transport.send(
|
|
199
|
+
CLA,
|
|
200
|
+
INS.GET_ADDR_SECP256K1,
|
|
201
|
+
showInDevice ? P1_VALUES.SHOW_ADDRESS_IN_DEVICE : P1_VALUES.ONLY_RETRIEVE,
|
|
202
|
+
0,
|
|
203
|
+
data,
|
|
204
|
+
[ERROR_CODE.NoError],
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const compressedPk = Buffer.from(response.slice(0, 33));
|
|
208
|
+
const bech32Address = Buffer.from(response.slice(33, -2)).toString();
|
|
209
|
+
const returnCode = response.readUInt16BE(response.length - 2);
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
bech32_address: bech32Address,
|
|
213
|
+
compressed_pk: compressedPk,
|
|
214
|
+
error_message: errorCodeToString(returnCode),
|
|
215
|
+
return_code: returnCode,
|
|
216
|
+
};
|
|
217
|
+
} catch (err) {
|
|
218
|
+
return processErrorResponse(err);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
showAddressAndPubKey(path: number[], hrp: string) {
|
|
223
|
+
return this.getAddressAndPubKey(path, hrp, true);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
|
|
227
|
+
switch (this.versionResponse.major) {
|
|
228
|
+
case 1:
|
|
229
|
+
return signSendChunkv1(this, chunkIdx, chunkNum, chunk, txType);
|
|
230
|
+
case 2:
|
|
231
|
+
return signSendChunkv2(this, chunkIdx, chunkNum, chunk, txType);
|
|
232
|
+
default:
|
|
233
|
+
return { error_message: "App Version is not supported", return_code: 0x6400 };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async sign(path: number[], message: string, txType = P2_VALUES.JSON) {
|
|
238
|
+
const buffer = Buffer.from(message);
|
|
239
|
+
let chunks: Buffer[] = [];
|
|
240
|
+
let response: any;
|
|
241
|
+
try {
|
|
242
|
+
chunks = await this.signGetChunks(path, buffer);
|
|
243
|
+
response = await this.signSendChunk(1, chunks.length, chunks[0] as Buffer, txType);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
processErrorResponse(error);
|
|
246
|
+
}
|
|
247
|
+
let result = { error_message: response.error_message, return_code: response.return_code, signature: null };
|
|
248
|
+
|
|
249
|
+
for (let i = 1; i < chunks.length; i += 1) {
|
|
250
|
+
result = await this.signSendChunk(1 + i, chunks.length, chunks[i] as Buffer, txType);
|
|
251
|
+
if (result.return_code !== ERROR_CODE.NoError) {
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return { error_message: result.error_message, return_code: result.return_code, signature: result.signature };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { base64 } from "@scure/base";
|
|
2
|
+
import { SwapKitError } from "@swapkit/helpers";
|
|
3
|
+
|
|
4
|
+
export const getSignature = (signatureArray: any) => {
|
|
5
|
+
// Check Type Length Value encoding
|
|
6
|
+
if (signatureArray.length < 64) {
|
|
7
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "Too short" });
|
|
8
|
+
}
|
|
9
|
+
if (signatureArray[0] !== 0x30) {
|
|
10
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "TLV encoding: expected first byte 0x30" });
|
|
11
|
+
}
|
|
12
|
+
if (signatureArray[1] + 2 !== signatureArray.length) {
|
|
13
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "signature length does not match TLV" });
|
|
14
|
+
}
|
|
15
|
+
if (signatureArray[2] !== 0x02) {
|
|
16
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "TLV encoding: expected length type 0x02" });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// r signature
|
|
20
|
+
const rLength = signatureArray[3];
|
|
21
|
+
let rSignature = signatureArray.slice(4, rLength + 4);
|
|
22
|
+
|
|
23
|
+
// Drop leading zero on some 'r' signatures that are 33 bytes.
|
|
24
|
+
if (rSignature.length === 33 && rSignature[0] === 0) {
|
|
25
|
+
rSignature = rSignature.slice(1, 33);
|
|
26
|
+
} else if (rSignature.length === 33) {
|
|
27
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "r too long" });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// add leading zero's to pad to 32 bytes
|
|
31
|
+
while (rSignature.length < 32) {
|
|
32
|
+
rSignature.unshift(0);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// s signature
|
|
36
|
+
if (signatureArray[rLength + 4] !== 0x02) {
|
|
37
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", {
|
|
38
|
+
reason: "TLV encoding: expected length type 0x02 for s",
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const sLength = signatureArray[rLength + 5];
|
|
43
|
+
|
|
44
|
+
if (4 + rLength + 2 + sLength !== signatureArray.length) {
|
|
45
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", {
|
|
46
|
+
reason: "TLV byte lengths do not match message length",
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let sSignature = signatureArray.slice(rLength + 6, signatureArray.length);
|
|
51
|
+
|
|
52
|
+
// Drop leading zero on 's' signatures that are 33 bytes. This shouldn't occur since ledger signs using "Small s" math. But just to be sure...
|
|
53
|
+
if (sSignature.length === 33 && sSignature[0] === 0) {
|
|
54
|
+
sSignature = sSignature.slice(1, 33);
|
|
55
|
+
} else if (sSignature.length === 33) {
|
|
56
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "s too long" });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// add leading zero's to pad to 32 bytes
|
|
60
|
+
while (sSignature.length < 32) {
|
|
61
|
+
sSignature.unshift(0);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (rSignature.length !== 32 || sSignature.length !== 32) {
|
|
65
|
+
throw new SwapKitError("wallet_ledger_invalid_signature", { reason: "must be 32 bytes each" });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return base64.encode(Buffer.concat([rSignature, sSignature]));
|
|
69
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type TronApp from "@ledgerhq/hw-app-trx";
|
|
2
|
+
import {
|
|
3
|
+
type DerivationPathArray,
|
|
4
|
+
derivationPathToString,
|
|
5
|
+
NetworkDerivationPath,
|
|
6
|
+
SwapKitError,
|
|
7
|
+
} from "@swapkit/helpers";
|
|
8
|
+
import type { TronSignedTransaction, TronSigner, TronTransaction } from "@swapkit/toolboxes/tron";
|
|
9
|
+
|
|
10
|
+
import { getLedgerTransport } from "../helpers/getLedgerTransport";
|
|
11
|
+
|
|
12
|
+
export class TronLedgerInterface implements TronSigner {
|
|
13
|
+
derivationPath: string;
|
|
14
|
+
ledgerApp: InstanceType<typeof TronApp> | null = null;
|
|
15
|
+
ledgerTimeout = 50000;
|
|
16
|
+
|
|
17
|
+
constructor(derivationPath?: DerivationPathArray | string) {
|
|
18
|
+
this.derivationPath =
|
|
19
|
+
typeof derivationPath === "string"
|
|
20
|
+
? derivationPath
|
|
21
|
+
: derivationPathToString(derivationPath || NetworkDerivationPath.TRON);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
checkOrCreateTransportAndLedger = async () => {
|
|
25
|
+
if (this.ledgerApp) return;
|
|
26
|
+
await this.createTransportAndLedger();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
createTransportAndLedger = async () => {
|
|
30
|
+
const transport = await getLedgerTransport();
|
|
31
|
+
const TronApp = (await import("@ledgerhq/hw-app-trx")).default;
|
|
32
|
+
|
|
33
|
+
this.ledgerApp = new TronApp(transport);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
getAddress = async (): Promise<string> => {
|
|
37
|
+
const response = await this.getAddressAndPubKey();
|
|
38
|
+
if (!response) throw new SwapKitError("wallet_ledger_failed_to_get_address");
|
|
39
|
+
return response.address;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
getAddressAndPubKey = async () => {
|
|
43
|
+
await this.createTransportAndLedger();
|
|
44
|
+
const result = await this.ledgerApp?.getAddress(this.derivationPath);
|
|
45
|
+
|
|
46
|
+
if (!result) throw new SwapKitError("wallet_ledger_failed_to_get_address");
|
|
47
|
+
|
|
48
|
+
return { address: result.address, publicKey: result.publicKey };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
showAddressAndPubKey = async () => {
|
|
52
|
+
await this.createTransportAndLedger();
|
|
53
|
+
return this.ledgerApp?.getAddress(this.derivationPath, true);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
signTransaction = async (transaction: TronTransaction): Promise<TronSignedTransaction> => {
|
|
57
|
+
await this.createTransportAndLedger();
|
|
58
|
+
|
|
59
|
+
if (!this.ledgerApp) {
|
|
60
|
+
throw new SwapKitError("wallet_ledger_transport_error");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Tron transactions need to be serialized before signing
|
|
64
|
+
const serializedTx = JSON.stringify(transaction);
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const signature = await this.ledgerApp.signTransaction(
|
|
68
|
+
this.derivationPath,
|
|
69
|
+
serializedTx,
|
|
70
|
+
[], // Token signatures array - empty for native TRX transfers
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
if (!signature) {
|
|
74
|
+
throw new SwapKitError("wallet_ledger_signing_error");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Return the signed transaction in Tron's expected format
|
|
78
|
+
return { ...transaction, signature: [signature] };
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw new SwapKitError("wallet_ledger_signing_error", { error });
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const TronLedger = (derivationPath?: DerivationPathArray) => new TronLedgerInterface(derivationPath);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { hex } from "@scure/base";
|
|
2
|
+
import type { UTXOChain } from "@swapkit/helpers";
|
|
3
|
+
import type { UTXOType } from "@swapkit/toolboxes/utxo";
|
|
4
|
+
import type { Transaction } from "@swapkit/utxo-signer";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extract per-input metadata from a V3 PSBT in the shape the legacy
|
|
8
|
+
* `@ledgerhq/hw-app-btc.createPaymentTransaction` adapter expects.
|
|
9
|
+
*
|
|
10
|
+
* For segwit inputs the SwapKit V3 API populates `witnessUtxo`; for legacy
|
|
11
|
+
* (BCH/DOGE/DASH) it populates `nonWitnessUtxo` with the full prior-tx bytes.
|
|
12
|
+
* We re-encode the parsed `nonWitnessUtxo` back to hex via `RawTx.encode` so
|
|
13
|
+
* `btcApp.splitTransaction(hex)` can consume it.
|
|
14
|
+
*
|
|
15
|
+
* Single-address account assumption: all inputs share our derivation path.
|
|
16
|
+
*/
|
|
17
|
+
export async function extractInputsFromPsbt(tx: Transaction): Promise<UTXOType[]> {
|
|
18
|
+
const { RawTx } = await import("@swapkit/utxo-signer");
|
|
19
|
+
const inputs: UTXOType[] = [];
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < tx.inputsLength; i++) {
|
|
22
|
+
const input = tx.getInput(i);
|
|
23
|
+
|
|
24
|
+
if (!input.txid || input.index === undefined) {
|
|
25
|
+
throw new Error(`PSBT input ${i} is missing txid/index`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const txHex = input.nonWitnessUtxo ? hex.encode(RawTx.encode(input.nonWitnessUtxo)) : "";
|
|
29
|
+
const witnessUtxo = input.witnessUtxo
|
|
30
|
+
? { script: input.witnessUtxo.script, value: Number(input.witnessUtxo.amount) }
|
|
31
|
+
: undefined;
|
|
32
|
+
|
|
33
|
+
inputs.push({
|
|
34
|
+
hash: hex.encode(input.txid),
|
|
35
|
+
index: input.index,
|
|
36
|
+
txHex,
|
|
37
|
+
value: witnessUtxo?.value ?? 0,
|
|
38
|
+
witnessUtxo,
|
|
39
|
+
} as UTXOType);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return inputs;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Build a toolbox-compatible signer from the existing legacy Ledger UTXO
|
|
47
|
+
* client. The toolbox synthesizes `signAndBroadcastTransaction` on top of
|
|
48
|
+
* `signer.signTransaction(tx) → Transaction`.
|
|
49
|
+
*/
|
|
50
|
+
export function createLegacyPsbtSigner({
|
|
51
|
+
legacyClient,
|
|
52
|
+
chain: _chain,
|
|
53
|
+
address,
|
|
54
|
+
}: {
|
|
55
|
+
legacyClient: { signTransaction: (tx: Transaction, inputUtxos: UTXOType[]) => Promise<string> };
|
|
56
|
+
chain: UTXOChain;
|
|
57
|
+
address: string;
|
|
58
|
+
}) {
|
|
59
|
+
return {
|
|
60
|
+
getAddress: async () => address,
|
|
61
|
+
signTransaction: async (tx: Transaction): Promise<Transaction> => {
|
|
62
|
+
const inputUtxos = await extractInputsFromPsbt(tx);
|
|
63
|
+
const signedTxHex = await legacyClient.signTransaction(tx, inputUtxos);
|
|
64
|
+
|
|
65
|
+
const { Transaction: TxClass } = await import("@swapkit/utxo-signer");
|
|
66
|
+
// `Transaction.fromRaw` parses a serialised tx (no PSBT envelope) — exactly
|
|
67
|
+
// what `createPaymentTransaction` returns.
|
|
68
|
+
return TxClass.fromRaw(hex.decode(signedTxHex));
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { base64 } from "@scure/base";
|
|
2
|
+
import { HDKey } from "@scure/bip32";
|
|
3
|
+
import { type DerivationPathArray, derivationPathToString, getWalletFormatFor, SwapKitError } from "@swapkit/helpers";
|
|
4
|
+
import type { Transaction } from "@swapkit/utxo-signer";
|
|
5
|
+
|
|
6
|
+
import { getLedgerTransport } from "../helpers/getLedgerTransport";
|
|
7
|
+
|
|
8
|
+
type SupportedCoin = "bitcoin" | "litecoin";
|
|
9
|
+
|
|
10
|
+
type DefaultDescriptorTemplate = "wpkh(@0/**)" | "tr(@0/**)" | "sh(wpkh(@0/**))" | "pkh(@0/**)";
|
|
11
|
+
|
|
12
|
+
function templateForFormat(format: ReturnType<typeof getWalletFormatFor>): DefaultDescriptorTemplate {
|
|
13
|
+
switch (format) {
|
|
14
|
+
case "bech32":
|
|
15
|
+
return "wpkh(@0/**)";
|
|
16
|
+
case "p2sh":
|
|
17
|
+
return "sh(wpkh(@0/**))";
|
|
18
|
+
case "legacy":
|
|
19
|
+
return "pkh(@0/**)";
|
|
20
|
+
default:
|
|
21
|
+
return "wpkh(@0/**)";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function pathToString(path: DerivationPathArray | string): string {
|
|
26
|
+
return typeof path === "string" ? path : derivationPathToString(path);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function pathToNumberArray(path: string): number[] {
|
|
30
|
+
return path
|
|
31
|
+
.replace(/^m\//, "")
|
|
32
|
+
.split("/")
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.map((p) => {
|
|
35
|
+
const hardened = p.endsWith("'");
|
|
36
|
+
const num = Number.parseInt(hardened ? p.slice(0, -1) : p, 10);
|
|
37
|
+
return hardened ? (num | 0x80000000) >>> 0 : num;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const BaseLedgerPsbtUTXO = ({ chain }: { chain: SupportedCoin }) => {
|
|
42
|
+
let appClient: import("ledger-bitcoin").AppClient | undefined;
|
|
43
|
+
let masterFingerprint: string | undefined;
|
|
44
|
+
|
|
45
|
+
async function getAppClient() {
|
|
46
|
+
if (!appClient) {
|
|
47
|
+
const transport = await getLedgerTransport();
|
|
48
|
+
const { AppClient } = await import("ledger-bitcoin");
|
|
49
|
+
appClient = new AppClient(transport);
|
|
50
|
+
}
|
|
51
|
+
return appClient;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function getFingerprint() {
|
|
55
|
+
if (!masterFingerprint) {
|
|
56
|
+
const app = await getAppClient();
|
|
57
|
+
masterFingerprint = await app.getMasterFingerprint();
|
|
58
|
+
}
|
|
59
|
+
return masterFingerprint;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (derivationPathArray?: DerivationPathArray | string) => {
|
|
63
|
+
// Single-address account: change == index == 0 by default.
|
|
64
|
+
const derivationPath = derivationPathArray ? pathToString(derivationPathArray) : "84'/0'/0'/0/0";
|
|
65
|
+
const accountPath = derivationPath.split("/").slice(0, 3).join("/");
|
|
66
|
+
const leafSegments = derivationPath.split("/").slice(3);
|
|
67
|
+
const change = Number(leafSegments[0] ?? 0);
|
|
68
|
+
const addressIndex = Number(leafSegments[1] ?? 0);
|
|
69
|
+
const format = getWalletFormatFor(derivationPath);
|
|
70
|
+
const template = templateForFormat(format);
|
|
71
|
+
|
|
72
|
+
let cachedAccountXpub: string | undefined;
|
|
73
|
+
let cachedLeafPubkey: Uint8Array | undefined;
|
|
74
|
+
|
|
75
|
+
async function buildPolicy() {
|
|
76
|
+
const app = await getAppClient();
|
|
77
|
+
const fpr = await getFingerprint();
|
|
78
|
+
if (!cachedAccountXpub) {
|
|
79
|
+
cachedAccountXpub = await app.getExtendedPubkey(`m/${accountPath}`);
|
|
80
|
+
}
|
|
81
|
+
const { DefaultWalletPolicy } = await import("ledger-bitcoin");
|
|
82
|
+
const policy = new DefaultWalletPolicy(template, `[${fpr}/${accountPath}]${cachedAccountXpub}`);
|
|
83
|
+
return { app, fpr, policy, xpub: cachedAccountXpub };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function getLeafPubkey() {
|
|
87
|
+
if (!cachedLeafPubkey) {
|
|
88
|
+
const { xpub } = await buildPolicy();
|
|
89
|
+
const accountKey = HDKey.fromExtendedKey(xpub);
|
|
90
|
+
const leaf = accountKey.derive(`m/${change}/${addressIndex}`);
|
|
91
|
+
if (!leaf.publicKey) {
|
|
92
|
+
throw new SwapKitError("wallet_ledger_get_address_error", {
|
|
93
|
+
message: `Cannot derive leaf pubkey for ${chain}`,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
cachedLeafPubkey = leaf.publicKey;
|
|
97
|
+
}
|
|
98
|
+
return cachedLeafPubkey;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
connect: async () => {
|
|
103
|
+
await getAppClient();
|
|
104
|
+
},
|
|
105
|
+
getAddress: async () => {
|
|
106
|
+
const { app, policy } = await buildPolicy();
|
|
107
|
+
const address = await app.getWalletAddress(policy, null, change, addressIndex, false);
|
|
108
|
+
if (!address) {
|
|
109
|
+
throw new SwapKitError("wallet_ledger_get_address_error", {
|
|
110
|
+
message: `Cannot get ${chain} address from ledger derivation path: ${derivationPath}`,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return address;
|
|
114
|
+
},
|
|
115
|
+
getExtendedPublicKey: async (path = `m/${accountPath}`) => {
|
|
116
|
+
const app = await getAppClient();
|
|
117
|
+
return app.getExtendedPubkey(path);
|
|
118
|
+
},
|
|
119
|
+
signTransaction: async (tx: Transaction): Promise<Transaction> => {
|
|
120
|
+
const { app, policy, fpr } = await buildPolicy();
|
|
121
|
+
const fingerprintBE = Number.parseInt(fpr, 16) >>> 0;
|
|
122
|
+
const pathNumbers = pathToNumberArray(derivationPath);
|
|
123
|
+
const leafPubkey = await getLeafPubkey();
|
|
124
|
+
|
|
125
|
+
// Single-address account: every input is owned by the same key + path.
|
|
126
|
+
for (let i = 0; i < tx.inputsLength; i++) {
|
|
127
|
+
tx.updateInput(i, { bip32Derivation: [[leafPubkey, { fingerprint: fingerprintBE, path: pathNumbers }]] });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const psbtB64 = base64.encode(tx.toPSBT(0));
|
|
131
|
+
const sigs = await app.signPsbt(psbtB64, policy, null);
|
|
132
|
+
|
|
133
|
+
for (const [idx, partial] of sigs) {
|
|
134
|
+
tx.updateInput(idx, { partialSig: [[new Uint8Array(partial.pubkey), new Uint8Array(partial.signature)]] });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
tx.finalize();
|
|
138
|
+
return tx;
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const BitcoinPsbtLedger = BaseLedgerPsbtUTXO({ chain: "bitcoin" });
|
|
145
|
+
export const LitecoinPsbtLedger = BaseLedgerPsbtUTXO({ chain: "litecoin" });
|