Skip to content

Commit fa2a179

Browse files
authored
Merge pull request #206 from KeystoneHQ/update-core-wallet-sdk
feat: update core wallet new path
2 parents ce4be4b + 94eac2a commit fa2a179

File tree

5 files changed

+141
-40
lines changed

5 files changed

+141
-40
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ describe("avalanche-sign-request", () => {
88
"00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000",
99
"hex"
1010
);
11-
const mfp = "1250B6BC";
12-
const xpub =
13-
"xpub661MyMwAqRbcFFDMuFiGQmA1EqWxxgDLdtNvxxiucf9qkfoVrvwgnYyshxWoewWtkZ1aLhKoVDrpeDvn1YRqxX2szhGKi3UiSEv1hYRMF8q";
14-
const walletIndex = 0;
11+
const derivationPath = "m/44'/133'/0'/0/0";
12+
const utxos = [];
13+
1514

1615
const avalancheSignRequest = AvalancheSignRequest.constructAvalancheRequest(
1716
avalancheData,
18-
mfp,
19-
xpub,
20-
walletIndex
17+
derivationPath,
18+
utxos
2119
);
2220

2321
const request = AvalancheSignRequest.fromDataItem(

packages/ur-registry-avalanche/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@keystonehq/bc-ur-registry-avalanche",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "bc-ur-registry extension for Avalanche",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",

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

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,48 @@ import {
33
RegistryItem,
44
extend,
55
DataItemMap,
6+
CryptoKeypath,
67
} from "@keystonehq/bc-ur-registry";
78
import { ExtendedRegistryTypes } from "./RegistryType";
89
import * as uuid from "uuid";
10+
import { AvalancheUtxo, AvalancheUtxoData } from "./AvalancheUtxo";
911

1012
const { RegistryTypes } = extend;
1113

1214
type signRequestProps = {
1315
requestId?: Buffer;
1416
data: Buffer;
15-
mfp: Buffer;
16-
xpub: string;
17-
walletIndex: number;
17+
derivationPath: CryptoKeypath;
18+
utxos: AvalancheUtxo[];
1819
};
1920

2021
enum Keys {
2122
requestId = 1,
22-
signData = 2,
23-
mfp = 3,
24-
xpub = 6,
25-
walletIndex = 7,
23+
signData,
24+
derivationPath,
25+
utxos,
2626
}
27-
2827
export class AvalancheSignRequest extends RegistryItem {
2928
private requestId?: Buffer;
3029
private data: Buffer;
31-
private mfp: Buffer;
32-
private xpub: string;
33-
private walletIndex: number;
30+
private derivationPath: CryptoKeypath;
31+
private utxos: AvalancheUtxo[];
32+
3433

3534
getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_SIGN_REQUEST;
3635

3736
constructor(args: signRequestProps) {
3837
super();
3938
this.requestId = args.requestId;
4039
this.data = args.data;
41-
this.mfp = args.mfp;
42-
this.xpub = args.xpub;
43-
this.walletIndex = args.walletIndex;
40+
this.derivationPath = args.derivationPath;
41+
this.utxos = args.utxos;
4442
}
4543

4644
public getRequestId = () => this.requestId;
4745
public getSignData = () => this.data;
46+
public getUtxos = () => this.utxos;
47+
public getDerivationPath = () => this.derivationPath;
4848

4949
public toDataItem = () => {
5050
const map: DataItemMap = {};
@@ -56,40 +56,40 @@ export class AvalancheSignRequest extends RegistryItem {
5656
}
5757

5858
map[Keys.signData] = Buffer.from(this.data);
59-
map[Keys.mfp] = this.mfp.readUInt32BE(0);
60-
map[Keys.xpub] = this.xpub;
61-
map[Keys.walletIndex] = Number(this.walletIndex);
59+
map[Keys.derivationPath] = this.derivationPath;
60+
map[Keys.utxos] = this.utxos.map((utxo) => {
61+
const res = utxo.toDataItem();
62+
res.setTag(utxo.getRegistryType().getTag());
63+
return res;
64+
});
6265

6366
return new DataItem(map);
6467
};
6568

6669
public static fromDataItem = (dataItem: DataItem) => {
6770
const map = dataItem.getData();
68-
const masterFingerprint = Buffer.alloc(4);
69-
const _masterFingerprint = map[Keys.mfp];
70-
masterFingerprint.writeUInt32BE(_masterFingerprint, 0);
7171
const requestId = map[Keys.requestId]
7272
? map[Keys.requestId].getData()
7373
: undefined;
7474
const data = map[Keys.signData];
75-
const xpub = map[Keys.xpub];
76-
const walletIndex = map[Keys.signData];
75+
const derivationPath = map[Keys.signData];
76+
const utxos: AvalancheUtxo[] = map[Keys.utxos].map((utxo: DataItem) =>
77+
AvalancheUtxo.fromDataItem(utxo)
78+
);
7779

7880
return new AvalancheSignRequest({
7981
requestId,
8082
data,
81-
xpub,
82-
walletIndex,
83-
mfp: masterFingerprint,
83+
derivationPath,
84+
utxos,
8485
});
8586
};
8687

8788
public static constructAvalancheRequest(
8889
data: Buffer,
89-
mfp: string,
90-
xpub: string,
91-
walletIndex: number,
92-
requestId?: string | Buffer
90+
derivationPath: CryptoKeypath,
91+
utxos: AvalancheUtxoData[],
92+
requestId?: string | Buffer,
9393
) {
9494
let _requestId;
9595
if (typeof requestId === "string") {
@@ -99,13 +99,15 @@ export class AvalancheSignRequest extends RegistryItem {
9999
} else {
100100
_requestId = Buffer.from(uuid.parse(uuid.v4()) as Uint8Array);
101101
}
102+
const avalancheUtxos = utxos.map((utxo) =>
103+
AvalancheUtxo.constructAvalancheUtxo(utxo)
104+
);
102105

103106
return new AvalancheSignRequest({
104107
data,
105108
requestId: _requestId,
106-
mfp: Buffer.from(mfp, "hex"),
107-
xpub,
108-
walletIndex,
109+
derivationPath,
110+
utxos: avalancheUtxos,
109111
});
110112
}
111113
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import {
2+
CryptoKeypath,
3+
extend,
4+
DataItem,
5+
PathComponent,
6+
RegistryItem,
7+
DataItemMap,
8+
} from "@keystonehq/bc-ur-registry";
9+
import { ExtendedRegistryTypes } from "./RegistryType";
10+
11+
const { decodeToDataItem } = extend;
12+
13+
enum Keys {
14+
txid = 1,
15+
vout,
16+
path,
17+
}
18+
19+
export interface AvalancheUtxoProps {
20+
txid: Buffer;
21+
vout: number;
22+
path: CryptoKeypath;
23+
}
24+
25+
export interface AvalancheUtxoData {
26+
txid: string;
27+
vout: number;
28+
path: string;
29+
}
30+
31+
export class AvalancheUtxo extends RegistryItem {
32+
private txid: Buffer;
33+
private vout: number;
34+
private path: CryptoKeypath;
35+
36+
getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_UTXO;
37+
38+
constructor(args: AvalancheUtxoProps) {
39+
super();
40+
this.txid = args.txid;
41+
this.vout = args.vout;
42+
this.path = args.path;
43+
}
44+
45+
public getTxid = () => this.txid;
46+
public getVout = () => this.vout;
47+
public getKeyPath = () => this.path.getPath();
48+
49+
public toDataItem = () => {
50+
const map: DataItemMap = {};
51+
map[Keys.txid] = this.txid;
52+
map[Keys.vout] = this.vout;
53+
54+
const keyPath = this.path.toDataItem();
55+
keyPath.setTag(this.path.getRegistryType().getTag());
56+
map[Keys.path] = keyPath;
57+
58+
return new DataItem(map);
59+
};
60+
61+
public static fromDataItem = (dataItem: DataItem) => {
62+
const map = dataItem.getData();
63+
const txid = map[Keys.txid];
64+
const vout = map[Keys.vout];
65+
const path = CryptoKeypath.fromDataItem(map[Keys.path]);
66+
67+
return new AvalancheUtxo({
68+
txid,
69+
vout,
70+
path,
71+
});
72+
};
73+
74+
public static fromCBOR = (_cborPayload: Buffer) => {
75+
const dataItem = decodeToDataItem(_cborPayload);
76+
return AvalancheUtxo.fromDataItem(dataItem);
77+
};
78+
79+
public static constructAvalancheUtxo({
80+
txid,
81+
vout,
82+
path,
83+
}: AvalancheUtxoData) {
84+
const paths = path.replace(/[m|M]\//, "").split("/");
85+
const hdPathObject = new CryptoKeypath(
86+
paths.map((path) => {
87+
const index = parseInt(path.replace("'", ""));
88+
const isHardened = path.endsWith("'");
89+
return new PathComponent({ index, hardened: isHardened });
90+
}),
91+
);
92+
93+
return new AvalancheUtxo({
94+
txid: Buffer.from(txid, "hex"),
95+
vout,
96+
path: hdPathObject,
97+
});
98+
}
99+
}
100+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { RegistryType } from "@keystonehq/bc-ur-registry";
33
export const ExtendedRegistryTypes = {
44
AVALANCHE_SIGN_REQUEST: new RegistryType("avax-sign-request", 8301),
55
AVALANCHE_SIGNATURE: new RegistryType("avax-signature", 8302),
6+
AVALANCHE_UTXO: new RegistryType("avax-utxo", 8303),
67
};

0 commit comments

Comments
 (0)