Skip to content

Commit a094dcd

Browse files
authored
Merge pull request #212 from KeystoneHQ/update-core-wallet-sdk
feat: update derivationPaths to array
2 parents 438aad9 + bbbd460 commit a094dcd

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

packages/ur-registry-avalanche/__tests__/AvalancheSignRequest.test.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { UREncoder } from "@ngraveio/bc-ur";
66
describe("avalanche-sign-request", () => {
77
it("test should generate avalanche-sign-reqeust", () => {
88
const avalancheData = Buffer.from(
9-
"0000000000220000000100000000000000000000000000000000000000000000000000000000000000000000000221e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000700000000006c2b4400000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c521e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000176bbb400000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c500000001cd53226620f4be2b6f6e43b1470f4f715b3bc7f40c4530cd5237a1c4156b537f0000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000001e2fb33000000010000000000000000",
9+
"0000000000220000000100000000000000000000000000000000000000000000000000000000000000000000000221e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000031cb3a00000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c521e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000070000000005dabac900000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c500000008120d0def706b8b759935b8ea9727662aafa5381e598a074daddc82492549cd760000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000046239d0000000100000000174d1a9b28e1d4d518f1999d4f8ac422b8a3a4755001f5965e8d05c93359feb10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000131021c0000000100000000174d1a9b28e1d4d518f1999d4f8ac422b8a3a4755001f5965e8d05c93359feb10000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000176b6a5000000010000000065a3b1de10620296debfa01aa953e45ddd19d2c39e3dacb9a92e6a85ca8a309c0000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000098968000000001000000006f6522ae52b0231076dc63ff95f7ea22e2fd80943e37235302c7ee32afce4cd60000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000500000000009896800000000100000000845649c3d1a630d8b466f7b727f6577cb4a17864699e6de756e484b81d84cd2a0000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000500000000006c2b440000000100000000d1e6480c1825197e2ec293a60bacdc7f60bfba2f3cc5383855180b45d595a7030000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000500000000007a12000000000100000000f59b9a175ebe4ccd8de5dcfc6a26870414f30c696cce19283f30145624b445b70000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000107a493000000010000000000000000",
1010
"hex"
1111
);
12-
const derivationPath = "m/44'/9000'/0'/0/1";
12+
const derivationPath = ["m/44'/9000'/0'/0/0","m/44'/9000'/0'/0/1"];
1313
const utxos = [];
1414
const xfp = '2d0bdabc'
1515

@@ -29,28 +29,33 @@ describe("avalanche-sign-request", () => {
2929
avalancheSignRequest.toDataItem()
3030
);
3131

32-
const path = request.getDerivationPath();
33-
expect(path).toBeDefined();
34-
expect(path.getPath()).toBe("44'/9000'/0'/0/1");
32+
const paths = request.getDerivationPaths();
33+
expect(paths).toBeDefined();
34+
expect(paths.length).toBe(2);
3535

36-
const recoveredXfp = request.getDerivationPath().getSourceFingerprint().toString('hex');
36+
expect(paths[0].getPath()).toBe("44'/9000'/0'/0/0");
37+
expect(paths[1].getPath()).toBe("44'/9000'/0'/0/1");
38+
39+
const recoveredXfp = paths[0].getSourceFingerprint().toString('hex');
3740
expect(recoveredXfp).toBe(xfp)
3841

3942
expect(request.getSignData().toString("hex")).toBe(
40-
"0000000000220000000100000000000000000000000000000000000000000000000000000000000000000000000221e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000700000000006c2b4400000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c521e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000176bbb400000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c500000001cd53226620f4be2b6f6e43b1470f4f715b3bc7f40c4530cd5237a1c4156b537f0000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000001e2fb33000000010000000000000000"
43+
"0000000000220000000100000000000000000000000000000000000000000000000000000000000000000000000221e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000031cb3a00000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c521e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000070000000005dabac900000000000000000000000100000001b5e66be5c7093d1114d74940333c0c45f81092c500000008120d0def706b8b759935b8ea9727662aafa5381e598a074daddc82492549cd760000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000046239d0000000100000000174d1a9b28e1d4d518f1999d4f8ac422b8a3a4755001f5965e8d05c93359feb10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000131021c0000000100000000174d1a9b28e1d4d518f1999d4f8ac422b8a3a4755001f5965e8d05c93359feb10000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000176b6a5000000010000000065a3b1de10620296debfa01aa953e45ddd19d2c39e3dacb9a92e6a85ca8a309c0000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000098968000000001000000006f6522ae52b0231076dc63ff95f7ea22e2fd80943e37235302c7ee32afce4cd60000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000500000000009896800000000100000000845649c3d1a630d8b466f7b727f6577cb4a17864699e6de756e484b81d84cd2a0000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000500000000006c2b440000000100000000d1e6480c1825197e2ec293a60bacdc7f60bfba2f3cc5383855180b45d595a7030000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff0000000500000000007a12000000000100000000f59b9a175ebe4ccd8de5dcfc6a26870414f30c696cce19283f30145624b445b70000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000005000000000107a493000000010000000000000000"
4144
);
4245
});
4346

4447
it("test parse signature", () => {
4548
const cbor = Buffer.from(
46-
"a201501a79a2072e114014837e792e003384c10258418d1b6e955f1b2a94aed0b2c29939d68bae89fce2d436cc814efc4731f4a43e8e78ae46e3fdd58e7e0f7c5fa7876239fe7ab0adb6a75c09a07132b741be62612000",
49+
"a20150d797b45aef4b483cb106506e288b2c770282584116ef664a04634a748ff13b6bd43bc2657b699f90c86b0fa946e90514089f75b24e7f1fc3183733c9c4a90972d7ce756c4464acfd5e4ecb43e6505abca2125b9c005841ddc5acd7adb746a519d85fff8cdc609e94bec04603c867acd926eef683ccb2a40fe1690cd3d9d931b0f2ff9eda6dbf4ffdec20606126bc37cee53ada832d19b700",
4750
"hex"
4851
);
4952

50-
const signature = AvalancheSignature.fromCBOR(cbor);
53+
const signatures = AvalancheSignature.fromCBOR(cbor);
54+
const sigBuffers = signatures.getSignatures();
55+
const finalHexArray = sigBuffers.map(sig => sig.toString('hex'));
56+
console.log('signature 1:', finalHexArray[0]);
57+
console.log('signature 2:', finalHexArray[1]);
5158

52-
expect(signature.getSignature().toString("hex")).toBe(
53-
"8d1b6e955f1b2a94aed0b2c29939d68bae89fce2d436cc814efc4731f4a43e8e78ae46e3fdd58e7e0f7c5fa7876239fe7ab0adb6a75c09a07132b741be62612000"
54-
);
59+
expect(finalHexArray.length).toBe(2);
5560
});
5661
});

packages/ur-registry-avalanche/src/AvalancheSignRequest.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ const { RegistryTypes } = extend;
1515
type signRequestProps = {
1616
requestId?: Buffer;
1717
data: Buffer;
18-
derivationPath: CryptoKeypath;
18+
derivationPaths: CryptoKeypath[];
1919
utxos: AvalancheUtxo[];
2020
};
2121

2222
enum Keys {
2323
requestId = 1,
2424
signData,
25-
derivationPath,
25+
derivationPaths,
2626
utxos,
2727
}
2828
export class AvalancheSignRequest extends RegistryItem {
2929
private requestId?: Buffer;
3030
private data: Buffer;
31-
private derivationPath: CryptoKeypath;
31+
private derivationPaths: CryptoKeypath[];
3232
private utxos: AvalancheUtxo[];
3333

3434

@@ -38,14 +38,14 @@ export class AvalancheSignRequest extends RegistryItem {
3838
super();
3939
this.requestId = args.requestId;
4040
this.data = args.data;
41-
this.derivationPath = args.derivationPath;
41+
this.derivationPaths = args.derivationPaths;
4242
this.utxos = args.utxos;
4343
}
4444

4545
public getRequestId = () => this.requestId;
4646
public getSignData = () => this.data;
4747
public getUtxos = () => this.utxos;
48-
public getDerivationPath = () => this.derivationPath;
48+
public getDerivationPaths = () => this.derivationPaths;
4949

5050
public toDataItem = () => {
5151
const map: DataItemMap = {};
@@ -58,9 +58,11 @@ export class AvalancheSignRequest extends RegistryItem {
5858

5959
map[Keys.signData] = Buffer.from(this.data);
6060

61-
const keyPath = this.derivationPath.toDataItem();
62-
keyPath.setTag(this.derivationPath.getRegistryType().getTag());
63-
map[Keys.derivationPath] = keyPath;
61+
map[Keys.derivationPaths] = this.derivationPaths.map((keypath) => {
62+
const item = keypath.toDataItem();
63+
item.setTag(keypath.getRegistryType().getTag());
64+
return item;
65+
});
6466

6567
map[Keys.utxos] = this.utxos.map((utxo) => {
6668
const res = utxo.toDataItem();
@@ -77,22 +79,24 @@ export class AvalancheSignRequest extends RegistryItem {
7779
? map[Keys.requestId].getData()
7880
: undefined;
7981
const data = map[Keys.signData];
80-
const derivationPath = CryptoKeypath.fromDataItem(map[Keys.derivationPath]);
82+
const derivationPaths: CryptoKeypath[] = map[Keys.derivationPaths].map(
83+
(item: DataItem) => CryptoKeypath.fromDataItem(item)
84+
);
8185
const utxos: AvalancheUtxo[] = map[Keys.utxos].map((utxo: DataItem) =>
8286
AvalancheUtxo.fromDataItem(utxo)
8387
);
8488

8589
return new AvalancheSignRequest({
8690
requestId,
8791
data,
88-
derivationPath,
92+
derivationPaths,
8993
utxos,
9094
});
9195
};
9296

9397
public static constructAvalancheRequest(
9498
data: Buffer,
95-
hdPath: string,
99+
hdPaths: string[],
96100
utxos: AvalancheUtxoData[],
97101
xfp: string,
98102
requestId?: string | Buffer
@@ -110,23 +114,22 @@ export class AvalancheSignRequest extends RegistryItem {
110114
AvalancheUtxo.constructAvalancheUtxo(utxo)
111115
);
112116

113-
const paths = hdPath.replace(/[m|M]\//, "").split("/");
114-
const hdpathObject = new CryptoKeypath(
115-
paths.map((path) => {
116-
const index = parseInt(path.replace("'", ""));
117-
let isHardened = false;
118-
if (path.endsWith("'")) {
119-
isHardened = true;
120-
}
121-
return new PathComponent({ index, hardened: isHardened });
122-
}),
123-
Buffer.from(xfp, "hex")
124-
);
117+
const derivationPaths = hdPaths.map(hdPath => {
118+
const paths = hdPath.replace(/[m|M]\//, "").split("/");
119+
return new CryptoKeypath(
120+
paths.map((path) => {
121+
const index = parseInt(path.replace(/[^0-9]/g, ""));
122+
const isHardened = path.endsWith("'") || path.toLowerCase().endsWith("h");
123+
return new PathComponent({ index, hardened: isHardened });
124+
}),
125+
Buffer.from(xfp, "hex")
126+
);
127+
});
125128

126129
return new AvalancheSignRequest({
127130
data,
128131
requestId: _requestId,
129-
derivationPath: hdpathObject,
132+
derivationPaths: derivationPaths,
130133
utxos: avalancheUtxos,
131134
});
132135
}

packages/ur-registry-avalanche/src/AvalancheSignature.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ const { RegistryTypes, decodeToDataItem } = extend;
1010

1111
enum Keys {
1212
requestId = 1,
13-
signature,
13+
signatures,
1414
}
1515

1616
export class AvalancheSignature extends RegistryItem {
1717
private requestId?: Buffer;
18-
private signature: Buffer;
18+
private signatures: Buffer[];
1919

2020
getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_SIGNATURE;
2121

22-
constructor(signature: Buffer, requestId?: Buffer) {
22+
constructor(signatures: Buffer[], requestId?: Buffer) {
2323
super();
24-
this.signature = signature;
24+
this.signatures = Array.isArray(signatures) ? signatures : [signatures];
2525
this.requestId = requestId;
2626
}
2727

2828
public getRequestId = () => this.requestId;
29-
public getSignature = () => this.signature;
29+
public getSignatures = () => this.signatures;
3030

3131
public toDataItem = () => {
3232
const map: DataItemMap = {};
@@ -36,19 +36,19 @@ export class AvalancheSignature extends RegistryItem {
3636
RegistryTypes.UUID.getTag()
3737
);
3838
}
39-
map[Keys.signature] = this.signature;
39+
map[Keys.signatures] = this.signatures;
4040
return new DataItem(map);
4141
};
4242

4343
public static fromDataItem = (dataItem: DataItem) => {
4444
const map = dataItem.getData();
45-
const signature = map[Keys.signature];
45+
const signatures = map[Keys.signatures];
4646

47-
return new AvalancheSignature(signature);
47+
return new AvalancheSignature(signatures);
4848
};
4949

5050
public static fromCBOR = (_cborPayload: Buffer) => {
5151
const dataItem = decodeToDataItem(_cborPayload);
5252
return AvalancheSignature.fromDataItem(dataItem);
5353
};
54-
}
54+
}

0 commit comments

Comments
 (0)