Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Adjust CLI options, fix public key caching
Browse files Browse the repository at this point in the history
Changed usage:

    near ... --useLedgerKey
    near create_account new_account_name --newLedgerKey --useLedgerKey --masterAccount account_that_creates
  • Loading branch information
vgrichina committed May 21, 2020
1 parent 1df2f9c commit e934d22
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
9 changes: 2 additions & 7 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,8 @@ yargs // eslint-disable-line
desc: 'Unique identifier for the account',
type: 'string',
})
.option('useLedger', {
desc: 'Use Ledger for signing',
type: 'boolean',
default: false
})
.option('hdKeyPath', {
desc: 'HD key path to use with Ledger',
.option('useLedgerKey', {
desc: 'Use Ledger for signing with given HD key path',
type: 'string',
default: "44'/397'/0'/0'/1'"
})
Expand Down
2 changes: 1 addition & 1 deletion commands/create-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
type: 'string',
required: false
})
.option('newHdKeyPath', {
.option('newLedgerKey', {
desc: 'HD key path to use with Ledger. Used to generate public key if not specified directly',
type: 'string',
default: "44'/397'/0'/0'/1'"
Expand Down
27 changes: 14 additions & 13 deletions middleware/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@ const { utils: { PublicKey, key_pair: { KeyType } } } = require('near-api-js');
const { createClient } = require('near-ledger-js');
const { default: TransportNodeHid } = require('@ledgerhq/hw-transport-node-hid');

// near ... --useLedger --hdKeyPath
// near create_account new_account_name --newHdKeyPath --useLedger --hdKeyPath --masterAccount account_that_creates
module.exports = async function useLedgerSigner({ useLedger, hdKeyPath, newHdKeyPath, publicKey }) {
if (!useLedger) {
// near ... --useLedgerKey
// near create_account new_account_name --newLedgerKey --useLedgerKey --masterAccount account_that_creates
module.exports = async function useLedgerSigner({ useLedgerKey: ledgerKeyPath, newLedgerKey, publicKey }) {
if (!ledgerKeyPath) {
return;
}

console.log('Make sure to connect your Ledger and open NEAR app');
const transport = await TransportNodeHid.create();
const client = await createClient(transport);

let cachedPublicKeys = {};
async function getPublicKeyForPath(hdKeyPath) {
// NOTE: Public key is cached to avoid confirming on Ledger multiple times
if (cachedPublicKeys[ledgerKeyPath]) {
return cachedPublicKeys[hdKeyPath];
}

console.log('Waiting for confirmation on Ledger...');
const rawPublicKey = await client.getPublicKey(hdKeyPath);
const publicKey = new PublicKey({ keyType: KeyType.ED25519, data: rawPublicKey });
cachedPublicKeys[hdKeyPath] = publicKey;
console.log('Using public key:', publicKey.toString());
return publicKey;
}

let cachedPublicKey;
let signer = {
async getPublicKey() {
// NOTE: Public key is cached to avoid confirming on Ledger multiple times
if (!cachedPublicKey) {
cachedPublicKey = await getPublicKeyForPath(hdKeyPath);
}
return cachedPublicKey;
return getPublicKeyForPath(ledgerKeyPath);
},
async signMessage(message) {
const publicKey = await this.getPublicKey();
Expand All @@ -38,9 +40,8 @@ module.exports = async function useLedgerSigner({ useLedger, hdKeyPath, newHdKey
}
};

if (newHdKeyPath) {
publicKey = await getPublicKeyForPath(hdKeyPath);
console.log('publicKey', publicKey);
if (newLedgerKey) {
publicKey = await getPublicKeyForPath(newLedgerKey);
}

return { signer, publicKey };
Expand Down

0 comments on commit e934d22

Please sign in to comment.