Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stellar integration #152

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/ur-registry-stellar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# BC-UR-Registry-Stellar

This repository is the Stellar extension of [bc-ur-registry](https://github.com/KeystoneHQ/ur-registry)

## Installing

To install, run:

```bash
yarn add @keystonehq/bc-ur-registry-stellar
```

```bash
npm install --save @keystonehq/bc-ur-registry-stellar
```
76 changes: 76 additions & 0 deletions packages/ur-registry-stellar/__tests__/StellarSignRequest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// @ts-nocheck

import { StellarSignRequest, SignType } from "../src";
import { CryptoKeypath, PathComponent } from "../src";
import * as uuid from "uuid";

describe("stellar-sign-request", () => {
it("test should generate stellar-sign-reqeust", () => {
const stellarData = Buffer.from(
"01000103c8d842a2f17fd7aab608ce2ea535a6e958dffa20caf669b347b911c4171965530f957620b228bae2b94c82ddd4c093983a67365555b737ec7ddc1117e61c72e0000000000000000000000000000000000000000000000000000000000000000010295cc2f1f39f3604718496ea00676d6a72ec66ad09d926e3ece34f565f18d201020200010c0200000000e1f50500000000",
"hex"
);

const signKeyPath = new CryptoKeypath(
[
new PathComponent({ index: 44, hardened: true }),
new PathComponent({ index: 501, hardened: true }),
new PathComponent({ index: 0, hardened: true }),
new PathComponent({ index: 0, hardened: true }),
],
Buffer.from("12345678", "hex")
);

const stellarRequestId = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d";
const idBuffer = uuid.parse(stellarRequestId) as Uint8Array;

const stellarSignRequest = new StellarSignRequest({
signData: stellarData,
derivationPath: signKeyPath,
requestId: Buffer.from(idBuffer),
origin: "solflare",
signType: SignType.Transaction,
});

const cborHex = stellarSignRequest.toCBOR().toString("hex");
const ur = stellarSignRequest.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:stellar-sign-request/onadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohdmtadaeadaxsptpfwoewnlbtspkrpaytodmonecolwlhdurzscxsgyninqdflrhbysschcfihgubsmdkocxprderdvorhgslfuttyrtmumkftioengogorlemwpkiuobychvacejpvtaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaebedthhsawnwfneenaajslrmtwdaeiojnimjpwpiypmastadsvlwpvlgwhfhecstdadaoaoaeadbnaoaeaeaeaevyykahaeaeaeaeaxtaaddyoeadlocsdwykcfadykykaeykaeykaocybgeehfksahisjkjljziyjzhsjpihamadkkgseofg"
);
const stellarSignRequestDecoded = StellarSignRequest.fromCBOR(
Buffer.from(cborHex, "hex")
);
expect(uuid.stringify(stellarSignRequest.getRequestId())).toBe(
stellarRequestId
);
expect(stellarSignRequest.getOrigin()).toBe("solflare");
expect(stellarSignRequestDecoded.getSignData().toString("hex")).toEqual(
"01000103c8d842a2f17fd7aab608ce2ea535a6e958dffa20caf669b347b911c4171965530f957620b228bae2b94c82ddd4c093983a67365555b737ec7ddc1117e61c72e0000000000000000000000000000000000000000000000000000000000000000010295cc2f1f39f3604718496ea00676d6a72ec66ad09d926e3ece34f565f18d201020200010c0200000000e1f50500000000"
);
expect(stellarSignRequestDecoded.getSignType()).toBe(SignType.Transaction);
});

it("should construct an StellarSignRequest object from string", () => {
const hdPath = "M/44'/501'/0'/0'";
const xfp = "12345678";
const stellarData = Buffer.from(
"01000103c8d842a2f17fd7aab608ce2ea535a6e958dffa20caf669b347b911c4171965530f957620b228bae2b94c82ddd4c093983a67365555b737ec7ddc1117e61c72e0000000000000000000000000000000000000000000000000000000000000000010295cc2f1f39f3604718496ea00676d6a72ec66ad09d926e3ece34f565f18d201020200010c0200000000e1f50500000000",
"hex"
);
const requestID = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d";

const request = StellarSignRequest.constructStellarRequest(
stellarData,
hdPath,
xfp,
SignType.Message,
requestID,
undefined,
"solflare"
);
const ur = request.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:stellar-sign-request/onadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohdmtadaeadaxsptpfwoewnlbtspkrpaytodmonecolwlhdurzscxsgyninqdflrhbysschcfihgubsmdkocxprderdvorhgslfuttyrtmumkftioengogorlemwpkiuobychvacejpvtaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaeaebedthhsawnwfneenaajslrmtwdaeiojnimjpwpiypmastadsvlwpvlgwhfhecstdadaoaoaeadbnaoaeaeaeaevyykahaeaeaeaeaxtaaddyoeadlocsdwykcfadykykaeykaeykaocybgeehfksahisjkjljziyjzhsjpihamaovtfeidzt"
);
});
});
35 changes: 35 additions & 0 deletions packages/ur-registry-stellar/__tests__/StellarSignature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// @ts-nocheck

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

