terra-money / station-legacy

Web Application to interact with Terra Core
https://station.terra.money
64 stars 45 forks source link

Feature: Import Keplr private key in TerraStation (includes basic solution) #205

Open asotirov opened 3 years ago

asotirov commented 3 years ago

Right now you cannot use the private key from the Keplr extension to be imported in TerraStation.

This is important, because Keplr accounts which have used Google login to generate access to Keplr extension do not have their mnemonics. However, they do have the private key which should be good enough for everything in the Cosmos universe. Maybe add a simple form that allows to input a private key, account alias, terra address and password and it will import an account from the hex encoded private key (such as the one Keplr provides)

Here is how to generate the TerraPrivate key from the Keplr hex encoded private key.

const KEPLR_PRIVATE_KEY = "keplr_private_key_without_0x"
const TERRA_ADDRESS_IN_KEPLR = "keplr_Terra_address"
const NAME = "account_alias_to_be_used_in_terra_station"
const PASSWORD_IN_TERRA = "password-for-terra-station-screen"
const ENCRYPTED_KEY= encrypt(KEPLR_PRIVATE_KEY, PASSWORD_IN_TERRA)
const key = Buffer.from(`{
    "name":"${NAME}",
    "address":"${TERRA_ADDRESS_IN_KEPLR}",
    "encrypted_key":"${ENCRYPTED_KEY}"
}`).toString('base64')

The encrypt method is this one https://github.com/terra-money/station/blob/f55a28de9812f72bb36f9072cb86e54f4e64efc7/src/utils/terra-keystore.ts#L6

sascha1337 commented 3 years ago

God bless you for this one, this non-mnemoric google login on keplr been slowing me down, same problem on AKASH CLI too

asotirov commented 3 years ago

I've been using the following @cosmjs method to init a base wallet successfully. Hope it helps.

import { DirectSecp256k1Wallet } from '@cosmjs/proto-signing'

const KEPLR_PRIVATE_KEY = "keplr_private_key_without_0x"
const uint8arr = new Uint8Array(Buffer.from(KEPLR_PRIVATE_KEY, 'hex'));
const wallet = await DirectSecp256k1Wallet.fromKey(uint8arr, prefix)