@swapkit/wallet-hardware 4.2.10 → 4.6.0
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/LICENSE +246 -0
- package/dist/{chunk-7fsaymh4.js → chunk-1jexf7qt.js} +3 -3
- package/dist/{chunk-7fsaymh4.js.map → chunk-1jexf7qt.js.map} +1 -1
- package/dist/chunk-1jpcb30j.js +5 -0
- package/dist/{chunk-fazw0jvt.js.map → chunk-1jpcb30j.js.map} +1 -1
- package/dist/chunk-4fapcrj9.js +5 -0
- package/dist/chunk-4fapcrj9.js.map +10 -0
- package/dist/chunk-d54qkn5p.js +4 -0
- package/dist/chunk-d54qkn5p.js.map +10 -0
- package/dist/chunk-jj2v7mke.js +4 -0
- package/dist/chunk-jj2v7mke.js.map +10 -0
- package/dist/chunk-p94hfx8x.js +4 -0
- package/dist/{chunk-zzfbcc7e.js.map → chunk-p94hfx8x.js.map} +1 -1
- package/dist/chunk-skvfn1dg.js +4 -0
- package/dist/chunk-skvfn1dg.js.map +10 -0
- package/dist/{chunk-37bgpz1y.js → chunk-ybje7f3s.js} +3 -3
- package/dist/{chunk-37bgpz1y.js.map → chunk-ybje7f3s.js.map} +1 -1
- package/dist/keepkey/index.cjs +2 -2
- package/dist/keepkey/index.cjs.map +5 -5
- package/dist/keepkey/index.js +2 -2
- package/dist/keepkey/index.js.map +5 -5
- package/dist/ledger/index.cjs +3 -3
- package/dist/ledger/index.cjs.map +11 -10
- package/dist/ledger/index.js +3 -3
- package/dist/ledger/index.js.map +11 -10
- package/dist/trezor/index.cjs +2 -2
- package/dist/trezor/index.cjs.map +3 -3
- package/dist/trezor/index.js +2 -2
- package/dist/trezor/index.js.map +3 -3
- package/dist/types/core.d.ts +18 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/keepkey/chains/evm.d.ts +2 -2
- package/dist/types/keepkey/chains/evm.d.ts.map +1 -1
- package/dist/types/keepkey/chains/ripple.d.ts +15 -8
- package/dist/types/keepkey/chains/ripple.d.ts.map +1 -1
- package/dist/types/keepkey/chains/utxo.d.ts +331 -5
- package/dist/types/keepkey/chains/utxo.d.ts.map +1 -1
- package/dist/types/keepkey/index.d.ts +3 -3
- package/dist/types/keepkey/index.d.ts.map +1 -1
- package/dist/types/ledger/clients/evm.d.ts +3 -2
- package/dist/types/ledger/clients/evm.d.ts.map +1 -1
- package/dist/types/ledger/clients/sui.d.ts +27 -0
- package/dist/types/ledger/clients/sui.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/lib.d.ts.map +1 -1
- package/dist/types/ledger/clients/utxo.d.ts +43 -7
- package/dist/types/ledger/clients/utxo.d.ts.map +1 -1
- package/dist/types/ledger/clients/xrp.d.ts +2 -2
- package/dist/types/ledger/clients/xrp.d.ts.map +1 -1
- package/dist/types/ledger/helpers/getLedgerAddress.d.ts.map +1 -1
- package/dist/types/ledger/helpers/getLedgerClient.d.ts +4 -1
- package/dist/types/ledger/helpers/getLedgerClient.d.ts.map +1 -1
- package/dist/types/ledger/index.d.ts +3 -3
- package/dist/types/ledger/index.d.ts.map +1 -1
- package/dist/types/ledger/types.d.ts +2 -0
- package/dist/types/ledger/types.d.ts.map +1 -1
- package/dist/types/trezor/evmSigner.d.ts +2 -2
- package/dist/types/trezor/evmSigner.d.ts.map +1 -1
- package/dist/types/trezor/index.d.ts +3 -3
- package/dist/types/trezor/index.d.ts.map +1 -1
- package/package.json +51 -40
- package/dist/chunk-4k7gb7ss.js +0 -4
- package/dist/chunk-4k7gb7ss.js.map +0 -10
- package/dist/chunk-93cj7bky.js +0 -4
- package/dist/chunk-93cj7bky.js.map +0 -10
- package/dist/chunk-fazw0jvt.js +0 -4
- package/dist/chunk-zzfbcc7e.js +0 -5
- package/src/index.ts +0 -1
- package/src/keepkey/chains/cosmos.ts +0 -69
- package/src/keepkey/chains/evm.ts +0 -114
- package/src/keepkey/chains/mayachain.ts +0 -98
- package/src/keepkey/chains/ripple.ts +0 -88
- package/src/keepkey/chains/thorchain.ts +0 -93
- package/src/keepkey/chains/utxo.ts +0 -137
- package/src/keepkey/coins.ts +0 -67
- package/src/keepkey/index.ts +0 -155
- package/src/ledger/clients/cosmos.ts +0 -84
- package/src/ledger/clients/evm.ts +0 -141
- package/src/ledger/clients/near.ts +0 -63
- package/src/ledger/clients/thorchain/common.ts +0 -93
- package/src/ledger/clients/thorchain/helpers.ts +0 -120
- package/src/ledger/clients/thorchain/index.ts +0 -87
- package/src/ledger/clients/thorchain/lib.ts +0 -278
- package/src/ledger/clients/thorchain/utils.ts +0 -69
- package/src/ledger/clients/tron.ts +0 -85
- package/src/ledger/clients/utxo.ts +0 -154
- package/src/ledger/clients/xrp.ts +0 -50
- package/src/ledger/cosmosTypes.ts +0 -98
- package/src/ledger/helpers/getLedgerAddress.ts +0 -69
- package/src/ledger/helpers/getLedgerClient.ts +0 -117
- package/src/ledger/helpers/getLedgerTransport.ts +0 -102
- package/src/ledger/helpers/index.ts +0 -3
- package/src/ledger/index.ts +0 -299
- package/src/ledger/interfaces/CosmosLedgerInterface.ts +0 -54
- package/src/ledger/types.ts +0 -40
- package/src/trezor/evmSigner.ts +0 -177
- package/src/trezor/index.ts +0 -349
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import type Transport from "@ledgerhq/hw-transport";
|
|
2
|
-
import { SwapKitError } from "@swapkit/helpers";
|
|
3
|
-
/** ******************************************************************************
|
|
4
|
-
* (c) 2019 ZondaX GmbH
|
|
5
|
-
* (c) 2016-2017 Ledger
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
******************************************************************************* */
|
|
19
|
-
|
|
20
|
-
import {
|
|
21
|
-
CHUNK_SIZE,
|
|
22
|
-
CLA,
|
|
23
|
-
ERROR_CODE,
|
|
24
|
-
errorCodeToString,
|
|
25
|
-
getVersion,
|
|
26
|
-
INS,
|
|
27
|
-
P1_VALUES,
|
|
28
|
-
P2_VALUES,
|
|
29
|
-
processErrorResponse,
|
|
30
|
-
} from "./common";
|
|
31
|
-
import {
|
|
32
|
-
publicKeyv1,
|
|
33
|
-
publicKeyv2,
|
|
34
|
-
serializePathv1,
|
|
35
|
-
serializePathv2,
|
|
36
|
-
signSendChunkv1,
|
|
37
|
-
signSendChunkv2,
|
|
38
|
-
} from "./helpers";
|
|
39
|
-
|
|
40
|
-
export class THORChainApp {
|
|
41
|
-
transport: Transport;
|
|
42
|
-
versionResponse: any;
|
|
43
|
-
|
|
44
|
-
constructor(transport: any) {
|
|
45
|
-
if (!transport) {
|
|
46
|
-
throw new SwapKitError("wallet_ledger_transport_not_defined");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.transport = transport;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
static serializeHRP(hrp: string) {
|
|
53
|
-
if (hrp == null || hrp.length < 3 || hrp.length > 83) {
|
|
54
|
-
throw new SwapKitError("wallet_ledger_invalid_params", { reason: "Invalid HRP" });
|
|
55
|
-
}
|
|
56
|
-
const buf = Buffer.alloc(1 + hrp.length);
|
|
57
|
-
buf.writeUInt8(hrp.length, 0);
|
|
58
|
-
buf.write(hrp, 1);
|
|
59
|
-
return buf;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async serializePath(path: number[]) {
|
|
63
|
-
this.versionResponse = await getVersion(this.transport);
|
|
64
|
-
|
|
65
|
-
if (this.versionResponse.return_code !== ERROR_CODE.NoError) {
|
|
66
|
-
throw this.versionResponse;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
switch (this.versionResponse.major) {
|
|
70
|
-
case 1:
|
|
71
|
-
return serializePathv1(path);
|
|
72
|
-
case 2:
|
|
73
|
-
return serializePathv2(path);
|
|
74
|
-
default:
|
|
75
|
-
return Buffer.alloc(0);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
async signGetChunks(path: number[], buffer: Buffer) {
|
|
80
|
-
const serializedPath = await this.serializePath(path);
|
|
81
|
-
|
|
82
|
-
const chunks = [];
|
|
83
|
-
chunks.push(serializedPath);
|
|
84
|
-
|
|
85
|
-
for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
|
|
86
|
-
let end = i + CHUNK_SIZE;
|
|
87
|
-
if (i > buffer.length) {
|
|
88
|
-
end = buffer.length;
|
|
89
|
-
}
|
|
90
|
-
chunks.push(buffer.slice(i, end));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return chunks;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async getVersion() {
|
|
97
|
-
try {
|
|
98
|
-
this.versionResponse = await getVersion(this.transport);
|
|
99
|
-
return this.versionResponse;
|
|
100
|
-
} catch (e) {
|
|
101
|
-
return processErrorResponse(e);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
appInfo() {
|
|
106
|
-
return this.transport.send(0xb0, 0x01, 0, 0).then((response: any) => {
|
|
107
|
-
const errorCodeData = response.slice(-2);
|
|
108
|
-
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
109
|
-
|
|
110
|
-
let appName = "";
|
|
111
|
-
let appVersion = "";
|
|
112
|
-
let flagLen = 0;
|
|
113
|
-
let flagsValue = 0;
|
|
114
|
-
|
|
115
|
-
if (response[0] !== 1) {
|
|
116
|
-
// Ledger responds with format ID 1. There is no spec for any format != 1
|
|
117
|
-
return { error_message: "response format ID not recognized", return_code: 0x9001 };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const appNameLen = response[1];
|
|
121
|
-
appName = response.slice(2, 2 + appNameLen).toString("ascii");
|
|
122
|
-
let idx = 2 + appNameLen;
|
|
123
|
-
const appVersionLen = response[idx];
|
|
124
|
-
idx += 1;
|
|
125
|
-
appVersion = response.slice(idx, idx + appVersionLen).toString("ascii");
|
|
126
|
-
idx += appVersionLen;
|
|
127
|
-
const appFlagsLen = response[idx];
|
|
128
|
-
idx += 1;
|
|
129
|
-
flagLen = appFlagsLen;
|
|
130
|
-
flagsValue = response[idx];
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
appName,
|
|
134
|
-
appVersion,
|
|
135
|
-
error_message: errorCodeToString(returnCode),
|
|
136
|
-
flag_onboarded: (flagsValue & 4) !== 0,
|
|
137
|
-
flag_pin_validated: (flagsValue & 128) !== 0,
|
|
138
|
-
flag_recovery: (flagsValue & 1) !== 0,
|
|
139
|
-
flag_signed_mcu_code: (flagsValue & 2) !== 0,
|
|
140
|
-
flagLen,
|
|
141
|
-
flagsValue,
|
|
142
|
-
return_code: returnCode,
|
|
143
|
-
};
|
|
144
|
-
}, processErrorResponse);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
deviceInfo() {
|
|
148
|
-
return this.transport
|
|
149
|
-
.send(0xe0, 0x01, 0, 0, Buffer.from([]), [ERROR_CODE.NoError, 0x6e00])
|
|
150
|
-
.then((response: any) => {
|
|
151
|
-
const errorCodeData = response.slice(-2);
|
|
152
|
-
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
153
|
-
|
|
154
|
-
if (returnCode === 0x6e00) {
|
|
155
|
-
return { error_message: "This command is only available in the Dashboard", return_code: returnCode };
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const targetId = response.slice(0, 4).toString("hex");
|
|
159
|
-
|
|
160
|
-
let pos = 4;
|
|
161
|
-
const secureElementVersionLen = response[pos];
|
|
162
|
-
pos += 1;
|
|
163
|
-
const seVersion = response.slice(pos, pos + secureElementVersionLen).toString();
|
|
164
|
-
pos += secureElementVersionLen;
|
|
165
|
-
|
|
166
|
-
const flagsLen = response[pos];
|
|
167
|
-
pos += 1;
|
|
168
|
-
const flag = response.slice(pos, pos + flagsLen).toString("hex");
|
|
169
|
-
pos += flagsLen;
|
|
170
|
-
|
|
171
|
-
const mcuVersionLen = response[pos];
|
|
172
|
-
pos += 1;
|
|
173
|
-
// Patch issue in mcu version
|
|
174
|
-
let tmp = response.slice(pos, pos + mcuVersionLen);
|
|
175
|
-
if (tmp[mcuVersionLen - 1] === 0) {
|
|
176
|
-
tmp = response.slice(pos, pos + mcuVersionLen - 1);
|
|
177
|
-
}
|
|
178
|
-
const mcuVersion = tmp.toString();
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
error_message: errorCodeToString(returnCode),
|
|
182
|
-
flag,
|
|
183
|
-
mcuVersion,
|
|
184
|
-
return_code: returnCode,
|
|
185
|
-
seVersion,
|
|
186
|
-
// //
|
|
187
|
-
targetId,
|
|
188
|
-
};
|
|
189
|
-
}, processErrorResponse);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async publicKey(path: number[]) {
|
|
193
|
-
try {
|
|
194
|
-
const serializedPath = await this.serializePath(path);
|
|
195
|
-
|
|
196
|
-
switch (this.versionResponse.major) {
|
|
197
|
-
case 1:
|
|
198
|
-
return publicKeyv1(this, serializedPath);
|
|
199
|
-
case 2: {
|
|
200
|
-
const data = Buffer.concat([THORChainApp.serializeHRP("thor"), serializedPath]);
|
|
201
|
-
return publicKeyv2(this, data);
|
|
202
|
-
}
|
|
203
|
-
default:
|
|
204
|
-
return { error_message: "App Version is not supported", return_code: 0x6400 };
|
|
205
|
-
}
|
|
206
|
-
} catch (e) {
|
|
207
|
-
return processErrorResponse(e);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
getAddressAndPubKey(path: number[], hrp: string, showInDevice = false) {
|
|
212
|
-
return this.serializePath(path)
|
|
213
|
-
.then((serializedPath: Buffer) => {
|
|
214
|
-
const data = Buffer.concat([THORChainApp.serializeHRP(hrp), serializedPath]);
|
|
215
|
-
return this.transport
|
|
216
|
-
.send(
|
|
217
|
-
CLA,
|
|
218
|
-
INS.GET_ADDR_SECP256K1,
|
|
219
|
-
showInDevice ? P1_VALUES.SHOW_ADDRESS_IN_DEVICE : P1_VALUES.ONLY_RETRIEVE,
|
|
220
|
-
0,
|
|
221
|
-
data,
|
|
222
|
-
[ERROR_CODE.NoError],
|
|
223
|
-
)
|
|
224
|
-
.then((response: any) => {
|
|
225
|
-
const errorCodeData = response.slice(-2);
|
|
226
|
-
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
227
|
-
|
|
228
|
-
const compressedPk = Buffer.from(response.slice(0, 33));
|
|
229
|
-
const bech32Address = Buffer.from(response.slice(33, -2)).toString();
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
bech32_address: bech32Address,
|
|
233
|
-
compressed_pk: compressedPk,
|
|
234
|
-
error_message: errorCodeToString(returnCode),
|
|
235
|
-
return_code: returnCode,
|
|
236
|
-
};
|
|
237
|
-
}, processErrorResponse);
|
|
238
|
-
})
|
|
239
|
-
.catch((err) => processErrorResponse(err));
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
showAddressAndPubKey(path: number[], hrp: string) {
|
|
243
|
-
return this.getAddressAndPubKey(path, hrp, true);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
|
|
247
|
-
switch (this.versionResponse.major) {
|
|
248
|
-
case 1:
|
|
249
|
-
return signSendChunkv1(this, chunkIdx, chunkNum, chunk, txType);
|
|
250
|
-
case 2:
|
|
251
|
-
return signSendChunkv2(this, chunkIdx, chunkNum, chunk, txType);
|
|
252
|
-
default:
|
|
253
|
-
return { error_message: "App Version is not supported", return_code: 0x6400 };
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
async sign(path: number[], message: string, txType = P2_VALUES.JSON) {
|
|
258
|
-
const buffer = Buffer.from(message);
|
|
259
|
-
let chunks: Buffer[] = [];
|
|
260
|
-
let response: any;
|
|
261
|
-
try {
|
|
262
|
-
chunks = await this.signGetChunks(path, buffer);
|
|
263
|
-
response = await this.signSendChunk(1, chunks.length, chunks[0] as Buffer, txType);
|
|
264
|
-
} catch (error) {
|
|
265
|
-
processErrorResponse(error);
|
|
266
|
-
}
|
|
267
|
-
let result = { error_message: response.error_message, return_code: response.return_code, signature: null };
|
|
268
|
-
|
|
269
|
-
for (let i = 1; i < chunks.length; i += 1) {
|
|
270
|
-
result = await this.signSendChunk(1 + i, chunks.length, chunks[i] as Buffer, txType);
|
|
271
|
-
if (result.return_code !== ERROR_CODE.NoError) {
|
|
272
|
-
break;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
return { error_message: result.error_message, return_code: result.return_code, signature: result.signature };
|
|
277
|
-
}
|
|
278
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,85 +0,0 @@
|
|
|
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);
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import type BitcoinApp from "@ledgerhq/hw-app-btc";
|
|
2
|
-
import type { CreateTransactionArg } from "@ledgerhq/hw-app-btc/lib-es/createTransaction";
|
|
3
|
-
import { type DerivationPathArray, derivationPathToString, getWalletFormatFor, SwapKitError } from "@swapkit/helpers";
|
|
4
|
-
import type { UTXOType } from "@swapkit/toolboxes/utxo";
|
|
5
|
-
import type { Psbt } from "bitcoinjs-lib";
|
|
6
|
-
|
|
7
|
-
import { getLedgerTransport } from "../helpers/getLedgerTransport";
|
|
8
|
-
|
|
9
|
-
const nonSegwitLedgerChains = ["bitcoin-cash", "dash", "dogecoin", "zcash"];
|
|
10
|
-
|
|
11
|
-
type Params = {
|
|
12
|
-
psbt: Psbt;
|
|
13
|
-
inputUtxos: UTXOType[];
|
|
14
|
-
btcApp: BitcoinApp;
|
|
15
|
-
derivationPath: string;
|
|
16
|
-
chain: "bitcoin-cash" | "bitcoin" | "litecoin" | "dogecoin" | "dash" | "zcash";
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const signUTXOTransaction = (
|
|
20
|
-
{ psbt, inputUtxos, btcApp, derivationPath, chain }: Params,
|
|
21
|
-
options?: Partial<CreateTransactionArg>,
|
|
22
|
-
) => {
|
|
23
|
-
const inputs = inputUtxos.map((item) => {
|
|
24
|
-
const splitTx = btcApp.splitTransaction(
|
|
25
|
-
item.txHex || "",
|
|
26
|
-
!nonSegwitLedgerChains.includes(chain),
|
|
27
|
-
chain === "zcash",
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
return [splitTx, item.index, undefined as string | null | undefined, undefined as number | null | undefined] as any;
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
const newTxHex = psbt.data.globalMap.unsignedTx.toBuffer().toString("hex");
|
|
34
|
-
|
|
35
|
-
const splitNewTx = btcApp.splitTransaction(newTxHex, true);
|
|
36
|
-
const outputScriptHex = btcApp.serializeTransactionOutputs(splitNewTx).toString("hex");
|
|
37
|
-
|
|
38
|
-
const params: CreateTransactionArg = {
|
|
39
|
-
additionals: ["bech32"],
|
|
40
|
-
associatedKeysets: inputs.map(() => derivationPath),
|
|
41
|
-
inputs,
|
|
42
|
-
outputScriptHex,
|
|
43
|
-
segwit: true,
|
|
44
|
-
useTrustedInputForSegwit: true,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
return btcApp.createPaymentTransaction({ ...params, ...options });
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const BaseLedgerUTXO = ({
|
|
51
|
-
chain,
|
|
52
|
-
additionalSignParams,
|
|
53
|
-
}: {
|
|
54
|
-
chain: "bitcoin-cash" | "bitcoin" | "litecoin" | "dogecoin" | "dash" | "zcash";
|
|
55
|
-
additionalSignParams?: Partial<CreateTransactionArg>;
|
|
56
|
-
}) => {
|
|
57
|
-
let btcApp: InstanceType<typeof BitcoinApp>;
|
|
58
|
-
let transport: any = null;
|
|
59
|
-
|
|
60
|
-
async function checkBtcAppAndCreateTransportWebUSB(checkBtcApp = true) {
|
|
61
|
-
if (checkBtcApp && !btcApp) {
|
|
62
|
-
new SwapKitError("wallet_ledger_connection_error", {
|
|
63
|
-
message: `Ledger connection failed:\n${JSON.stringify({ btcApp, checkBtcApp })}`,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
transport ||= await getLedgerTransport();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async function createTransportWebUSB() {
|
|
71
|
-
transport = await getLedgerTransport();
|
|
72
|
-
const BitcoinApp = (await import("@ledgerhq/hw-app-btc")).default;
|
|
73
|
-
|
|
74
|
-
btcApp = new BitcoinApp({ currency: chain, transport });
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return (derivationPathArray?: DerivationPathArray | string) => {
|
|
78
|
-
const derivationPath =
|
|
79
|
-
typeof derivationPathArray === "string"
|
|
80
|
-
? derivationPathArray
|
|
81
|
-
: derivationPathToString(derivationPathArray as DerivationPathArray);
|
|
82
|
-
|
|
83
|
-
const format = getWalletFormatFor(derivationPath);
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
connect: async () => {
|
|
87
|
-
await checkBtcAppAndCreateTransportWebUSB(false);
|
|
88
|
-
const BitcoinApp = (await import("@ledgerhq/hw-app-btc")).default;
|
|
89
|
-
|
|
90
|
-
btcApp = new BitcoinApp({ currency: chain, transport });
|
|
91
|
-
},
|
|
92
|
-
getAddress: async () => {
|
|
93
|
-
const { toCashAddress } = await import("@swapkit/toolboxes/utxo");
|
|
94
|
-
|
|
95
|
-
await checkBtcAppAndCreateTransportWebUSB(false);
|
|
96
|
-
|
|
97
|
-
const { bitcoinAddress: address } = await btcApp.getWalletPublicKey(derivationPath, { format });
|
|
98
|
-
|
|
99
|
-
if (!address) {
|
|
100
|
-
throw new SwapKitError("wallet_ledger_get_address_error", {
|
|
101
|
-
message: `Cannot get ${chain} address from ledger derivation path: ${derivationPath}`,
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return chain === "bitcoin-cash" && format === "legacy"
|
|
106
|
-
? toCashAddress(address).replace(/(bchtest:|bitcoincash:)/, "")
|
|
107
|
-
: address;
|
|
108
|
-
},
|
|
109
|
-
getExtendedPublicKey: async (path = "84'/0'/0'", xpubVersion = 76067358) => {
|
|
110
|
-
await checkBtcAppAndCreateTransportWebUSB(false);
|
|
111
|
-
|
|
112
|
-
return btcApp.getWalletXpub({ path, xpubVersion });
|
|
113
|
-
},
|
|
114
|
-
signTransaction: async (psbt: Psbt, inputUtxos: UTXOType[]) => {
|
|
115
|
-
await createTransportWebUSB();
|
|
116
|
-
|
|
117
|
-
return signUTXOTransaction({ btcApp, chain, derivationPath, inputUtxos, psbt }, additionalSignParams);
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
};
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
export const BitcoinLedger = BaseLedgerUTXO({ chain: "bitcoin" });
|
|
124
|
-
export const LitecoinLedger = BaseLedgerUTXO({ chain: "litecoin" });
|
|
125
|
-
|
|
126
|
-
export const BitcoinCashLedger = BaseLedgerUTXO({
|
|
127
|
-
additionalSignParams: { additionals: ["abc"], segwit: false, sigHashType: 0x41 },
|
|
128
|
-
chain: "bitcoin-cash",
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
export const DogecoinLedger = BaseLedgerUTXO({
|
|
132
|
-
additionalSignParams: { additionals: [], segwit: false, useTrustedInputForSegwit: false },
|
|
133
|
-
chain: "dogecoin",
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
export const DashLedger = BaseLedgerUTXO({
|
|
137
|
-
additionalSignParams: { additionals: [], segwit: false, useTrustedInputForSegwit: false },
|
|
138
|
-
chain: "dash",
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
export const ZcashLedger = BaseLedgerUTXO({
|
|
142
|
-
additionalSignParams: {
|
|
143
|
-
additionals: ["zcash", "sapling"],
|
|
144
|
-
expiryHeight: (() => {
|
|
145
|
-
const buf = Buffer.allocUnsafe(4);
|
|
146
|
-
buf.writeUInt32LE(0);
|
|
147
|
-
return buf;
|
|
148
|
-
})(),
|
|
149
|
-
lockTime: 0,
|
|
150
|
-
segwit: false,
|
|
151
|
-
useTrustedInputForSegwit: false,
|
|
152
|
-
},
|
|
153
|
-
chain: "zcash",
|
|
154
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
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 { Transaction } 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 | Transaction) {
|
|
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
|
-
};
|