Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add speculos #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ to control the log output. Possible log levels include: `none`, `info`,
`warning`, `debug`, `error`, `spam`.


>Note: the end-to-end tests require a connected Ledger Nano S using the
>Note: the end-to-end tests require a connected Ledger Nano using the
seed phrase:
```
abandon abandon abandon abandon abandon abandon
abandon abandon abandon abandon abandon about
```

#### Using Ledger Nano S
#### Using Ledger Nano
```bash
$ npm run test-hid
```

#### Using Ledger Nano S and hsd
#### Using Ledger Nano and hsd
```bash
$ npm run test-hsd
```
Expand Down
42 changes: 28 additions & 14 deletions bin/hsd-ledger
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
const Config = require('bcfg');
const {NodeClient, WalletClient} = require('hs-client');
const {hd, MTX, Network} = require('hsd');
const {util, USB, LedgerHSD, LedgerChange} = require('..');
const {Device} = USB;
const {util, HID, Speculos, LedgerHSD, LedgerChange} = require('..');

/**
* Global constants
Expand Down Expand Up @@ -167,13 +166,6 @@ async function getBalance(client, config, args) {
}

async function main() {
const device = await Device.requestDevice();
device.set({
timeout: 300000 // 5 minutes.
});

await device.open();

const config = new Config('hsd', {
suffix: 'network',
fallback: 'main',
Expand Down Expand Up @@ -210,16 +202,38 @@ async function main() {
const id = config.str('wallet-id');
const token = config.str('token');

if(config.str('help') && argv.length === 0) {
if (config.str('help') && argv.length === 0) {
usage();
process.exit(0);
}

if(config.str('version') && argv.length === 0) {
if (config.str('version') && argv.length === 0) {
version();
process.exit(0);
}

const deviceType = config.str('device', 'hid');
let device;

switch (deviceType) {
case 'hid':
device = await HID.Device.requestDevice();
break;

case 'speculos':
device = await Speculos.Device.requestDevice();
break;

default:
usage(new Error('Invalid device type.'));
process.exit(1);
}

device.set({
timeout: 300000 // 5 minutes.
});
await device.open();

const ledger = new LedgerHSD({
device: device,
network: type
Expand Down Expand Up @@ -293,10 +307,9 @@ async function main() {
default:
usage(new Error('Must provide valid command.'));
process.exit(1);
break;
}
} catch(e) {
throw(e);
} catch (e) {
throw (e);
} finally {
await wclient.close();
await nclient.close();
Expand Down Expand Up @@ -340,6 +353,7 @@ function usage(err) {
console.log('options:');
console.log(' --help');
console.log(' --version');
console.log(' --device <type> ({"hid", "speculos"}, default "hid")');
console.log(' -n, --network <id> (default "main")');
console.log(' -w, --wallet-id <id> (default "hsd-ledger")');
console.log(' -a, --account-name <name> (default "default")');
Expand Down
35 changes: 20 additions & 15 deletions docs/cli.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# How to use Ledger Nano S with Handshake
# How to use Ledger Nano with Handshake

The [hsd-ledger][repo] client library includes a CLI application that can
connect to an [HSD][hsd] wallet/node server and the Ledger Handshake app.
This CLI app allows users to manage accounts, view HNS balances, create HNS
addresses, and send HNS. Any addresses created with the app must be verified
on-device. All transactions are signed on the Ledger Nano S and require
on-device. All transactions are signed on the Ledger Nano and require
on-device verification as well.

The CLI app requires Node.js v8.0.0+, access to HSD wallet and node servers,
and access to a Ledger Nano S running the latest firmware version.
and access to a Ledger Nano running the latest firmware version.

## Before you begin

Before you begin this tutorial, be sure:
- You have initialized your Ledger Nano S
- Your Ledger Nano S is running the latest firmware
- You have initialized your Ledger Nano
- Your Ledger Nano is running the latest firmware
- You have installed Ledger Live
- Ledger Live is open and ready to use

## Install the Handshake app on Ledger Nano S
## Install the Handshake app on Ledger Nano

1. Open the Manager in Ledger Live.
2. Connect and unlock your Ledger Nano S.
2. Connect and unlock your Ledger Nano.
3. If asked, allow the manager on your device by pressing the right button.
4. Find Handshake in the app catalog.
5. Click the Install button of the app.
Expand All @@ -32,7 +32,7 @@ Before you begin this tutorial, be sure:
## Download and install the HSD Ledger CLI app

Follow the instructions [here][install] to download and install the CLI app.
Once the CLI app is installed, unlock your Ledger Nano S and open the
Once the CLI app is installed, unlock your Ledger Nano and open the
Handshake app. Also make sure you have access to a running HSD node.
For instructions on setting up an HSD node, please see the official
[documentation][docs].
Expand All @@ -50,6 +50,9 @@ For more information on configuring clients for connection to HSD, please see
the official documentation [here][config]. The flags supported by the HSD
Ledger CLI app are [below](#usage).

To connect to an emulator ([speculos][speculos]) instead of a physical device,
set the device type with `--device speculos`.

<a href="#first"></a>Upon its first run, the CLI app will create a watch-only
wallet using `hsd-ledger` as its wallet-id. This wallet acts as the default
wallet for the app. Any commands that do not specify an alternative wallet will
Expand Down Expand Up @@ -78,6 +81,7 @@ usage:
options:
--help
--version
--device <type> ({"hid", "speculos"}, default "hid")
-n, --network <id> (default "main")
-w, --wallet-id <id> (default "hsd-ledger")
-a, --account-name <name> (default "default")
Expand Down Expand Up @@ -129,7 +133,7 @@ $ cd hsd-ledger
$ npm install
```

Make sure you have your Ledger Nano S connected and unlocked.
Make sure you have your Ledger Nano connected and unlocked.
Also, make sure that you have the Handshake app open.

### Wallets and Accounts
Expand All @@ -150,7 +154,7 @@ $ ./bin/hsd-ledger getwallets -n regtest

If you started a fresh instance of HSD, you will notice there are two wallets
shown: `["primary", "hsd-ledger"]`. The "primary" wallet was generated by HSD
upon startup. However, it does not contain keys from you Ledger Nano S and
upon startup. However, it does not contain keys from you Ledger Nano and
should not be used with the HSD Ledger CLI app. By default, all commands will
use the "hsd-ledger" wallet.

Expand All @@ -167,7 +171,7 @@ You should see the "default" account listed.
#### Create an account

It is possible to create a new account using a different XPUB from your Ledger
Nano S. To create a new account named "second" using the XPUB at account
Nano. To create a new account named "second" using the XPUB at account
index `M/44'/5355'/1'`run:

```bash
Expand Down Expand Up @@ -202,7 +206,7 @@ $ ./bin/hsd-ledger createaddress -n regtest
```

You will be asked to verify that the address shown in the terminal matches the
address shown on your Ledger Nano S.
address shown on your Ledger Nano.

#### Fund your account

Expand Down Expand Up @@ -231,7 +235,7 @@ $ ./bin/hsd-ledger getbalance -n regtest

### HNS Transactions

Now we are ready to send a transaction that is signed by your Ledger Nano S.
Now we are ready to send a transaction that is signed by your Ledger Nano.
Let's send some coins from the "default" account to the "second" account.
First, we need to generate a receiving address for the "second" account.
To generate a new address run:
Expand All @@ -250,13 +254,13 @@ $ ./bin/hsd-ledger sendtoaddress <address> <amount> -n regtest
`<amount>` is the amount in full HNS, e.g. 5 HNS _not_ 5000000 dollarydoos.

You will be asked to verify that the transaction details shown in the terminal
matches the details shown on your Ledger Nano S. If you reject any of the details,
matches the details shown on your Ledger Nano. If you reject any of the details,
the transaction will not be signed or sent to the network.

Once you verify the transaction details, you can mine a block on your HSD node
and check that the balances of the "default" and "second" accounts have been updated.

Congrats! You have signed a valid transaction using your Ledger Nano S.
Congrats! You have signed a valid transaction using your Ledger Nano.

## Final Notes

Expand All @@ -269,3 +273,4 @@ please open an issue on the GitHub [repo][issues].
[issues]: https://github.com/handshake-org/hsd-ledger/issues
[docs]: https://handshake-org.github.io/
[config]: https://handshake-org.github.io/api-docs/index.html#configuring-clients
[speculos]: https://github.com/LedgerHQ/speculos
2 changes: 1 addition & 1 deletion lib/device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const VENDOR_ID = 0x2c97;
* @property {Number} timeout
* @property {blgr.Logger?} logger
* @property {Buffer} scrambleKey
* @property {String} type - ['usb']
* @property {String} type - ['usb', 'hid', 'speculos']
* @property {Number} productId
* @property {Number} vendorId
* @property {String} productName
Expand Down
Loading