Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: crypto/keys: detach DecodeSignature from baseKeybase struct #5838

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions crypto/keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keys

import (
"fmt"
"io"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -68,23 +70,27 @@ var (
//
// NOTE: dbKeybase will be deprecated in favor of keyringKeybase.
type dbKeybase struct {
base baseKeybase
db dbm.DB
base baseKeybase
db dbm.DB
input io.Reader
}

// newDBKeybase creates a new dbKeybase instance using the provided DB for
// reading and writing keys.
func newDBKeybase(db dbm.DB, opts ...KeybaseOption) Keybase {
func newDBKeybase(db dbm.DB, input io.Reader, opts ...KeybaseOption) Keybase {
return dbKeybase{
base: newBaseKeybase(opts...),
db: db,
base: newBaseKeybase(opts...),
db: db,
input: input,
}
}

// NewInMemory creates a transient keybase on top of in-memory storage
// instance useful for testing purposes and on-the-fly key generation.
// Keybase options can be applied when generating this new Keybase.
func NewInMemory(opts ...KeybaseOption) Keybase { return newDBKeybase(dbm.NewMemDB(), opts...) }
func NewInMemory(opts ...KeybaseOption) Keybase {
return newDBKeybase(dbm.NewMemDB(), os.Stdin, opts...)
}

// CreateMnemonic generates a new key and persists it to storage, encrypted
// using the provided password. It returns the generated mnemonic and the key Info.
Expand Down Expand Up @@ -215,7 +221,7 @@ func (kb dbKeybase) Sign(name, passphrase string, msg []byte) (sig []byte, pub t
return SignWithLedger(info, msg)

case offlineInfo, multiInfo:
return kb.base.DecodeSignature(info, msg)
return nil, info.GetPubKey(), errors.New("cannot sign with offline keys")
}

sig, err = priv.Sign(msg)
Expand Down
31 changes: 0 additions & 31 deletions crypto/keys/keybase_base.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package keys

import (
"bufio"
"fmt"
"os"

"github.com/cosmos/go-bip39"
"github.com/pkg/errors"
tmcrypto "github.com/tendermint/tendermint/crypto"
Expand Down Expand Up @@ -104,33 +100,6 @@ func SecpPrivKeyGen(bz []byte) tmcrypto.PrivKey {
return secp256k1.PrivKeySecp256k1(bzArr)
}

// DecodeSignature decodes a an length-prefixed binary signature from standard input
// and return it as a byte slice.
func (kb baseKeybase) DecodeSignature(info Info, msg []byte) (sig []byte, pub tmcrypto.PubKey, err error) {
_, err = fmt.Fprintf(os.Stderr, "Message to sign:\n\n%s\n", msg)
if err != nil {
return nil, nil, err
}

buf := bufio.NewReader(os.Stdin)
_, err = fmt.Fprintf(os.Stderr, "\nEnter Amino-encoded signature:\n")
if err != nil {
return nil, nil, err
}

// will block until user inputs the signature
signed, err := buf.ReadString('\n')
if err != nil {
return nil, nil, err
}

if err := CryptoCdc.UnmarshalBinaryLengthPrefixed([]byte(signed), sig); err != nil {
return nil, nil, errors.Wrap(err, "failed to decode signature")
}

return sig, info.GetPubKey(), nil
}

// CreateAccount creates an account Info object.
func (kb baseKeybase) CreateAccount(
keyWriter keyWriter, name, mnemonic, bip39Passphrase, encryptPasswd, hdPath string, algo SigningAlgo,
Expand Down
16 changes: 9 additions & 7 deletions crypto/keys/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ var _ Keybase = keyringKeybase{}
// keyringKeybase implements the Keybase interface by using the Keyring library
// for account key persistence.
type keyringKeybase struct {
base baseKeybase
db keyring.Keyring
base baseKeybase
db keyring.Keyring
input io.Reader
}

var maxPassphraseEntryAttempts = 3

func newKeyringKeybase(db keyring.Keyring, opts ...KeybaseOption) Keybase {
func newKeyringKeybase(db keyring.Keyring, input io.Reader, opts ...KeybaseOption) Keybase {
return keyringKeybase{
db: db,
base: newBaseKeybase(opts...),
db: db,
base: newBaseKeybase(opts...),
input: input,
}
}

Expand Down Expand Up @@ -82,7 +84,7 @@ func NewKeyring(
return nil, err
}

return newKeyringKeybase(db, opts...), nil
return newKeyringKeybase(db, userInput, opts...), nil
}

// CreateMnemonic generates a new key and persists it to storage, encrypted
Expand Down Expand Up @@ -219,7 +221,7 @@ func (kb keyringKeybase) Sign(name, passphrase string, msg []byte) (sig []byte,
return SignWithLedger(info, msg)

case offlineInfo, multiInfo:
return kb.base.DecodeSignature(info, msg)
return nil, info.GetPubKey(), errors.New("cannot sign with offline keys")
}

sig, err = priv.Sign(msg)
Expand Down
3 changes: 2 additions & 1 deletion crypto/keys/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ func TestLazySignVerifyKeyRingWithLedger(t *testing.T) {
func TestLazySignVerifyKeyRing(t *testing.T) {
dir, cleanup := tests.NewTestCaseDir(t)
t.Cleanup(cleanup)
kb, err := NewKeyring("keybasename", "test", dir, nil)
buf := bytes.NewBufferString("")
kb, err := NewKeyring("keybasename", "test", dir, buf)
require.NoError(t, err)
algo := Secp256k1

Expand Down
38 changes: 19 additions & 19 deletions crypto/keys/lazy_keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keys

import (
"fmt"
"os"

"github.com/tendermint/tendermint/crypto"
tmos "github.com/tendermint/tendermint/libs/os"
Expand Down Expand Up @@ -34,7 +35,7 @@ func (lkb lazyKeybase) List() ([]Info, error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).List()
return newDBKeybase(db, os.Stdin, lkb.options...).List()
}

func (lkb lazyKeybase) Get(name string) (Info, error) {
Expand All @@ -44,7 +45,7 @@ func (lkb lazyKeybase) Get(name string) (Info, error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).Get(name)
return newDBKeybase(db, os.Stdin, lkb.options...).Get(name)
}

func (lkb lazyKeybase) GetByAddress(address sdk.AccAddress) (Info, error) {
Expand All @@ -54,7 +55,7 @@ func (lkb lazyKeybase) GetByAddress(address sdk.AccAddress) (Info, error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).GetByAddress(address)
return newDBKeybase(db, os.Stdin, lkb.options...).GetByAddress(address)
}

func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error {
Expand All @@ -64,7 +65,7 @@ func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).Delete(name, passphrase, skipPass)
return newDBKeybase(db, os.Stdin, lkb.options...).Delete(name, passphrase, skipPass)
}

func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error) {
Expand All @@ -74,7 +75,7 @@ func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto
}
defer db.Close()

return newDBKeybase(db, lkb.options...).Sign(name, passphrase, msg)
return newDBKeybase(db, os.Stdin, lkb.options...).Sign(name, passphrase, msg)
}

func (lkb lazyKeybase) CreateMnemonic(name string, language Language, passwd string, algo SigningAlgo) (info Info, seed string, err error) {
Expand All @@ -84,7 +85,7 @@ func (lkb lazyKeybase) CreateMnemonic(name string, language Language, passwd str
}
defer db.Close()

return newDBKeybase(db, lkb.options...).CreateMnemonic(name, language, passwd, algo)
return newDBKeybase(db, os.Stdin, lkb.options...).CreateMnemonic(name, language, passwd, algo)
}

func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath string, algo SigningAlgo) (Info, error) {
Expand All @@ -94,8 +95,7 @@ func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd,
}
defer db.Close()

return newDBKeybase(db,
lkb.options...).CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath, algo)
return newDBKeybase(db, os.Stdin, lkb.options...).CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd, hdPath, algo)
}

