-
Notifications
You must be signed in to change notification settings - Fork 16
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 #183 from KeystoneHQ/feat/avax
Feat/avax
- Loading branch information
Showing
7 changed files
with
7,397 additions
and
9,030 deletions.
There are no files selected for viewing
37 changes: 30 additions & 7 deletions
37
packages/ur-registry-avalanche/__tests__/AvalancheSignRequest.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 |
---|---|---|
@@ -1,21 +1,44 @@ | ||
// @ts-nocheck | ||
|
||
import { StellarSignRequest, SignType, AvalancheSignRequest } from "../src"; | ||
import { CryptoKeypath, PathComponent } from "../src"; | ||
import * as uuid from "uuid"; | ||
import { AvalancheSignRequest, AvalancheSignature } from "../src"; | ||
|
||
describe("avalanche-sign-request", () => { | ||
it("test should generate avalanche-sign-reqeust", () => { | ||
const avalancheData = Buffer.from( | ||
"00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000", | ||
"hex" | ||
); | ||
const mfp = "1250B6BC"; | ||
const xpub = | ||
"xpub661MyMwAqRbcFFDMuFiGQmA1EqWxxgDLdtNvxxiucf9qkfoVrvwgnYyshxWoewWtkZ1aLhKoVDrpeDvn1YRqxX2szhGKi3UiSEv1hYRMF8q"; | ||
const walletIndex = 0; | ||
|
||
const avalancheSignRequest = | ||
AvalancheSignRequest.constructAvalancheRequest(avalancheData); | ||
const avalancheSignRequest = AvalancheSignRequest.constructAvalancheRequest( | ||
avalancheData, | ||
mfp, | ||
xpub, | ||
walletIndex | ||
); | ||
|
||
const request = AvalancheSignRequest.fromDataItem( | ||
avalancheSignRequest.toDataItem() | ||
); | ||
|
||
expect(request.getSignData().toString("hex")).toBe( | ||
"00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000" | ||
); | ||
}); | ||
|
||
it("test parse signature", () => { | ||
const cbor = Buffer.from( | ||
"a201501a79a2072e114014837e792e003384c10258418d1b6e955f1b2a94aed0b2c29939d68bae89fce2d436cc814efc4731f4a43e8e78ae46e3fdd58e7e0f7c5fa7876239fe7ab0adb6a75c09a07132b741be62612000", | ||
"hex" | ||
); | ||
|
||
const signature = AvalancheSignature.fromCBOR(cbor); | ||
|
||
expect(avalancheSignRequest.toUR().cbor.toString("hex")).toBe( | ||
"58de00000000000000000001ed5f38341e436e5d46e2bb00b45d62ae97d1b050c64bc634ae10626739e35c4b0000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000007000000000089544000000000000000000000000100000001512e7191685398f00663e12197a3d8f6012d9ea300000001db720ad6707915cc4751fb7e5491a3af74e127a1d81817abe9438590c0833fe10000000021e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000050000000000989680000000010000000000000000" | ||
expect(signature.getSignature().toString("hex")).toBe( | ||
"8d1b6e955f1b2a94aed0b2c29939d68bae89fce2d436cc814efc4731f4a43e8e78ae46e3fdd58e7e0f7c5fa7876239fe7ab0adb6a75c09a07132b741be62612000" | ||
); | ||
}); | ||
}); |
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
73 changes: 70 additions & 3 deletions
73
packages/ur-registry-avalanche/src/AvalancheSignRequest.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 |
---|---|---|
@@ -1,37 +1,104 @@ | ||
import { DataItem, RegistryItem } from "@keystonehq/bc-ur-registry"; | ||
import { | ||
DataItem, | ||
RegistryItem, | ||
extend, | ||
DataItemMap, | ||
} from "@keystonehq/bc-ur-registry"; | ||
import { ExtendedRegistryTypes } from "./RegistryType"; | ||
import * as uuid from "uuid"; | ||
|
||
const { RegistryTypes } = extend; | ||
|
||
type signRequestProps = { | ||
requestId?: Buffer; | ||
data: Buffer; | ||
mfp: Buffer; | ||
xpub: string; | ||
walletIndex: number; | ||
}; | ||
|
||
enum Keys { | ||
requestId = 1, | ||
signData = 2, | ||
mfp = 3, | ||
xpub = 6, | ||
walletIndex = 7, | ||
} | ||
|
||
export class AvalancheSignRequest extends RegistryItem { | ||
private requestId?: Buffer; | ||
private data: Buffer; | ||
private mfp: Buffer; | ||
private xpub: string; | ||
private walletIndex: number; | ||
|
||
getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_SIGN_REQUEST; | ||
|
||
constructor(args: signRequestProps) { | ||
super(); | ||
this.requestId = args.requestId; | ||
this.data = args.data; | ||
this.mfp = args.mfp; | ||
this.xpub = args.xpub; | ||
this.walletIndex = args.walletIndex; | ||
} | ||
|
||
public getRequestId = () => this.requestId; | ||
public getSignData = () => this.data; | ||
|
||
public toDataItem = () => { | ||
return new DataItem(this.data); | ||
const map: DataItemMap = {}; | ||
if (this.requestId) { | ||
map[Keys.requestId] = new DataItem( | ||
this.requestId, | ||
RegistryTypes.UUID.getTag() | ||
); | ||
} | ||
|
||
map[Keys.signData] = Buffer.from(this.data); | ||
map[Keys.mfp] = this.mfp.readUInt32BE(0); | ||
map[Keys.xpub] = this.xpub; | ||
map[Keys.walletIndex] = Number(this.walletIndex); | ||
|
||
return new DataItem(map); | ||
}; | ||
|
||
public static fromDataItem = (dataItem: DataItem) => { | ||
const map = dataItem.getData(); | ||
const masterFingerprint = Buffer.alloc(4); | ||
const _masterFingerprint = map[Keys.mfp]; | ||
masterFingerprint.writeUInt32BE(_masterFingerprint, 0); | ||
const requestId = map[Keys.requestId] | ||
? map[Keys.requestId].getData() | ||
: undefined; | ||
const data = map[Keys.signData]; | ||
const xpub = map[Keys.xpub]; | ||
const walletIndex = map[Keys.signData]; | ||
|
||
return new AvalancheSignRequest({ | ||
requestId, | ||
data, | ||
xpub, | ||
walletIndex, | ||
mfp: masterFingerprint, | ||
}); | ||
}; | ||
|
||
public static constructAvalancheRequest(data: Buffer, uuidString?: string) { | ||
public static constructAvalancheRequest( | ||
data: Buffer, | ||
mfp: string, | ||
xpub: string, | ||
walletIndex: number, | ||
uuidString?: string | ||
) { | ||
return new AvalancheSignRequest({ | ||
data, | ||
requestId: uuidString | ||
? Buffer.from(uuid.parse(uuidString) as Uint8Array) | ||
: undefined, | ||
mfp: Buffer.from(mfp, "hex"), | ||
xpub, | ||
walletIndex, | ||
}); | ||
} | ||
} |
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,54 @@ | ||
import { | ||
extend, | ||
DataItem, | ||
RegistryItem, | ||
DataItemMap, | ||
} from "@keystonehq/bc-ur-registry"; | ||
import { ExtendedRegistryTypes } from "./RegistryType"; | ||
|
||
const { RegistryTypes, decodeToDataItem } = extend; | ||
|
||
enum Keys { | ||
requestId = 1, | ||
signature, | ||
} | ||
|
||
export class AvalancheSignature extends RegistryItem { | ||
private requestId?: Buffer; | ||
private signature: Buffer; | ||
|
||
getRegistryType = () => ExtendedRegistryTypes.AVALANCHE_SIGNATURE; | ||
|
||
constructor(signature: Buffer, requestId?: Buffer) { | ||
super(); | ||
this.signature = signature; | ||
this.requestId = requestId; | ||
} | ||
|
||
public getRequestId = () => this.requestId; | ||
public getSignature = () => this.signature; | ||
|
||
public toDataItem = () => { | ||
const map: DataItemMap = {}; | ||
if (this.requestId) { | ||
map[Keys.requestId] = new DataItem( | ||
this.requestId, | ||
RegistryTypes.UUID.getTag() | ||
); | ||
} | ||
map[Keys.signature] = this.signature; | ||
return new DataItem(map); | ||
}; | ||
|
||
public static fromDataItem = (dataItem: DataItem) => { | ||
const map = dataItem.getData(); | ||
const signature = map[Keys.signature]; | ||
|
||
return new AvalancheSignature(signature); | ||
}; | ||
|
||
public static fromCBOR = (_cborPayload: Buffer) => { | ||
const dataItem = decodeToDataItem(_cborPayload); | ||
return AvalancheSignature.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
Oops, something went wrong.