archway-network / cli

Develop WASM smart contracts with the Archway network developer CLI
https://docs.archway.io
Apache License 2.0
44 stars 21 forks source link

feat(accounts): Allow importing/exporting private keys #212

Closed eliasmpw closed 11 months ago

eliasmpw commented 1 year ago

Description

We should allow users to import and export keys generated in other wallets. All chains built with Cosmos SDK support exporting the unarmored private key hex. This same export method can be used in Keplr, Cosmostation, and Leap Wallet. To allow interoperability with all those wallets, the Archway CLI should allow users to import and export the private key hex.

Technical Details

Import

  1. Export an unarmored private key hex from archwayd:

    $ archwayd keys export --unarmored-hex --unsafe key-name
    WARNING: The private key will be exported as an unarmored hexadecimal string.
    USE AT YOUR OWN RISK. Continue? [y/N]: y
    7bd0664ea...
  2. Import the key from stdin or prompt the user if the stdin pipe is empty.

[!WARNING] Do not add an argument for the private key, as this information will be stored in the shell history file).

archwayd keys export --unarmored-hex --unsafe key-name | \
  archway accounts import key-name

or

$ archway accounts import key-name
> Private key hex: 7bd0664ea...

✅ Account key-name imported

Address: archway1...

Import example:

const unarmoredHex = "[ ... read from stdin ... ]"
const privKey = Buffer.from(unarmoredHex, "hex");
const keyPair = await Secp256k1.makeKeypair(privKey);

// Store the keyPair in the keyring

const wallet = new DirectSecp256k1Wallet(
    keyPair.privkey,
    Secp256k1.compressPubkey(keyPair.pubkey),
    "archway"
);
const accounts = await wallet.getAccounts();
console.log("Accounts:", accounts);

Export

$ archway accounts export [acc-name]

Reference code:

const keyPair = ... // fetch Secp256k1 keyPair from the keyring
console.log("Priv key hex:", keyPair.privkey.toString("hex"));

Tasks