forked from cerc-io/laconicd-deprecated
cfac906f92
* WIP setting up Ethereum key CLI commands * Functional key gen and showing Ethereum address * Cleaned up changes * WIP setting up Ethereum key CLI commands * Functional key gen and showing Ethereum address * Cleaned up changes * Changed address to cosmos specific address * Remove default bech32 prefixes and add basic add command test * Changed Private key type to slice of bytes for compatibility and storability * switch back to using cosmos crypto Keybase interfaces * Changed key output to ethereum addressing instead of bitcoin and key generation to allow seeding from mnemonic and bip39 password * Updated show command and added test * Remove prefix requirement for showing keys and added existing keys commands to CLI temporarily * Removed unnecessary duplicate code * Readd prefixes for accounts temporarily * Fix linting issue * Remove TODO for setting PK to specific length of bytes (all functions use slice) * Cleaned up descriptions to remove multi-sigs
223 lines
5.6 KiB
Go
223 lines
5.6 KiB
Go
package keys
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/tendermint/tendermint/crypto"
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
cosmosKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
var _ cosmosKeys.Keybase = lazyKeybase{}
|
|
|
|
type lazyKeybase struct {
|
|
name string
|
|
dir string
|
|
}
|
|
|
|
// New creates a new instance of a lazy keybase.
|
|
func New(name, dir string) cosmosKeys.Keybase {
|
|
if err := cmn.EnsureDir(dir, 0700); err != nil {
|
|
panic(fmt.Sprintf("failed to create Keybase directory: %s", err))
|
|
}
|
|
|
|
return lazyKeybase{name: name, dir: dir}
|
|
}
|
|
|
|
func (lkb lazyKeybase) List() ([]cosmosKeys.Info, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).List()
|
|
}
|
|
|
|
func (lkb lazyKeybase) Get(name string) (cosmosKeys.Info, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Get(name)
|
|
}
|
|
|
|
func (lkb lazyKeybase) GetByAddress(address sdk.AccAddress) (cosmosKeys.Info, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).GetByAddress(address)
|
|
}
|
|
|
|
func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Delete(name, passphrase, skipPass)
|
|
}
|
|
|
|
func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Sign(name, passphrase, msg)
|
|
}
|
|
|
|
func (lkb lazyKeybase) CreateMnemonic(name string, language cosmosKeys.Language, passwd string, algo cosmosKeys.SigningAlgo) (info cosmosKeys.Info, seed string, err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).CreateMnemonic(name, language, passwd, algo)
|
|
}
|
|
|
|
func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account uint32, index uint32) (cosmosKeys.Info, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, account, index)
|
|
}
|
|
|
|
func (lkb lazyKeybase) Derive(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (cosmosKeys.Info, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Derive(name, mnemonic, bip39Passwd, encryptPasswd, params)
|
|
}
|
|
|
|
func (lkb lazyKeybase) CreateLedger(name string, algo cosmosKeys.SigningAlgo, hrp string, account, index uint32) (info cosmosKeys.Info, err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).CreateLedger(name, algo, hrp, account, index)
|
|
}
|
|
|
|
func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey) (info cosmosKeys.Info, err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).CreateOffline(name, pubkey)
|
|
}
|
|
|
|
func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info cosmosKeys.Info, err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).CreateMulti(name, pubkey)
|
|
}
|
|
|
|
func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, error)) error {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Update(name, oldpass, getNewpass)
|
|
}
|
|
|
|
func (lkb lazyKeybase) Import(name string, armor string) (err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Import(name, armor)
|
|
}
|
|
|
|
func (lkb lazyKeybase) ImportPrivKey(name string, armor string, passphrase string) error {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).ImportPrivKey(name, armor, passphrase)
|
|
}
|
|
|
|
func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).ImportPubKey(name, armor)
|
|
}
|
|
|
|
func (lkb lazyKeybase) Export(name string) (armor string, err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).Export(name)
|
|
}
|
|
|
|
func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).ExportPubKey(name)
|
|
}
|
|
|
|
func (lkb lazyKeybase) ExportPrivateKeyObject(name string, passphrase string) (crypto.PrivKey, error) {
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).ExportPrivateKeyObject(name, passphrase)
|
|
}
|
|
|
|
func (lkb lazyKeybase) ExportPrivKey(name string, decryptPassphrase string,
|
|
encryptPassphrase string) (armor string, err error) {
|
|
|
|
db, err := sdk.NewLevelDB(lkb.name, lkb.dir)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer db.Close()
|
|
|
|
return newDbKeybase(db).ExportPrivKey(name, decryptPassphrase, encryptPassphrase)
|
|
}
|
|
|
|
func (lkb lazyKeybase) CloseDB() {}
|