Skip to content

Commit

Permalink
Merge pull request #162 from KeystoneHQ/feat/ada-integration-v3
Browse files Browse the repository at this point in the history
chore: update Cardano registry with new delegation type
  • Loading branch information
Charon-Fan authored Jul 8, 2024
2 parents 147c230 + bc45dc0 commit 54865e8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// @ts-nocheck

import { CardanoCatalystRequest } from "../src";
import * as uuid from "uuid";

describe("cardano-catalyst-request", () => {
it("test should generate cardano-catalyst-request", () => {
const cardanoCatalystVotingRequest = {
requestId: "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
path: "m/1852'/1815'/0'/2/0",
delegations: [
{
xfp: "52744703",
hdPath: "m/1694'/1815'/0'/0'/0'",
weight: 1,
},
{
xfp: "52744703",
hdPath: "m/1694'/1815'/1'/0'/0'",
weight: 3,
},
],
stakePub:
"ad4b948699193634a39dd56f779a2951a24779ad52aa7916f6912b8ec4702cee",
paymentAddress:
"00588e8e1d18cba576a4d35758069fe94e53f638b6faf7c07b8abd2bc5c5cdee47b60edc7772855324c85033c638364214cbfc6627889f81c4",
nonce: 5479467,
voting_purpose: 0,
xfp: "52744703",
origin: "cardano-wallet",
};

const request = CardanoCatalystRequest.constructCardanoCatalystRequest(
cardanoCatalystVotingRequest.delegations,
cardanoCatalystVotingRequest.stakePub,
cardanoCatalystVotingRequest.paymentAddress,
cardanoCatalystVotingRequest.nonce,
cardanoCatalystVotingRequest.voting_purpose,
cardanoCatalystVotingRequest.path,
cardanoCatalystVotingRequest.xfp,
cardanoCatalystVotingRequest.requestId,
cardanoCatalystVotingRequest.origin
);

const ur = request.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:cardano-catalyst-voting-registration/pdadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaolftaayoyoeadtaaddyoeadlecfamnnykcfatchykaeykaeykaeykaocygmjyflaxaoadtaayoyoeadtaaddyoeadlecfamnnykcfatchykadykaeykaeykaocygmjyflaxaoaxaxhdcxpmgrmwlnnlcfeneeotnttljlktnydtgyoeflkkpmgmpkkkcmynmednmnssjodwwyaahdesaehdmnmncacssbonkooxtehghdamnewlglguynetrpzsylrtkglerydnsksksnwyflrpbauoktjplpgudkspgdeoswetenfwbbsbztiydilonelyssahcyaegunsdnamaeattaaddyoeadlecfatfnykcfatchykaeykaowkaewkaocygmjyflaxayjtiahsjpiehsjtjldpkthsjzjzihjyrtmhkoee"
);
});
});
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.3",
"version": "0.3.4",
"description": "bc-ur-registry extension for Cardano",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
39 changes: 13 additions & 26 deletions packages/ur-registry-cardano/src/CardanoCatalystRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";
import { CardanoDelegation } from "./CardanoDelegation";
import * as uuid from "uuid";

