near-cli icon indicating copy to clipboard operation
near-cli copied to clipboard

Create seed phrase option on add-key

Open tituszban opened this issue 2 years ago • 4 comments

The problem: There is currently no way in the CLI to add a seed phrase to an already existing account. As a result, if an account was generated on the CLI, it is not easy to also add it on the web wallet.

The solution: If add-key is called not with a public key, but with the --seedPhrase option, the key generated by that seed phrase is added to the account

tituszban avatar Jun 23 '22 22:06 tituszban

You can already add a seed phrase key via the CLI, but in 2 commands:

# First generate the key
near generate-key --seedPhrase 'your seed phrase ... 12 words' key.example

# Get the generated public key
pub_key=$(jq -r .public_key < ~/.near-credentials/testnet/key.example.json)

# Add the key to your current account
near add-key your_account.testnet "$pub_key"

The --seedPhrase option in add-key command directly overwrites the current account. So it works only as a recovery or import method rather than "adding a mnemonic" to an account.

MattKetmo avatar Jul 11 '22 15:07 MattKetmo

The --seedPhrase option in add-key command directly overwrites the current account. So it works only as a recovery or import method rather than "adding a mnemonic" to an account.

It doesn't override the current account. It uses the current (already logged in) account to add the key generated by the mnemonic.

As to your example, yes, you can do that. But the discoverability or generating a key to an account which is then not used is quite poor, compared to just adding a new key using the seed phrase to the account directly.

tituszban avatar Jul 11 '22 18:07 tituszban

yes my bad, I confused add-key & generate-key when talking about "overwrites".

A few things about your code:

  • Setting access-key required to false changes the behavior of the command. It's no more a 2nd arg but an option --access-key. Which means part 1 in README should be edited too (as well as doc pages)

  • Also, since it's a non-required option, a non-empty check should be added before calling await account.addKey(options.accessKey at line 55 in order to avoid this error

Adding full access key = undefined to example.testnet.
An error occured
TypeError: Cannot read properties of undefined (reading 'borshSerialize')
    at serializeStruct (/private/tmp/near-cli/node_modules/borsh/lib/index.js:302:20)
...
  • I would not console.log the mnemonic for security reasons (or maybe you wanted to write seedPhrasePublicKey at line 44?)

Otherwise yes I think this improve ux. We could even have an option to auto generate the seed phrase.

MattKetmo avatar Jul 11 '22 19:07 MattKetmo

  • Updated the readme, and PR to update the docs: https://github.com/near/docs/pull/1102
  • Added a non-empty check
  • Removed seed phrase from console log

tituszban avatar Jul 14 '22 10:07 tituszban