describe("stellar-sign-request", () => {
it("test should generate stellar-signature", () => {
const signature = Buffer.from(
"d4f0a7bcd95bba1fbb1051885054730e3f47064288575aacc102fbbf6a9a14daa066991e360d3e3406c20c00a40973eff37c7d641e5b351ec4a99bfe86f335f7",
"hex"
);
const stellarRequestId = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d";
const idBuffer = uuid.parse(stellarRequestId) as Uint8Array;

const stellarSignature = new StellarSignature(
signature,
Buffer.from(idBuffer)
);

const cborHex = stellarSignature.toCBOR().toString("hex");
const ur = stellarSignature.toUREncoder(1000).nextPart();
expect(ur).toBe(
"ur:stellar-signature/oeadtpdagdndcawmgtfrkigrpmndutdnbtkgfssbjnaohdfztywtosrftahprdctrkbegylogdghjkbafhflamfwlohghtpsseaozorsimnybbtnnbiynlckenbtfmeeamsabnaeoxasjkwswfkekiieckhpecckssptndzelnwfecyldrcyhkws"
);
const StellarSignatureDecoded = StellarSignature.fromCBOR(
Buffer.from(cborHex, "hex")
);
expect(uuid.stringify(StellarSignatureDecoded.getRequestId())).toBe(
stellarRequestId
);
expect(StellarSignatureDecoded.getSignature().toString("hex")).toEqual(
"d4f0a7bcd95bba1fbb1051885054730e3f47064288575aacc102fbbf6a9a14daa066991e360d3e3406c20c00a40973eff37c7d641e5b351ec4a99bfe86f335f7"
);
});
});
6 changes: 6 additions & 0 deletions packages/ur-registry-stellar/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
38 changes: 38 additions & 0 deletions packages/ur-registry-stellar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@keystonehq/bc-ur-registry-stellar",
"version": "0.0.1",
"description": "bc-ur-registry extension for Stellar",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"directories": {
"lib": "src",
"test": "__tests__"
},
"files": [
"src",
"dist"
],
"scripts": {
"clean": "rm -rf ./dist",
"start": "tsdx watch",
"build": "tsdx build",
"test": "jest --passWithNoTests"
},
"publishConfig": {
"access": "public"
},
"author": "xi@keyst.com",
"license": "ISC",
"dependencies": {
"@keystonehq/bc-ur-registry": "^0.5.4",
"bs58check": "^2.1.2",
"uuid": "^8.3.2"
},
"devDependencies": {
"@babel/preset-typescript": "^7.15.0",
"@types/uuid": "^8.3.1",
"tsdx": "^0.14.1",
"typescript": "^4.6.2"
},
"gitHead": "83d8e223d29e5cc71dccc963388d65a87c894636"
}
6 changes: 6 additions & 0 deletions packages/ur-registry-stellar/src/RegistryType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { RegistryType } from "@keystonehq/bc-ur-registry";

export const ExtendedRegistryTypes = {
STELLAR_SIGN_REQUEST: new RegistryType("stellar-sign-request", 8201),
STELLAR_SIGNATURE: new RegistryType("stellar-signature", 8202),
};
151 changes: 151 additions & 0 deletions packages/ur-registry-stellar/src/StellarSignRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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;

export enum SignType {
Transaction = 1,
Message = 2,
}

enum Keys {
requestId = 1,
signData,
derivationPath,
address,
origin,
signType,
}

type signRequestProps = {
requestId?: Buffer;
signData: Buffer;
derivationPath: CryptoKeypath;
address?: Buffer;
origin?: string;
signType: SignType;
};

export class StellarSignRequest extends RegistryItem {
private requestId?: Buffer;
private signData: Buffer;
private derivationPath: CryptoKeypath;
private address?: Buffer;
private origin?: string;
private signType: SignType;

getRegistryType = () => ExtendedRegistryTypes.STELLAR_SIGN_REQUEST;

constructor(args: signRequestProps) {
super();
this.requestId = args.requestId;
this.signData = args.signData;
this.derivationPath = args.derivationPath;
this.address = args.address;
this.origin = args.origin;
this.signType = args.signType;
}

public getRequestId = () => this.requestId;
public getSignData = () => this.signData;
public getDerivationPath = () => this.derivationPath.getPath();
public getSignRequestAddress = () => this.address;
public getOrigin = () => this.origin;
public getSignType = () => this.signType;

public toDataItem = () => {
const map: DataItemMap = {};
if (this.requestId) {
map[Keys.requestId] = new DataItem(
this.requestId,
RegistryTypes.UUID.getTag()
);
}
if (this.address) {
map[Keys.address] = this.address;
}

if (this.origin) {
map[Keys.origin] = this.origin;
}

map[Keys.signData] = this.signData;
map[Keys.signType] = this.signType;

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 address = map[Keys.address] ? map[Keys.address] : undefined;
const requestId = map[Keys.requestId]
? map[Keys.requestId].getData()
: undefined;
const origin = map[Keys.origin] ? map[Keys.origin] : undefined;
const signType = map[Keys.signType];

return new StellarSignRequest({
requestId,
signData,
derivationPath,
address,
origin,
signType,
});
};

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

public static constructStellarRequest(
signData: Buffer,
hdPath: string,
xfp: string,
signType: SignType,
uuidString?: string,
address?: 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 StellarSignRequest({
requestId: uuidString
? Buffer.from(uuid.parse(uuidString) as Uint8Array)
: undefined,
signData,
derivationPath: hdpathObject,
address: address
? Buffer.from(address.replace("0x", ""), "hex")
: undefined,
origin: origin || undefined,
signType,
});
}
}
57 changes: 57 additions & 0 deletions packages/ur-registry-stellar/src/StellarSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 StellarSignature extends RegistryItem {
private requestId?: Buffer;
private signature: Buffer;

getRegistryType = () => ExtendedRegistryTypes.STELLAR_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];
const requestId = map[Keys.requestId]
? map[Keys.requestId].getData()
: undefined;

return new StellarSignature(signature, requestId);
};

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