func (lkb lazyKeybase) CreateLedger(name string, algo SigningAlgo, hrp string, account, index uint32) (info Info, err error) {
Expand All @@ -105,7 +105,7 @@ func (lkb lazyKeybase) CreateLedger(name string, algo SigningAlgo, hrp string, a
}
defer db.Close()

return newDBKeybase(db, lkb.options...).CreateLedger(name, algo, hrp, account, index)
return newDBKeybase(db, os.Stdin, lkb.options...).CreateLedger(name, algo, hrp, account, index)
}

func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey, algo SigningAlgo) (info Info, err error) {
Expand All @@ -115,7 +115,7 @@ func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey, algo Sig
}
defer db.Close()

return newDBKeybase(db, lkb.options...).CreateOffline(name, pubkey, algo)
return newDBKeybase(db, os.Stdin, lkb.options...).CreateOffline(name, pubkey, algo)
}

func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info Info, err error) {
Expand All @@ -125,7 +125,7 @@ func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info Info
}
defer db.Close()

return newDBKeybase(db, lkb.options...).CreateMulti(name, pubkey)
return newDBKeybase(db, os.Stdin, lkb.options...).CreateMulti(name, pubkey)
}

func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, error)) error {
Expand All @@ -135,7 +135,7 @@ func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, e
}
defer db.Close()

