Socket
Socket
Sign inDemoInstall

@ledgerhq/hw-transport

Package Overview
Dependencies
7
Maintainers
21
Versions
347
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ledgerhq/hw-transport

Ledger Hardware Wallet common interface of the communication layer


Version published
Weekly downloads
123K
decreased by-21.2%
Maintainers
21
Install size
5.47 MB
Created
Weekly downloads
 

Readme

Source

GitHub, Ledger Devs Discord, Developer Portal

@ledgerhq/hw-transport

@ledgerhq/hw-transport implements the generic interface of a Ledger Hardware Wallet transport.

API

Table of Contents

Subscription

Type: {unsubscribe: function (): void}

Properties
  • unsubscribe function (): void

Device

Type: any

DescriptorEvent

A "descriptor" is a parameter that is specific to the implementation, and can be an ID, file path, or URL. type: add or remove event descriptor: a parameter that can be passed to open(descriptor) deviceModel: device info on the model (is it a nano s, nano x, ...) device: transport specific device info

Observer

Observer generic type, following the Observer pattern

Type: Readonly<{next: function (event: EventType): any, error: function (e: EventError): any, complete: function (): any}>

Transport

The Transport class defines a generic interface for communicating with a Ledger hardware wallet. There are different kind of transports based on the technology (channels like U2F, HID, Bluetooth, Webusb) and environment (Node, Web,...). It is an abstract class that needs to be implemented.

exchange

Send data to the device using a low level API. It's recommended to use the "send" method for a higher level API.

Parameters

Returns Promise<Buffer> A promise that resolves with the response data from the device.

exchangeBulk

Send apdus in batch to the device using a low level API. The default implementation is to call exchange for each apdu.

Parameters
  • apdus Array<Buffer> array of apdus to send.
  • observer Observer<Buffer> an observer that will receive the response of each apdu.

Returns Subscription A Subscription object on which you can call ".unsubscribe()" to stop sending apdus.

setScrambleKey

Set the "scramble key" for the next data exchanges with the device. Each app can have a different scramble key and it is set internally during instantiation.

Parameters

Meta

  • deprecated: This method is no longer needed for modern transports and should be migrated away from.
close

Close the connection with the device.

Returns Promise<void> A promise that resolves when the transport is closed.

on

Listen for an event on the transport instance. Transport implementations may have specific events. Common events include: "disconnect" : triggered when the transport is disconnected.

Parameters
  • eventName string The name of the event to listen for.
  • cb function (...args: Array<any>): any

Returns void

off

Stop listening to an event on an instance of transport.

Parameters

Returns void

setDebugMode

Enable or not logs of the binary exchange

setExchangeTimeout

Set a timeout (in milliseconds) for the exchange call. Only some transport might implement it. (e.g. U2F)

Parameters

Returns void

setExchangeUnresponsiveTimeout

Define the delay before emitting "unresponsive" on an exchange that does not respond

Parameters

Returns void

send

Send data to the device using the higher level API.

Parameters
  • cla number The instruction class for the command.
  • ins number The instruction code for the command.
  • p1 number The first parameter for the instruction.
  • p2 number The second parameter for the instruction.
  • data Buffer The data to be sent. Defaults to an empty buffer. (optional, default Buffer.alloc(0))
  • statusList Array<number> A list of acceptable status codes for the response. Defaults to [StatusCodes.OK]. (optional, default [StatusCodes.OK])

Returns Promise<Buffer> A promise that resolves with the response data from the device.

isSupported

Check if the transport is supported on the current platform/browser.

Type: function (): Promise<boolean>

Returns Promise<boolean> A promise that resolves with a boolean indicating support.

list

List all available descriptors for the transport. For a better granularity, checkout listen().

Type: function (): Promise<Array<any>>

Examples
TransportFoo.list().then(descriptors => ...)

Returns Promise<Array<any>> A promise that resolves with an array of descriptors.

listen

Listen for device events for the transport. The method takes an observer of DescriptorEvent and returns a Subscription. A DescriptorEvent is an object containing a "descriptor" and a "type" field. The "type" field can be "add" or "remove", and the "descriptor" field can be passed to the "open" method. The "listen" method will first emit all currently connected devices and then will emit events as they occur, such as when a USB device is plugged in or a Bluetooth device becomes discoverable.

Type: function (observer: Observer<DescriptorEvent<any>>): Subscription

Parameters
  • observer Observer<DescriptorEvent<any>> An object with "next", "error", and "complete" functions, following the observer pattern.
Examples
const sub = TransportFoo.listen({
next: e => {
if (e.type==="add") {
sub.unsubscribe();
const transport = await TransportFoo.open(e.descriptor);
...
}
},
error: error => {},
complete: () => {}
})

Returns Subscription A Subscription object on which you can call ".unsubscribe()" to stop listening to descriptors.

open

Attempt to create a Transport instance with a specific descriptor.

Type: function (descriptor: any, timeout: number): Promise<Transport>

Parameters
  • descriptor any The descriptor to open the transport with.
  • timeout number An optional timeout for the transport connection.
Examples
TransportFoo.open(descriptor).then(transport => ...)

Returns Promise<Transport> A promise that resolves with a Transport instance.

create

create() allows to open the first descriptor available or throw if there is none or if timeout is reached. This is a light helper, alternative to using listen() and open() (that you may need for any more advanced usecase)

Parameters
  • openTimeout (optional, default 3000)
  • listenTimeout number?
Examples
TransportFoo.create().then(transport => ...)

Returns Promise<Transport>

Keywords

FAQs

Last updated on 16 Aug 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc