@ledgerhq/hw-app-canton 0.3.0-nightly.2 → 0.3.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 +1 -1
- package/.unimportedrc.json +2 -1
- package/CHANGELOG.md +14 -7
- package/README.md +6 -3
- package/{src → __tests__}/Canton.test.ts +1 -21
- package/lib/Canton.d.ts +5 -10
- package/lib/Canton.d.ts.map +1 -1
- package/lib/Canton.integ.test.js +4 -49
- package/lib/Canton.integ.test.js.map +1 -1
- package/lib/Canton.js +13 -17
- package/lib/Canton.js.map +1 -1
- 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 +5 -10
- package/lib-es/Canton.d.ts.map +1 -1
- package/lib-es/Canton.integ.test.js +4 -26
- package/lib-es/Canton.integ.test.js.map +1 -1
- package/lib-es/Canton.js +13 -17
- package/lib-es/Canton.js.map +1 -1
- 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 +6 -5
- package/src/Canton.integ.test.ts +7 -33
- package/src/Canton.ts +17 -42
- package/src/MockDevice.ts +170 -0
- package/lib/Canton.test.d.ts +0 -2
- package/lib/Canton.test.d.ts.map +0 -1
- package/lib/Canton.test.js +0 -101
- package/lib/Canton.test.js.map +0 -1
- package/lib-es/Canton.test.d.ts +0 -2
- package/lib-es/Canton.test.d.ts.map +0 -1
- package/lib-es/Canton.test.js +0 -96
- package/lib-es/Canton.test.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Canton.integ.test.js","sourceRoot":"","sources":["../src/Canton.integ.test.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,
|
|
1
|
+
{"version":3,"file":"Canton.integ.test.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AE;QAC/B,EAAE,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib-es/Canton.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { UserRefusedAddress, UserRefusedOnDevice } from "@ledgerhq/errors";
|
|
2
2
|
import BIPPath from "bip32-path";
|
|
3
|
+
import { MockCantonDevice } from "./MockDevice";
|
|
3
4
|
const CLA = 0xe0;
|
|
4
5
|
const P1_NON_CONFIRM = 0x00;
|
|
5
6
|
const P1_CONFIRM = 0x01;
|
|
6
|
-
|
|
7
|
-
const P2_NONE = 0x00;
|
|
8
|
-
// P2 indicating first APDU in a large request.
|
|
9
|
-
const P2_FIRST = 0x01;
|
|
10
|
-
// P2 indicating that this is not the last APDU in a large request.
|
|
11
|
-
const P2_MORE = 0x02;
|
|
7
|
+
const P2 = 0x00;
|
|
12
8
|
const INS = {
|
|
13
9
|
GET_VERSION: 0x03,
|
|
14
10
|
GET_APP_NAME: 0x04,
|
|
@@ -24,8 +20,10 @@ const STATUS = {
|
|
|
24
20
|
*/
|
|
25
21
|
export default class Canton {
|
|
26
22
|
transport;
|
|
23
|
+
transportMock;
|
|
27
24
|
constructor(transport, scrambleKey = "canton_default_scramble_key") {
|
|
28
25
|
this.transport = transport;
|
|
26
|
+
this.transportMock = new MockCantonDevice();
|
|
29
27
|
transport.decorateAppAPIMethods(this, ["getAddress", "signTransaction", "getAppConfiguration"], scrambleKey);
|
|
30
28
|
}
|
|
31
29
|
/**
|
|
@@ -39,14 +37,15 @@ export default class Canton {
|
|
|
39
37
|
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
40
38
|
const serializedPath = this.serializePath(bipPath);
|
|
41
39
|
const p1 = display ? P1_CONFIRM : P1_NON_CONFIRM;
|
|
42
|
-
const response = await this.transport.send(CLA, INS.GET_ADDR, p1,
|
|
40
|
+
const response = await this.transport.send(CLA, INS.GET_ADDR, p1, P2, serializedPath);
|
|
43
41
|
const responseData = this.handleTransportResponse(response, "address");
|
|
44
42
|
const { pubKey } = this.extractPubkeyAndChainCode(responseData);
|
|
43
|
+
// Handle 65-byte uncompressed SECP256R1 public key
|
|
45
44
|
const publicKey = "0x" + pubKey;
|
|
46
45
|
const addressHash = this.hashString(publicKey);
|
|
47
46
|
const address = "canton_" + addressHash.substring(0, 36);
|
|
48
47
|
return {
|
|
49
|
-
publicKey
|
|
48
|
+
publicKey,
|
|
50
49
|
address,
|
|
51
50
|
};
|
|
52
51
|
}
|
|
@@ -54,19 +53,16 @@ export default class Canton {
|
|
|
54
53
|
* Sign a Canton transaction.
|
|
55
54
|
*
|
|
56
55
|
* @param path a path in BIP-32 format
|
|
57
|
-
* @param
|
|
56
|
+
* @param rawTx the raw transaction to sign
|
|
58
57
|
* @return the signature
|
|
59
58
|
*/
|
|
60
|
-
async signTransaction(path,
|
|
61
|
-
// 1. Send the derivation path
|
|
59
|
+
async signTransaction(path, rawTx) {
|
|
62
60
|
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
63
61
|
const serializedPath = this.serializePath(bipPath);
|
|
64
|
-
const
|
|
65
|
-
this.
|
|
66
|
-
// 2. Send the transaction hash
|
|
67
|
-
const response = await this.transport.send(CLA, INS.SIGN, P1_NON_CONFIRM, P2_NONE, Buffer.from(txHash, "hex"));
|
|
62
|
+
const payload = Buffer.concat([serializedPath, Buffer.from(rawTx, "hex")]);
|
|
63
|
+
const response = await this.transportMock.send(CLA, INS.SIGN, P1_CONFIRM, P2, payload);
|
|
68
64
|
const responseData = this.handleTransportResponse(response, "transaction");
|
|
69
|
-
const signature = responseData.toString("hex");
|
|
65
|
+
const signature = "0x" + responseData.toString("hex");
|
|
70
66
|
return signature;
|
|
71
67
|
}
|
|
72
68
|
/**
|
|
@@ -74,7 +70,7 @@ export default class Canton {
|
|
|
74
70
|
* @return the app configuration including version
|
|
75
71
|
*/
|
|
76
72
|
async getAppConfiguration() {
|
|
77
|
-
const response = await this.transport.send(CLA, INS.GET_VERSION, P1_NON_CONFIRM,
|
|
73
|
+
const response = await this.transport.send(CLA, INS.GET_VERSION, P1_NON_CONFIRM, P2, Buffer.alloc(0));
|
|
78
74
|
const responseData = this.handleTransportResponse(response, "version");
|
|
79
75
|
const { major, minor, patch } = this.extractVersion(responseData);
|
|
80
76
|
return {
|
package/lib-es/Canton.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Canton.js","sourceRoot":"","sources":["../src/Canton.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"Canton.js","sourceRoot":"","sources":["../src/Canton.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,OAAO,OAAO,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAE,gBAAgB,EAAa,MAAM,cAAc,CAAC;AAE3D,MAAM,GAAG,GAAG,IAAI,CAAC;AAEjB,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,UAAU,GAAG,IAAI,CAAC;AAExB,MAAM,EAAE,GAAG,IAAI,CAAC;AAEhB,MAAM,GAAG,GAAG;IACV,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE,IAAI;IAClB,QAAQ,EAAE,IAAI;IACd,IAAI,EAAE,IAAI;CACX,CAAC;AAEF,MAAM,MAAM,GAAG;IACb,EAAE,EAAE,MAAM;IACV,WAAW,EAAE,MAAM;CACpB,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,SAAS,CAAY;IACrB,aAAa,CAAmB;IAEhC,YAAY,SAAoB,EAAE,WAAW,GAAG,6BAA6B;QAC3E,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,aAAa,GAAG,IAAI,gBAAgB,EAAE,CAAC;QAE5C,SAAS,C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<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":[],"mappings":"AAEA,MAAM,GAAG,GAAG;IACV,WAAW,EAAE,IAAI;IACjB,QAAQ,EAAE,IAAI;IACd,IAAI,EAAE,IAAI;CACX,CAAC;AAEF,MAAM,MAAM,GAAG;IACb,EAAE,EAAE,MAAM;IACV,WAAW,EAAE,MAAM;CACpB,CAAC;AAMF,sCAAsC;AACtC,MAAM,wBAAwB,GAAG;IAC/B,oBAAoB,EAAE;QACpB,6EAA6E;QAC7E,SAAS,EACP,MAAM;YACN,kEAAkE;YAClE,kEAAkE;QACpE,OAAO,EAAE,6DAA6D;KACvE;IACD,oBAAoB,EAAE;QACpB,qCAAqC;QACrC,SAAS,EACP,MAAM;YACN,kEAAkE;YAClE,gEAAgE;QAClE,OAAO,EAAE,6DAA6D;KACvE;CACF,CAAC;AAEF;;GAEG;AACH,MAAM,OAAO,gBAAgB;IACnB,aAAa,GAA+B,IAAI,GAAG,EAAE,CAAC;IACtD,cAAc,GAAiC,IAAI,GAAG,EAAE,CAAC;IAEjE;QACE,iDAAiD;QACjD,M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AC,CAAC,4BAA4B;QAClD,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IACrC,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ledgerhq/hw-app-canton",
|
|
3
|
-
"version": "0.3.0
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Ledger Hardware Wallet Canton Application API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ledger",
|
|
@@ -26,8 +26,9 @@
|
|
|
26
26
|
"license": "Apache-2.0",
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"bip32-path": "^0.4.2",
|
|
29
|
-
"@ledgerhq/
|
|
30
|
-
"@ledgerhq/
|
|
29
|
+
"@ledgerhq/errors": "^6.25.0",
|
|
30
|
+
"@ledgerhq/hw-transport": "^6.31.10",
|
|
31
|
+
"@ledgerhq/coin-canton": "^0.4.0"
|
|
31
32
|
},
|
|
32
33
|
"devDependencies": {
|
|
33
34
|
"@types/jest": "^29.5.10",
|
|
@@ -38,8 +39,8 @@
|
|
|
38
39
|
"source-map-support": "^0.5.21",
|
|
39
40
|
"ts-jest": "^29.1.1",
|
|
40
41
|
"ts-node": "^10.4.0",
|
|
41
|
-
"@ledgerhq/hw-transport-mocker": "^6.29.10
|
|
42
|
-
"@ledgerhq/hw-transport-node-speculos-http": "^6.
|
|
42
|
+
"@ledgerhq/hw-transport-mocker": "^6.29.10",
|
|
43
|
+
"@ledgerhq/hw-transport-node-speculos-http": "^6.29.10"
|
|
43
44
|
},
|
|
44
45
|
"scripts": {
|
|
45
46
|
"clean": "rimraf lib lib-es",
|
package/src/Canton.integ.test.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import SpeculosTransportHttp
|
|
1
|
+
import SpeculosTransportHttp from "@ledgerhq/hw-transport-node-speculos-http";
|
|
2
2
|
import Canton from "./Canton";
|
|
3
|
+
import Transport from "@ledgerhq/hw-transport";
|
|
3
4
|
|
|
4
5
|
describe("AppCanton", () => {
|
|
5
|
-
let transport:
|
|
6
|
+
let transport: Transport;
|
|
6
7
|
|
|
7
8
|
beforeAll(async () => {
|
|
8
9
|
transport = await SpeculosTransportHttp.open({});
|
|
9
10
|
});
|
|
10
|
-
afterAll(async () => {
|
|
11
|
-
transport.close();
|
|
12
|
-
});
|
|
13
11
|
|
|
14
12
|
describe("getAppConfiguration", () => {
|
|
15
13
|
it("returns app version", async () => {
|
|
@@ -37,38 +35,14 @@ describe("AppCanton", () => {
|
|
|
37
35
|
|
|
38
36
|
// THEN
|
|
39
37
|
expect(result).toEqual({
|
|
40
|
-
address: "
|
|
41
|
-
publicKey:
|
|
38
|
+
address: "canton_10cd9ed0",
|
|
39
|
+
publicKey:
|
|
40
|
+
"0x043b462de34ec31fba274f2a381947aef26697912194312fc289c46cc1b2b4f6b00828dc1e4f96001b10463083edf85f2e0550862a3dc99ed411ca6d25f2bc19a8",
|
|
42
41
|
});
|
|
43
42
|
});
|
|
44
43
|
});
|
|
45
44
|
|
|
46
45
|
describe("signTransaction", () => {
|
|
47
|
-
it("returns sign transaction"
|
|
48
|
-
// GIVEN
|
|
49
|
-
const app = new Canton(transport);
|
|
50
|
-
const derivationPath = "44'/6767'/0'/0'/0'";
|
|
51
|
-
const txHash = "d1e98829444207b0e170346b2e80b58a2ffc602b01e190fb742016d407c84efd";
|
|
52
|
-
|
|
53
|
-
// WHEN
|
|
54
|
-
const signPromise = app.signTransaction(derivationPath, txHash);
|
|
55
|
-
|
|
56
|
-
// Waiting Speculos receive APDUs
|
|
57
|
-
const delay = (ms: number): Promise<void> => new Promise(f => setTimeout(f, ms));
|
|
58
|
-
await delay(500);
|
|
59
|
-
// Valid transaction butotn interaction sequence
|
|
60
|
-
await transport.button(SpeculosButton.BOTH);
|
|
61
|
-
await transport.button(SpeculosButton.RIGHT);
|
|
62
|
-
await transport.button(SpeculosButton.RIGHT);
|
|
63
|
-
await transport.button(SpeculosButton.RIGHT);
|
|
64
|
-
await transport.button(SpeculosButton.BOTH);
|
|
65
|
-
|
|
66
|
-
const result = await signPromise;
|
|
67
|
-
|
|
68
|
-
// THEN
|
|
69
|
-
expect(result).toEqual(
|
|
70
|
-
"40a65f53c3657bc04efefb67a425ba093a5cb5391d18142f148bb2c48daacf316114cff920a58d5996ca828c7ce265f537f1d7fca8fa82c3c73bd944a96e701a0000",
|
|
71
|
-
);
|
|
72
|
-
});
|
|
46
|
+
it.todo("returns sign transaction");
|
|
73
47
|
});
|
|
74
48
|
});
|
package/src/Canton.ts
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import type Transport from "@ledgerhq/hw-transport";
|
|
2
2
|
import { UserRefusedAddress, UserRefusedOnDevice } from "@ledgerhq/errors";
|
|
3
|
+
import { CantonAddress, CantonSignature } from "@ledgerhq/coin-canton";
|
|
3
4
|
import BIPPath from "bip32-path";
|
|
4
5
|
|
|
6
|
+
import { MockCantonDevice, AppConfig } from "./MockDevice";
|
|
7
|
+
|
|
5
8
|
const CLA = 0xe0;
|
|
6
9
|
|
|
7
10
|
const P1_NON_CONFIRM = 0x00;
|
|
8
11
|
const P1_CONFIRM = 0x01;
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
const P2_NONE = 0x00;
|
|
12
|
-
// P2 indicating first APDU in a large request.
|
|
13
|
-
const P2_FIRST = 0x01;
|
|
14
|
-
// P2 indicating that this is not the last APDU in a large request.
|
|
15
|
-
const P2_MORE = 0x02;
|
|
13
|
+
const P2 = 0x00;
|
|
16
14
|
|
|
17
15
|
const INS = {
|
|
18
16
|
GET_VERSION: 0x03,
|
|
@@ -26,25 +24,16 @@ const STATUS = {
|
|
|
26
24
|
USER_CANCEL: 0x6985,
|
|
27
25
|
};
|
|
28
26
|
|
|
29
|
-
export type AppConfig = {
|
|
30
|
-
version: string;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export type CantonAddress = {
|
|
34
|
-
publicKey: string;
|
|
35
|
-
address: string;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type CantonSignature = string;
|
|
39
|
-
|
|
40
27
|
/**
|
|
41
28
|
* Canton BOLOS API
|
|
42
29
|
*/
|
|
43
30
|
export default class Canton {
|
|
44
31
|
transport: Transport;
|
|
32
|
+
transportMock: MockCantonDevice;
|
|
45
33
|
|
|
46
34
|
constructor(transport: Transport, scrambleKey = "canton_default_scramble_key") {
|
|
47
35
|
this.transport = transport;
|
|
36
|
+
this.transportMock = new MockCantonDevice();
|
|
48
37
|
|
|
49
38
|
transport.decorateAppAPIMethods(
|
|
50
39
|
this,
|
|
@@ -65,17 +54,19 @@ export default class Canton {
|
|
|
65
54
|
const serializedPath = this.serializePath(bipPath);
|
|
66
55
|
|
|
67
56
|
const p1 = display ? P1_CONFIRM : P1_NON_CONFIRM;
|
|
68
|
-
const response = await this.transport.send(CLA, INS.GET_ADDR, p1,
|
|
57
|
+
const response = await this.transport.send(CLA, INS.GET_ADDR, p1, P2, serializedPath);
|
|
69
58
|
|
|
70
59
|
const responseData = this.handleTransportResponse(response, "address");
|
|
71
60
|
const { pubKey } = this.extractPubkeyAndChainCode(responseData);
|
|
72
61
|
|
|
62
|
+
// Handle 65-byte uncompressed SECP256R1 public key
|
|
73
63
|
const publicKey = "0x" + pubKey;
|
|
64
|
+
|
|
74
65
|
const addressHash = this.hashString(publicKey);
|
|
75
66
|
const address = "canton_" + addressHash.substring(0, 36);
|
|
76
67
|
|
|
77
68
|
return {
|
|
78
|
-
publicKey
|
|
69
|
+
publicKey,
|
|
79
70
|
address,
|
|
80
71
|
};
|
|
81
72
|
}
|
|
@@ -84,35 +75,19 @@ export default class Canton {
|
|
|
84
75
|
* Sign a Canton transaction.
|
|
85
76
|
*
|
|
86
77
|
* @param path a path in BIP-32 format
|
|
87
|
-
* @param
|
|
78
|
+
* @param rawTx the raw transaction to sign
|
|
88
79
|
* @return the signature
|
|
89
80
|
*/
|
|
90
|
-
async signTransaction(path: string,
|
|
91
|
-
// 1. Send the derivation path
|
|
81
|
+
async signTransaction(path: string, rawTx: string): Promise<CantonSignature> {
|
|
92
82
|
const bipPath = BIPPath.fromString(path).toPathArray();
|
|
93
83
|
const serializedPath = this.serializePath(bipPath);
|
|
84
|
+
const payload = Buffer.concat([serializedPath, Buffer.from(rawTx, "hex")]);
|
|
94
85
|
|
|
95
|
-
const
|
|
96
|
-
CLA,
|
|
97
|
-
INS.SIGN,
|
|
98
|
-
P1_NON_CONFIRM,
|
|
99
|
-
P2_FIRST | P2_MORE,
|
|
100
|
-
serializedPath,
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
this.handleTransportResponse(pathResponse, "transaction");
|
|
104
|
-
|
|
105
|
-
// 2. Send the transaction hash
|
|
106
|
-
const response = await this.transport.send(
|
|
107
|
-
CLA,
|
|
108
|
-
INS.SIGN,
|
|
109
|
-
P1_NON_CONFIRM,
|
|
110
|
-
P2_NONE,
|
|
111
|
-
Buffer.from(txHash, "hex"),
|
|
112
|
-
);
|
|
86
|
+
const response = await this.transportMock.send(CLA, INS.SIGN, P1_CONFIRM, P2, payload);
|
|
113
87
|
|
|
114
88
|
const responseData = this.handleTransportResponse(response, "transaction");
|
|
115
|
-
|
|
89
|
+
|
|
90
|
+
const signature = "0x" + responseData.toString("hex");
|
|
116
91
|
return signature;
|
|
117
92
|
}
|
|
118
93
|
|
|
@@ -125,7 +100,7 @@ export default class Canton {
|
|
|
125
100
|
CLA,
|
|
126
101
|
INS.GET_VERSION,
|
|
127
102
|
P1_NON_CONFIRM,
|
|
128
|
-
|
|
103
|
+
P2,
|
|
129
104
|
Buffer.alloc(0),
|
|
130
105
|
);
|
|
131
106
|
|