From 98bc419d5e3cbf01b1d0be7e99291ef8306b2314 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Thu, 12 Jul 2018 15:12:20 -0700 Subject: [PATCH] Merge PR #1659: crypto/keys: Allow one to export their private key * crypto/keys: Allow one to export their private key * Update changelog --- CHANGELOG.md | 1 + crypto/keys/keybase.go | 25 +++++++++++++++++++++++++ crypto/keys/types.go | 3 +++ 3 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e57b8a32..1cadfc352 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,7 @@ FEATURES - Auth has its invariants checked within the framework * [tests] Add WaitForNextNBlocksTM helper method * [keys] New keys now have 24 word recovery keys, for heightened security +- [keys] Add a temporary method for exporting the private key IMPROVEMENTS * [x/bank] Now uses go-wire codec instead of 'encoding/json' diff --git a/crypto/keys/keybase.go b/crypto/keys/keybase.go index 80de47d3d..ef2c6243a 100644 --- a/crypto/keys/keybase.go +++ b/crypto/keys/keybase.go @@ -240,6 +240,31 @@ func (kb dbKeybase) Sign(name, passphrase string, msg []byte) (sig tcrypto.Signa return sig, pub, nil } +func (kb dbKeybase) ExportPrivateKeyObject(name string, passphrase string) (tcrypto.PrivKey, error) { + info, err := kb.Get(name) + if err != nil { + return nil, err + } + var priv tcrypto.PrivKey + switch info.(type) { + case localInfo: + linfo := info.(localInfo) + if linfo.PrivKeyArmor == "" { + err = fmt.Errorf("private key not available") + return nil, err + } + priv, err = unarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase) + if err != nil { + return nil, err + } + case ledgerInfo: + return nil, errors.New("Only works on local private keys") + case offlineInfo: + return nil, errors.New("Only works on local private keys") + } + return priv, nil +} + func (kb dbKeybase) Export(name string) (armor string, err error) { bz := kb.db.Get(infoKey(name)) if bz == nil { diff --git a/crypto/keys/types.go b/crypto/keys/types.go index 74cedf919..c3f5d7834 100644 --- a/crypto/keys/types.go +++ b/crypto/keys/types.go @@ -39,6 +39,9 @@ type Keybase interface { ImportPubKey(name string, armor string) (err error) Export(name string) (armor string, err error) ExportPubKey(name string) (armor string, err error) + + // *only* works on locally-stored keys. Temporary method until we redo the exporting API + ExportPrivateKeyObject(name string, passphrase string) (crypto.PrivKey, error) } // Info is the publicly exposed information about a keypair