@ledgerhq/hw-app-canton 0.2.0-nightly.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/.turbo/turbo-build.log +4 -0
- package/.unimportedrc.json +5 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE.txt +21 -0
- package/README.md +76 -0
- package/__tests__/Canton.test.ts +122 -0
- package/jest.config.ts +24 -0
- package/lib/Canton.d.ts +48 -0
- package/lib/Canton.d.ts.map +1 -0
- package/lib/Canton.js +125 -0
- package/lib/Canton.js.map +1 -0
- package/lib/MockDevice.d.ts +24 -0
- package/lib/MockDevice.d.ts.map +1 -0
- package/lib/MockDevice.js +141 -0
- package/lib/MockDevice.js.map +1 -0
- package/lib-es/Canton.d.ts +48 -0
- package/lib-es/Canton.d.ts.map +1 -0
- package/lib-es/Canton.js +119 -0
- package/lib-es/Canton.js.map +1 -0
- package/lib-es/MockDevice.d.ts +24 -0
- package/lib-es/MockDevice.d.ts.map +1 -0
- package/lib-es/MockDevice.js +137 -0
- package/lib-es/MockDevice.js.map +1 -0
- package/package.json +57 -0
- package/src/Canton.ts +153 -0
- package/src/MockDevice.ts +170 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
2
|
+
import { CantonAddress, CantonSignature } from "@ledgerhq/coin-canton";
|
|
3
|
+
import { MockCantonDevice, AppConfig } from "./MockDevice";
|
|
4
|
+
/**
|
|
5
|
+
* Canton BOLOS API
|
|
6
|
+
*/
|
|
7
|
+
export default class Canton {
|
|
8
|
+
transport: Transport;
|
|
9
|
+
transportMock: MockCantonDevice;
|
|
10
|
+
constructor(transport: Transport, scrambleKey?: string);
|
|
11
|
+
/**
|
|
12
|
+
* Get a Canton address for a given BIP-32 path.
|
|
13
|
+
*
|
|
14
|
+
* @param path a path in BIP-32 format
|
|
15
|
+
* @param display whether to display the address on the device
|
|
16
|
+
* @return the address and public key
|
|
17
|
+
*/
|
|
18
|
+
getAddress(path: string, display?: boolean): Promise<CantonAddress>;
|
|
19
|
+
/**
|
|
20
|
+
* Sign a Canton transaction.
|
|
21
|
+
*
|
|
22
|
+
* @param path a path in BIP-32 format
|
|
23
|
+
* @param rawTx the raw transaction to sign
|
|
24
|
+
* @return the signature
|
|
25
|
+
*/
|
|
26
|
+
signTransaction(path: string, rawTx: string): Promise<CantonSignature>;
|
|
27
|
+
/**
|
|
28
|
+
* Get the app configuration.
|
|
29
|
+
* @return the app configuration including version
|
|
30
|
+
*/
|
|
31
|
+
getAppConfiguration(): Promise<AppConfig>;
|
|
32
|
+
/**
|
|
33
|
+
* Helper method to handle transport response and check for errors
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
36
|
+
private handleTransportResponse;
|
|
37
|
+
/**
|
|
38
|
+
* Serialize a BIP path to a data buffer for Canton BOLOS
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
41
|
+
private serializePath;
|
|
42
|
+
/**
|
|
43
|
+
* Simple deterministic hash function for generating mock addresses
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
private hashString;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=Canton.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Canton.d.ts","sourceRoot":"","sources":["../src/Canton.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,wBAAwB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGvE,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAoB3D;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,SAAS,EAAE,SAAS,CAAC;IACrB,aAAa,EAAE,gBAAgB,CAAC;gBAEpB,SAAS,EAAE,SAAS,EAAE,WAAW,SAAgC;IAO7E;;;;;;OAMG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,OAAe,GAAG,OAAO,CAAC,aAAa,CAAC;IAqBhF;;;;;;OAMG;IACG,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAa5E;;;OAGG;IACG,mBAAmB,IAAI,OAAO,CAAC,SAAS,CAAC;IAc/C;;;OAGG;IACH,OAAO,CAAC,uBAAuB;IAe/B;;;OAGG;IACH,OAAO,CAAC,aAAa;IAWrB;;;OAGG;IACH,OAAO,CAAC,UAAU;CASnB"}
|
package/lib-es/Canton.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { UserRefusedAddress, UserRefusedOnDevice } from "@ledgerhq/errors";
|
|
2
|
+
import BIPPath from "bip32-path";
|
|
3
|
+
import { MockCantonDevice } from "./MockDevice";
|
|
4
|
+
const CLA = 0xe0;
|
|
5
|
+
const P1_NON_CONFIRM = 0x00;
|
|
6
|
+
const P1_CONFIRM = 0x01;
|
|
7
|
+
const P2 = 0x00;
|
|
8
|
+
const INS = {
|
|
9
|
+
GET_VERSION: 0x04,
|
|
10
|
+
GET_ADDR: 0x05,
|
|
11
|
+
SIGN: 0x06,
|
|
12
|
+
};
|
|
13
|
+
const STATUS = {
|
|
14
|
+
OK: 0x9000,
|
|
15
|
+
USER_CANCEL: 0x6985,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Canton BOLOS API
|
|
19
|
+
*/
|
|
20
|
+
export default class Canton {
|
|
21
|
+
transport;
|
|
22
|
+
transportMock;
|
|
23
|
+
constructor(transport, scrambleKey = "canton_default_scramble_key") {
|
|
24
|
+
this.transport = transport;
|
|
25
|
+
this.transportMock = new MockCantonDevice();
|
|
26
|
+
transport.decorateAppAPIMethods(this, ["getAddress", "signTransaction"], scrambleKey);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get a Canton address for a given BIP-32 path.
|
|
30
|
+
*
|
|
31
|
+
* @param path a path in BIP-32 format
|
|
32
|
+
* @param display whether to display the address on the device
|
|
33
|
+
* @return the address and public key
|
|
34
|
+
*/
|
|
35
|
+
async getAddress(path, display = false) {
|
|
36
|
+
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
37
|
+
const serializedPath = this.serializePath(bipPath);
|
|
38
|
+
const p1 = display ? P1_CONFIRM : P1_NON_CONFIRM;
|
|
39
|
+
const response = await this.transportMock.send(CLA, INS.GET_ADDR, p1, P2, serializedPath);
|
|
40
|
+
const responseData = this.handleTransportResponse(response, "address");
|
|
41
|
+
// Handle 65-byte uncompressed SECP256R1 public key
|
|
42
|
+
const publicKey = "0x" + responseData.toString("hex");
|
|
43
|
+
const addressHash = this.hashString(publicKey);
|
|
44
|
+
const address = "canton_" + addressHash.substring(0, 36);
|
|
45
|
+
return {
|
|
46
|
+
publicKey,
|
|
47
|
+
address,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Sign a Canton transaction.
|
|
52
|
+
*
|
|
53
|
+
* @param path a path in BIP-32 format
|
|
54
|
+
* @param rawTx the raw transaction to sign
|
|
55
|
+
* @return the signature
|
|
56
|
+
*/
|
|
57
|
+
async signTransaction(path, rawTx) {
|
|
58
|
+
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
59
|
+
const serializedPath = this.serializePath(bipPath);
|
|
60
|
+
const payload = Buffer.concat([serializedPath, Buffer.from(rawTx, "hex")]);
|
|
61
|
+
const response = await this.transportMock.send(CLA, INS.SIGN, P1_CONFIRM, P2, payload);
|
|
62
|
+
const responseData = this.handleTransportResponse(response, "transaction");
|
|
63
|
+
const signature = "0x" + responseData.toString("hex");
|
|
64
|
+
return signature;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get the app configuration.
|
|
68
|
+
* @return the app configuration including version
|
|
69
|
+
*/
|
|
70
|
+
async getAppConfiguration() {
|
|
71
|
+
const [major, minor, patch] = await this.transportMock.send(CLA, INS.GET_VERSION, P1_NON_CONFIRM, P2, Buffer.alloc(0));
|
|
72
|
+
return {
|
|
73
|
+
version: `${major}.${minor}.${patch}`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Helper method to handle transport response and check for errors
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
handleTransportResponse(response, errorType) {
|
|
81
|
+
const statusCode = response.readUInt16BE(response.length - 2);
|
|
82
|
+
const responseData = response.slice(0, response.length - 2);
|
|
83
|
+
if (statusCode === STATUS.USER_CANCEL) {
|
|
84
|
+
if (errorType === "address") {
|
|
85
|
+
throw new UserRefusedAddress();
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new UserRefusedOnDevice();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return responseData;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Serialize a BIP path to a data buffer for Canton BOLOS
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
serializePath(path) {
|
|
98
|
+
const data = Buffer.alloc(1 + path.length * 4);
|
|
99
|
+
data.writeUInt8(path.length, 0); // Write path length as first byte
|
|
100
|
+
path.forEach((segment, index) => {
|
|
101
|
+
data.writeUInt32BE(segment, 1 + index * 4); // Write each segment as 32-bit integer
|
|
102
|
+
});
|
|
103
|
+
return data;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Simple deterministic hash function for generating mock addresses
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
109
|
+
hashString(str) {
|
|
110
|
+
let hash = 0;
|
|
111
|
+
for (let i = 0; i < str.length; i++) {
|
|
112
|
+
const char = str.charCodeAt(i);
|
|
113
|
+
hash = (hash << 5) - hash + char;
|
|
114
|
+
hash = hash & hash; // Convert to 32-bit integer
|
|
115
|
+
}
|
|
116
|
+
return Math.abs(hash).toString(16);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=Canton.js.map
|
|
@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CAAC,CAAC,uCAAuC;QACrF,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACK,UAAU,CAAC,GAAW;QAC5B,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YAC/B,IAAI,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;YACjC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,4BAA4B;QAClD,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
export type AppConfig = {
|
|
4
|
+
version: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Mock Canton "@ledgerhq/hw-app-canton" device implementation for development and testing
|
|
8
|
+
*/
|
|
9
|
+
export declare class MockCantonDevice {
|
|
10
|
+
private mockAddresses;
|
|
11
|
+
private mockSignatures;
|
|
12
|
+
constructor();
|
|
13
|
+
send(cla: number, ins: number, p1: number, p2: number, data: Buffer): Promise<Buffer>;
|
|
14
|
+
private getAddressResponse;
|
|
15
|
+
private signTransactionResponse;
|
|
16
|
+
private getAppConfigurationResponse;
|
|
17
|
+
private parsePathFromData;
|
|
18
|
+
private simulateDeviceDelay;
|
|
19
|
+
/**
|
|
20
|
+
* Simple deterministic hash function for generating mock data
|
|
21
|
+
*/
|
|
22
|
+
private hashString;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=MockDevice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MockDevice.d.ts","sourceRoot":"","sources":["../src/MockDevice.ts"],"names":[],"mappings":";;AAaA,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAsBF;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,aAAa,CAAyC;IAC9D,OAAO,CAAC,cAAc,CAA2C;;IAS3D,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;YAa7E,kBAAkB;YAiClB,uBAAuB;YA0BvB,2BAA2B;IAYzC,OAAO,CAAC,iBAAiB;YAkBX,mBAAmB;IAIjC;;OAEG;IACH,OAAO,CAAC,UAAU;CASnB"}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const INS = {
|
|
2
|
+
GET_VERSION: 0x04,
|
|
3
|
+
GET_ADDR: 0x05,
|
|
4
|
+
SIGN: 0x06,
|
|
5
|
+
};
|
|
6
|
+
const STATUS = {
|
|
7
|
+
OK: 0x9000,
|
|
8
|
+
USER_CANCEL: 0x6985,
|
|
9
|
+
};
|
|
10
|
+
// SECP256R1-compatible mock addresses
|
|
11
|
+
const SECP256R1_MOCK_ADDRESSES = {
|
|
12
|
+
"44'/6767'/0'/0'/0'": {
|
|
13
|
+
// Uncompressed SECP256R1 public key (65 bytes: 0x04 + 32-byte X + 32-byte Y)
|
|
14
|
+
publicKey: "0x04" +
|
|
15
|
+
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" +
|
|
16
|
+
"abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
|
|
17
|
+
address: "canton_1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z",
|
|
18
|
+
},
|
|
19
|
+
"44'/6767'/0'/0'/1'": {
|
|
20
|
+
// Another valid SECP256R1 public key
|
|
21
|
+
publicKey: "0x04" +
|
|
22
|
+
"abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890" +
|
|
23
|
+
"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcd",
|
|
24
|
+
address: "canton_2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a",
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Mock Canton "@ledgerhq/hw-app-canton" device implementation for development and testing
|
|
29
|
+
*/
|
|
30
|
+
export class MockCantonDevice {
|
|
31
|
+
mockAddresses = new Map();
|
|
32
|
+
mockSignatures = new Map();
|
|
33
|
+
constructor() {
|
|
34
|
+
// Initialize with SECP256R1-compatible addresses
|
|
35
|
+
Object.entries(SECP256R1_MOCK_ADDRESSES).forEach(([path, address]) => {
|
|
36
|
+
this.mockAddresses.set(path, address);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async send(cla, ins, p1, p2, data) {
|
|
40
|
+
switch (ins) {
|
|
41
|
+
case INS.GET_ADDR:
|
|
42
|
+
return this.getAddressResponse(data);
|
|
43
|
+
case INS.SIGN:
|
|
44
|
+
return this.signTransactionResponse(data);
|
|
45
|
+
case INS.GET_VERSION:
|
|
46
|
+
return this.getAppConfigurationResponse();
|
|
47
|
+
default:
|
|
48
|
+
throw new Error(`Unsupported instruction: ${ins}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async getAddressResponse(data) {
|
|
52
|
+
await this.simulateDeviceDelay();
|
|
53
|
+
// Parse the path from the data
|
|
54
|
+
const pathData = data.slice(1); // Skip length byte
|
|
55
|
+
const path = this.parsePathFromData(pathData);
|
|
56
|
+
const mockAddress = this.mockAddresses.get(path);
|
|
57
|
+
if (!mockAddress) {
|
|
58
|
+
// Generate SECP256R1-compatible mock address
|
|
59
|
+
const pathHash = this.hashString(path);
|
|
60
|
+
const newAddress = {
|
|
61
|
+
// Create valid uncompressed SECP256R1 public key format
|
|
62
|
+
publicKey: "0x04" +
|
|
63
|
+
pathHash.substring(0, 64).padEnd(64, "0") +
|
|
64
|
+
pathHash.substring(64, 128).padEnd(64, "0"),
|
|
65
|
+
address: `canton_${pathHash.substring(0, 36)}`,
|
|
66
|
+
};
|
|
67
|
+
this.mockAddresses.set(path, newAddress);
|
|
68
|
+
}
|
|
69
|
+
const address = this.mockAddresses.get(path);
|
|
70
|
+
// Return 65-byte uncompressed public key (0x04 + 32-byte X + 32-byte Y)
|
|
71
|
+
const publicKeyBytes = Buffer.from(address.publicKey.slice(2), "hex");
|
|
72
|
+
const response = Buffer.alloc(65 + 2);
|
|
73
|
+
publicKeyBytes.copy(response, 0);
|
|
74
|
+
response.writeUInt16BE(STATUS.OK, 65);
|
|
75
|
+
return response;
|
|
76
|
+
}
|
|
77
|
+
async signTransactionResponse(data) {
|
|
78
|
+
await this.simulateDeviceDelay();
|
|
79
|
+
// Parse the path and transaction from the data
|
|
80
|
+
const pathData = data.slice(0, 21); // First 21 bytes are path
|
|
81
|
+
const txData = data.slice(21); // Rest is transaction data
|
|
82
|
+
const path = this.parsePathFromData(pathData);
|
|
83
|
+
const signatureKey = `${path}:${txData.toString("hex")}`;
|
|
84
|
+
let signature = this.mockSignatures.get(signatureKey);
|
|
85
|
+
if (!signature) {
|
|
86
|
+
// Generate SECP256R1-compatible mock signature (64 bytes: r + s)
|
|
87
|
+
const combinedHash = this.hashString(signatureKey);
|
|
88
|
+
signature = `0x${combinedHash.substring(0, 64).padEnd(64, "0")}`;
|
|
89
|
+
this.mockSignatures.set(signatureKey, signature);
|
|
90
|
+
}
|
|
91
|
+
// Return 64-byte signature (r + s components)
|
|
92
|
+
const response = Buffer.alloc(64 + 2);
|
|
93
|
+
Buffer.from(signature.slice(2), "hex").copy(response, 0); // Remove '0x' prefix
|
|
94
|
+
response.writeUInt16BE(STATUS.OK, 64);
|
|
95
|
+
return response;
|
|
96
|
+
}
|
|
97
|
+
async getAppConfigurationResponse() {
|
|
98
|
+
await this.simulateDeviceDelay();
|
|
99
|
+
// Create response buffer: version data + status code
|
|
100
|
+
const versionData = Buffer.from([0x00, 0x01, 0x00, 0x06]); // Version 0.1.0
|
|
101
|
+
const response = Buffer.alloc(versionData.length + 2);
|
|
102
|
+
versionData.copy(response, 0);
|
|
103
|
+
response.writeUInt16BE(STATUS.OK, versionData.length);
|
|
104
|
+
return response;
|
|
105
|
+
}
|
|
106
|
+
parsePathFromData(data) {
|
|
107
|
+
// Convert the path data back to a BIP32 path string
|
|
108
|
+
const segments = [];
|
|
109
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
110
|
+
const segment = data.readUInt32BE(i);
|
|
111
|
+
segments.push(segment);
|
|
112
|
+
}
|
|
113
|
+
// Convert to BIP32 path string
|
|
114
|
+
const pathParts = segments.map(seg => {
|
|
115
|
+
const isHardened = (seg & 0x80000000) !== 0;
|
|
116
|
+
const value = seg & 0x7fffffff;
|
|
117
|
+
return isHardened ? `${value}'` : `${value}`;
|
|
118
|
+
});
|
|
119
|
+
return pathParts.join("/");
|
|
120
|
+
}
|
|
121
|
+
async simulateDeviceDelay() {
|
|
122
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Simple deterministic hash function for generating mock data
|
|
126
|
+
*/
|
|
127
|
+
hashString(str) {
|
|
128
|
+
let hash = 0;
|
|
129
|
+
for (let i = 0; i < str.length; i++) {
|
|
130
|
+
const char = str.charCodeAt(i);
|
|
131
|
+
hash = (hash << 5) - hash + char;
|
|
132
|
+
hash = hash & hash; // Convert to 32-bit integer
|
|
133
|
+
}
|
|
134
|
+
return Math.abs(hash).toString(16);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=MockDevice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MockDevice.js","sourceRoot":"","sources":["../src/MockDevice.ts"],"names":[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AG,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;QAC/C,CAAC,CAAC,CAAC;QAEH,OAAO,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAEO,KAAK,CAAC,mBAAmB;QAC/B,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;IACxD,CAAC;IAED;;OAEG;IACK,UAAU,CAAC,GAAW;QAC5B,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACpC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YAC/B,IAAI,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;YACjC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,4BAA4B;QAClD,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ledgerhq/hw-app-canton",
|
|
3
|
+
"version": "0.2.0-nightly.0",
|
|
4
|
+
"description": "Ledger Hardware Wallet Canton Application API",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Ledger",
|
|
7
|
+
"LedgerWallet",
|
|
8
|
+
"canton",
|
|
9
|
+
"Canton",
|
|
10
|
+
"Hardware Wallet"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/LedgerHQ/ledger-live.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/LedgerHQ/ledger-live/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/LedgerHQ/ledger-live/tree/develop/libs/ledgerjs/packages/hw-app-canton",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"main": "lib/Canton.js",
|
|
24
|
+
"module": "lib-es/Canton.js",
|
|
25
|
+
"types": "lib/Canton.d.ts",
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"bip32-path": "^0.4.2",
|
|
29
|
+
"@ledgerhq/errors": "^6.23.0",
|
|
30
|
+
"@ledgerhq/hw-transport": "^6.31.8",
|
|
31
|
+
"@ledgerhq/coin-canton": "^0.2.0-nightly.2"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/jest": "^29.5.10",
|
|
35
|
+
"@types/node": "^22.10.10",
|
|
36
|
+
"documentation": "14.0.2",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"rimraf": "^4.4.1",
|
|
39
|
+
"source-map-support": "^0.5.21",
|
|
40
|
+
"ts-jest": "^29.1.1",
|
|
41
|
+
"ts-node": "^10.4.0",
|
|
42
|
+
"@ledgerhq/hw-transport-mocker": "^6.29.8"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"clean": "rimraf lib lib-es",
|
|
46
|
+
"build": "tsc && tsc -m esnext --moduleResolution bundler --outDir lib-es",
|
|
47
|
+
"coverage": "jest --coverage --passWithNoTests && mv coverage/coverage-final.json coverage/coverage-hw-app-canton.json",
|
|
48
|
+
"prewatch": "pnpm build",
|
|
49
|
+
"watch": "tsc --watch",
|
|
50
|
+
"watch:es": "tsc --watch -m esnext --moduleResolution bundler --outDir lib-es",
|
|
51
|
+
"doc": "documentation readme src/** --section=API --pe ts --re ts --re d.ts",
|
|
52
|
+
"lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx --cache",
|
|
53
|
+
"lint:fix": "pnpm lint --fix",
|
|
54
|
+
"test": "jest",
|
|
55
|
+
"unimported": "unimported"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/Canton.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type Transport from "@ledgerhq/hw-transport";
|
|
2
|
+
import { UserRefusedAddress, UserRefusedOnDevice } from "@ledgerhq/errors";
|
|
3
|
+
import { CantonAddress, CantonSignature } from "@ledgerhq/coin-canton";
|
|
4
|
+
import BIPPath from "bip32-path";
|
|
5
|
+
|
|
6
|
+
import { MockCantonDevice, AppConfig } from "./MockDevice";
|
|
7
|
+
|
|
8
|
+
const CLA = 0xe0;
|
|
9
|
+
|
|
10
|
+
const P1_NON_CONFIRM = 0x00;
|
|
11
|
+
const P1_CONFIRM = 0x01;
|
|
12
|
+
|
|
13
|
+
const P2 = 0x00;
|
|
14
|
+
|
|
15
|
+
const INS = {
|
|
16
|
+
GET_VERSION: 0x04,
|
|
17
|
+
GET_ADDR: 0x05,
|
|
18
|
+
SIGN: 0x06,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const STATUS = {
|
|
22
|
+
OK: 0x9000,
|
|
23
|
+
USER_CANCEL: 0x6985,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Canton BOLOS API
|
|
28
|
+
*/
|
|
29
|
+
export default class Canton {
|
|
30
|
+
transport: Transport;
|
|
31
|
+
transportMock: MockCantonDevice;
|
|
32
|
+
|
|
33
|
+
constructor(transport: Transport, scrambleKey = "canton_default_scramble_key") {
|
|
34
|
+
this.transport = transport;
|
|
35
|
+
this.transportMock = new MockCantonDevice();
|
|
36
|
+
|
|
37
|
+
transport.decorateAppAPIMethods(this, ["getAddress", "signTransaction"], scrambleKey);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get a Canton address for a given BIP-32 path.
|
|
42
|
+
*
|
|
43
|
+
* @param path a path in BIP-32 format
|
|
44
|
+
* @param display whether to display the address on the device
|
|
45
|
+
* @return the address and public key
|
|
46
|
+
*/
|
|
47
|
+
async getAddress(path: string, display: boolean = false): Promise<CantonAddress> {
|
|
48
|
+
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
49
|
+
const serializedPath = this.serializePath(bipPath);
|
|
50
|
+
|
|
51
|
+
const p1 = display ? P1_CONFIRM : P1_NON_CONFIRM;
|
|
52
|
+
const response = await this.transportMock.send(CLA, INS.GET_ADDR, p1, P2, serializedPath);
|
|
53
|
+
|
|
54
|
+
const responseData = this.handleTransportResponse(response, "address");
|
|
55
|
+
|
|
56
|
+
// Handle 65-byte uncompressed SECP256R1 public key
|
|
57
|
+
const publicKey = "0x" + responseData.toString("hex");
|
|
58
|
+
|
|
59
|
+
const addressHash = this.hashString(publicKey);
|
|
60
|
+
const address = "canton_" + addressHash.substring(0, 36);
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
publicKey,
|
|
64
|
+
address,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Sign a Canton transaction.
|
|
70
|
+
*
|
|
71
|
+
* @param path a path in BIP-32 format
|
|
72
|
+
* @param rawTx the raw transaction to sign
|
|
73
|
+
* @return the signature
|
|
74
|
+
*/
|
|
75
|
+
async signTransaction(path: string, rawTx: string): Promise<CantonSignature> {
|
|
76
|
+
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
77
|
+
const serializedPath = this.serializePath(bipPath);
|
|
78
|
+
const payload = Buffer.concat([serializedPath, Buffer.from(rawTx, "hex")]);
|
|
79
|
+
|
|
80
|
+
const response = await this.transportMock.send(CLA, INS.SIGN, P1_CONFIRM, P2, payload);
|
|
81
|
+
|
|
82
|
+
const responseData = this.handleTransportResponse(response, "transaction");
|
|
83
|
+
|
|
84
|
+
const signature = "0x" + responseData.toString("hex");
|
|
85
|
+
return signature;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Get the app configuration.
|
|
90
|
+
* @return the app configuration including version
|
|
91
|
+
*/
|
|
92
|
+
async getAppConfiguration(): Promise<AppConfig> {
|
|
93
|
+
const [major, minor, patch] = await this.transportMock.send(
|
|
94
|
+
CLA,
|
|
95
|
+
INS.GET_VERSION,
|
|
96
|
+
P1_NON_CONFIRM,
|
|
97
|
+
P2,
|
|
98
|
+
Buffer.alloc(0),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
version: `${major}.${minor}.${patch}`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Helper method to handle transport response and check for errors
|
|
108
|
+
* @private
|
|
109
|
+
*/
|
|
110
|
+
private handleTransportResponse(response: Buffer, errorType: "address" | "transaction"): Buffer {
|
|
111
|
+
const statusCode = response.readUInt16BE(response.length - 2);
|
|
112
|
+
const responseData = response.slice(0, response.length - 2);
|
|
113
|
+
|
|
114
|
+
if (statusCode === STATUS.USER_CANCEL) {
|
|
115
|
+
if (errorType === "address") {
|
|
116
|
+
throw new UserRefusedAddress();
|
|
117
|
+
} else {
|
|
118
|
+
throw new UserRefusedOnDevice();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return responseData;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Serialize a BIP path to a data buffer for Canton BOLOS
|
|
127
|
+
* @private
|
|
128
|
+
*/
|
|
129
|
+
private serializePath(path: number[]): Buffer {
|
|
130
|
+
const data = Buffer.alloc(1 + path.length * 4);
|
|
131
|
+
|
|
132
|
+
data.writeUInt8(path.length, 0); // Write path length as first byte
|
|
133
|
+
path.forEach((segment, index) => {
|
|
134
|
+
data.writeUInt32BE(segment, 1 + index * 4); // Write each segment as 32-bit integer
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return data;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Simple deterministic hash function for generating mock addresses
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
private hashString(str: string): string {
|
|
145
|
+
let hash = 0;
|
|
146
|
+
for (let i = 0; i < str.length; i++) {
|
|
147
|
+
const char = str.charCodeAt(i);
|
|
148
|
+
hash = (hash << 5) - hash + char;
|
|
149
|
+
hash = hash & hash; // Convert to 32-bit integer
|
|
150
|
+
}
|
|
151
|
+
return Math.abs(hash).toString(16);
|
|
152
|
+
}
|
|
153
|
+
}
|