return newDBKeybase(db, lkb.options...).Update(name, oldpass, getNewpass)
return newDBKeybase(db, os.Stdin, lkb.options...).Update(name, oldpass, getNewpass)
}

func (lkb lazyKeybase) Import(name string, armor string) (err error) {
Expand All @@ -145,7 +145,7 @@ func (lkb lazyKeybase) Import(name string, armor string) (err error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).Import(name, armor)
return newDBKeybase(db, os.Stdin, lkb.options...).Import(name, armor)
}

func (lkb lazyKeybase) ImportPrivKey(name string, armor string, passphrase string) error {
Expand All @@ -155,7 +155,7 @@ func (lkb lazyKeybase) ImportPrivKey(name string, armor string, passphrase strin
}
defer db.Close()

return newDBKeybase(db, lkb.options...).ImportPrivKey(name, armor, passphrase)
return newDBKeybase(db, os.Stdin, lkb.options...).ImportPrivKey(name, armor, passphrase)
}

func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) {
Expand All @@ -165,7 +165,7 @@ func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).ImportPubKey(name, armor)
return newDBKeybase(db, os.Stdin, lkb.options...).ImportPubKey(name, armor)
}

func (lkb lazyKeybase) Export(name string) (armor string, err error) {
Expand All @@ -175,7 +175,7 @@ func (lkb lazyKeybase) Export(name string) (armor string, err error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).Export(name)
return newDBKeybase(db, os.Stdin, lkb.options...).Export(name)
}

func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) {
Expand All @@ -185,7 +185,7 @@ func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) {
}
defer db.Close()

return newDBKeybase(db, lkb.options...).ExportPubKey(name)
return newDBKeybase(db, os.Stdin, lkb.options...).ExportPubKey(name)
}

func (lkb lazyKeybase) ExportPrivateKeyObject(name string, passphrase string) (crypto.PrivKey, error) {
Expand All @@ -195,7 +195,7 @@ func (lkb lazyKeybase) ExportPrivateKeyObject(name string, passphrase string) (c
}
defer db.Close()

return newDBKeybase(db, lkb.options...).ExportPrivateKeyObject(name, passphrase)
return newDBKeybase(db, os.Stdin, lkb.options...).ExportPrivateKeyObject(name, passphrase)
}

func (lkb lazyKeybase) ExportPrivKey(name string, decryptPassphrase string,
Expand All @@ -207,7 +207,7 @@ func (lkb lazyKeybase) ExportPrivKey(name string, decryptPassphrase string,
}
defer db.Close()

return newDBKeybase(db, lkb.options...).ExportPrivKey(name, decryptPassphrase, encryptPassphrase)
return newDBKeybase(db, os.Stdin, lkb.options...).ExportPrivKey(name, decryptPassphrase, encryptPassphrase)
}

// SupportedAlgos returns a list of supported signing algorithms.
Expand Down