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

feat: update generic app code, add migration app #104

Merged
merged 4 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 41 additions & 15 deletions src/generic_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,67 @@ import {
P1_VALUES,
} from "./common";

const PolkadotCLA = 0x90;
const PolkadotSlip0044 = 0x80000162;

interface TxMetadata {
txMetadata: string;
}

export function newGenericApp(transport: Transport, chainTicker: string, txMetadataSrvUrl: string): GenericApp {
return GenericApp.new(transport, chainTicker, txMetadataSrvUrl);
export function newGenericApp(transport: Transport, chainId: string, txMetadataSrvUrl: string): GenericApp {
return GenericApp.new(transport, chainId, txMetadataSrvUrl);
}
export function newMigrationApp(
transport: Transport,
chainId: string,
cla: number,
sip0044: number,
txMetadataSrvUrl: string,
): GenericApp {
return GenericApp.newMigrationApp(transport, cla, sip0044, chainId, txMetadataSrvUrl);
}

export class GenericApp {
transport: Transport;
cla: number;
slip0044: number;
txMetadataSrvUrl: string;
chainTicker: string;
chainId: string;

static new(transport: Transport, chainId: string, txMetadataSrvUrl: string): GenericApp {
return new GenericApp(transport, PolkadotCLA, PolkadotSlip0044, chainId, txMetadataSrvUrl);
}

static new(transport: Transport, chainTicker: string, txMetadataSrvUrl: string): GenericApp {
return new GenericApp(transport, 0x90, 0x80000162, chainTicker, txMetadataSrvUrl);
static newApp(transport: Transport, chainId: string, txMetadataSrvUrl: string): GenericApp {
return new GenericApp(transport, PolkadotCLA, PolkadotSlip0044, chainId, txMetadataSrvUrl);
}

private constructor(
static newMigrationApp(
transport: Transport,
cla: number,
slip0044: number,
chainTicker: string,
chainId: string,
txMetadataSrvUrl: string,
) {
): GenericApp {
return new GenericApp(transport, cla, slip0044, chainId, txMetadataSrvUrl);
}

private constructor(transport: Transport, cla: number, slip0044: number, chainId: string, txMetadataSrvUrl: string) {
if (transport == null) {
throw new Error("Transport has not been defined");
}
this.transport = transport;
this.cla = cla;
this.slip0044 = slip0044;
this.txMetadataSrvUrl = txMetadataSrvUrl;
this.chainTicker = chainTicker;
this.chainId = chainId;
}

async getTxMetadata(txBlob: Buffer): Promise<Buffer> {
async getTxMetadata(callData: Buffer, signedExtensions: Buffer): Promise<Buffer> {
const resp = await axios.post<TxMetadata>(this.txMetadataSrvUrl, {
txBlob: txBlob.toString("hex"),
chain: { id: this.chainTicker },
callData: callData.toString("hex"),
signedExtensions: signedExtensions.toString("hex"),
chain: { id: this.chainId },
});

return Buffer.from(resp.data.txMetadata, "hex");
Expand Down Expand Up @@ -245,9 +266,14 @@ export class GenericApp {
}
}

async sign(account: number, change: number, addressIndex: number, blob: Buffer) {
const txMetadata = await this.getTxMetadata(blob);
return await this.signImpl(account, change, addressIndex, INS.SIGN, blob, txMetadata);
async sign(account: number, change: number, addressIndex: number, callData: Buffer, signedExtensions: Buffer) {
const txMetadata = await this.getTxMetadata(callData, signedExtensions);
const txBlob = Buffer.concat([callData, signedExtensions]);
return await this.signImpl(account, change, addressIndex, INS.SIGN, txBlob, txMetadata);
}

async signAdvanced(account: number, change: number, addressIndex: number, txBlob: Buffer, txMetadata: Buffer) {
return await this.signImpl(account, change, addressIndex, INS.SIGN, txBlob, txMetadata);
}

async signRaw(account: number, change: number, addressIndex: number, blob: Buffer) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
export * from "./legacy_apps";
export * from "./generic_app";
export { SubstrateApp } from "./substrate_app";
export { newSubstrateApp, supportedApps } from "./supported_apps";
export { newSubstrateApp, newMigrationGenericApp, supportedApps } from "./supported_apps";
17 changes: 17 additions & 0 deletions src/supported_apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { SubstrateApp } from "./substrate_app";
import { type SubstrateAppParams } from "./common";
import type Transport from "@ledgerhq/hw-transport";
import { GenericApp } from "./generic_app";

export function newSubstrateApp(transport: Transport, chainName: string) {
const requestedApp = supportedApps.find((app: SubstrateAppParams) => {
Expand All @@ -28,6 +29,22 @@ export function newSubstrateApp(transport: Transport, chainName: string) {
throw new Error(`Error: ${chainName} not supported`);
}

export function newMigrationGenericApp(transport: Transport, chainName: string, txMetadataSrvUrl: string) {
const requestedApp = supportedApps.find((app: SubstrateAppParams) => {
return app.name.toLowerCase() === chainName.toLowerCase();
});
if (requestedApp != null) {
return GenericApp.newMigrationApp(
transport,
requestedApp.cla,
requestedApp.slip0044,
requestedApp.name,
txMetadataSrvUrl,
);
}
throw new Error(`Error: ${chainName} not supported`);
}

export function getAppParams(chainName: string) {
const params = supportedApps.find((app: SubstrateAppParams) => {
return app.name.toLowerCase() === chainName.toLowerCase();
Expand Down
27 changes: 17 additions & 10 deletions tests/integration_generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ const CHAIN_NAME = "Kusama";
const CHAIN_TICKER = "ksm";
const YOUR_PUBKEY = "d280b24dface41f31006e5a2783971fc5a66c862dd7d08f97603d2902b75e47a";
const YOUR_ADDRESS = "HLKocKgeGjpXkGJU6VACtTYJK4ApTCfcGRw51E5jWntcsXv";
const YOUR_BLOB =
"040000313233343536373839303132333435363738393031323334353637383930313233158139ae28a3dfaac5fe1560a5e9e05cd5038d2433158139ae28a3dfaac5fe1560a5e9e05c362400000c000000b0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafeb0a8d493285c2df73290dfb7e61f870f17b41801197a149ca93654499ea3dafe";
const YOUR_CALL_DATA =
"0000d050f0c8c0a9706b7c0c4e439245a347627901c89d4791239533d1d2c961f1a72ad615c8530de078e565ba644b38b01bcad249e8c0";
const YOUR_SIGNED_EXTENSIONS =
"a80aceb4befe330990a59f74ed976c933db269c64dda40104a0f001900000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3a071db11cdbfd29285f25d402f1aee7a1c0384269c9c2edb476688d35e346998";

let transport = {};

Expand Down Expand Up @@ -111,8 +113,9 @@ describe("Integration", function () {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);

const txBlob = Buffer.from(YOUR_BLOB, "hex");
const resp = await app.getTxMetadata(txBlob);
const callData = Buffer.from(YOUR_CALL_DATA, "hex");
const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex");
const resp = await app.getTxMetadata(callData, signedExtensions);

expect(resp).toBeDefined();
});
Expand All @@ -121,9 +124,10 @@ describe("Integration", function () {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, "xxx", TX_METADATA_SRV_URL);

const txBlob = Buffer.from(YOUR_BLOB, "hex");
const callData = Buffer.from(YOUR_CALL_DATA, "hex");
const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex");
try {
await app.getTxMetadata(txBlob);
await app.getTxMetadata(callData, signedExtensions);
} catch (e: any) {
expect(e.response.status).toBe(404);
}
Expand All @@ -133,9 +137,10 @@ describe("Integration", function () {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, "ksm", "");

const txBlob = Buffer.from(YOUR_BLOB, "hex");
const callData = Buffer.from(YOUR_CALL_DATA, "hex");
const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex");
try {
await app.getTxMetadata(txBlob);
await app.getTxMetadata(callData, signedExtensions);
} catch (e: any) {
expect(e.code).toBe("ECONNREFUSED");
}
Expand All @@ -149,7 +154,9 @@ describe("Integration", function () {
return;
}

const txBlob = Buffer.from(YOUR_BLOB, "hex");
const callData = Buffer.from(YOUR_CALL_DATA, "hex");
const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex");
const txBlob = Buffer.concat([callData, signedExtensions]);

// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
Expand All @@ -159,7 +166,7 @@ describe("Integration", function () {
const pathIndex = 0x80000000;

const responseAddr = await app.getAddress(pathAccount, pathChange, pathIndex, ss58prefix);
const responseSign = await app.sign(pathAccount, pathChange, pathIndex, txBlob);
const responseSign = await app.sign(pathAccount, pathChange, pathIndex, callData, signedExtensions);

const pubkey = responseAddr.pubKey;

Expand Down
Loading