@alephium/ledger-app 0.1.1 → 0.1.3
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/src/index.d.ts +1 -1
- package/dist/src/index.js +5 -3
- package/dist/test/release.test.js +1 -1
- package/dist/test/speculos.test.js +8 -4
- package/package.json +1 -1
- package/src/index.ts +7 -4
- package/test/release.test.ts +1 -1
- package/test/speculos.test.ts +8 -4
package/dist/src/index.d.ts
CHANGED
|
@@ -13,6 +13,6 @@ export default class AlephiumApp {
|
|
|
13
13
|
readonly transport: Transport;
|
|
14
14
|
constructor(transport: Transport);
|
|
15
15
|
getVersion(): Promise<string>;
|
|
16
|
-
getAccount(startPath: string, targetGroup?: number): Promise<Account>;
|
|
16
|
+
getAccount(startPath: string, targetGroup?: number): Promise<readonly [Account, number]>;
|
|
17
17
|
signHash(path: string, hash: Buffer): Promise<string>;
|
|
18
18
|
}
|
package/dist/src/index.js
CHANGED
|
@@ -28,7 +28,6 @@ const web3_1 = require("@alephium/web3");
|
|
|
28
28
|
const hw_transport_1 = require("@ledgerhq/hw-transport");
|
|
29
29
|
const serde = __importStar(require("./serde"));
|
|
30
30
|
const elliptic_1 = require("elliptic");
|
|
31
|
-
const console_1 = require("console");
|
|
32
31
|
const ec = new elliptic_1.ec('secp256k1');
|
|
33
32
|
exports.CLA = 0x80;
|
|
34
33
|
var INS;
|
|
@@ -50,14 +49,17 @@ class AlephiumApp {
|
|
|
50
49
|
}
|
|
51
50
|
// TODO: make address display optional
|
|
52
51
|
async getAccount(startPath, targetGroup) {
|
|
53
|
-
|
|
52
|
+
if ((targetGroup ?? 0) >= exports.GROUP_NUM) {
|
|
53
|
+
throw Error(`Invalid targetGroup: ${targetGroup}`);
|
|
54
|
+
}
|
|
54
55
|
const p1 = targetGroup === undefined ? 0x00 : exports.GROUP_NUM;
|
|
55
56
|
const p2 = targetGroup === undefined ? 0x00 : targetGroup;
|
|
56
57
|
const response = await this.transport.send(exports.CLA, INS.GET_PUBLIC_KEY, p1, p2, serde.serializePath(startPath));
|
|
57
58
|
const publicKey = ec.keyFromPublic(response.slice(0, 65)).getPublic(true, 'hex');
|
|
58
59
|
const address = (0, web3_1.addressFromPublicKey)(publicKey);
|
|
59
60
|
const group = (0, web3_1.groupOfAddress)(address);
|
|
60
|
-
|
|
61
|
+
const hdIndex = response.slice(65, 69).readUInt32BE(0);
|
|
62
|
+
return [{ publicKey: publicKey, address: address, group: group, keyType: 'default' }, hdIndex];
|
|
61
63
|
}
|
|
62
64
|
async signHash(path, hash) {
|
|
63
65
|
if (hash.length !== exports.HASH_LEN) {
|
|
@@ -15,7 +15,7 @@ describe.skip('Integration', () => {
|
|
|
15
15
|
const transport = await hw_transport_node_hid_1.default.open('');
|
|
16
16
|
(0, logs_1.listen)((log) => console.log(log));
|
|
17
17
|
const app = new src_1.default(transport);
|
|
18
|
-
const account = await app.getAccount(path);
|
|
18
|
+
const [account] = await app.getAccount(path);
|
|
19
19
|
console.log(`${JSON.stringify(account)}`);
|
|
20
20
|
const hash = Buffer.from(blakejs_1.default.blake2b(Buffer.from([0, 1, 2, 3, 4]), undefined, 32));
|
|
21
21
|
const signature = await app.signHash(path, hash);
|
|
@@ -48,9 +48,11 @@ function getRandomInt(min, max) {
|
|
|
48
48
|
}
|
|
49
49
|
describe('sdk', () => {
|
|
50
50
|
const apduPort = 9999;
|
|
51
|
+
let pathIndex;
|
|
51
52
|
let path;
|
|
52
53
|
beforeEach(() => {
|
|
53
|
-
|
|
54
|
+
pathIndex = getRandomInt(0, 1000000);
|
|
55
|
+
path = `m/44'/1234'/0'/0/` + pathIndex;
|
|
54
56
|
});
|
|
55
57
|
it('should get version', async () => {
|
|
56
58
|
const transport = await hw_transport_node_speculos_1.default.open({ apduPort });
|
|
@@ -62,7 +64,8 @@ describe('sdk', () => {
|
|
|
62
64
|
it('should get public key', async () => {
|
|
63
65
|
const transport = await hw_transport_node_speculos_1.default.open({ apduPort });
|
|
64
66
|
const app = new src_1.default(transport);
|
|
65
|
-
const account = await app.getAccount(path);
|
|
67
|
+
const [account, hdIndex] = await app.getAccount(path);
|
|
68
|
+
expect(hdIndex).toBe(pathIndex);
|
|
66
69
|
console.log(account);
|
|
67
70
|
await transport.close();
|
|
68
71
|
});
|
|
@@ -70,7 +73,8 @@ describe('sdk', () => {
|
|
|
70
73
|
const transport = await hw_transport_node_speculos_1.default.open({ apduPort });
|
|
71
74
|
const app = new src_1.default(transport);
|
|
72
75
|
Array(src_1.GROUP_NUM).forEach(async (_, group) => {
|
|
73
|
-
const account = await app.getAccount(path, group);
|
|
76
|
+
const [account, hdIndex] = await app.getAccount(path, group);
|
|
77
|
+
expect(hdIndex >= pathIndex).toBe(true);
|
|
74
78
|
expect((0, web3_1.groupOfAddress)(account.address)).toBe(group);
|
|
75
79
|
});
|
|
76
80
|
await transport.close();
|
|
@@ -78,7 +82,7 @@ describe('sdk', () => {
|
|
|
78
82
|
it('should sign hash', async () => {
|
|
79
83
|
const transport = await hw_transport_node_speculos_1.default.open({ apduPort });
|
|
80
84
|
const app = new src_1.default(transport);
|
|
81
|
-
const account = await app.getAccount(path);
|
|
85
|
+
const [account] = await app.getAccount(path);
|
|
82
86
|
console.log(account);
|
|
83
87
|
const hash = Buffer.from(blakejs_1.default.blake2b(Buffer.from([0, 1, 2, 3, 4]), undefined, 32));
|
|
84
88
|
setTimeout(async () => {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,7 +2,6 @@ import { Account, addressFromPublicKey, encodeHexSignature, groupOfAddress } fro
|
|
|
2
2
|
import Transport, { StatusCodes } from '@ledgerhq/hw-transport'
|
|
3
3
|
import * as serde from './serde'
|
|
4
4
|
import { ec as EC } from 'elliptic'
|
|
5
|
-
import { assert } from 'console'
|
|
6
5
|
|
|
7
6
|
const ec = new EC('secp256k1')
|
|
8
7
|
|
|
@@ -30,16 +29,20 @@ export default class AlephiumApp {
|
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
// TODO: make address display optional
|
|
33
|
-
async getAccount(startPath: string, targetGroup?: number): Promise<Account> {
|
|
34
|
-
|
|
32
|
+
async getAccount(startPath: string, targetGroup?: number): Promise<readonly [Account, number]> {
|
|
33
|
+
if ((targetGroup ?? 0) >= GROUP_NUM) {
|
|
34
|
+
throw Error(`Invalid targetGroup: ${targetGroup}`)
|
|
35
|
+
}
|
|
36
|
+
|
|
35
37
|
const p1 = targetGroup === undefined ? 0x00 : GROUP_NUM
|
|
36
38
|
const p2 = targetGroup === undefined ? 0x00 : targetGroup
|
|
37
39
|
const response = await this.transport.send(CLA, INS.GET_PUBLIC_KEY, p1, p2, serde.serializePath(startPath))
|
|
38
40
|
const publicKey = ec.keyFromPublic(response.slice(0, 65)).getPublic(true, 'hex')
|
|
39
41
|
const address = addressFromPublicKey(publicKey)
|
|
40
42
|
const group = groupOfAddress(address)
|
|
43
|
+
const hdIndex = response.slice(65, 69).readUInt32BE(0)
|
|
41
44
|
|
|
42
|
-
return { publicKey: publicKey, address: address, group: group, keyType: 'default' }
|
|
45
|
+
return [{ publicKey: publicKey, address: address, group: group, keyType: 'default' }, hdIndex] as const
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
async signHash(path: string, hash: Buffer): Promise<string> {
|
package/test/release.test.ts
CHANGED
|
@@ -15,7 +15,7 @@ describe.skip('Integration', () => {
|
|
|
15
15
|
listen((log) => console.log(log))
|
|
16
16
|
const app = new AlephiumApp(transport)
|
|
17
17
|
|
|
18
|
-
const account = await app.getAccount(path)
|
|
18
|
+
const [account] = await app.getAccount(path)
|
|
19
19
|
console.log(`${JSON.stringify(account)}`)
|
|
20
20
|
|
|
21
21
|
const hash = Buffer.from(blake.blake2b(Buffer.from([0, 1, 2, 3, 4]), undefined, 32))
|
package/test/speculos.test.ts
CHANGED
|
@@ -24,10 +24,12 @@ function getRandomInt(min, max) {
|
|
|
24
24
|
|
|
25
25
|
describe('sdk', () => {
|
|
26
26
|
const apduPort = 9999
|
|
27
|
+
let pathIndex: number
|
|
27
28
|
let path: string
|
|
28
29
|
|
|
29
30
|
beforeEach(() => {
|
|
30
|
-
|
|
31
|
+
pathIndex = getRandomInt(0, 1000000)
|
|
32
|
+
path = `m/44'/1234'/0'/0/` + pathIndex
|
|
31
33
|
})
|
|
32
34
|
|
|
33
35
|
it('should get version', async () => {
|
|
@@ -41,7 +43,8 @@ describe('sdk', () => {
|
|
|
41
43
|
it('should get public key', async () => {
|
|
42
44
|
const transport = await SpeculosTransport.open({ apduPort })
|
|
43
45
|
const app = new AlephiumApp(transport)
|
|
44
|
-
const account = await app.getAccount(path)
|
|
46
|
+
const [account, hdIndex] = await app.getAccount(path)
|
|
47
|
+
expect(hdIndex).toBe(pathIndex)
|
|
45
48
|
console.log(account)
|
|
46
49
|
await transport.close()
|
|
47
50
|
})
|
|
@@ -50,7 +53,8 @@ describe('sdk', () => {
|
|
|
50
53
|
const transport = await SpeculosTransport.open({ apduPort })
|
|
51
54
|
const app = new AlephiumApp(transport)
|
|
52
55
|
Array(GROUP_NUM).forEach(async (_, group) => {
|
|
53
|
-
const account = await app.getAccount(path, group)
|
|
56
|
+
const [account, hdIndex] = await app.getAccount(path, group)
|
|
57
|
+
expect(hdIndex >= pathIndex).toBe(true)
|
|
54
58
|
expect(groupOfAddress(account.address)).toBe(group)
|
|
55
59
|
})
|
|
56
60
|
await transport.close()
|
|
@@ -60,7 +64,7 @@ describe('sdk', () => {
|
|
|
60
64
|
const transport = await SpeculosTransport.open({ apduPort })
|
|
61
65
|
const app = new AlephiumApp(transport)
|
|
62
66
|
|
|
63
|
-
const account = await app.getAccount(path)
|
|
67
|
+
const [account] = await app.getAccount(path)
|
|
64
68
|
console.log(account)
|
|
65
69
|
|
|
66
70
|
const hash = Buffer.from(blake.blake2b(Buffer.from([0, 1, 2, 3, 4]), undefined, 32))
|