Small Ledger process dedupe (#1774)
* Small Ledger process dedupe * Add yarn.lock
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"],
|
||||
|
||||
Reference in New Issue
Block a user