near / near-cli

General purpose command line tools for interacting with NEAR Protocol
https://docs.near.org
MIT License
193 stars 93 forks source link

Create seed phrase option on add-key #997

Open tituszban opened 2 years ago

tituszban commented 2 years ago

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

MattKetmo commented 2 years ago

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.

tituszban commented 2 years ago

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.

MattKetmo commented 2 years ago

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

A few things about your code:

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)
...

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

tituszban commented 2 years ago