From a2e70e62ad33adc0af89d13372fbf7c833090845 Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Wed, 8 May 2024 14:26:12 -0300 Subject: [PATCH 1/4] feat: update generic app code, add migration app --- src/generic_app.ts | 56 ++++++++++---- src/migration_apps.ts | 123 ++++++++++++++++++++++++++++++ src/supported_apps.ts | 17 +++++ tests/integration_generic.test.ts | 27 ++++--- 4 files changed, 198 insertions(+), 25 deletions(-) create mode 100644 src/migration_apps.ts diff --git a/src/generic_app.ts b/src/generic_app.ts index 8be69b2..18e2171 100644 --- a/src/generic_app.ts +++ b/src/generic_app.ts @@ -33,12 +33,24 @@ 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 { @@ -46,19 +58,27 @@ export class GenericApp { 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"); } @@ -66,13 +86,14 @@ export class GenericApp { this.cla = cla; this.slip0044 = slip0044; this.txMetadataSrvUrl = txMetadataSrvUrl; - this.chainTicker = chainTicker; + this.chainId = chainId; } - async getTxMetadata(txBlob: Buffer): Promise { + async getTxMetadata(callData: Buffer, signedExtensions: Buffer): Promise { const resp = await axios.post(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"); @@ -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) { diff --git a/src/migration_apps.ts b/src/migration_apps.ts new file mode 100644 index 0000000..a3141d8 --- /dev/null +++ b/src/migration_apps.ts @@ -0,0 +1,123 @@ +/** ****************************************************************************** + * (c) 2019 - 2022 ZondaX AG + * (c) 2016-2017 Ledger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************* */ +import type Transport from "@ledgerhq/hw-transport"; +import { newSubstrateApp } from "./supported_apps"; + +// Legacy code +export function newKusamaApp(transport: Transport) { + return newSubstrateApp(transport, "Kusama"); +} + +export function newPolkadotApp(transport: Transport) { + return newSubstrateApp(transport, "Polkadot"); +} + +export function newPolymeshApp(transport: Transport) { + return newSubstrateApp(transport, "Polymesh"); +} + +export function newDockApp(transport: Transport) { + return newSubstrateApp(transport, "Dock"); +} + +export function newCentrifugeApp(transport: Transport) { + return newSubstrateApp(transport, "Centrifuge"); +} + +export function newEdgewareApp(transport: Transport) { + return newSubstrateApp(transport, "Edgeware"); +} + +export function newEquilibriumApp(transport: Transport) { + return newSubstrateApp(transport, "Equilibrium"); +} + +export function newGenshiroApp(transport: Transport) { + return newSubstrateApp(transport, "Genshiro"); +} + +export function newStatemintApp(transport: Transport) { + return newSubstrateApp(transport, "Statemint"); +} + +export function newStatemineApp(transport: Transport) { + return newSubstrateApp(transport, "Statemine"); +} + +export function newNodleApp(transport: Transport) { + return newSubstrateApp(transport, "Nodle"); +} + +export function newSoraApp(transport: Transport) { + return newSubstrateApp(transport, "Sora"); +} + +export function newPolkadexApp(transport: Transport) { + return newSubstrateApp(transport, "Polkadex"); +} + +export function newBifrostApp(transport: Transport) { + return newSubstrateApp(transport, "Bifrost"); +} + +export function newKaruraApp(transport: Transport) { + return newSubstrateApp(transport, "Karura"); +} + +export function newReefApp(transport: Transport) { + return newSubstrateApp(transport, "Reef"); +} + +export function newAcalaApp(transport: Transport) { + return newSubstrateApp(transport, "Acala"); +} + +export function newXXNetworkApp(transport: Transport) { + return newSubstrateApp(transport, "XXNetwork"); +} + +export function newParallelApp(transport: Transport) { + return newSubstrateApp(transport, "Parallel"); +} + +export function newAstarApp(transport: Transport) { + return newSubstrateApp(transport, "Astar"); +} + +export function newComposableApp(transport: Transport) { + return newSubstrateApp(transport, "Composable"); +} + +export function newStafiApp(transport: Transport) { + return newSubstrateApp(transport, "Stafi"); +} + +export function newAlephZeroApp(transport: Transport) { + return newSubstrateApp(transport, "AlephZero"); +} + +export function newInterlayApp(transport: Transport) { + return newSubstrateApp(transport, "Interlay"); +} + +export function newUniqueApp(transport: Transport) { + return newSubstrateApp(transport, "Unique"); +} + +export function newBifrostKusamaApp(transport: Transport) { + return newSubstrateApp(transport, "BifrostKusama"); +} diff --git a/src/supported_apps.ts b/src/supported_apps.ts index 5b680b2..02c411d 100644 --- a/src/supported_apps.ts +++ b/src/supported_apps.ts @@ -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) => { @@ -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(); diff --git a/tests/integration_generic.test.ts b/tests/integration_generic.test.ts index d9acce9..3d2a0f5 100644 --- a/tests/integration_generic.test.ts +++ b/tests/integration_generic.test.ts @@ -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 = {}; @@ -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(); }); @@ -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); } @@ -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"); } @@ -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); @@ -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; From 61cc9629bb735b2b9790e5b0125ec86984c5a72f Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Wed, 8 May 2024 14:37:50 -0300 Subject: [PATCH 2/4] fix: export newMigrationApp method --- src/index.ts | 2 +- src/migration_apps.ts | 123 ------------------------------------------ 2 files changed, 1 insertion(+), 124 deletions(-) delete mode 100644 src/migration_apps.ts diff --git a/src/index.ts b/src/index.ts index 9e1b46c..9b5ad67 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/migration_apps.ts b/src/migration_apps.ts deleted file mode 100644 index a3141d8..0000000 --- a/src/migration_apps.ts +++ /dev/null @@ -1,123 +0,0 @@ -/** ****************************************************************************** - * (c) 2019 - 2022 ZondaX AG - * (c) 2016-2017 Ledger - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - ******************************************************************************* */ -import type Transport from "@ledgerhq/hw-transport"; -import { newSubstrateApp } from "./supported_apps"; - -// Legacy code -export function newKusamaApp(transport: Transport) { - return newSubstrateApp(transport, "Kusama"); -} - -export function newPolkadotApp(transport: Transport) { - return newSubstrateApp(transport, "Polkadot"); -} - -export function newPolymeshApp(transport: Transport) { - return newSubstrateApp(transport, "Polymesh"); -} - -export function newDockApp(transport: Transport) { - return newSubstrateApp(transport, "Dock"); -} - -export function newCentrifugeApp(transport: Transport) { - return newSubstrateApp(transport, "Centrifuge"); -} - -export function newEdgewareApp(transport: Transport) { - return newSubstrateApp(transport, "Edgeware"); -} - -export function newEquilibriumApp(transport: Transport) { - return newSubstrateApp(transport, "Equilibrium"); -} - -export function newGenshiroApp(transport: Transport) { - return newSubstrateApp(transport, "Genshiro"); -} - -export function newStatemintApp(transport: Transport) { - return newSubstrateApp(transport, "Statemint"); -} - -export function newStatemineApp(transport: Transport) { - return newSubstrateApp(transport, "Statemine"); -} - -export function newNodleApp(transport: Transport) { - return newSubstrateApp(transport, "Nodle"); -} - -export function newSoraApp(transport: Transport) { - return newSubstrateApp(transport, "Sora"); -} - -export function newPolkadexApp(transport: Transport) { - return newSubstrateApp(transport, "Polkadex"); -} - -export function newBifrostApp(transport: Transport) { - return newSubstrateApp(transport, "Bifrost"); -} - -export function newKaruraApp(transport: Transport) { - return newSubstrateApp(transport, "Karura"); -} - -export function newReefApp(transport: Transport) { - return newSubstrateApp(transport, "Reef"); -} - -export function newAcalaApp(transport: Transport) { - return newSubstrateApp(transport, "Acala"); -} - -export function newXXNetworkApp(transport: Transport) { - return newSubstrateApp(transport, "XXNetwork"); -} - -export function newParallelApp(transport: Transport) { - return newSubstrateApp(transport, "Parallel"); -} - -export function newAstarApp(transport: Transport) { - return newSubstrateApp(transport, "Astar"); -} - -export function newComposableApp(transport: Transport) { - return newSubstrateApp(transport, "Composable"); -} - -export function newStafiApp(transport: Transport) { - return newSubstrateApp(transport, "Stafi"); -} - -export function newAlephZeroApp(transport: Transport) { - return newSubstrateApp(transport, "AlephZero"); -} - -export function newInterlayApp(transport: Transport) { - return newSubstrateApp(transport, "Interlay"); -} - -export function newUniqueApp(transport: Transport) { - return newSubstrateApp(transport, "Unique"); -} - -export function newBifrostKusamaApp(transport: Transport) { - return newSubstrateApp(transport, "BifrostKusama"); -} From 3b6d0fd500e2c1f1da8e9792bc0a6be4b028e0ef Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 10 May 2024 11:45:34 -0300 Subject: [PATCH 3/4] feat: adjust code to last api version --- src/generic_app.ts | 21 ++++++++++++--------- tests/integration_generic.test.ts | 31 ++++++++++++------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/generic_app.ts b/src/generic_app.ts index 18e2171..a09aa8e 100644 --- a/src/generic_app.ts +++ b/src/generic_app.ts @@ -89,14 +89,18 @@ export class GenericApp { this.chainId = chainId; } - async getTxMetadata(callData: Buffer, signedExtensions: Buffer): Promise { + async getTxMetadata(txBlob: Buffer): Promise { const resp = await axios.post(this.txMetadataSrvUrl, { - callData: callData.toString("hex"), - signedExtensions: signedExtensions.toString("hex"), + txBlob: txBlob.toString("hex"), chain: { id: this.chainId }, }); - return Buffer.from(resp.data.txMetadata, "hex"); + let txMetadata = resp.data.txMetadata; + if (txMetadata.slice(0, 2) === "0x") { + txMetadata = txMetadata.slice(2); + } + + return Buffer.from(txMetadata, "hex"); } async getVersion(): Promise { @@ -266,9 +270,8 @@ export class GenericApp { } } - 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]); + async sign(account: number, change: number, addressIndex: number, txBlob: Buffer) { + const txMetadata = await this.getTxMetadata(txBlob); return await this.signImpl(account, change, addressIndex, INS.SIGN, txBlob, txMetadata); } @@ -276,7 +279,7 @@ export class GenericApp { return await this.signImpl(account, change, addressIndex, INS.SIGN, txBlob, txMetadata); } - async signRaw(account: number, change: number, addressIndex: number, blob: Buffer) { - return await this.signImpl(account, change, addressIndex, INS.SIGN_RAW, blob); + async signRaw(account: number, change: number, addressIndex: number, txBlob: Buffer) { + return await this.signImpl(account, change, addressIndex, INS.SIGN_RAW, txBlob); } } diff --git a/tests/integration_generic.test.ts b/tests/integration_generic.test.ts index 3d2a0f5..ac0e641 100644 --- a/tests/integration_generic.test.ts +++ b/tests/integration_generic.test.ts @@ -24,14 +24,12 @@ import { supportedApps } from "../src/supported_apps"; import { AxiosError } from "axios"; const TX_METADATA_SRV_URL = "https://api.zondax.ch/polkadot/transaction/metadata"; -const CHAIN_NAME = "Kusama"; -const CHAIN_TICKER = "ksm"; +const CHAIN_NAME = "Polkadot"; +const CHAIN_TICKER = "dot"; const YOUR_PUBKEY = "d280b24dface41f31006e5a2783971fc5a66c862dd7d08f97603d2902b75e47a"; const YOUR_ADDRESS = "HLKocKgeGjpXkGJU6VACtTYJK4ApTCfcGRw51E5jWntcsXv"; -const YOUR_CALL_DATA = - "0000d050f0c8c0a9706b7c0c4e439245a347627901c89d4791239533d1d2c961f1a72ad615c8530de078e565ba644b38b01bcad249e8c0"; -const YOUR_SIGNED_EXTENSIONS = - "a80aceb4befe330990a59f74ed976c933db269c64dda40104a0f001900000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3a071db11cdbfd29285f25d402f1aee7a1c0384269c9c2edb476688d35e346998"; +const YOUR_BLOB = + "0000d050f0c8c0a9706b7c0c4e439245a347627901c89d4791239533d1d2c961f1a72ad615c8530de078e565ba644b38b01bcad249e8c0a80aceb4befe330990a59f74ed976c933db269c64dda40104a0f001900000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3a071db11cdbfd29285f25d402f1aee7a1c0384269c9c2edb476688d35e346998"; let transport = {}; @@ -113,9 +111,8 @@ describe("Integration", function () { // @ts-expect-error transport will be there const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL); - const callData = Buffer.from(YOUR_CALL_DATA, "hex"); - const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex"); - const resp = await app.getTxMetadata(callData, signedExtensions); + const txBlob = Buffer.from(YOUR_BLOB, "hex"); + const resp = await app.getTxMetadata(txBlob); expect(resp).toBeDefined(); }); @@ -124,10 +121,9 @@ describe("Integration", function () { // @ts-expect-error transport will be there const app = newGenericApp(transport, "xxx", TX_METADATA_SRV_URL); - const callData = Buffer.from(YOUR_CALL_DATA, "hex"); - const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex"); + const txBlob = Buffer.from(YOUR_BLOB, "hex"); try { - await app.getTxMetadata(callData, signedExtensions); + await app.getTxMetadata(txBlob); } catch (e: any) { expect(e.response.status).toBe(404); } @@ -137,10 +133,9 @@ describe("Integration", function () { // @ts-expect-error transport will be there const app = newGenericApp(transport, "ksm", ""); - const callData = Buffer.from(YOUR_CALL_DATA, "hex"); - const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex"); + const txBlob = Buffer.from(YOUR_BLOB, "hex"); try { - await app.getTxMetadata(callData, signedExtensions); + await app.getTxMetadata(txBlob); } catch (e: any) { expect(e.code).toBe("ECONNREFUSED"); } @@ -154,9 +149,7 @@ describe("Integration", function () { return; } - const callData = Buffer.from(YOUR_CALL_DATA, "hex"); - const signedExtensions = Buffer.from(YOUR_SIGNED_EXTENSIONS, "hex"); - const txBlob = Buffer.concat([callData, signedExtensions]); + const txBlob = Buffer.from(YOUR_BLOB, "hex"); // @ts-expect-error transport will be there const app = newGenericApp(transport, CHAIN_TICKER, TX_METADATA_SRV_URL); @@ -166,7 +159,7 @@ describe("Integration", function () { const pathIndex = 0x80000000; const responseAddr = await app.getAddress(pathAccount, pathChange, pathIndex, ss58prefix); - const responseSign = await app.sign(pathAccount, pathChange, pathIndex, callData, signedExtensions); + const responseSign = await app.sign(pathAccount, pathChange, pathIndex, txBlob); const pubkey = responseAddr.pubKey; From 36f152f734337dcb67fbd7383b68c83e737781c8 Mon Sep 17 00:00:00 2001 From: emmanuelm41 Date: Fri, 10 May 2024 11:57:03 -0300 Subject: [PATCH 4/4] feat: rename classes and functions --- src/generic_app.ts | 34 +++++++++++++++++-------------- src/supported_apps.ts | 4 ++-- tests/integration_generic.test.ts | 27 +++++++++--------------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/generic_app.ts b/src/generic_app.ts index a09aa8e..f7eb127 100644 --- a/src/generic_app.ts +++ b/src/generic_app.ts @@ -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( @@ -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) { diff --git a/src/supported_apps.ts b/src/supported_apps.ts index 02c411d..b82e6a5 100644 --- a/src/supported_apps.ts +++ b/src/supported_apps.ts @@ -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) => { @@ -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, diff --git a/tests/integration_generic.test.ts b/tests/integration_generic.test.ts index ac0e641..e576b14 100644 --- a/tests/integration_generic.test.ts +++ b/tests/integration_generic.test.ts @@ -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"; @@ -31,7 +31,7 @@ const YOUR_ADDRESS = "HLKocKgeGjpXkGJU6VACtTYJK4ApTCfcGRw51E5jWntcsXv"; const YOUR_BLOB = "0000d050f0c8c0a9706b7c0c4e439245a347627901c89d4791239533d1d2c961f1a72ad615c8530de078e565ba644b38b01bcad249e8c0a80aceb4befe330990a59f74ed976c933db269c64dda40104a0f001900000091b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3a071db11cdbfd29285f25d402f1aee7a1c0384269c9c2edb476688d35e346998"; -let transport = {}; +let transport: Transport; jest.setTimeout(60000); @@ -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); @@ -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; @@ -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; @@ -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); @@ -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 { @@ -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 { @@ -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;