Skip to content

Commit

Permalink
Merge pull request #178 from KeystoneHQ/feat/ada_sign_hash
Browse files Browse the repository at this point in the history
Feat/ada sign hash
  • Loading branch information
soralit authored Dec 6, 2024
2 parents 1585e86 + acba4ec commit fd8c811
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @ts-nocheck
import { CryptoKeypath, PathComponent } from "../src";
import * as uuid from "uuid";
import { CardanoSignTxHashRequest } from "../src";
describe("cardano-sign-tx-hash-request", () => {
it("test should generate cardano-sign-tx-hash-request", () => {
const txHash =
"52a1f5596f31358030f0d9d3a2db2b119b8f766386071684d26d0d37439c144e";

const signKeyPath1 = new CryptoKeypath(
[
new PathComponent({ index: 1852, hardened: true }),
new PathComponent({ index: 1815, hardened: true }),
new PathComponent({ index: 0, hardened: true }),
new PathComponent({ index: 0, hardened: false }),
new PathComponent({ index: 0, hardened: false }),
],
Buffer.from("1250b6bc", "hex")
);

const signKeyPath2 = new CryptoKeypath(
[
new PathComponent({ index: 1852, hardened: true }),
new PathComponent({ index: 1815, hardened: true }),
new PathComponent({ index: 0, hardened: true }),
new PathComponent({ index: 2, hardened: false }),
new PathComponent({ index: 0, hardened: false }),
],
Buffer.from("1250b6bc", "hex")
);

const requestId = uuid.stringify(
Buffer.from("52090a1c29394842a9adba0bc021a58b", "hex")
);
const addressList = [
"addr1qy8ac7qqy0vtulyl7wntmsxc6wex80gvcyjy33qffrhm7sh927ysx5sftuw0dlft05dz3c7revpf7jx0xnlcjz3g69mq4afdhv",
"addr1qyz85693g4fr8c55mfyxhae8j2u04pydxrgqr73vmwpx3azv4dgkyrgylj5yl2m0jlpdpeswyyzjs0vhwvnl6xg9f7ssrxkz90",
];
const cardanoSignTxHashRequest =
CardanoSignTxHashRequest.constructCardanoSignTxHashRequest(
txHash,
[signKeyPath1, signKeyPath2],
addressList,
requestId,
"eternl"
);

const cborHex = cardanoSignTxHashRequest.toCBOR().toString("hex");
expect(cborHex).toBe(
"a501d8255052090a1c29394842a9adba0bc021a58b027840353261316635353936663331333538303330663064396433613264623262313139623866373636333836303731363834643236643064333734333963313434650382d90130a2018a19073cf5190717f500f500f400f4021a1250b6bcd90130a2018a19073cf5190717f500f502f400f4021a1250b6bc0466657465726e6c058278676164647231717938616337717179307674756c796c37776e746d737863367765783830677663796a79333371666672686d37736839323779737835736674757730646c66743035647a3363377265767066376a7830786e6c636a7a336736396d713461666468767867616464723171797a383536393367346672386335356d667978686165386a3275303470796478726771723733766d77707833617a763464676b797267796c6a35796c326d306a6c70647065737779797a6a7330766877766e6c367867396637737372786b7a3930"
);
const ur = cardanoSignTxHashRequest.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:cardano-sign-tx-hash-request/onadtpdagdgmasbkcedtesfdfwptpmrdbdrtclonluaoksfzeceyhsehiyececeseniyeoeheoecetdyeodyiydyieesieeohseyieideyidehehesidetiyemeneneoetendyemeheneteeieeyeniedyieeoemeeeoesiaeheeeeihaxlftaaddyoeadlecfatfnykcfatchykaeykaewkaewkaocybggdrprftaaddyoeadlecfatfnykcfatchykaeykaowkaewkaocybggdrprfaaiyihjyihjpjtjzahlfksiohsieiejpehjskkethsiaemjsjskkdykojykpjzkkjzemktjtjyjnjkksiaenktihksetdyiokoiakkimkkeoeojsiyiyjpisjnemjkiseseyemkkjkksecjkiyjykpktdyiejziyjydyeciekneoiaemjpihkojoiyemimksdyksjtjziaimkneoioenesjnjseehsiyieiskoksiohsieiejpehjskkkneteceneseoioeeiyjpetiaececjniykkksishsihetimeykpdyeejokkieksjpiojsjpemeokojnktjokseohsknkoeeieiojekkjpiokkjzimeckkjzeyjndyimjzjoiejoihjkktkkkkknimjkdykoisktkojtjzenksioesiyemjkjkjpksjeknesdyolcxsges"
);
});
});
114 changes: 114 additions & 0 deletions packages/ur-registry-cardano/src/CardanoSignTxHashRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
extend,
DataItem,
RegistryItem,
DataItemMap,
CryptoKeypath,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";
import * as uuid from "uuid";

