Class: LedgerInteraction

ledger.LedgerInteraction()

Base class for interactions with Ledger hardware wallets.

Subclasses must implement their own run() method. They may use the withTransport and withApp methods to connect to the Ledger API's transport or app layers, respectively.

Errors are not caught, so users of this class (and its subclasses) should use try...catch as always.

Constructor

new LedgerInteraction()

Source:
Example
import {LedgerInteraction} from "unchained-wallets";
// Simple subclass

class SimpleLedgerInteraction extends LedgerInteraction {

  constructor({param}) {
    super({});
    this.param =  param;
  }

  async run() {
    return await this.withApp(async (app, transport) => {
      return app.doSomething(this.param); // Not a real Ledger API call
    });
  }

}

// usage
const interaction = new SimpleLedgerInteraction({param: "foo"});
const result = await interaction.run();
console.log(result); // whatever value `app.doSomething(...)` returns

Extends

Methods

closeTransport() → {Promise}

Close the Transport to free the interface (E.g. could be used in another tab now that the interaction is over)

The way the pubkey/xpub/fingerprints are grabbed makes this a little tricky. Instead of re-writing how that works, let's just add a way to explicitly close the transport.

Source:
Returns:
  • promise to close the transport
Type
Promise

messages() → {Array.<module:interaction.Message>}

Adds pending messages at the info level about ensuring the device is plugged in (device.connect) and unlocked (device.unlock). Adds an active message at the info level when communicating with the device (device.active).

Source:
Returns:

messages for ths interaction

Type
Array.<module:interaction.Message>

parse() → {void}

Throws an error.

Inherited From:
Source:
Throws:

An error since this is a direct interaction.

Returns:
Type
void

request() → {void}

Throws an error.

Inherited From:
Source:
Throws:

An error since this is a direct interaction.

Returns:
Type
void

(async) run() → {Promise}

Initiate the intended interaction and return a result.

Subclasses must override this function. This function must always return a promise as it is designed to be called within an await block.

Inherited From:
Source:
Returns:

Does the work of interacting with the keystore.

Type
Promise

withApp(callback) → {Promise}

Can be called by a subclass during its run() method.

Creates a transport layer connection, initializes a bitcoin app object, and passes control to the callback function, with the app API as the first argument to the function and the transport API as the second.

See the Ledger API for general information or the bitcoin app API for examples of API calls.

Parameters:
Name Type Description
callback function
  • accepts two parameters, app and transport, which are the Ledger APIs for the bitcoin app and the transport layer, respectively.
Source:
Returns:

does the work of setting up an app instance (and transport connection)

Type
Promise
Example
async run() {
  return await this.withApp(async (app, transport) => {
    return app.doSomething(); // Not a real Ledger bitcoin app API call
  });
}

(async) withTransport(callback) → {Promise}

Can be called by a subclass during its run() method.

Creates a transport layer connection and passes control to the callback function, with the transport API as the first argument to the function.

See the Ledger API for general information or a specific transport API for examples of API calls.

Parameters:
Name Type Description
callback function
  • asynchronous function accepting a single parameter transport
Source:
Returns:

does the work of setting up a transport connection

Type
Promise
Example
async run() {
  return await this.withTransport(async (transport) => {
    return transport.doSomething(); // Not a real Ledger transport API call
  });
}