Skip to content

Commit

Permalink
Adding options to provide seed phrase and show implicit account when …
Browse files Browse the repository at this point in the history
…not provided
  • Loading branch information
ilblackdragon committed Sep 7, 2020
1 parent 7aa1711 commit 71e2cea
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 15 deletions.
48 changes: 37 additions & 11 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
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');
}

module.exports = {
command: 'generate-key <account-id>',
desc: 'generate key ',
builder: (yargs) => yargs,
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
}),
handler: exitOnError(async (argv) => {
let near = await require('../utils/connect')(argv);
if (!argv.accountId) {
return;
}

if (argv.usingLedger) {
await argv.signer.getPublicKey();
// NOTE: Command above already prints public key
const publicKey = await argv.signer.getPublicKey();
// NOTE: Command above already prints public key.
console.log(`Implicit account: ${implicitAccountId(publicKey.toString())}`);
// TODO: query all accounts with this public key here.
// TODO: check if implicit account exist, and if the key doen't match already.
return;
}

Expand All @@ -24,8 +41,17 @@ module.exports = {
return;
}

const keyPair = KeyPair.fromRandom('ed25519');
await keyStore.setKey(argv.networkId, argv.accountId, keyPair);
console.log(`Generated key pair with ${keyPair.publicKey} public key`);
let publicKey, accountId;
if (argv.seedPhrase) {
const result = parseSeedPhrase(argv.seedPhrase, argv.seedPath);
publicKey = result.publicKey;
accountId = argv.accountId || implicitAccountId(publicKey);
} else {
const keyPair = KeyPair.fromRandom('ed25519');
publicKey = keyPair.publicKey.toString();
accountId = argv.accountId || implicitAccountId(publicKey);
await keyStore.setKey(argv.networkId, accountId, keyPair);
}
console.log(`Key pair with ${publicKey} public key for account "${accountId}"`);
})
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"mixpanel": "^0.11.0",
"ncp": "^2.0.0",
"near-api-js": "^0.29.0",
"near-seed-phrase": "^0.0.2",
"open": "^7.0.1",
"rimraf": "^3.0.0",
"stoppable": "^1.1.0",
Expand Down
146 changes: 142 additions & 4 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 71e2cea

Please sign in to comment.