@tcswap/wallet-hardware 4.2.16
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/chunk-1w9rn6fj.js +5 -0
- package/dist/chunk-1w9rn6fj.js.map +10 -0
- package/dist/chunk-1z3vq8v6.js +4 -0
- package/dist/chunk-1z3vq8v6.js.map +10 -0
- package/dist/chunk-3jd7804n.js +4 -0
- package/dist/chunk-3jd7804n.js.map +10 -0
- package/dist/chunk-4pybhxzx.js +4 -0
- package/dist/chunk-4pybhxzx.js.map +10 -0
- package/dist/chunk-9kbkdt6e.js +4 -0
- package/dist/chunk-9kbkdt6e.js.map +10 -0
- package/dist/chunk-eng8tyvd.js +4 -0
- package/dist/chunk-eng8tyvd.js.map +10 -0
- package/dist/chunk-fazw0jvt.js +4 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-zzfbcc7e.js +5 -0
- package/dist/chunk-zzfbcc7e.js.map +9 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +9 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +9 -0
- package/dist/keepkey/index.cjs +4 -0
- package/dist/keepkey/index.cjs.map +15 -0
- package/dist/keepkey/index.js +4 -0
- package/dist/keepkey/index.js.map +15 -0
- package/dist/ledger/index.cjs +5 -0
- package/dist/ledger/index.cjs.map +25 -0
- package/dist/ledger/index.js +5 -0
- package/dist/ledger/index.js.map +25 -0
- package/dist/trezor/index.cjs +4 -0
- package/dist/trezor/index.cjs.map +10 -0
- package/dist/trezor/index.js +4 -0
- package/dist/trezor/index.js.map +10 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/keepkey/chains/cosmos.d.ts +7 -0
- package/dist/types/keepkey/chains/cosmos.d.ts.map +1 -0
- package/dist/types/keepkey/chains/evm.d.ts +29 -0
- package/dist/types/keepkey/chains/evm.d.ts.map +1 -0
- package/dist/types/keepkey/chains/mayachain.d.ts +10 -0
- package/dist/types/keepkey/chains/mayachain.d.ts.map +1 -0
- package/dist/types/keepkey/chains/ripple.d.ts +37 -0
- package/dist/types/keepkey/chains/ripple.d.ts.map +1 -0
- package/dist/types/keepkey/chains/thorchain.d.ts +10 -0
- package/dist/types/keepkey/chains/thorchain.d.ts.map +1 -0
- package/dist/types/keepkey/chains/utxo.d.ts +25 -0
- package/dist/types/keepkey/chains/utxo.d.ts.map +1 -0
- package/dist/types/keepkey/coins.d.ts +15 -0
- package/dist/types/keepkey/coins.d.ts.map +1 -0
- package/dist/types/keepkey/index.d.ts +15 -0
- package/dist/types/keepkey/index.d.ts.map +1 -0
- package/dist/types/ledger/clients/cosmos.d.ts +23 -0
- package/dist/types/ledger/clients/cosmos.d.ts.map +1 -0
- package/dist/types/ledger/clients/evm.d.ts +52 -0
- package/dist/types/ledger/clients/evm.d.ts.map +1 -0
- package/dist/types/ledger/clients/near.d.ts +7 -0
- package/dist/types/ledger/clients/near.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/common.d.ts +28 -0
- package/dist/types/ledger/clients/thorchain/common.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/helpers.d.ts +10 -0
- package/dist/types/ledger/clients/thorchain/helpers.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/index.d.ts +25 -0
- package/dist/types/ledger/clients/thorchain/index.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/lib.d.ts +25 -0
- package/dist/types/ledger/clients/thorchain/lib.d.ts.map +1 -0
- package/dist/types/ledger/clients/thorchain/utils.d.ts +5 -0
- package/dist/types/ledger/clients/thorchain/utils.d.ts.map +1 -0
- package/dist/types/ledger/clients/tron.d.ts +26 -0
- package/dist/types/ledger/clients/tron.d.ts.map +1 -0
- package/dist/types/ledger/clients/utxo.d.ts +43 -0
- package/dist/types/ledger/clients/utxo.d.ts.map +1 -0
- package/dist/types/ledger/clients/xrp.d.ts +11 -0
- package/dist/types/ledger/clients/xrp.d.ts.map +1 -0
- package/dist/types/ledger/cosmosTypes.d.ts +43 -0
- package/dist/types/ledger/cosmosTypes.d.ts.map +1 -0
- package/dist/types/ledger/helpers/getLedgerAddress.d.ts +10 -0
- package/dist/types/ledger/helpers/getLedgerAddress.d.ts.map +1 -0
- package/dist/types/ledger/helpers/getLedgerClient.d.ts +42 -0
- package/dist/types/ledger/helpers/getLedgerClient.d.ts.map +1 -0
- package/dist/types/ledger/helpers/getLedgerTransport.d.ts +6 -0
- package/dist/types/ledger/helpers/getLedgerTransport.d.ts.map +1 -0
- package/dist/types/ledger/helpers/index.d.ts +4 -0
- package/dist/types/ledger/helpers/index.d.ts.map +1 -0
- package/dist/types/ledger/index.d.ts +14 -0
- package/dist/types/ledger/index.d.ts.map +1 -0
- package/dist/types/ledger/interfaces/CosmosLedgerInterface.d.ts +14 -0
- package/dist/types/ledger/interfaces/CosmosLedgerInterface.d.ts.map +1 -0
- package/dist/types/ledger/types.d.ts +16 -0
- package/dist/types/ledger/types.d.ts.map +1 -0
- package/dist/types/trezor/evmSigner.d.ts +32 -0
- package/dist/types/trezor/evmSigner.d.ts.map +1 -0
- package/dist/types/trezor/index.d.ts +14 -0
- package/dist/types/trezor/index.d.ts.map +1 -0
- package/package.json +101 -0
- package/src/index.ts +1 -0
- package/src/keepkey/chains/cosmos.ts +69 -0
- package/src/keepkey/chains/evm.ts +117 -0
- package/src/keepkey/chains/mayachain.ts +102 -0
- package/src/keepkey/chains/ripple.ts +88 -0
- package/src/keepkey/chains/thorchain.ts +97 -0
- package/src/keepkey/chains/utxo.ts +141 -0
- package/src/keepkey/coins.ts +70 -0
- package/src/keepkey/index.ts +159 -0
- package/src/ledger/clients/cosmos.ts +83 -0
- package/src/ledger/clients/evm.ts +145 -0
- package/src/ledger/clients/near.ts +67 -0
- package/src/ledger/clients/thorchain/common.ts +93 -0
- package/src/ledger/clients/thorchain/helpers.ts +124 -0
- package/src/ledger/clients/thorchain/index.ts +91 -0
- package/src/ledger/clients/thorchain/lib.ts +282 -0
- package/src/ledger/clients/thorchain/utils.ts +71 -0
- package/src/ledger/clients/tron.ts +84 -0
- package/src/ledger/clients/utxo.ts +158 -0
- package/src/ledger/clients/xrp.ts +50 -0
- package/src/ledger/cosmosTypes.ts +102 -0
- package/src/ledger/helpers/getLedgerAddress.ts +73 -0
- package/src/ledger/helpers/getLedgerClient.ts +121 -0
- package/src/ledger/helpers/getLedgerTransport.ts +106 -0
- package/src/ledger/helpers/index.ts +3 -0
- package/src/ledger/index.ts +303 -0
- package/src/ledger/interfaces/CosmosLedgerInterface.ts +58 -0
- package/src/ledger/types.ts +40 -0
- package/src/trezor/evmSigner.ts +181 -0
- package/src/trezor/index.ts +350 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export const CLA = 0x55;
|
|
2
|
+
export const CHUNK_SIZE = 250;
|
|
3
|
+
export const APP_KEY = "CSM";
|
|
4
|
+
|
|
5
|
+
export const INS = {
|
|
6
|
+
GET_ADDR_SECP256K1: 0x04,
|
|
7
|
+
GET_VERSION: 0x00,
|
|
8
|
+
INS_PUBLIC_KEY_SECP256K1: 0x01, // Obsolete
|
|
9
|
+
SIGN_SECP256K1: 0x02,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const PAYLOAD_TYPE = { ADD: 0x01, INIT: 0x00, LAST: 0x02 };
|
|
13
|
+
|
|
14
|
+
export const P1_VALUES = { ONLY_RETRIEVE: 0x00, SHOW_ADDRESS_IN_DEVICE: 0x01 };
|
|
15
|
+
|
|
16
|
+
export const P2_VALUES = { JSON: 0x0 };
|
|
17
|
+
|
|
18
|
+
export const ERROR_CODE = { NoError: 0x9000 };
|
|
19
|
+
|
|
20
|
+
const ERROR_DESCRIPTION: any = {
|
|
21
|
+
1: "U2F: Unknown",
|
|
22
|
+
2: "U2F: Bad request",
|
|
23
|
+
3: "U2F: Configuration unsupported",
|
|
24
|
+
4: "U2F: Device Ineligible",
|
|
25
|
+
5: "U2F: Timeout",
|
|
26
|
+
14: "Timeout",
|
|
27
|
+
25600: "Execution Error",
|
|
28
|
+
26368: "Wrong Length",
|
|
29
|
+
26626: "Error deriving keys",
|
|
30
|
+
27010: "Empty Buffer",
|
|
31
|
+
27011: "Output buffer too small",
|
|
32
|
+
27012: "Data is invalid",
|
|
33
|
+
27013: "Conditions not satisfied",
|
|
34
|
+
27014: "Transaction rejected",
|
|
35
|
+
27264: "Bad key handle",
|
|
36
|
+
27392: "Invalid P1/P2",
|
|
37
|
+
27904: "Instruction not supported",
|
|
38
|
+
28160: "App does not seem to be open",
|
|
39
|
+
28416: "Unknown error",
|
|
40
|
+
28417: "Sign/verify error",
|
|
41
|
+
36864: "No errors",
|
|
42
|
+
36865: "Device is busy",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export function errorCodeToString(statusCode: number) {
|
|
46
|
+
if (statusCode in ERROR_DESCRIPTION) return ERROR_DESCRIPTION[statusCode];
|
|
47
|
+
return `Unknown Status Code: ${statusCode}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isDict(v: any) {
|
|
51
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Date);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function processErrorResponse(response: any) {
|
|
55
|
+
if (response) {
|
|
56
|
+
if (isDict(response)) {
|
|
57
|
+
if (Object.hasOwn(response, "statusCode")) {
|
|
58
|
+
return { error_message: errorCodeToString(response.statusCode), return_code: response.statusCode };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (Object.hasOwn(response, "return_code") && Object.hasOwn(response, "error_message")) {
|
|
62
|
+
return response;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { error_message: response.toString(), return_code: 0xffff };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { error_message: response.toString(), return_code: 0xffff };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function getVersion(transport: any) {
|
|
72
|
+
return transport.send(CLA, INS.GET_VERSION, 0, 0).then((response: any) => {
|
|
73
|
+
const errorCodeData = response.slice(-2);
|
|
74
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
75
|
+
|
|
76
|
+
let targetId = 0;
|
|
77
|
+
if (response.length >= 9) {
|
|
78
|
+
targetId = (response[5] << 24) + (response[6] << 16) + (response[7] << 8) + (response[8] << 0);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
device_locked: response[4] === 1,
|
|
83
|
+
error_message: errorCodeToString(returnCode),
|
|
84
|
+
major: response[1],
|
|
85
|
+
minor: response[2],
|
|
86
|
+
patch: response[3],
|
|
87
|
+
return_code: returnCode,
|
|
88
|
+
target_id: targetId.toString(16),
|
|
89
|
+
// ///
|
|
90
|
+
test_mode: response[0] !== 0,
|
|
91
|
+
};
|
|
92
|
+
}, processErrorResponse);
|
|
93
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { USwapError } from "@tcswap/helpers";
|
|
6
|
+
import { CLA, ERROR_CODE, errorCodeToString, INS, P2_VALUES, PAYLOAD_TYPE, processErrorResponse } from "./common";
|
|
7
|
+
|
|
8
|
+
export function serializePathv1(path: number[]) {
|
|
9
|
+
if (path == null || path.length < 3) {
|
|
10
|
+
throw new USwapError("wallet_ledger_invalid_params", { reason: "Path too short" });
|
|
11
|
+
}
|
|
12
|
+
if (path.length > 10) {
|
|
13
|
+
throw new USwapError("wallet_ledger_invalid_params", { reason: "Path too long" });
|
|
14
|
+
}
|
|
15
|
+
const buf = Buffer.alloc(1 + 4 * path.length);
|
|
16
|
+
buf.writeUInt8(path.length, 0);
|
|
17
|
+
for (let i = 0; i < path.length; i += 1) {
|
|
18
|
+
let v = path[i] || 0;
|
|
19
|
+
if (i < 3) {
|
|
20
|
+
// eslint-disable-next-line no-bitwise
|
|
21
|
+
v |= 0x80000000; // Harden
|
|
22
|
+
}
|
|
23
|
+
buf.writeInt32LE(v, 1 + i * 4);
|
|
24
|
+
}
|
|
25
|
+
return buf;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function signSendChunkv1(app: any, chunkIdx: number, _chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
|
|
29
|
+
return app.transport
|
|
30
|
+
.send(CLA, INS.SIGN_SECP256K1, chunkIdx, txType, chunk, [ERROR_CODE.NoError, 0x6984, 0x6a80])
|
|
31
|
+
.then((response: any) => {
|
|
32
|
+
const errorCodeData = response.slice(-2);
|
|
33
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
34
|
+
let errorMessage = errorCodeToString(returnCode);
|
|
35
|
+
|
|
36
|
+
if (returnCode === 0x6a80 || returnCode === 0x6984) {
|
|
37
|
+
errorMessage = `${errorMessage} : ${response.slice(0, response.length - 2).toString("ascii")}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let signature: any = null;
|
|
41
|
+
if (response.length > 2) {
|
|
42
|
+
signature = response.slice(0, response.length - 2);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return { error_message: errorMessage, return_code: returnCode, signature };
|
|
46
|
+
}, processErrorResponse);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function compressPublicKey(publicKey: Buffer) {
|
|
50
|
+
if (publicKey.length !== 65) {
|
|
51
|
+
throw new USwapError("wallet_ledger_invalid_params", {
|
|
52
|
+
reason: "decompressed public key length should be 65 bytes",
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const y = publicKey.slice(33, 65);
|
|
56
|
+
|
|
57
|
+
// @ts-expect-error
|
|
58
|
+
const z = Buffer.from([2 + (y[y.length - 1] & 1)]);
|
|
59
|
+
return Buffer.concat([z, publicKey.slice(1, 33)]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function publicKeyv1(app: any, data: Buffer) {
|
|
63
|
+
return app.transport
|
|
64
|
+
.send(CLA, INS.INS_PUBLIC_KEY_SECP256K1, 0, 0, data, [ERROR_CODE.NoError])
|
|
65
|
+
.then((response: any) => {
|
|
66
|
+
const errorCodeData = response.slice(-2);
|
|
67
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
68
|
+
const pk = Buffer.from(response.slice(0, 65));
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
compressed_pk: compressPublicKey(pk),
|
|
72
|
+
error_message: errorCodeToString(returnCode),
|
|
73
|
+
pk,
|
|
74
|
+
return_code: returnCode,
|
|
75
|
+
};
|
|
76
|
+
}, processErrorResponse);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function serializePathv2(path: number[]) {
|
|
80
|
+
if (!path || path.length !== 5) {
|
|
81
|
+
throw new USwapError("wallet_ledger_invalid_params", { reason: "Path must be exactly 5 elements" });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const buf = Buffer.alloc(20);
|
|
85
|
+
// @ts-expect-error
|
|
86
|
+
buf.writeUInt32LE(0x80000000 + path[0], 0);
|
|
87
|
+
// @ts-expect-error
|
|
88
|
+
buf.writeUInt32LE(0x80000000 + path[1], 4);
|
|
89
|
+
// @ts-expect-error
|
|
90
|
+
buf.writeUInt32LE(0x80000000 + path[2], 8);
|
|
91
|
+
// @ts-expect-error
|
|
92
|
+
buf.writeUInt32LE(path[3], 12);
|
|
93
|
+
// @ts-expect-error
|
|
94
|
+
buf.writeUInt32LE(path[4], 16);
|
|
95
|
+
|
|
96
|
+
return buf;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function signSendChunkv2(app: any, chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
|
|
100
|
+
let payloadType = PAYLOAD_TYPE.ADD;
|
|
101
|
+
if (chunkIdx === 1) {
|
|
102
|
+
payloadType = PAYLOAD_TYPE.INIT;
|
|
103
|
+
}
|
|
104
|
+
if (chunkIdx === chunkNum) {
|
|
105
|
+
payloadType = PAYLOAD_TYPE.LAST;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return signSendChunkv1(app, payloadType, 0, chunk, txType);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function publicKeyv2(app: any, data: Buffer) {
|
|
112
|
+
return app.transport.send(CLA, INS.GET_ADDR_SECP256K1, 0, 0, data, [ERROR_CODE.NoError]).then((response: any) => {
|
|
113
|
+
const errorCodeData = response.slice(-2);
|
|
114
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
115
|
+
const compressedPk = Buffer.from(response.slice(0, 33));
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
compressed_pk: compressedPk,
|
|
119
|
+
error_message: errorCodeToString(returnCode),
|
|
120
|
+
pk: "OBSOLETE PROPERTY",
|
|
121
|
+
return_code: returnCode,
|
|
122
|
+
};
|
|
123
|
+
}, processErrorResponse);
|
|
124
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { base64 } from "@scure/base";
|
|
6
|
+
import { type DerivationPathArray, NetworkDerivationPath, USwapError } from "@tcswap/helpers";
|
|
7
|
+
|
|
8
|
+
import { CosmosLedgerInterface } from "../../interfaces/CosmosLedgerInterface";
|
|
9
|
+
import type { GetAddressAndPubKeyResponse } from "../../types";
|
|
10
|
+
import { getSignature } from "./utils";
|
|
11
|
+
|
|
12
|
+
export class THORChainLedger extends CosmosLedgerInterface {
|
|
13
|
+
private pubKey: string | null = null;
|
|
14
|
+
|
|
15
|
+
derivationPath: DerivationPathArray;
|
|
16
|
+
|
|
17
|
+
constructor(derivationPath: DerivationPathArray = NetworkDerivationPath.THOR) {
|
|
18
|
+
super();
|
|
19
|
+
this.chain = "thor";
|
|
20
|
+
this.derivationPath = derivationPath;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get pubkey() {
|
|
24
|
+
return this.pubKey;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
connect = async () => {
|
|
28
|
+
await this.checkOrCreateTransportAndLedger();
|
|
29
|
+
const { compressed_pk, bech32_address }: GetAddressAndPubKeyResponse = await this.getAddressAndPubKey();
|
|
30
|
+
|
|
31
|
+
this.pubKey = base64.encode(compressed_pk);
|
|
32
|
+
|
|
33
|
+
return bech32_address;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
getAddressAndPubKey = async () => {
|
|
37
|
+
await this.checkOrCreateTransportAndLedger(true);
|
|
38
|
+
|
|
39
|
+
const response: GetAddressAndPubKeyResponse = await this.ledgerApp.getAddressAndPubKey(
|
|
40
|
+
this.derivationPath,
|
|
41
|
+
this.chain,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
this.validateResponse(response.return_code, response.error_message);
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
showAddressAndPubKey = async () => {
|
|
50
|
+
await this.checkOrCreateTransportAndLedger(true);
|
|
51
|
+
|
|
52
|
+
const response: GetAddressAndPubKeyResponse = await this.ledgerApp.showAddressAndPubKey(
|
|
53
|
+
this.derivationPath,
|
|
54
|
+
this.chain,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
this.validateResponse(response.return_code, response.error_message);
|
|
58
|
+
|
|
59
|
+
return response;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
signTransaction = async (rawTx: string, sequence = "0") => {
|
|
63
|
+
await this.checkOrCreateTransportAndLedger(true);
|
|
64
|
+
|
|
65
|
+
const { return_code, error_message, signature } = await this.ledgerApp.sign(this.derivationPath, rawTx);
|
|
66
|
+
|
|
67
|
+
if (!this.pubKey) throw new USwapError("wallet_ledger_pubkey_not_found");
|
|
68
|
+
|
|
69
|
+
this.validateResponse(return_code, error_message);
|
|
70
|
+
|
|
71
|
+
return [
|
|
72
|
+
{
|
|
73
|
+
pub_key: { type: "tendermint/PubKeySecp256k1", value: this.pubKey },
|
|
74
|
+
sequence,
|
|
75
|
+
signature: getSignature(signature),
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
sign = async (message: string) => {
|
|
81
|
+
await this.checkOrCreateTransportAndLedger(true);
|
|
82
|
+
|
|
83
|
+
const { return_code, error_message, signature } = await this.ledgerApp.sign(this.derivationPath, message);
|
|
84
|
+
|
|
85
|
+
if (!this.pubKey) throw new USwapError("wallet_ledger_pubkey_not_found");
|
|
86
|
+
|
|
87
|
+
this.validateResponse(return_code, error_message);
|
|
88
|
+
|
|
89
|
+
return getSignature(signature);
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
6
|
+
import { USwapError } from "@tcswap/helpers";
|
|
7
|
+
/** ******************************************************************************
|
|
8
|
+
* (c) 2019 ZondaX GmbH
|
|
9
|
+
* (c) 2016-2017 Ledger
|
|
10
|
+
*
|
|
11
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License.
|
|
13
|
+
* You may obtain a copy of the License at
|
|
14
|
+
*
|
|
15
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
* See the License for the specific language governing permissions and
|
|
21
|
+
* limitations under the License.
|
|
22
|
+
******************************************************************************* */
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
CHUNK_SIZE,
|
|
26
|
+
CLA,
|
|
27
|
+
ERROR_CODE,
|
|
28
|
+
errorCodeToString,
|
|
29
|
+
getVersion,
|
|
30
|
+
INS,
|
|
31
|
+
P1_VALUES,
|
|
32
|
+
P2_VALUES,
|
|
33
|
+
processErrorResponse,
|
|
34
|
+
} from "./common";
|
|
35
|
+
import {
|
|
36
|
+
publicKeyv1,
|
|
37
|
+
publicKeyv2,
|
|
38
|
+
serializePathv1,
|
|
39
|
+
serializePathv2,
|
|
40
|
+
signSendChunkv1,
|
|
41
|
+
signSendChunkv2,
|
|
42
|
+
} from "./helpers";
|
|
43
|
+
|
|
44
|
+
export class THORChainApp {
|
|
45
|
+
transport: Transport;
|
|
46
|
+
versionResponse: any;
|
|
47
|
+
|
|
48
|
+
constructor(transport: any) {
|
|
49
|
+
if (!transport) {
|
|
50
|
+
throw new USwapError("wallet_ledger_transport_not_defined");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
this.transport = transport;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static serializeHRP(hrp: string) {
|
|
57
|
+
if (hrp == null || hrp.length < 3 || hrp.length > 83) {
|
|
58
|
+
throw new USwapError("wallet_ledger_invalid_params", { reason: "Invalid HRP" });
|
|
59
|
+
}
|
|
60
|
+
const buf = Buffer.alloc(1 + hrp.length);
|
|
61
|
+
buf.writeUInt8(hrp.length, 0);
|
|
62
|
+
buf.write(hrp, 1);
|
|
63
|
+
return buf;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async serializePath(path: number[]) {
|
|
67
|
+
this.versionResponse = await getVersion(this.transport);
|
|
68
|
+
|
|
69
|
+
if (this.versionResponse.return_code !== ERROR_CODE.NoError) {
|
|
70
|
+
throw this.versionResponse;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
switch (this.versionResponse.major) {
|
|
74
|
+
case 1:
|
|
75
|
+
return serializePathv1(path);
|
|
76
|
+
case 2:
|
|
77
|
+
return serializePathv2(path);
|
|
78
|
+
default:
|
|
79
|
+
return Buffer.alloc(0);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async signGetChunks(path: number[], buffer: Buffer) {
|
|
84
|
+
const serializedPath = await this.serializePath(path);
|
|
85
|
+
|
|
86
|
+
const chunks = [];
|
|
87
|
+
chunks.push(serializedPath);
|
|
88
|
+
|
|
89
|
+
for (let i = 0; i < buffer.length; i += CHUNK_SIZE) {
|
|
90
|
+
let end = i + CHUNK_SIZE;
|
|
91
|
+
if (i > buffer.length) {
|
|
92
|
+
end = buffer.length;
|
|
93
|
+
}
|
|
94
|
+
chunks.push(buffer.slice(i, end));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return chunks;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async getVersion() {
|
|
101
|
+
try {
|
|
102
|
+
this.versionResponse = await getVersion(this.transport);
|
|
103
|
+
return this.versionResponse;
|
|
104
|
+
} catch (e) {
|
|
105
|
+
return processErrorResponse(e);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
appInfo() {
|
|
110
|
+
return this.transport.send(0xb0, 0x01, 0, 0).then((response: any) => {
|
|
111
|
+
const errorCodeData = response.slice(-2);
|
|
112
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
113
|
+
|
|
114
|
+
let appName = "";
|
|
115
|
+
let appVersion = "";
|
|
116
|
+
let flagLen = 0;
|
|
117
|
+
let flagsValue = 0;
|
|
118
|
+
|
|
119
|
+
if (response[0] !== 1) {
|
|
120
|
+
// Ledger responds with format ID 1. There is no spec for any format != 1
|
|
121
|
+
return { error_message: "response format ID not recognized", return_code: 0x9001 };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const appNameLen = response[1];
|
|
125
|
+
appName = response.slice(2, 2 + appNameLen).toString("ascii");
|
|
126
|
+
let idx = 2 + appNameLen;
|
|
127
|
+
const appVersionLen = response[idx];
|
|
128
|
+
idx += 1;
|
|
129
|
+
appVersion = response.slice(idx, idx + appVersionLen).toString("ascii");
|
|
130
|
+
idx += appVersionLen;
|
|
131
|
+
const appFlagsLen = response[idx];
|
|
132
|
+
idx += 1;
|
|
133
|
+
flagLen = appFlagsLen;
|
|
134
|
+
flagsValue = response[idx];
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
appName,
|
|
138
|
+
appVersion,
|
|
139
|
+
error_message: errorCodeToString(returnCode),
|
|
140
|
+
flag_onboarded: (flagsValue & 4) !== 0,
|
|
141
|
+
flag_pin_validated: (flagsValue & 128) !== 0,
|
|
142
|
+
flag_recovery: (flagsValue & 1) !== 0,
|
|
143
|
+
flag_signed_mcu_code: (flagsValue & 2) !== 0,
|
|
144
|
+
flagLen,
|
|
145
|
+
flagsValue,
|
|
146
|
+
return_code: returnCode,
|
|
147
|
+
};
|
|
148
|
+
}, processErrorResponse);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
deviceInfo() {
|
|
152
|
+
return this.transport
|
|
153
|
+
.send(0xe0, 0x01, 0, 0, Buffer.from([]), [ERROR_CODE.NoError, 0x6e00])
|
|
154
|
+
.then((response: any) => {
|
|
155
|
+
const errorCodeData = response.slice(-2);
|
|
156
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
157
|
+
|
|
158
|
+
if (returnCode === 0x6e00) {
|
|
159
|
+
return { error_message: "This command is only available in the Dashboard", return_code: returnCode };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const targetId = response.slice(0, 4).toString("hex");
|
|
163
|
+
|
|
164
|
+
let pos = 4;
|
|
165
|
+
const secureElementVersionLen = response[pos];
|
|
166
|
+
pos += 1;
|
|
167
|
+
const seVersion = response.slice(pos, pos + secureElementVersionLen).toString();
|
|
168
|
+
pos += secureElementVersionLen;
|
|
169
|
+
|
|
170
|
+
const flagsLen = response[pos];
|
|
171
|
+
pos += 1;
|
|
172
|
+
const flag = response.slice(pos, pos + flagsLen).toString("hex");
|
|
173
|
+
pos += flagsLen;
|
|
174
|
+
|
|
175
|
+
const mcuVersionLen = response[pos];
|
|
176
|
+
pos += 1;
|
|
177
|
+
// Patch issue in mcu version
|
|
178
|
+
let tmp = response.slice(pos, pos + mcuVersionLen);
|
|
179
|
+
if (tmp[mcuVersionLen - 1] === 0) {
|
|
180
|
+
tmp = response.slice(pos, pos + mcuVersionLen - 1);
|
|
181
|
+
}
|
|
182
|
+
const mcuVersion = tmp.toString();
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
error_message: errorCodeToString(returnCode),
|
|
186
|
+
flag,
|
|
187
|
+
mcuVersion,
|
|
188
|
+
return_code: returnCode,
|
|
189
|
+
seVersion,
|
|
190
|
+
// //
|
|
191
|
+
targetId,
|
|
192
|
+
};
|
|
193
|
+
}, processErrorResponse);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async publicKey(path: number[]) {
|
|
197
|
+
try {
|
|
198
|
+
const serializedPath = await this.serializePath(path);
|
|
199
|
+
|
|
200
|
+
switch (this.versionResponse.major) {
|
|
201
|
+
case 1:
|
|
202
|
+
return publicKeyv1(this, serializedPath);
|
|
203
|
+
case 2: {
|
|
204
|
+
const data = Buffer.concat([THORChainApp.serializeHRP("thor"), serializedPath]);
|
|
205
|
+
return publicKeyv2(this, data);
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
return { error_message: "App Version is not supported", return_code: 0x6400 };
|
|
209
|
+
}
|
|
210
|
+
} catch (e) {
|
|
211
|
+
return processErrorResponse(e);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
getAddressAndPubKey(path: number[], hrp: string, showInDevice = false) {
|
|
216
|
+
return this.serializePath(path)
|
|
217
|
+
.then((serializedPath: Buffer) => {
|
|
218
|
+
const data = Buffer.concat([THORChainApp.serializeHRP(hrp), serializedPath]);
|
|
219
|
+
return this.transport
|
|
220
|
+
.send(
|
|
221
|
+
CLA,
|
|
222
|
+
INS.GET_ADDR_SECP256K1,
|
|
223
|
+
showInDevice ? P1_VALUES.SHOW_ADDRESS_IN_DEVICE : P1_VALUES.ONLY_RETRIEVE,
|
|
224
|
+
0,
|
|
225
|
+
data,
|
|
226
|
+
[ERROR_CODE.NoError],
|
|
227
|
+
)
|
|
228
|
+
.then((response: any) => {
|
|
229
|
+
const errorCodeData = response.slice(-2);
|
|
230
|
+
const returnCode = errorCodeData[0] * 256 + errorCodeData[1];
|
|
231
|
+
|
|
232
|
+
const compressedPk = Buffer.from(response.slice(0, 33));
|
|
233
|
+
const bech32Address = Buffer.from(response.slice(33, -2)).toString();
|
|
234
|
+
|
|
235
|
+
return {
|
|
236
|
+
bech32_address: bech32Address,
|
|
237
|
+
compressed_pk: compressedPk,
|
|
238
|
+
error_message: errorCodeToString(returnCode),
|
|
239
|
+
return_code: returnCode,
|
|
240
|
+
};
|
|
241
|
+
}, processErrorResponse);
|
|
242
|
+
})
|
|
243
|
+
.catch((err) => processErrorResponse(err));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
showAddressAndPubKey(path: number[], hrp: string) {
|
|
247
|
+
return this.getAddressAndPubKey(path, hrp, true);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
signSendChunk(chunkIdx: number, chunkNum: number, chunk: Buffer, txType = P2_VALUES.JSON) {
|
|
251
|
+
switch (this.versionResponse.major) {
|
|
252
|
+
case 1:
|
|
253
|
+
return signSendChunkv1(this, chunkIdx, chunkNum, chunk, txType);
|
|
254
|
+
case 2:
|
|
255
|
+
return signSendChunkv2(this, chunkIdx, chunkNum, chunk, txType);
|
|
256
|
+
default:
|
|
257
|
+
return { error_message: "App Version is not supported", return_code: 0x6400 };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async sign(path: number[], message: string, txType = P2_VALUES.JSON) {
|
|
262
|
+
const buffer = Buffer.from(message);
|
|
263
|
+
let chunks: Buffer[] = [];
|
|
264
|
+
let response: any;
|
|
265
|
+
try {
|
|
266
|
+
chunks = await this.signGetChunks(path, buffer);
|
|
267
|
+
response = await this.signSendChunk(1, chunks.length, chunks[0] as Buffer, txType);
|
|
268
|
+
} catch (error) {
|
|
269
|
+
processErrorResponse(error);
|
|
270
|
+
}
|
|
271
|
+
let result = { error_message: response.error_message, return_code: response.return_code, signature: null };
|
|
272
|
+
|
|
273
|
+
for (let i = 1; i < chunks.length; i += 1) {
|
|
274
|
+
result = await this.signSendChunk(1 + i, chunks.length, chunks[i] as Buffer, txType);
|
|
275
|
+
if (result.return_code !== ERROR_CODE.NoError) {
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return { error_message: result.error_message, return_code: result.return_code, signature: result.signature };
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { base64 } from "@scure/base";
|
|
6
|
+
import { USwapError } from "@tcswap/helpers";
|
|
7
|
+
|
|
8
|
+
export const getSignature = (signatureArray: any) => {
|
|
9
|
+
// Check Type Length Value encoding
|
|
10
|
+
if (signatureArray.length < 64) {
|
|
11
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "Too short" });
|
|
12
|
+
}
|
|
13
|
+
if (signatureArray[0] !== 0x30) {
|
|
14
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "TLV encoding: expected first byte 0x30" });
|
|
15
|
+
}
|
|
16
|
+
if (signatureArray[1] + 2 !== signatureArray.length) {
|
|
17
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "signature length does not match TLV" });
|
|
18
|
+
}
|
|
19
|
+
if (signatureArray[2] !== 0x02) {
|
|
20
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "TLV encoding: expected length type 0x02" });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// r signature
|
|
24
|
+
const rLength = signatureArray[3];
|
|
25
|
+
let rSignature = signatureArray.slice(4, rLength + 4);
|
|
26
|
+
|
|
27
|
+
// Drop leading zero on some 'r' signatures that are 33 bytes.
|
|
28
|
+
if (rSignature.length === 33 && rSignature[0] === 0) {
|
|
29
|
+
rSignature = rSignature.slice(1, 33);
|
|
30
|
+
} else if (rSignature.length === 33) {
|
|
31
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "r too long" });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// add leading zero's to pad to 32 bytes
|
|
35
|
+
while (rSignature.length < 32) {
|
|
36
|
+
rSignature.unshift(0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// s signature
|
|
40
|
+
if (signatureArray[rLength + 4] !== 0x02) {
|
|
41
|
+
throw new USwapError("wallet_ledger_invalid_signature", {
|
|
42
|
+
reason: "TLV encoding: expected length type 0x02 for s",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const sLength = signatureArray[rLength + 5];
|
|
47
|
+
|
|
48
|
+
if (4 + rLength + 2 + sLength !== signatureArray.length) {
|
|
49
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "TLV byte lengths do not match message length" });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let sSignature = signatureArray.slice(rLength + 6, signatureArray.length);
|
|
53
|
+
|
|
54
|
+
// 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...
|
|
55
|
+
if (sSignature.length === 33 && sSignature[0] === 0) {
|
|
56
|
+
sSignature = sSignature.slice(1, 33);
|
|
57
|
+
} else if (sSignature.length === 33) {
|
|
58
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "s too long" });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// add leading zero's to pad to 32 bytes
|
|
62
|
+
while (sSignature.length < 32) {
|
|
63
|
+
sSignature.unshift(0);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (rSignature.length !== 32 || sSignature.length !== 32) {
|
|
67
|
+
throw new USwapError("wallet_ledger_invalid_signature", { reason: "must be 32 bytes each" });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return base64.encode(Buffer.concat([rSignature, sSignature]));
|
|
71
|
+
};
|