<img src="https://user-images.githubusercontent.com/4631227/191834116-59cf590e-25cc-4956-ae5c-812ea464f324.png" height="100" />

## @ledgerhq/hw-transport-node-speculos

> \[!NOTE]
> **Status: STABLE** — Production-ready; API is considered stable.

A transport for <https://github.com/LedgerHQ/speculos> Nano simulator.

[GitHub](https://github.com/LedgerHQ/ledger-live/),
[Ledger Devs Discord](https://developers.ledger.com/discord-pro),
[Developer Portal](https://developers.ledger.com/)

### Getting started

*   Install <https://github.com/LedgerHQ/speculos>
*   Make sure to have a speculos running with an APDU port and (optionally) a buttons port available.

```js
import SpeculosTransport from "@ledgerhq/hw-transport-node-speculos";

const apduPort = 40000;

async function exampleSimple() {
  const transport = await SpeculosTransport.open({ apduPort });
  const res = await transport.send(0xE0, 0x01, 0x00, 0x00);
}

async function exampleAdvanced() {
  const transport = await SpeculosTransport.open({ apduPort });
  setTimeout(() => {
    // in 1s i'll click on right button and release
    transport.button("Rr");
  }, 1000); // 1s is a tradeoff here. In future, we need to be able to "await & expect a text" but that will need a feature from speculos to notify us when text changes.
  // derivate btc address and ask for device verification
  const res = await transport.send(0xE0, 0x40, 0x01, 0x00, Buffer.from("058000002c8000000080000000000000000000000f"));
}
```

### With ledger-live CLI

The `ledger-live` CLI (`@ledgerhq/live-cli`) is Speculos-only internal test/dev plumbing.
It works with the SPECULOS\_APDU\_PORT and SPECULOS\_HOST envs.

```sh
SPECULOS_APDU_PORT=40000 ledger-live liveData -c btc
```

To expose a Speculos device over HTTP (e.g. to drive it from LWD/LWM), use `pnpm apdu-proxy`.

For real-device flows, use the Ledger Live desktop or mobile app, or `@ledgerhq/wallet-cli`.

To make it work with Docker, I had to expose some port and do this:

```sh
docker run -it -p 40000:40000 -v "$(pwd)"/apps:/speculos/apps speculos /bin/bash

$ pipenv shell
$ ./speculos.py -m nanos ./apps/btc.elf --sdk 1.6 --seed "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" --display headless --apdu-port 40000
```

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

*   [SpeculosTransportOpts](#speculostransportopts)
    *   [Properties](#properties)
*   [SpeculosTransport](#speculostransport)
    *   [Parameters](#parameters)
    *   [Examples](#examples)
    *   [button](#button)
        *   [Parameters](#parameters-1)
    *   [open](#open)
        *   [Parameters](#parameters-2)

### SpeculosTransportOpts

Type: {apduPort: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number), buttonPort: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?, automationPort: [number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?, host: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?}

#### Properties

*   `apduPort` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;
*   `buttonPort` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**&#x20;
*   `automationPort` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**&#x20;
*   `host` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?**&#x20;

### SpeculosTransport

**Extends Transport**

Speculos TCP transport implementation

#### Parameters

*   `apduSocket` **net.Socket**&#x20;
*   `opts` **[SpeculosTransportOpts](#speculostransportopts)**&#x20;

#### Examples

```javascript
import SpeculosTransport from "@ledgerhq/hw-transport-node-speculos";
const transport = await SpeculosTransport.open({ apduPort });
const res = await transport.send(0xE0, 0x01, 0, 0);
```

#### button

Send a speculos button command
typically "Ll" would press and release the left button
typically "Rr" would press and release the right button

##### Parameters

*   `command` **any**&#x20;

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<void>**&#x20;

#### open

##### Parameters

*   `opts` **[SpeculosTransportOpts](#speculostransportopts)**&#x20;

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[SpeculosTransport](#speculostransport)>**&#x20;
