From a9f4b91201424d89541f60681c7e1db651105a59 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Tue, 19 Jul 2022 12:58:40 +0200 Subject: [PATCH] Fixing generate-key with --seedPhrase --- commands/generate-key.js | 22 ++++++++++------------ middleware/seed-phrase.js | 7 +++++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/commands/generate-key.js b/commands/generate-key.js index 2bae5499..748bcb2b 100644 --- a/commands/generate-key.js +++ b/commands/generate-key.js @@ -4,7 +4,7 @@ const implicitAccountId = require('../utils/implicit-accountid'); module.exports = { command: 'generate-key [account-id]', - desc: 'generate key or show key from Ledger', + desc: 'generate key (or extract it from seed phrase) or show key from Ledger', builder: (yargs) => yargs .option('yolo', { description: 'Do not ask for extra confirmation when using Ledger', @@ -13,6 +13,7 @@ module.exports = { handler: exitOnError(async (argv) => { let near = await require('../utils/connect')(argv); + // TODO: this ledger part should be moved to separate command. if (argv.usingLedger) { if (argv.accountId) { console.log('WARN: Account id is provided but ignored in case of using Ledger.'); @@ -34,6 +35,10 @@ module.exports = { return; } + if (argv.seedPhraseAccountId) { + // Nothing to do -- the seed key was already added to keyStore in seed-phrase.js. + return; + } const { deps: { keyStore } } = near.config; const existingKey = await keyStore.getKey(argv.networkId, argv.accountId); if (existingKey) { @@ -42,17 +47,10 @@ module.exports = { } // 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'); - argv.publicKey = keyPair.publicKey.toString(); - argv.accountId = argv.accountId || implicitAccountId(argv.publicKey); - await keyStore.setKey(argv.networkId, argv.accountId, keyPair); - } else if (argv.seedPhrase) { - const seededKeyPair = await argv.signer.keyStore.getKey(argv.networkId, argv.accountId); - await keyStore.setKey(argv.networkId, argv.accountId, seededKeyPair); - } - + const keyPair = KeyPair.fromRandom('ed25519'); + 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 ${argv.publicKey} public key for an account "${argv.accountId}"`); }) }; diff --git a/middleware/seed-phrase.js b/middleware/seed-phrase.js index 4c326492..47394fe7 100644 --- a/middleware/seed-phrase.js +++ b/middleware/seed-phrase.js @@ -18,8 +18,11 @@ module.exports = async function useSeedPhrase({ seedPhrase, seedPath, keyStore, const seedPhraseKeystore = new InMemoryKeyStore(); const seedPhraseAccountId = masterAccount ? masterAccount : accountId || implicitAccountId(publicKey); + console.log(`Adding a seed-based key ${publicKey} to account ${seedPhraseAccountId}`); + // FIXME: something is wrong here - as we create the "seedPhraseKeystore", but it stays "empty" + // and we add the seed-based key directly into "main" keystore. await keyStore.setKey(networkId, seedPhraseAccountId, KeyPair.fromString(secretKey)); - if(keyStore instanceof MergeKeyStore) keyStore.keyStores.push(seedPhraseKeystore); + if (keyStore instanceof MergeKeyStore) keyStore.keyStores.push(seedPhraseKeystore); - return { keyStore, accountId }; + return { keyStore, accountId, seedPhraseAccountId }; };