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

Commit

Permalink
Move seed phrase into a middleware to allow other actions to work wit…
Browse files Browse the repository at this point in the history
…h it
  • Loading branch information
ilblackdragon committed Sep 15, 2020
1 parent ac854a4 commit 9f310e7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 29 deletions.
12 changes: 12 additions & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ yargs // eslint-disable-line
type: 'string',
default: "44'/397'/0'/0'/1'"
})
.options('seedPhrase', {
desc: 'Seed phrase mnemonic',
type: 'string',
required: false
})
.options('seedPath', {
desc: 'HD path derivation',
type: 'string',
default: "m/44'/397'/0'",
required: false
})
.option('walletUrl', {
desc: 'Website for NEAR Wallet',
type: 'string'
Expand Down Expand Up @@ -212,6 +223,7 @@ yargs // eslint-disable-line
.middleware(require('../middleware/print-options'))
.middleware(require('../middleware/key-store'))
.middleware(require('../middleware/ledger'))
.middleware(require('../middleware/seed-phrase'))
.command(require('../commands/create-account').createAccountCommand)
.command(require('../commands/create-account').createAccountCommandDeprecated)
.command(viewAccount)
Expand Down
38 changes: 10 additions & 28 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
const { decode } = require('bs58');
const KeyPair = require('near-api-js').KeyPair;
const exitOnError = require('../utils/exit-on-error');
const { parseSeedPhrase } = require('near-seed-phrase');

function implicitAccountId(publicKey) {
return decode(publicKey.replace('ed25519:', '')).toString('hex');
}
const implicitAccountId = require('../utils/implicit-accountid');

module.exports = {
command: 'generate-key [account-id]',
desc: 'generate key or show key from Ledger',
builder: (yargs) => yargs
.options('seed-phrase', {
desc: 'Seed phrase mnemonic',
type: 'string',
required: false
})
.options('seed-path', {
desc: 'HD path derivation',
type: 'string',
default: "m/44'/397'/0'",
required: false
}),
builder: (yargs) => yargs,
handler: exitOnError(async (argv) => {
let near = await require('../utils/connect')(argv);

Expand All @@ -44,17 +28,15 @@ module.exports = {
return;
}

let publicKey, accountId;
if (argv.seedPhrase) {
const result = parseSeedPhrase(argv.seedPhrase, argv.seedPath);
publicKey = result.publicKey;
accountId = argv.accountId || implicitAccountId(publicKey);
} else {
// If key doesn't exist, create one and store in the keyStore.
// Otherwise, it's expected that both key and accountId are already provided in arguments.
if (!argv.publicKey) {
const keyPair = KeyPair.fromRandom('ed25519');
publicKey = keyPair.publicKey.toString();
accountId = argv.accountId || implicitAccountId(publicKey);
await keyStore.setKey(argv.networkId, accountId, keyPair);
argv.publicKey = keyPair.publicKey.toString();
argv.accountId = argv.accountId || implicitAccountId(argv.publicKey);
await keyStore.setKey(argv.networkId, argv.accountId, keyPair);
}
console.log(`Key pair with ${publicKey} public key for account "${accountId}"`);

console.log(`Key pair with ${argv.publicKey} public key for an account "${argv.accountId}"`);
})
};
23 changes: 23 additions & 0 deletions middleware/seed-phrase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { parseSeedPhrase } = require('near-seed-phrase');
const { utils: { KeyPair }, InMemorySigner } = require('near-api-js');
const { InMemoryKeyStore } = require('near-api-js/lib/key_stores');

const implicitAccountId = require('../utils/implicit-accountid');

// near ... --seedPhrase="phrase" --seedPath="m/44'/397'/0'"
// near generate-key --seedPhrase="phrase"
module.exports = async function useSeedPhrase({ seedPhrase, seedPath, publicKey, accountId }, yargs) {
if (!seedPhrase) {
return;
}
if (yargs.usingLedger) {
throw new Error('Can not use both --useLedgerKey and --seedPhrase at the same time');
}
const result = parseSeedPhrase(seedPhrase, seedPath);
publicKey = result.publicKey;
let keyStore = new InMemoryKeyStore();
accountId = accountId || implicitAccountId(publicKey);
await keyStore.setKey(yargs.networkId, accountId, KeyPair.fromString(result.secretKey));
let signer = new InMemorySigner(keyStore);
return { signer, publicKey, accountId };
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"mixpanel": "^0.11.0",
"ncp": "^2.0.0",
"near-api-js": "^0.29.1",
"near-seed-phrase": "^0.0.2",
"near-seed-phrase": "https://github.com/near/near-seed-phrase#8504106e5e9cb65d6cdfa61c57b6b7cb91812c1a",
"open": "^7.0.1",
"rimraf": "^3.0.0",
"stoppable": "^1.1.0",
Expand Down
5 changes: 5 additions & 0 deletions utils/implicit-accountid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { decode } = require('bs58');

module.exports = (publicKey) => {
return decode(publicKey.replace('ed25519:', '')).toString('hex');
};

0 comments on commit 9f310e7

Please sign in to comment.