diff --git a/packages/hw-ledger/src/bundle.ts b/packages/hw-ledger/src/bundle.ts index 7ef554878..f32aa7210 100644 --- a/packages/hw-ledger/src/bundle.ts +++ b/packages/hw-ledger/src/bundle.ts @@ -36,22 +36,21 @@ async function wrapError (promise: Promise): Promis export class Ledger { #app: SubstrateApp | null = null; - #chain: Chain; + #ledgerName: string; #transportDef: TransportDef; constructor (transport: TransportType, chain: Chain) { - if (!Object.keys(ledgerApps).includes(chain)) { - throw new Error(`Unsupported chain ${chain}`); - } - + const ledgerName = ledgerApps[chain]; const transportDef = transports.find(({ type }) => type === transport); - if (!transportDef) { - throw new Error(`Unsupported transport ${transport}`); + if (!ledgerName) { + throw new Error(`Unsupported Ledger chain ${chain}`); + } else if (!transportDef) { + throw new Error(`Unsupported Ledger transport ${transport}`); } - this.#chain = chain; + this.#ledgerName = ledgerName; this.#transportDef = transportDef; } @@ -102,28 +101,18 @@ export class Ledger { /** * @internal * - * Retrieves the Ledger app that we are operating with - */ - async getApp (): Promise { - if (!this.#app) { - const transport = await this.#transportDef.create(); - - this.#app = newSubstrateApp(transport, ledgerApps[this.#chain]); - } - - return this.#app; - } - - /** - * @internal - * - * Performs an operation against an app, with error code wrapping + * Returns a created SubstrateApp to perform operations against. Generally + * this is only used internally, to ensure consistent bahavior. */ async withApp (fn: (app: SubstrateApp) => Promise): Promise { try { - const app = await this.getApp(); + if (!this.#app) { + const transport = await this.#transportDef.create(); - return await fn(app); + this.#app = newSubstrateApp(transport, this.#ledgerName); + } + + return await fn(this.#app); } catch (error) { this.#app = null;