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

Fixing generate-key with --seedPhrase #1003

Open
wants to merge 2 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
22 changes: 10 additions & 12 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.');
Expand All @@ -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) {
Expand All @@ -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}"`);
})
};
7 changes: 5 additions & 2 deletions middleware/seed-phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Loading