const { decodeToDataItem, RegistryTypes } = extend;

enum Keys {
requestId = 1,
txHash,
paths,
origin,
address_list,
}

type CardanoSignTxHashRequestProps = {
requestId?: Buffer;
txHash: string;
paths: CryptoKeypath[];
origin?: string;
addressList: string[];
};

export class CardanoSignTxHashRequest extends RegistryItem {
private requestId?: Buffer;
private txHash: string;
private paths: CryptoKeypath[];
private origin?: string;
private addressList: string[];
getRegistryType = () => ExtendedRegistryTypes.CARDANO_SIGN_TX_HASH_REQUEST;

constructor(args: CardanoSignTxHashRequestProps) {
super();
this.requestId = args.requestId;
this.txHash = args.txHash;
this.paths = args.paths;
this.origin = args.origin;
this.addressList = args.addressList;
}

public getRequestId = () => this.requestId;
public getTxHash = () => this.txHash;
public getPaths = () => this.paths;
public getOrigin = () => this.origin;
public getAddressList = () => this.addressList;
public toDataItem = () => {
const map: DataItemMap = {};
if (this.requestId) {
map[Keys.requestId] = new DataItem(
this.requestId,
RegistryTypes.UUID.getTag()
);
}

map[Keys.txHash] = this.txHash;
map[Keys.paths] = this.paths.map((path) => {
const res = path.toDataItem();
res.setTag(path.getRegistryType().getTag());
return res;
});

if (this.origin) {
map[Keys.origin] = this.origin;
}
map[Keys.address_list] = this.addressList;
return new DataItem(map);
};

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const txHash = map[Keys.txHash];
const paths = map[Keys.paths];
const requestId = map[Keys.requestId]
? map[Keys.requestId].getData()
: undefined;
const origin = map[Keys.origin] ? map[Keys.origin] : undefined;
const addressList = map[Keys.address_list] ? map[Keys.address_list] : [];
return new CardanoSignTxHashRequest({
requestId,
txHash,
paths,
origin,
addressList,
});
};

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

public static constructCardanoSignTxHashRequest(
txHash: string,
paths: CryptoKeypath[],
addressList: string[],
uuidString?: string,
origin?: string
) {
return new CardanoSignTxHashRequest({
requestId: uuidString
? Buffer.from(uuid.parse(uuidString) as Uint8Array)
: undefined,
txHash,
paths,
origin: origin || undefined,
addressList,
});
}
}
4 changes: 4 additions & 0 deletions packages/ur-registry-cardano/src/RegistryType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ export const ExtendedRegistryTypes = {
"cardano-sign-cip8-data-signature",
2211
),
CARDANO_SIGN_TX_HASH_REQUEST: new RegistryType(
"cardano-sign-tx-hash-request",
2212
),
};
1 change: 1 addition & 0 deletions packages/ur-registry-cardano/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export { CardanoCatalystSignature } from "./CardanoCatalystSignature";
export { CardanoSignCip8DataRequest } from "./CardanoSignMessageCip8DataRequest";
export { CardanoSignCip8DataSignature } from "./CardanoSignMessageCip8DataSignature";
export { MessageAddressFieldType } from "./CardanoSignMessageCip8DataRequest";
export { CardanoSignTxHashRequest } from "./CardanoSignTxHashRequest";

0 comments on commit fd8c811

Please sign in to comment.