Skip to content

Commit

Permalink
feat: rename classes and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelm41 committed May 10, 2024
1 parent 3b6d0fd commit 36f152f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
34 changes: 19 additions & 15 deletions src/generic_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,44 @@ import {
getSignReqChunks,
P1_VALUES,
} from "./common";
import { supportedApps } from "./supported_apps";

const PolkadotCLA = 0x90;
const PolkadotSlip0044 = 0x80000162;
const GenericAppName = "Polkadot";

interface TxMetadata {
txMetadata: string;
}

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

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

static new(transport: Transport, chainId: string, txMetadataSrvUrl: string): GenericApp {
return new GenericApp(transport, PolkadotCLA, PolkadotSlip0044, chainId, txMetadataSrvUrl);
}
static newApp(transport: Transport, chainId: string, txMetadataSrvUrl: string): PolkadotGenericApp {
const polkadotAppParams = supportedApps.find(({ name }) => name === GenericAppName);
if (polkadotAppParams === undefined) throw new Error("polkadot app params missed");

static newApp(transport: Transport, chainId: string, txMetadataSrvUrl: string): GenericApp {
return new GenericApp(transport, PolkadotCLA, PolkadotSlip0044, chainId, txMetadataSrvUrl);
const { cla, slip0044 } = polkadotAppParams;
return new PolkadotGenericApp(transport, cla, slip0044, chainId, txMetadataSrvUrl);
}

static newMigrationApp(
Expand All @@ -74,8 +78,8 @@ export class GenericApp {
slip0044: number,
chainId: string,
txMetadataSrvUrl: string,
): GenericApp {
return new GenericApp(transport, cla, slip0044, chainId, txMetadataSrvUrl);
): PolkadotGenericApp {
return new PolkadotGenericApp(transport, cla, slip0044, chainId, txMetadataSrvUrl);
}

private constructor(transport: Transport, cla: number, slip0044: number, chainId: string, txMetadataSrvUrl: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/supported_apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { SubstrateApp } from "./substrate_app";
import { type SubstrateAppParams } from "./common";
import type Transport from "@ledgerhq/hw-transport";
import { GenericApp } from "./generic_app";
import { PolkadotGenericApp } from "./generic_app";

export function newSubstrateApp(transport: Transport, chainName: string) {
const requestedApp = supportedApps.find((app: SubstrateAppParams) => {
Expand All @@ -34,7 +34,7 @@ export function newMigrationGenericApp(transport: Transport, chainName: string,
return app.name.toLowerCase() === chainName.toLowerCase();
});
if (requestedApp != null) {
return GenericApp.newMigrationApp(
return PolkadotGenericApp.newMigrationApp(
transport,
requestedApp.cla,
requestedApp.slip0044,
Expand Down
27 changes: 10 additions & 17 deletions tests/integration_generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import { blake2bFinal, blake2bInit, blake2bUpdate } from "blakejs";

const ed25519 = require("ed25519-supercop");

import { newGenericApp } from "../src/generic_app";
import { newPolkadotGenericApp } from "../src/generic_app";
import { supportedApps } from "../src/supported_apps";
import { AxiosError } from "axios";
import Transport from "@ledgerhq/hw-transport";

const TX_METADATA_SRV_URL = "https://api.zondax.ch/polkadot/transaction/metadata";
const CHAIN_NAME = "Polkadot";
Expand All @@ -31,7 +31,7 @@ const YOUR_ADDRESS = "HLKocKgeGjpXkGJU6VACtTYJK4ApTCfcGRw51E5jWntcsXv";
const YOUR_BLOB =
"0000d050f0c8c0a9706b7c0c4e439245a347627901c89d4791239533d1d2c961f1a72ad615c8530de078e565ba644b38b01bcad249e8c0a80aceb4befe330990a59f74ed976c933db269c64dda40104a0f001900000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3a071db11cdbfd29285f25d402f1aee7a1c0384269c9c2edb476688d35e346998";

let transport = {};
let transport: Transport;

jest.setTimeout(60000);

Expand All @@ -41,8 +41,7 @@ beforeAll(async () => {

describe("Integration", function () {
test("get version", async () => {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
const app = newPolkadotGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
const resp = await app.getVersion();
console.log(resp);

Expand All @@ -62,8 +61,7 @@ describe("Integration", function () {
return;
}

// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
const app = newPolkadotGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);

const pathAccount = 0x80000000;
const pathChange = 0x80000000;
Expand All @@ -86,8 +84,7 @@ describe("Integration", function () {
return;
}

// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
const app = newPolkadotGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);

const pathAccount = 0x80000000;
const pathChange = 0x80000000;
Expand All @@ -108,8 +105,7 @@ describe("Integration", function () {

describe("Tx Metadata", () => {
test("Success", async () => {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
const app = newPolkadotGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);

const txBlob = Buffer.from(YOUR_BLOB, "hex");
const resp = await app.getTxMetadata(txBlob);
Expand All @@ -118,8 +114,7 @@ describe("Integration", function () {
});

test("Wrong/Invalid ticker", async () => {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, "xxx", TX_METADATA_SRV_URL);
const app = newPolkadotGenericApp(transport, "xxx", TX_METADATA_SRV_URL);

const txBlob = Buffer.from(YOUR_BLOB, "hex");
try {
Expand All @@ -130,8 +125,7 @@ describe("Integration", function () {
});

test("Empty/Wrong service url", async () => {
// @ts-expect-error transport will be there
const app = newGenericApp(transport, "ksm", "");
const app = newPolkadotGenericApp(transport, "ksm", "");

const txBlob = Buffer.from(YOUR_BLOB, "hex");
try {
Expand All @@ -151,8 +145,7 @@ describe("Integration", function () {

const txBlob = Buffer.from(YOUR_BLOB, "hex");

// @ts-expect-error transport will be there
const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);
const app = newPolkadotGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL);

const pathAccount = 0x80000000;
const pathChange = 0x80000000;
Expand Down

0 comments on commit 36f152f

Please sign in to comment.