Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/avax #183

Merged
merged 8 commits into from
Jan 9, 2025
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
// @ts-nocheck

import { StellarSignRequest, SignType, AvalancheSignRequest } from "../src";
import { CryptoKeypath, PathComponent } from "../src";
import * as uuid from "uuid";
import { AvalancheSignRequest, AvalancheSignature } from "../src";

describe("avalanche-sign-request", () => {
it("test should generate avalanche-sign-reqeust", () => {
const avalancheData = Buffer.from(
"00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000",
"hex"
);
const mfp = "1250B6BC";
const xpub =
"xpub661MyMwAqRbcFFDMuFiGQmA1EqWxxgDLdtNvxxiucf9qkfoVrvwgnYyshxWoewWtkZ1aLhKoVDrpeDvn1YRqxX2szhGKi3UiSEv1hYRMF8q";
const walletIndex = 0;

const avalancheSignRequest =
AvalancheSignRequest.constructAvalancheRequest(avalancheData);
const avalancheSignRequest = AvalancheSignRequest.constructAvalancheRequest(
avalancheData,
mfp,
xpub,
walletIndex
);

const request = AvalancheSignRequest.fromDataItem(
avalancheSignRequest.toDataItem()
);

expect(request.getSignData().toString("hex")).toBe(
"00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000"
);
});

it("test parse signature", () => {
const cbor = Buffer.from(
"a201501a79a2072e114014837e792e003384c10258418d1b6e955f1b2a94aed0b2c29939d68bae89fce2d436cc814efc4731f4a43e8e78ae46e3fdd58e7e0f7c5fa7876239fe7ab0adb6a75c09a07132b741be62612000",
"hex"
);

const signature = AvalancheSignature.fromCBOR(cbor);

expect(avalancheSignRequest.toUR().cbor.toString("hex")).toBe(
"58de00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000"
expect(signature.getSignature().toString("hex")).toBe(
"8d1b6e955f1b2a94aed0b2c29939d68bae89fce2d436cc814efc4731f4a43e8e78ae46e3fdd58e7e0f7c5fa7876239fe7ab0adb6a75c09a07132b741be62612000"
);
});
});
2 changes: 1 addition & 1 deletion packages/ur-registry-avalanche/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keystonehq/bc-ur-registry-avalanche",
"version": "0.0.2",
"version": "0.0.4",
"description": "bc-ur-registry extension for Avalanche",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
73 changes: 70 additions & 3 deletions packages/ur-registry-avalanche/src/AvalancheSignRequest.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,104 @@
import { DataItem, RegistryItem } from "@keystonehq/bc-ur-registry";
import {
DataItem,
RegistryItem,
extend,
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";
import * as uuid from "uuid";

const { RegistryTypes } = extend;

type signRequestProps = {
requestId?: Buffer;
data: Buffer;
mfp: Buffer;
xpub: string;
walletIndex: number;
};

enum Keys {
requestId = 1,
signData = 2,
mfp = 3,
xpub = 6,
walletIndex = 7,
}

export class AvalancheSignRequest extends RegistryItem {
private requestId?: Buffer;
private data: Buffer;
private mfp: Buffer;
private xpub: string;
private walletIndex: number;

getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_SIGN_REQUEST;

constructor(args: signRequestProps) {
super();
this.requestId = args.requestId;
this.data = args.data;
this.mfp = args.mfp;
this.xpub = args.xpub;
this.walletIndex = args.walletIndex;
}

public getRequestId = () => this.requestId;
public getSignData = () => this.data;

public toDataItem = () => {
return new DataItem(this.data);
const map: DataItemMap = {};
if (this.requestId) {
map[Keys.requestId] = new DataItem(
this.requestId,
RegistryTypes.UUID.getTag()
);
}

map[Keys.signData] = Buffer.from(this.data);
map[Keys.mfp] = this.mfp.readUInt32BE(0);
map[Keys.xpub] = this.xpub;
map[Keys.walletIndex] = Number(this.walletIndex);

return new DataItem(map);
};

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const masterFingerprint = Buffer.alloc(4);
const _masterFingerprint = map[Keys.mfp];
masterFingerprint.writeUInt32BE(_masterFingerprint, 0);
const requestId = map[Keys.requestId]
? map[Keys.requestId].getData()
: undefined;
const data = map[Keys.signData];
const xpub = map[Keys.xpub];
const walletIndex = map[Keys.signData];

return new AvalancheSignRequest({
requestId,
data,
xpub,
walletIndex,
mfp: masterFingerprint,
});
};

public static constructAvalancheRequest(data: Buffer, uuidString?: string) {
public static constructAvalancheRequest(
data: Buffer,
mfp: string,
xpub: string,
walletIndex: number,
uuidString?: string
) {
return new AvalancheSignRequest({
data,
requestId: uuidString
? Buffer.from(uuid.parse(uuidString) as Uint8Array)
: undefined,
mfp: Buffer.from(mfp, "hex"),
xpub,
walletIndex,
});
}
}
54 changes: 54 additions & 0 deletions packages/ur-registry-avalanche/src/AvalancheSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
extend,
DataItem,
RegistryItem,
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";

const { RegistryTypes, decodeToDataItem } = extend;

enum Keys {
requestId = 1,
signature,
}

export class AvalancheSignature extends RegistryItem {
private requestId?: Buffer;
private signature: Buffer;

getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_SIGNATURE;

constructor(signature: Buffer, requestId?: Buffer) {
super();
this.signature = signature;
this.requestId = requestId;
}

public getRequestId = () => this.requestId;
public getSignature = () => this.signature;

public toDataItem = () => {
const map: DataItemMap = {};
if (this.requestId) {
map[Keys.requestId] = new DataItem(
this.requestId,
RegistryTypes.UUID.getTag()
);
}
map[Keys.signature] = this.signature;
return new DataItem(map);
};

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const signature = map[Keys.signature];

return new AvalancheSignature(signature);
};

public static fromCBOR = (_cborPayload: Buffer) => {
const dataItem = decodeToDataItem(_cborPayload);
return AvalancheSignature.fromDataItem(dataItem);
};
}
1 change: 1 addition & 0 deletions packages/ur-registry-avalanche/src/RegistryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { RegistryType } from "@keystonehq/bc-ur-registry";

export const ExtendedRegistryTypes = {
AVALANCHE_SIGN_REQUEST: new RegistryType("avax-sign-request", 8301),
AVALANCHE_SIGNATURE: new RegistryType("avax-signature", 8302),
};
1 change: 1 addition & 0 deletions packages/ur-registry-avalanche/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ patchTags(
);

export { AvalancheSignRequest } from "./AvalancheSignRequest";
export { AvalancheSignature } from "./AvalancheSignature";
Loading
Loading