Small Ledger process dedupe (#1774)

* Small Ledger process dedupe

* Add yarn.lock
This commit is contained in:
Jaco
2023-03-24 10:08:45 +02:00
committed by GitHub
parent 94692cbbb4
commit 803edbb8cc
7 changed files with 19 additions and 23 deletions

View File

@@ -1,7 +1,8 @@
// Copyright 2017-2023 @polkadot/hw-ledger authors & contributors
// SPDX-License-Identifier: Apache-2.0
export type LedgerTypes = 'hid' | 'u2f' | 'webusb';
// u2f is deprecated an therefore not added
export type TransportType = 'hid' | 'webusb';
// The actual namespaced Transport interface, imported via
//
@@ -19,5 +20,5 @@ export interface TransportDef {
/** Create a transport to be used in Ledger operations */
create (): Promise<Transport>;
/** The type of the underlying transport definition */
type: LedgerTypes;
type: TransportType;
}

View File

@@ -1,9 +1,9 @@
// Copyright 2017-2023 @polkadot/hw-ledger authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { LedgerTypes, Transport, TransportDef } from './types.js';
import type { Transport, TransportDef, TransportType } from './types.js';
export function createDefs (...items: readonly [type: LedgerTypes, Clazz: unknown][]): TransportDef[] {
export function createDefs (...items: readonly [type: TransportType, Clazz: unknown][]): TransportDef[] {
return items.map(([type, Clazz]): TransportDef => ({
create: (): Promise<Transport> =>
(Clazz as Pick<TransportDef, 'create'>).create(),

View File

@@ -20,7 +20,6 @@
"version": "11.1.2-1-x",
"main": "index.js",
"dependencies": {
"@ledgerhq/hw-transport": "^6.28.1",
"@polkadot/hw-ledger-transports": "11.1.2-1-x",
"@polkadot/util": "11.1.2-1-x",
"@zondax/ledger-substrate": "^0.40.6",

View File

@@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
import type { SubstrateApp } from '@zondax/ledger-substrate';
import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerTypes, LedgerVersion } from './types.js';
import type { TransportDef, TransportType } from '@polkadot/hw-ledger-transports/types';
import type { AccountOptions, LedgerAddress, LedgerSignature, LedgerVersion } from './types.js';
import { newSubstrateApp } from '@zondax/ledger-substrate';
@@ -37,18 +38,21 @@ export class Ledger {
#chain: Chain;
#transport: LedgerTypes;
#transportDef: TransportDef;
constructor (transport: LedgerTypes, chain: Chain) {
// u2f is deprecated
if (!['hid', 'webusb'].includes(transport)) {
throw new Error(`Unsupported transport ${transport}`);
} else if (!Object.keys(ledgerApps).includes(chain)) {
constructor (transport: TransportType, chain: Chain) {
if (!Object.keys(ledgerApps).includes(chain)) {
throw new Error(`Unsupported chain ${chain}`);
}
const transportDef = transports.find(({ type }) => type === transport);
if (!transportDef) {
throw new Error(`Unsupported transport ${transport}`);
}
this.#chain = chain;
this.#transport = transport;
this.#transportDef = transportDef;
}
/**
@@ -102,13 +106,7 @@ export class Ledger {
*/
async getApp (): Promise<SubstrateApp> {
if (!this.#app) {
const def = transports.find(({ type }) => type === this.#transport);
if (!def) {
throw new Error(`Unable to find a transport for ${this.#transport}`);
}
const transport = await def.create();
const transport = await this.#transportDef.create();
this.#app = newSubstrateApp(transport, ledgerApps[this.#chain]);
}

View File

@@ -3,8 +3,6 @@
import type { HexString } from '@polkadot/util/types';
export type LedgerTypes = 'hid' | 'u2f' | 'webusb';
export interface AccountOptions {
/** The index of the account */
account: number;

View File

@@ -8,6 +8,7 @@
"@polkadot/hw-ledger/*": ["hw-ledger/src/*.ts"],
"@polkadot/hw-ledger-transports": ["hw-ledger-transports/src/browser.ts"],
"@polkadot/hw-ledger-transports/packageInfo": ["hw-ledger-transports/src/packageInfo.ts"],
"@polkadot/hw-ledger-transports/types": ["hw-ledger-transports/src/types.ts"],
"@polkadot/hw-ledger-transports/*": ["hw-ledger-transports/src/browser.ts"],
"@polkadot/keyring": ["keyring/src/index.ts"],
"@polkadot/keyring/packageInfo": ["keyring/src/packageInfo.ts"],

View File

@@ -599,7 +599,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@polkadot/hw-ledger@workspace:packages/hw-ledger"
dependencies:
"@ledgerhq/hw-transport": ^6.28.1
"@polkadot/hw-ledger-transports": 11.1.2-1-x
"@polkadot/util": 11.1.2-1-x
"@zondax/ledger-substrate": ^0.40.6