-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from KeystoneHQ/feat/ada-integration-v2
chore: update Cardano registry with new sign data request
- Loading branch information
Showing
6 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
packages/ur-registry-cardano/__tests__/CardanoSignDataRequest.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/ur-registry-cardano/src/CardanoSignDataRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
packages/ur-registry-cardano/src/CardanoSignDataSignature.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters