Class: TrezorInteraction

trezor.TrezorInteraction(options)

Base class for interactions with Trezor hardware wallets.

Assumes we are using TrezorConnect to talk to the device.

Subclasses must implement a method this.connectParams which returns a 2-element array. The first element of this array should be a TrezorConnect method to use (e.g. - TrezorConnect.getAddress). The second element of this array should be the parameters to pass to the given TrezorConnect method.

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

Unsuccessful responses (the request succeeded but the Trezor device returned an error message) are intercepted and thrown as errors. This allows upstream try...catch blocks to intercept errors & failures uniformly.

Subclasses may implement the parse(payload) method which accepts the response payload object and returns the relevant data.

Subclasses will also want to implement a messages() method to manipulate the messages returned to the user for each interaction.

Constructor

new TrezorInteraction(options)

Trezor interactions require knowing the bitcoin network they are for.

Parameters:
Name Type Description
options object

options argument

Properties
Name Type Description
network string

bitcoin network

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

class SimpleTrezorInteraction extends TrezorInteraction {

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

  connectParams() {
    return [
      TrezorConnect.doSomething, // Not a real TrezorConnect function...
      {
        // Many Trezor methods require the `coin` parameter.  The
        // value of `this.trezorCoin` is set appropriately based on the
        // `network` provided in the constructor.
        coin: this.trezorCoin,

        // Pass whatever arguments are required
        // by the TrezorConnect function being called.
        param: this.param,
        // ...
      }
    ];
  }

  parsePayload(payload) {
    return payload.someValue;
  }

}
// usage
import {MAINNET} from "unchained-bitcoin";
const interaction = new SimpleTrezorInteraction({network: MAINNET, param: "foo"});
const result = await interaction.run();
console.log(result); // someValue from payload

Extends

Methods

connectParams() → {Array.<function(), Object>}

Override this method in a subclass to return a 2-element array.

The first element should be a functin to call, typically a TrezorConnect method, e.g. TrezorConnect.getAddress.

The second element should be the parameters to pass to this function.

By default, the function passed just throws an error.

Source:
Returns:

the TrezorConnect parameters

Type
Array.<function(), Object>

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

Default messages are added asking the user to plug in their Trezor device (device.connect) and about the TrezorConnect popups (trezor.connect.generic).

Subclasses should override this method and add their own messages (don't forget to call super()).

Source:
Returns:

messages for this interaction

Type
Array.<module:interaction.Message>

parse() → {void}

Throws an error.

Overrides:
Source:
Throws:

An error since this is a direct interaction.

Returns:
Type
void

parsePayload(payload) → {Object}

Override this method in a subclass to parse the payload of a successful response from the device.

By default, the entire payload is returned.

Parameters:
Name Type Description
payload Object

the raw payload from the device response

Source:
Returns:
  • relevant or formatted data built from the raw payload
Type
Object

request() → {void}

Throws an error.

Overrides:
Source:
Throws:

An error since this is a direct interaction.

Returns:
Type
void

(async) run() → {Promise}

Awaits the call of this.method, passing in the output of this.params().

If the call returns but is unsuccessful (result.success) is false, will throw the returned error message. If some other error is thrown, it will not be caught.

Otherwise it returns the result of passing result.payload to this.parsePayload.

Overrides:
Source:
Returns:

handles the work of calling TrezorConnect

Type
Promise