Skip to content

Commit

Permalink
Merge pull request #160 from KeystoneHQ/feat/ada-integration-v2
Browse files Browse the repository at this point in the history
chore: update Cardano registry with new sign data request
  • Loading branch information
Charon-Fan authored Jun 26, 2024
2 parents 9635a98 + c52b74d commit f963a1e
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @ts-nocheck

import { CardanoCertKey, CardanoSignDataRequest } from "../src";
import { CryptoKeypath, PathComponent } from "../src";
import * as uuid from "uuid";

describe("cardano-sign-data-request", () => {
it("test should generate cardano-sign-data-request", () => {
const cardanoSignDataRequest =
CardanoSignDataRequest.constructCardanoSignDataRequest(
"e3bff0cb003cf867acfd117fb514dfaf7a8dd5dddf6e68cc71f553de5046ae2b",
"m/44'/148'/0'",
"1250B6BC",
"9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
"cardano-wallet"
);

const cborHex = cardanoSignDataRequest.toCBOR().toString("hex");
const ur = cardanoSignDataRequest.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:cardano-sign-data-request/oxadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohdcxvlrswtsbaefnyaiopszcbylbrebburpeknlgtluturjtissfjsykguuegdfgpldnaxtaaddyoeadlncsdwykcsmwykaeykaocybggdrprfaajtiahsjpiehsjtjldpkthsjzjzihjyqzmtpegy"
);

const cardanoSignDataRequestDecoded = CardanoSignDataRequest.fromCBOR(
Buffer.from(cborHex, "hex")
);
expect(cardanoSignDataRequestDecoded.getSignData().toString("hex")).toBe(
"e3bff0cb003cf867acfd117fb514dfaf7a8dd5dddf6e68cc71f553de5046ae2b"
);
});
});
2 changes: 1 addition & 1 deletion packages/ur-registry-cardano/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keystonehq/bc-ur-registry-cardano",
"version": "0.3.1",
"version": "0.3.2",
"description": "bc-ur-registry extension for Cardano",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
123 changes: 123 additions & 0 deletions packages/ur-registry-cardano/src/CardanoSignDataRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {
CryptoKeypath,
extend,
DataItem,
PathComponent,
RegistryItem,
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";
import * as uuid from "uuid";

const { decodeToDataItem, RegistryTypes } = extend;

enum Keys {
requestId = 1,
signData,
derivationPath,
origin,
signType,
}

type signRequestProps = {
requestId?: Buffer;
signData: Buffer;
derivationPath: CryptoKeypath;
origin?: string;
};

export class CardanoSignDataRequest extends RegistryItem {
private requestId?: Buffer;
private signData: Buffer;
private derivationPath: CryptoKeypath;
private origin?: string;

getRegistryType = () => ExtendedRegistryTypes.CARDANO_SIGN_DATA_REQUEST;

constructor(args: signRequestProps) {
super();
this.requestId = args.requestId;
this.signData = args.signData;
this.derivationPath = args.derivationPath;
this.origin = args.origin;
}

public getRequestId = () => this.requestId;
public getSignData = () => this.signData;
public getDerivationPath = () => this.derivationPath.getPath();
public getOrigin = () => this.origin;

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

if (this.origin) {
map[Keys.origin] = this.origin;
}

map[Keys.signData] = this.signData;

const keyPath = this.derivationPath.toDataItem();
keyPath.setTag(this.derivationPath.getRegistryType().getTag());
map[Keys.derivationPath] = keyPath;

return new DataItem(map);
};

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const signData = map[Keys.signData];
const derivationPath = CryptoKeypath.fromDataItem(map[Keys.derivationPath]);
const requestId = map[Keys.requestId]
? map[Keys.requestId].getData()
: undefined;
const origin = map[Keys.origin] ? map[Keys.origin] : undefined;

return new CardanoSignDataRequest({
requestId,
signData,
derivationPath,
origin,
});
};

public static fromCBOR = (_cborPayload: Buffer) => {
const dataItem = decodeToDataItem(_cborPayload);
return CardanoSignDataRequest.fromDataItem(dataItem);
};

public static constructCardanoSignDataRequest(
signData: string,
hdPath: string,
xfp: string,
uuidString?: string,
origin?: string
) {
const paths = hdPath.replace(/[m|M]\//, "").split("/");
const hdpathObject = new CryptoKeypath(
paths.map((path) => {
const index = parseInt(path.replace("'", ""));
let isHardened = false;
if (path.endsWith("'")) {
isHardened = true;
}
return new PathComponent({ index, hardened: isHardened });
}),
Buffer.from(xfp, "hex")
);

return new CardanoSignDataRequest({
requestId: uuidString
? Buffer.from(uuid.parse(uuidString) as Uint8Array)
: undefined,
signData: Buffer.from(signData, "hex"),
derivationPath: hdpathObject,
origin: origin || undefined,
});
}
}
63 changes: 63 additions & 0 deletions packages/ur-registry-cardano/src/CardanoSignDataSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
extend,
DataItem,
RegistryItem,
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";

const { RegistryTypes, decodeToDataItem } = extend;

enum Keys {
requestId = 1,
signature,
publicKey,
}

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

getRegistryType = () => ExtendedRegistryTypes.CARDANO_SIGNATURE;

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

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

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

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

return new CardanoSignDataSignature(signature, publicKey, requestId);
};

public static fromCBOR = (_cborPayload: Buffer) => {
const dataItem = decodeToDataItem(_cborPayload);
return CardanoSignDataSignature.fromDataItem(dataItem);
};
}
8 changes: 8 additions & 0 deletions packages/ur-registry-cardano/src/RegistryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ export const ExtendedRegistryTypes = {
CARDANO_SIGN_REQUEST: new RegistryType("cardano-sign-request", 2202),
CARDANO_SIGNATURE: new RegistryType("cardano-signature", 2203),
CARDANO_CERT_KEY: new RegistryType("cardano-cert-key", 2204),
CARDANO_SIGN_DATA_REQUEST: new RegistryType(
"cardano-sign-data-request",
2205
),
CARDANO_SIGN_DATA_SIGNATURE: new RegistryType(
"cardano-sign-data-signature",
2206
),
};
2 changes: 2 additions & 0 deletions packages/ur-registry-cardano/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ patchTags(

export { CardanoSignRequest } from "./CardanoSignRequest";
export { CardanoSignature } from "./CardanoSignature";
export { CardanoSignDataRequest } from "./CardanoSignDataRequest";
export { CardanoSignDataSignature } from "./CardanoSignDataSignature";
export { CardanoUtxoData } from "./CardanoUtxo";
export { CardanoCertKeyData } from "./CardanoCertKey";
export { CardanoCertKey } from "./CardanoCertKey";

0 comments on commit f963a1e

Please sign in to comment.