cosmos-sdk/crypto/keyring/legacy.go

193 lines
4.7 KiB
Go
Raw Normal View History

package keyring
import (
"fmt"
"strings"
"github.com/pkg/errors"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// LegacyKeybase is implemented by the legacy keybase implementation.
type LegacyKeybase interface {
List() ([]Info, error)
Export(name string) (armor string, err error)
ExportPrivKey(name, decryptPassphrase, encryptPassphrase string) (armor string, err error)
ExportPubKey(name string) (armor string, err error)
Close() error
}
// NewLegacy creates a new instance of a legacy keybase.
func NewLegacy(name, dir string, opts ...KeybaseOption) (LegacyKeybase, error) {
if err := tmos.EnsureDir(dir, 0700); err != nil {
return nil, fmt.Errorf("failed to create Keybase directory: %s", err)
}
db, err := sdk.NewLevelDB(name, dir)
if err != nil {
return nil, err
}
return newDBKeybase(db), nil
}
var _ LegacyKeybase = dbKeybase{}
// dbKeybase combines encryption and storage implementation to provide a
// full-featured key manager.
//
// Deprecated: dbKeybase will be removed in favor of keyringKeybase.
type dbKeybase struct {
db dbm.DB
}
// newDBKeybase creates a new dbKeybase instance using the provided DB for
// reading and writing keys.
func newDBKeybase(db dbm.DB) dbKeybase {
return dbKeybase{
db: db,
}
}
// List returns the keys from storage in alphabetical order.
func (kb dbKeybase) List() ([]Info, error) {
var res []Info
iter, err := kb.db.Iterator(nil, nil)
if err != nil {
return nil, err
}
defer iter.Close()
for ; iter.Valid(); iter.Next() {
key := string(iter.Key())
// need to include only keys in storage that have an info suffix
if strings.HasSuffix(key, infoSuffix) {
info, err := unmarshalInfo(iter.Value())
if err != nil {
return nil, err
}
res = append(res, info)
}
}
return res, nil
}
// Get returns the public information about one key.
func (kb dbKeybase) Get(name string) (Info, error) {
Remove bech32 PubKey support (#7477) * Move PubKey bech32 to legacy package and migrate the usage where possible * update /server * wip * move proto json encoding helper functions to internal * update internal/marshal * wip * update sections which needs legacybech32 * update validators output * fix conflicts * slashing update * add more tests and helper function for ANY JSON serialization * update slashing * Update function documentation * Rename code any-marshal helper functions * Update pubkey unpacking test * Update test comments * solve TestDecodeStore * solve legacytx issues * all code compiles * keyring tests * keyring cleanup * remove AssertMsg * fix some tests * fix add_ledger_test.go * update cli tests * debug cli test * rename clashed bech32 names * linter fixes * update tmservice tests * linter: update legacy deprecated checks * fix names * linting * legacybech32 pubkey type rename * fix staking client * fix test compilation * fix TestGetCmdQuerySigningInfo * rename NewIfcJSONAnyMarshaler * keyring: remove duplicated information from multinfo structure * todo cleanups * Update Changelog * remove some legacybech32 from tests * remove todos * remove printlnJSON from /server CLI and amino encoding * remove protocdc.MarshalJSON * client/show remove duplicated function * remove protocdc package * comment update * remove legacybech32.MustMarshalPubKey from a test * add todo * fix TestPublicKeyUnsafe test * review update * fix bech32 UnmarshalPubKey * Use codec.MarshalIfcJSON * fix linter issues * merging conflict: fix codec.Unmarshal calls * cleanups * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Reword changelog updates * use pubkey.String for comparison in Test_runAddCmdLedgerWithCustomCoinType * Update GetCmdQuerySigningInfo example * cli: update keys add docs * Add errors AsOf and errors.ErrIO type * restore multisigPubKeyInfo structure bring it back to multiInfo struct * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update crypto/keys/ed25519/ed25519_test.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * move pubkey any marshaling tests * Apply suggestions from code review Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review updates * adding missing return * errors: use IsOf instead of AsOf * keyring: add a correct check for key not found in keyring.Get * add checkKeyNotFound * fix linter issues * fix: keyring key not found check * fix keyring tests * fix linting issues * cli tests * fix: 'simd keys show <key> -p' * fix: TestVerifyMultisignature * rename keyring Bech32... functions to Mk... * fix RunAddCmd * Update pubkey display * wip * add more tests * udate keyring output tests * remove todo from ledger tests * rename MkKeyOutput * Changelog update * solve liner issues * add link to github issue Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2021-03-25 07:53:22 -07:00
bs, err := kb.db.Get(infoKeyBz(name))
if err != nil {
return nil, err
}
if len(bs) == 0 {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, name)
}
return unmarshalInfo(bs)
}
// ExportPrivateKeyObject returns a PrivKey object given the key name and
// passphrase. An error is returned if the key does not exist or if the Info for
// the key is invalid.
func (kb dbKeybase) ExportPrivateKeyObject(name string, passphrase string) (types.PrivKey, error) {
info, err := kb.Get(name)
if err != nil {
return nil, err
}
var priv types.PrivKey
switch i := info.(type) {
case localInfo:
linfo := i
if linfo.PrivKeyArmor == "" {
err = fmt.Errorf("private key not available")
return nil, err
}
priv, _, err = crypto.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase)
if err != nil {
return nil, err
}
case ledgerInfo, offlineInfo, multiInfo:
return nil, errors.New("only works on local private keys")
}
return priv, nil
}
func (kb dbKeybase) Export(name string) (armor string, err error) {
Remove bech32 PubKey support (#7477) * Move PubKey bech32 to legacy package and migrate the usage where possible * update /server * wip * move proto json encoding helper functions to internal * update internal/marshal * wip * update sections which needs legacybech32 * update validators output * fix conflicts * slashing update * add more tests and helper function for ANY JSON serialization * update slashing * Update function documentation * Rename code any-marshal helper functions * Update pubkey unpacking test * Update test comments * solve TestDecodeStore * solve legacytx issues * all code compiles * keyring tests * keyring cleanup * remove AssertMsg * fix some tests * fix add_ledger_test.go * update cli tests * debug cli test * rename clashed bech32 names * linter fixes * update tmservice tests * linter: update legacy deprecated checks * fix names * linting * legacybech32 pubkey type rename * fix staking client * fix test compilation * fix TestGetCmdQuerySigningInfo * rename NewIfcJSONAnyMarshaler * keyring: remove duplicated information from multinfo structure * todo cleanups * Update Changelog * remove some legacybech32 from tests * remove todos * remove printlnJSON from /server CLI and amino encoding * remove protocdc.MarshalJSON * client/show remove duplicated function * remove protocdc package * comment update * remove legacybech32.MustMarshalPubKey from a test * add todo * fix TestPublicKeyUnsafe test * review update * fix bech32 UnmarshalPubKey * Use codec.MarshalIfcJSON * fix linter issues * merging conflict: fix codec.Unmarshal calls * cleanups * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Reword changelog updates * use pubkey.String for comparison in Test_runAddCmdLedgerWithCustomCoinType * Update GetCmdQuerySigningInfo example * cli: update keys add docs * Add errors AsOf and errors.ErrIO type * restore multisigPubKeyInfo structure bring it back to multiInfo struct * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update crypto/keys/ed25519/ed25519_test.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * move pubkey any marshaling tests * Apply suggestions from code review Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review updates * adding missing return * errors: use IsOf instead of AsOf * keyring: add a correct check for key not found in keyring.Get * add checkKeyNotFound * fix linter issues * fix: keyring key not found check * fix keyring tests * fix linting issues * cli tests * fix: 'simd keys show <key> -p' * fix: TestVerifyMultisignature * rename keyring Bech32... functions to Mk... * fix RunAddCmd * Update pubkey display * wip * add more tests * udate keyring output tests * remove todo from ledger tests * rename MkKeyOutput * Changelog update * solve liner issues * add link to github issue Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2021-03-25 07:53:22 -07:00
bz, err := kb.db.Get(infoKeyBz(name))
if err != nil {
return "", err
}
if bz == nil {
return "", fmt.Errorf("no key to export with name %s", name)
}
return crypto.ArmorInfoBytes(bz), nil
}
// ExportPubKey returns public keys in ASCII armored format. It retrieves a Info
// object by its name and return the public key in a portable format.
func (kb dbKeybase) ExportPubKey(name string) (armor string, err error) {
Remove bech32 PubKey support (#7477) * Move PubKey bech32 to legacy package and migrate the usage where possible * update /server * wip * move proto json encoding helper functions to internal * update internal/marshal * wip * update sections which needs legacybech32 * update validators output * fix conflicts * slashing update * add more tests and helper function for ANY JSON serialization * update slashing * Update function documentation * Rename code any-marshal helper functions * Update pubkey unpacking test * Update test comments * solve TestDecodeStore * solve legacytx issues * all code compiles * keyring tests * keyring cleanup * remove AssertMsg * fix some tests * fix add_ledger_test.go * update cli tests * debug cli test * rename clashed bech32 names * linter fixes * update tmservice tests * linter: update legacy deprecated checks * fix names * linting * legacybech32 pubkey type rename * fix staking client * fix test compilation * fix TestGetCmdQuerySigningInfo * rename NewIfcJSONAnyMarshaler * keyring: remove duplicated information from multinfo structure * todo cleanups * Update Changelog * remove some legacybech32 from tests * remove todos * remove printlnJSON from /server CLI and amino encoding * remove protocdc.MarshalJSON * client/show remove duplicated function * remove protocdc package * comment update * remove legacybech32.MustMarshalPubKey from a test * add todo * fix TestPublicKeyUnsafe test * review update * fix bech32 UnmarshalPubKey * Use codec.MarshalIfcJSON * fix linter issues * merging conflict: fix codec.Unmarshal calls * cleanups * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Reword changelog updates * use pubkey.String for comparison in Test_runAddCmdLedgerWithCustomCoinType * Update GetCmdQuerySigningInfo example * cli: update keys add docs * Add errors AsOf and errors.ErrIO type * restore multisigPubKeyInfo structure bring it back to multiInfo struct * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update crypto/keys/ed25519/ed25519_test.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * move pubkey any marshaling tests * Apply suggestions from code review Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review updates * adding missing return * errors: use IsOf instead of AsOf * keyring: add a correct check for key not found in keyring.Get * add checkKeyNotFound * fix linter issues * fix: keyring key not found check * fix keyring tests * fix linting issues * cli tests * fix: 'simd keys show <key> -p' * fix: TestVerifyMultisignature * rename keyring Bech32... functions to Mk... * fix RunAddCmd * Update pubkey display * wip * add more tests * udate keyring output tests * remove todo from ledger tests * rename MkKeyOutput * Changelog update * solve liner issues * add link to github issue Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2021-03-25 07:53:22 -07:00
bz, err := kb.db.Get(infoKeyBz(name))
if err != nil {
return "", err
}
if bz == nil {
return "", fmt.Errorf("no key to export with name %s", name)
}
info, err := unmarshalInfo(bz)
if err != nil {
return
}
return crypto.ArmorPubKeyBytes(info.GetPubKey().Bytes(), string(info.GetAlgo())), nil
}
// ExportPrivKey returns a private key in ASCII armored format.
// It returns an error if the key does not exist or a wrong encryption passphrase
// is supplied.
func (kb dbKeybase) ExportPrivKey(name string, decryptPassphrase string,
encryptPassphrase string) (armor string, err error) {
priv, err := kb.ExportPrivateKeyObject(name, decryptPassphrase)
if err != nil {
return "", err
}
info, err := kb.Get(name)
if err != nil {
return "", err
}
return crypto.EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil
}
// Close the underlying storage.
func (kb dbKeybase) Close() error { return kb.db.Close() }
Remove bech32 PubKey support (#7477) * Move PubKey bech32 to legacy package and migrate the usage where possible * update /server * wip * move proto json encoding helper functions to internal * update internal/marshal * wip * update sections which needs legacybech32 * update validators output * fix conflicts * slashing update * add more tests and helper function for ANY JSON serialization * update slashing * Update function documentation * Rename code any-marshal helper functions * Update pubkey unpacking test * Update test comments * solve TestDecodeStore * solve legacytx issues * all code compiles * keyring tests * keyring cleanup * remove AssertMsg * fix some tests * fix add_ledger_test.go * update cli tests * debug cli test * rename clashed bech32 names * linter fixes * update tmservice tests * linter: update legacy deprecated checks * fix names * linting * legacybech32 pubkey type rename * fix staking client * fix test compilation * fix TestGetCmdQuerySigningInfo * rename NewIfcJSONAnyMarshaler * keyring: remove duplicated information from multinfo structure * todo cleanups * Update Changelog * remove some legacybech32 from tests * remove todos * remove printlnJSON from /server CLI and amino encoding * remove protocdc.MarshalJSON * client/show remove duplicated function * remove protocdc package * comment update * remove legacybech32.MustMarshalPubKey from a test * add todo * fix TestPublicKeyUnsafe test * review update * fix bech32 UnmarshalPubKey * Use codec.MarshalIfcJSON * fix linter issues * merging conflict: fix codec.Unmarshal calls * cleanups * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * Reword changelog updates * use pubkey.String for comparison in Test_runAddCmdLedgerWithCustomCoinType * Update GetCmdQuerySigningInfo example * cli: update keys add docs * Add errors AsOf and errors.ErrIO type * restore multisigPubKeyInfo structure bring it back to multiInfo struct * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update crypto/keys/ed25519/ed25519_test.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * Update codec/proto_codec.go Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * move pubkey any marshaling tests * Apply suggestions from code review Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com> * review updates * adding missing return * errors: use IsOf instead of AsOf * keyring: add a correct check for key not found in keyring.Get * add checkKeyNotFound * fix linter issues * fix: keyring key not found check * fix keyring tests * fix linting issues * cli tests * fix: 'simd keys show <key> -p' * fix: TestVerifyMultisignature * rename keyring Bech32... functions to Mk... * fix RunAddCmd * Update pubkey display * wip * add more tests * udate keyring output tests * remove todo from ledger tests * rename MkKeyOutput * Changelog update * solve liner issues * add link to github issue Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
2021-03-25 07:53:22 -07:00
func infoKey(name string) string { return fmt.Sprintf("%s.%s", name, infoSuffix) }
func infoKeyBz(name string) []byte { return []byte(infoKey(name)) }
// KeybaseOption overrides options for the db.
type KeybaseOption func(*kbOptions)
type kbOptions struct {
}