near-cli
near-cli copied to clipboard
Create seed phrase option on add-key
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
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.
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.
yes my bad, I confused add-key & generate-key when talking about "overwrites".
A few things about your code:
-
Setting
access-key
required
tofalse
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 writeseedPhrasePublicKey
at line 44?)
Otherwise yes I think this improve ux. We could even have an option to auto generate the seed phrase.
- 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