const { decodeToDataItem, RegistryTypes } = extend;
Expand All @@ -32,14 +33,9 @@ interface CardanoCatalystRawDelegationProps {
export type CardanoCatalystRawDelegationsProps =
CardanoCatalystRawDelegationProps[];

interface CardanoCatalystDelegationProps {
hdPath: CryptoKeypath;
weight: number;
}

interface CardanoCatalystRequestProps {
requestId?: Buffer;
delegations: CardanoCatalystDelegationProps[];
delegations: CardanoDelegation[];
stakePub: Buffer;
paymentAddress: Buffer;
nonce: number;
Expand All @@ -48,11 +44,6 @@ interface CardanoCatalystRequestProps {
origin?: string;
}

type CardanoCatalystDelegation = {
hdPath: CryptoKeypath;
weight: number;
};

const genCryptoKeypath = (path: string, xfp: string) => {
const paths = path.replace(/[m|M]\//, "").split("/");
return new CryptoKeypath(
Expand All @@ -70,7 +61,7 @@ const genCryptoKeypath = (path: string, xfp: string) => {

export class CardanoCatalystRequest extends RegistryItem {
private requestId?: Buffer;
private delegations: CardanoCatalystDelegation[];
private delegations: CardanoDelegation[];
private stakePub: Buffer;
private paymentAddress: Buffer;
private nonce: number;
Expand Down Expand Up @@ -117,8 +108,8 @@ export class CardanoCatalystRequest extends RegistryItem {
map[Keys.voting_purpose] = this.voting_purpose;

map[Keys.delegations] = this.delegations.map((delegation) => {
const res = delegation.hdPath.toDataItem();
res.setTag(delegation.hdPath.getRegistryType().getTag());
const res = delegation.toDataItem();
res.setTag(delegation.getRegistryType().getTag());
return res;
});

Expand All @@ -135,14 +126,8 @@ export class CardanoCatalystRequest extends RegistryItem {

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const delegations: CardanoCatalystDelegation[] = map[Keys.delegations].map(
(delegation: DataItem) => {
const hdPath = CryptoKeypath.fromDataItem(delegation);
return {
hdPath,
weight: 0,
};
}
const delegations: CardanoDelegation[] = map[Keys.delegations].map(
(delegation: DataItem) => CardanoDelegation.fromDataItem(delegation)
);
const stakePub = map[Keys.stakePub];
const paymentAddress = map[Keys.paymentAddress];
Expand Down Expand Up @@ -185,10 +170,12 @@ export class CardanoCatalystRequest extends RegistryItem {
const requestId = uuidString
? Buffer.from(uuid.parse(uuidString) as Uint8Array)
: undefined;
const cardanoDelegations = delegations.map((delegation) => ({
hdPath: genCryptoKeypath(delegation.hdPath, delegation.xfp),
weight: delegation.weight,
}));
const cardanoDelegations = delegations.map((delegation) =>
CardanoDelegation.constructCardanoDelegation({
hdPath: genCryptoKeypath(delegation.hdPath, delegation.xfp),
weight: delegation.weight,
})
);

return new CardanoCatalystRequest({
requestId,
Expand Down
67 changes: 67 additions & 0 deletions packages/ur-registry-cardano/src/CardanoDelegation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
CryptoKeypath,
extend,
DataItem,
RegistryItem,
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";

const { decodeToDataItem } = extend;

export interface CardanoCatalystDelegationProps {
hdPath: CryptoKeypath;
weight: number;
}

enum Keys {
hdPath = 1,
weight,
}

export class CardanoDelegation extends RegistryItem {
private hdPath: CryptoKeypath;
private weight: number;

getRegistryType = () => ExtendedRegistryTypes.CARDANO_DELEGATION;

constructor(args: CardanoCatalystDelegationProps) {
super();
this.hdPath = args.hdPath;
this.weight = args.weight;
}

public getHdPath = () => this.hdPath;
public getWeight = () => this.weight;

public toDataItem = () => {
const map: DataItemMap = {};

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

map[Keys.weight] = this.getWeight();

return new DataItem(map);
};

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const hdPath = CryptoKeypath.fromDataItem(map[Keys.hdPath]);
const weight = map[Keys.weight];
return new CardanoDelegation({ hdPath, weight });
};

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

public static constructCardanoDelegation({
hdPath,
weight,
}: CardanoCatalystDelegationProps) {
return new CardanoDelegation({ hdPath, weight });
}
}
1 change: 1 addition & 0 deletions packages/ur-registry-cardano/src/RegistryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export const ExtendedRegistryTypes = {
"cardano-catalyst-voting-registration-signature",
2208
),
CARDANO_DELEGATION: new RegistryType("cardano-delegation", 2209),
};

0 comments on commit 54865e8

Please sign in to comment.