Skip to content

Commit

Permalink
Merge pull request #128 from KeystoneHQ/fix-sui
Browse files Browse the repository at this point in the history
fix: sui ur
  • Loading branch information
LiYanLance authored Jun 16, 2023
2 parents 4065494 + 133304f commit 22ba418
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 55 deletions.
36 changes: 16 additions & 20 deletions packages/ur-registry-sui/__tests__/SuiSignRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
SuiSignRequest,
CryptoKeypath,
PathComponent,
SignType
} from "../src";
import * as uuid from "uuid";

describe("sui-sign-request", () => {
it("test should generate sui-sign-request", () => {
const signData = Buffer.from(
"000002002086ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66000810270000000000000202000101010001010200000100000e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c71901d833a8eabc697a0b2e23740aca7be9b0b9e1560a39d2f390cf2534e94429f91ced0c00000000000020190ca0d64215ac63f50dbffa47563404182304e0c10ea30b5e4d671b7173a34c0e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c719e803000000000000640000000000000000",
const intentMessage = Buffer.from(
"000000000002002086ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66000810270000000000000202000101010001010200000100000e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c71901d833a8eabc697a0b2e23740aca7be9b0b9e1560a39d2f390cf2534e94429f91ced0c00000000000020190ca0d64215ac63f50dbffa47563404182304e0c10ea30b5e4d671b7173a34c0e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c719e803000000000000640000000000000000",
"hex"
);

Expand All @@ -23,7 +22,7 @@ describe("sui-sign-request", () => {
new PathComponent({ index: 0, hardened: true }),
new PathComponent({ index: 0, hardened: true })
],
Buffer.from("78230804", "hex")
Buffer.from("C9A8ED4A", "hex")
);
const signKeyPath1 = new CryptoKeypath(
[
Expand All @@ -40,34 +39,32 @@ describe("sui-sign-request", () => {

const signRequest = new SuiSignRequest({
requestId,
signData,
signType: SignType.Single,
intentMessage,
derivationPaths: [signKeyPath0, signKeyPath1],
origin: "Sui Wallet",
addresses: [
Buffer.from("86ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66", 'hex'),
Buffer.from("a698c879862b9b965a73bedfa83469d94e276f3ee7b99367331d8b340c17d196", 'hex'),
Buffer.from("68a42711caf03f82e5e45452eb4f1223675aeed4a80b4465892495c48648e3c7", 'hex'),
],
});

const cborHex = signRequest.toCBOR().toString("hex");
const ur = signRequest.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:sui-sign-request/oladtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohdtaaeaeaoaecxlnpshskksgimtaospasfqzjpaotiimvtnybwckiydynlfymodrytstfsfncxfriyaeaybediaeaeaeaeaeaeaoaoaeadadadaeadadaoaeaeadaeaebagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfadtpeopdwdrfinknbddmcnjybksgkgwlpfrhvyhfbkestdwfmhtkdaeewlfydtytcewebnaeaeaeaeaeaecxcfbnnbtbfwbzpsiaykbtrszsflhfeeaacscnaavtsebaotbdhygtiocwjsjkotgsbagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfvsaxaeaeaeaeaeaeieaeaeaeaeaeaeaeaeaxadaalftaaddyoeadlecsdwykcfaxbeykaeykaeykaeykaocykscnayaataaddyoeadlecsdwykcfaxbeykaeykaeykadykaocykscnayahahlfhdcxlnpshskksgimtaospasfqzjpaotiimvtnybwckiydynlfymodrytstfsfncxfriyhdcxisoxdibysgwtfhlfvwveghgmwmgwbgcniohtwytypdbdfyihlddkmdsslnfdvlstamimgukpincxhghsjzjzihjyzmpmwevo"
"ur:sui-sign-request/onadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohduoaeaeaeaeaeaoaecxlnpshskksgimtaospasfqzjpaotiimvtnybwckiydynlfymodrytstfsfncxfriyaeaybediaeaeaeaeaeaeaoaoaeadadadaeadadaoaeaeadaeaebagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfadtpeopdwdrfinknbddmcnjybksgkgwlpfrhvyhfbkestdwfmhtkdaeewlfydtytcewebnaeaeaeaeaeaecxcfbnnbtbfwbzpsiaykbtrszsflhfeeaacscnaavtsebaotbdhygtiocwjsjkotgsbagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfvsaxaeaeaeaeaeaeieaeaeaeaeaeaeaeaeaxlftaaddyoeadlecsdwykcfaxbeykaeykaeykaeykaocysopdwegetaaddyoeadlecsdwykcfaxbeykaeykaeykadykaocykscnayahaalfhdcxolmkspkklndnndmthtjkrnurpdeeintagldijlfmvdrhmuioeocalueebnchttmthdcxisoxdibysgwtfhlfvwveghgmwmgwbgcniohtwytypdbdfyihlddkmdsslnfdvlstahimgukpincxhghsjzjzihjynlbsbtwt"
);
const signRequestDecoded = SuiSignRequest.fromCBOR(
Buffer.from(cborHex, "hex")
);
expect(uuid.stringify(signRequest.getRequestId())).toBe(uuid.stringify(requestId));
expect(signRequest.getOrigin()).toBe("Sui Wallet");
expect(signRequestDecoded.getSignData().toString("hex")).toEqual(
"000002002086ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66000810270000000000000202000101010001010200000100000e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c71901d833a8eabc697a0b2e23740aca7be9b0b9e1560a39d2f390cf2534e94429f91ced0c00000000000020190ca0d64215ac63f50dbffa47563404182304e0c10ea30b5e4d671b7173a34c0e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c719e803000000000000640000000000000000"
expect(signRequestDecoded.getIntentMessage().toString("hex")).toEqual(
"000000000002002086ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66000810270000000000000202000101010001010200000100000e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c71901d833a8eabc697a0b2e23740aca7be9b0b9e1560a39d2f390cf2534e94429f91ced0c00000000000020190ca0d64215ac63f50dbffa47563404182304e0c10ea30b5e4d671b7173a34c0e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c719e803000000000000640000000000000000"
);
expect(signRequestDecoded.getDerivationPaths()[0]).toEqual("44'/784'/0'/0'/0'");
expect(signRequestDecoded.getDerivationPaths()[1]).toEqual("44'/784'/0'/0'/1'");
expect(signRequestDecoded.getSignType()).toBe(SignType.Single);
expect(signRequestDecoded.getAddresses()[0]).toEqual(
Buffer.from("86ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66", 'hex')
Buffer.from("a698c879862b9b965a73bedfa83469d94e276f3ee7b99367331d8b340c17d196", 'hex')
);
expect(signRequestDecoded.getAddresses()[1]).toEqual(
Buffer.from("68a42711caf03f82e5e45452eb4f1223675aeed4a80b4465892495c48648e3c7", 'hex')
Expand All @@ -76,29 +73,28 @@ describe("sui-sign-request", () => {

it("should construct an signRequest object from string", () => {
const publicKeyHdPaths = ["m/44'/784'/0'/0'/0'", "m/44'/784'/0'/0'/1'"];
const xfps = ["78230804", "78230805"];
const signData = Buffer.from(
"000002002086ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66000810270000000000000202000101010001010200000100000e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c71901d833a8eabc697a0b2e23740aca7be9b0b9e1560a39d2f390cf2534e94429f91ced0c00000000000020190ca0d64215ac63f50dbffa47563404182304e0c10ea30b5e4d671b7173a34c0e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c719e803000000000000640000000000000000",
const xfps = ["C9A8ED4A", "78230805"];
const intentMessage = Buffer.from(
"000000000002002086ac6179ca6ad9a7b1ccb47202d06ae09a131e66309944922af9c73d3c203b66000810270000000000000202000101010001010200000100000e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c71901d833a8eabc697a0b2e23740aca7be9b0b9e1560a39d2f390cf2534e94429f91ced0c00000000000020190ca0d64215ac63f50dbffa47563404182304e0c10ea30b5e4d671b7173a34c0e4d9313fb5b3f166bb6f2aea587edbe21fb1c094472ccd002f34b9d0633c719e803000000000000640000000000000000",
"hex"
);
const requestID = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d";
const addresses = [
Buffer.from("aa7420c68c16645775ecf69a5e2fdaa4f89d3293aee0dd280e2d97ad7b879650", 'hex'),
Buffer.from("97f95acfb04f84d228dce9bda4ad7e2a5cb324d5efdd6a7f0b959e755ebb3a70", 'hex'),
Buffer.from("a698c879862b9b965a73bedfa83469d94e276f3ee7b99367331d8b340c17d196", 'hex'),
Buffer.from("68a42711caf03f82e5e45452eb4f1223675aeed4a80b4465892495c48648e3c7", 'hex'),
];

const request = SuiSignRequest.constructSuiRequest(
signData,
intentMessage,
publicKeyHdPaths,
xfps,
SignType.Single,
requestID,
addresses,
"Sui Wallet"
);
const ur = request.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:sui-sign-request/oladtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohdtaaeaeaoaecxlnpshskksgimtaospasfqzjpaotiimvtnybwckiydynlfymodrytstfsfncxfriyaeaybediaeaeaeaeaeaeaoaoaeadadadaeadadaoaeaeadaeaebagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfadtpeopdwdrfinknbddmcnjybksgkgwlpfrhvyhfbkestdwfmhtkdaeewlfydtytcewebnaeaeaeaeaeaecxcfbnnbtbfwbzpsiaykbtrszsflhfeeaacscnaavtsebaotbdhygtiocwjsjkotgsbagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfvsaxaeaeaeaeaeaeieaeaeaeaeaeaeaeaeaxadaalftaaddyoeadlecsdwykcfaxbeykaeykaeykaeykaocykscnayaataaddyoeadlecsdwykcfaxbeykaeykaeykadykaocykscnayahahlfhdcxpkjycxswlkcmiehgkpwpynnyhydltnoxyanteymuplvtutdebadpmspmkgltmtgdhdcxmsythttkpfgwlrtddeuowlryoxpmkbdrhhqddktlwsutimlbbdmdnnkphyrkftjoamimgukpincxhghsjzjzihjysgtksbay"
"ur:sui-sign-request/onadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohduoaeaeaeaeaeaoaecxlnpshskksgimtaospasfqzjpaotiimvtnybwckiydynlfymodrytstfsfncxfriyaeaybediaeaeaeaeaeaeaoaoaeadadadaeadadaoaeaeadaeaebagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfadtpeopdwdrfinknbddmcnjybksgkgwlpfrhvyhfbkestdwfmhtkdaeewlfydtytcewebnaeaeaeaeaeaecxcfbnnbtbfwbzpsiaykbtrszsflhfeeaacscnaavtsebaotbdhygtiocwjsjkotgsbagtmubwzohpfhcmjerpwzplonltwernclzoceasfyjpsftiaowfgrntameostcfvsaxaeaeaeaeaeaeieaeaeaeaeaeaeaeaeaxlftaaddyoeadlecsdwykcfaxbeykaeykaeykaeykaocysopdwegetaaddyoeadlecsdwykcfaxbeykaeykaeykadykaocykscnayahaalfhdcxolmkspkklndnndmthtjkrnurpdeeintagldijlfmvdrhmuioeocalueebnchttmthdcxisoxdibysgwtfhlfvwveghgmwmgwbgcniohtwytypdbdfyihlddkmdsslnfdvlstahimgukpincxhghsjzjzihjynlbsbtwt"
);
});
});
53 changes: 21 additions & 32 deletions packages/ur-registry-sui/src/SuiSignRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,32 @@ import {
DataItem,
PathComponent,
RegistryItem,
DataItemMap
DataItemMap,
} from "@keystonehq/bc-ur-registry";
import { ExtendedRegistryTypes } from "./RegistryType";
import * as uuid from "uuid";

const { decodeToDataItem, RegistryTypes } = extend;

export enum SignType {
Single = 1,
Multi = 2,
Message = 3,
}

enum Keys {
requestId = 1,
signData,
signType,
intentMessage,
derivationPaths,
addresses,
origin,
}

type signRequestProps = {
requestId?: Buffer;
signData: Buffer;
signType: SignType;
intentMessage: Buffer;
derivationPaths: CryptoKeypath[];
addresses?: Buffer[];
origin?: string;
};

export class SuiSignRequest extends RegistryItem {
private requestId?: Buffer;
private signData: Buffer;
private signType: SignType;
private intentMessage: Buffer;
private derivationPaths: CryptoKeypath[];
private addresses?: Buffer[];
private origin?: string;
Expand All @@ -48,19 +39,18 @@ export class SuiSignRequest extends RegistryItem {
constructor(args: signRequestProps) {
super();
this.requestId = args.requestId;
this.signData = args.signData;
this.signType = args.signType;
this.intentMessage = args.intentMessage;
this.derivationPaths = args.derivationPaths;
this.addresses = args.addresses;
this.origin = args.origin;
}

public getRequestId = () => this.requestId;
public getSignData = () => this.signData;
public getDerivationPaths = () => this.derivationPaths.map(key => key.getPath());
public getIntentMessage = () => this.intentMessage;
public getDerivationPaths = () =>
this.derivationPaths.map((key) => key.getPath());
public getAddresses = () => this.addresses;
public getOrigin = () => this.origin;
public getSignType = () => this.signType;

public toDataItem = () => {
const map: DataItemMap = {};
Expand All @@ -77,9 +67,8 @@ export class SuiSignRequest extends RegistryItem {
map[Keys.origin] = this.origin;
}

map[Keys.signData] = this.signData;
map[Keys.signType] = this.signType;
map[Keys.derivationPaths] = this.derivationPaths.map(item => {
map[Keys.intentMessage] = this.intentMessage;
map[Keys.derivationPaths] = this.derivationPaths.map((item) => {
const dataItem = item.toDataItem();
dataItem.setTag(item.getRegistryType().getTag());
return dataItem;
Expand All @@ -89,16 +78,18 @@ export class SuiSignRequest extends RegistryItem {

public static fromDataItem = (dataItem: DataItem) => {
const map = dataItem.getData();
const signData = map[Keys.signData];
const signType = map[Keys.signType];
const derivationPaths = map[Keys.derivationPaths].map((item: DataItem) => CryptoKeypath.fromDataItem(item));
const intentMessage = map[Keys.intentMessage];
const derivationPaths = map[Keys.derivationPaths].map((item: DataItem) =>
CryptoKeypath.fromDataItem(item)
);
const addresses = map[Keys.addresses] ? map[Keys.addresses] : undefined;
const requestId = map[Keys.requestId] ? map[Keys.requestId].getData() : undefined;
const requestId = map[Keys.requestId]
? map[Keys.requestId].getData()
: undefined;
const origin = map[Keys.origin] ? map[Keys.origin] : undefined;
return new SuiSignRequest({
requestId,
signData,
signType,
intentMessage,
derivationPaths,
addresses,
origin,
Expand All @@ -111,17 +102,16 @@ export class SuiSignRequest extends RegistryItem {
};

public static constructSuiRequest(
signData: Buffer,
intentMessage: Buffer,
publicKeyHdPath: string[],
xfps: string[],
signType: SignType,
uuidString: string,
addresses?: Buffer[],
origin?: string
) {
const publicKeyHdPathObjects = publicKeyHdPath.map((path, index) => {
const paths = path.replace(/[m|M]\//, "").split("/");
const pathComponent = paths.map(path => {
const pathComponent = paths.map((path) => {
const index = parseInt(path.replace("'", ""));
let isHardened = false;
if (path.endsWith("'")) {
Expand All @@ -134,8 +124,7 @@ export class SuiSignRequest extends RegistryItem {

return new SuiSignRequest({
requestId: Buffer.from(uuid.parse(uuidString)),
signData,
signType: signType || SignType.Single,
intentMessage,
derivationPaths: publicKeyHdPathObjects,
addresses: addresses || undefined,
origin: origin || undefined,
Expand Down
6 changes: 3 additions & 3 deletions packages/ur-registry-sui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export * from "@keystonehq/bc-ur-registry";

patchTags(
Object.values(ExtendedRegistryTypes)
.filter(rt => !!rt.getTag())
.map(rt => rt.getTag())
.filter((rt) => !!rt.getTag())
.map((rt) => rt.getTag())
);

export { SuiSignRequest, SignType } from "./SuiSignRequest";
export { SuiSignRequest } from "./SuiSignRequest";
export { SuiSignature } from "./SuiSignature";

0 comments on commit 22ba418

Please sign in to comment.