From faa822aaf49ebbb8c1c990380e1ff0e25bc07b3a Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 3 Nov 2020 16:54:56 +0100 Subject: [PATCH 01/91] Move PubKey bech32 to legacy package and migrate the usage where possible --- client/keys/add.go | 43 ++++++------ client/keys/show.go | 12 ++-- server/tm_cmds.go | 3 + types/address.go | 119 +------------------------------ types/address_test.go | 42 ----------- types/bech32/legacybech32/pk.go | 120 ++++++++++++++++++++++++++++++++ types/tx/signing/signing.pb.go | 9 +-- x/auth/legacy/v038/types.go | 17 ++--- x/slashing/client/rest/query.go | 1 + x/staking/client/cli/tx.go | 13 +--- 10 files changed, 170 insertions(+), 209 deletions(-) create mode 100644 types/bech32/legacybech32/pk.go diff --git a/client/keys/add.go b/client/keys/add.go index b2e267e83e8a..885326416008 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -61,21 +61,21 @@ the flag --nosort is set. Args: cobra.ExactArgs(1), RunE: runAddCmd, } - - cmd.Flags().StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") - cmd.Flags().Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") - cmd.Flags().Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") - cmd.Flags().String(FlagPublicKey, "", "Parse a public key in bech32 format and save it to disk") - cmd.Flags().BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic") - cmd.Flags().Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") - cmd.Flags().Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating") - cmd.Flags().Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)") - cmd.Flags().Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore") - cmd.Flags().String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)") - cmd.Flags().Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") - cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation") - cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation") - cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + flags := cmd.Flags() + flags.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") + flags.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") + flags.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") + flags.String(FlagPublicKey, "", "Parse a public key in bech32 format and save it to disk") + flags.BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic") + flags.Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") + flags.Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating") + flags.Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)") + flags.Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore") + flags.String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)") + flags.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") + flags.Uint32(flagAccount, 0, "Account number for HD derivation") + flags.Uint32(flagIndex, 0, "Address index number for HD derivation") + flags.String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") cmd.SetOut(cmd.OutOrStdout()) cmd.SetErr(cmd.ErrOrStderr()) @@ -186,16 +186,15 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *buf pubKey, _ := cmd.Flags().GetString(FlagPublicKey) if pubKey != "" { - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, pubKey) - if err != nil { - return err - } - - if _, err := kb.SavePubKey(name, pk, algo.Name()); err != nil { + var pk crypto.PubKey + // TODO: shall we use KeysCdc here (global from this module, = codec.NewLegacyAmino)? + marshaller := client.GetClientContextFromCmd(cmd).JSONMarshaler + if err := marshaller.UnmarshalJSON([]byte(pubKey), &pk); err != nil { return err } - return nil + _, err := kb.SavePubKey(name, pk, algo.Name()) + return err } coinType, _ := cmd.Flags().GetUint32(flagCoinType) diff --git a/client/keys/show.go b/client/keys/show.go index 102f29793b4a..88611bfe9c32 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -41,12 +41,12 @@ consisting of all the keys provided by name and multisig threshold.`, Args: cobra.MinimumNArgs(1), RunE: runShowCmd, } - - cmd.Flags().String(FlagBechPrefix, sdk.PrefixAccount, "The Bech32 prefix encoding for a key (acc|val|cons)") - cmd.Flags().BoolP(FlagAddress, "a", false, "Output the address only (overrides --output)") - cmd.Flags().BoolP(FlagPublicKey, "p", false, "Output the public key only (overrides --output)") - cmd.Flags().BoolP(FlagDevice, "d", false, "Output the address in a ledger device") - cmd.Flags().Int(flagMultiSigThreshold, 1, "K out of N required signatures") + flags := cmd.Flags() + flags.String(FlagBechPrefix, sdk.PrefixAccount, "The Bech32 prefix encoding for a key (acc|val|cons)") + flags.BoolP(FlagAddress, "a", false, "Output the address only (overrides --output)") + flags.BoolP(FlagPublicKey, "p", false, "Output the public key only (overrides --output)") + flags.BoolP(FlagDevice, "d", false, "Output the address in a ledger device") + flags.Int(flagMultiSigThreshold, 1, "K out of N required signatures") return cmd } diff --git a/server/tm_cmds.go b/server/tm_cmds.go index d6c8b665f9c4..0e43fa8c5f70 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -12,6 +12,7 @@ import ( "github.com/tendermint/tendermint/p2p" pvm "github.com/tendermint/tendermint/privval" tversion "github.com/tendermint/tendermint/version" + "google.golang.org/protobuf/proto" yaml "gopkg.in/yaml.v2" "github.com/cosmos/cosmos-sdk/codec" @@ -59,6 +60,8 @@ func ShowValidatorCmd() *cobra.Command { return printlnJSON(valPubKey) } + pk, ok := valPubKey.(proto.Message) + pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey) if err != nil { return err diff --git a/types/address.go b/types/address.go index 345c44fa8e1c..0f92e8667b3f 100644 --- a/types/address.go +++ b/types/address.go @@ -9,15 +9,9 @@ import ( "strings" "github.com/tendermint/tendermint/crypto" - tmed25519 "github.com/tendermint/tendermint/crypto/ed25519" yaml "gopkg.in/yaml.v2" - "github.com/cosmos/cosmos-sdk/codec/legacy" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/types/bech32" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) const ( @@ -604,121 +598,12 @@ func (ca ConsAddress) Format(s fmt.State, verb rune) { // auxiliary // ---------------------------------------------------------------------------- -// Bech32PubKeyType defines a string type alias for a Bech32 public key type. -type Bech32PubKeyType string - -// Bech32 conversion constants -const ( - Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub" - Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub" - Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub" -) - -// Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate -// prefix based on the key type provided for a given PublicKey. -func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) (string, error) { - var bech32Prefix string - - switch pkt { - case Bech32PubKeyTypeAccPub: - bech32Prefix = GetConfig().GetBech32AccountPubPrefix() - - case Bech32PubKeyTypeValPub: - bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix() - - case Bech32PubKeyTypeConsPub: - bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix() - - } - - // This piece of code is to keep backwards-compatibility. - // For ed25519 keys, our own ed25519 is registered in Amino under a - // different name than TM's ed25519. But since users are already using - // TM's ed25519 bech32 encoding, we explicitly say to bech32-encode our own - // ed25519 the same way as TM's ed25519. - // TODO: Remove Bech32ifyPubKey and all usages (cosmos/cosmos-sdk/issues/#7357) - pkToMarshal := pubkey - if ed25519Pk, ok := pubkey.(*ed25519.PubKey); ok { - pkToMarshal = ed25519Pk.AsTmPubKey() - } - - return bech32.ConvertAndEncode(bech32Prefix, legacy.Cdc.MustMarshalBinaryBare(pkToMarshal)) -} - -// MustBech32ifyPubKey calls Bech32ifyPubKey except it panics on error. -func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) string { - res, err := Bech32ifyPubKey(pkt, pubkey) - if err != nil { - panic(err) - } - - return res -} - -// GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with -// a given key type. -func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (crypto.PubKey, error) { - var bech32Prefix string - - switch pkt { - case Bech32PubKeyTypeAccPub: - bech32Prefix = GetConfig().GetBech32AccountPubPrefix() - - case Bech32PubKeyTypeValPub: - bech32Prefix = GetConfig().GetBech32ValidatorPubPrefix() - - case Bech32PubKeyTypeConsPub: - bech32Prefix = GetConfig().GetBech32ConsensusPubPrefix() - - } - - bz, err := GetFromBech32(pubkeyStr, bech32Prefix) - if err != nil { - return nil, err - } - - aminoPk, err := cryptocodec.PubKeyFromBytes(bz) - if err != nil { - return nil, err - } - - var protoPk crypto.PubKey - switch aminoPk.(type) { - - // We are bech32ifying some secp256k1 keys in tests. - case *secp256k1.PubKey: - protoPk = aminoPk - case *ed25519.PubKey: - protoPk = aminoPk - - // Real-life case. - case tmed25519.PubKey: - protoPk = &ed25519.PubKey{ - Key: aminoPk.Bytes(), - } - - default: - // We only allow ed25519 pubkeys to be bech32-ed right now. - return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "bech32 pubkey does not support %T", aminoPk) - } - - return protoPk, nil -} - -// MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error. -func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) crypto.PubKey { - res, err := GetPubKeyFromBech32(pkt, pubkeyStr) - if err != nil { - panic(err) - } - - return res -} +var errBech32EmptyAddress = errors.New("decoding Bech32 address failed: must provide a non empty address") // GetFromBech32 decodes a bytestring from a Bech32 encoded string. func GetFromBech32(bech32str, prefix string) ([]byte, error) { if len(bech32str) == 0 { - return nil, errors.New("decoding Bech32 address failed: must provide an address") + return nil, errBech32EmptyAddress } hrp, bz, err := bech32.DecodeAndConvert(bech32str) diff --git a/types/address_test.go b/types/address_test.go index 9de4bea4f360..720995f8a5d4 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -68,48 +68,6 @@ func (s *addressTestSuite) TestEmptyAddresses() { s.Require().Error(err) } -func (s *addressTestSuite) TestRandBech32PubkeyConsistency() { - pubBz := make([]byte, ed25519.PubKeySize) - pub := &ed25519.PubKey{Key: pubBz} - - for i := 0; i < 1000; i++ { - rand.Read(pub.Key) - - mustBech32AccPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pub) - bech32AccPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pub) - s.Require().Nil(err) - s.Require().Equal(bech32AccPub, mustBech32AccPub) - - mustBech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pub) - bech32ValPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeValPub, pub) - s.Require().Nil(err) - s.Require().Equal(bech32ValPub, mustBech32ValPub) - - mustBech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pub) - bech32ConsPub, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pub) - s.Require().Nil(err) - s.Require().Equal(bech32ConsPub, mustBech32ConsPub) - - mustAccPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub) - accPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeAccPub, bech32AccPub) - s.Require().Nil(err) - s.Require().Equal(accPub, mustAccPub) - - mustValPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub) - valPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeValPub, bech32ValPub) - s.Require().Nil(err) - s.Require().Equal(valPub, mustValPub) - - mustConsPub := types.MustGetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub) - consPub, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, bech32ConsPub) - s.Require().Nil(err) - s.Require().Equal(consPub, mustConsPub) - - s.Require().Equal(valPub, accPub) - s.Require().Equal(valPub, consPub) - } -} - func (s *addressTestSuite) TestYAMLMarshalers() { addr := secp256k1.GenPrivKey().PubKey().Address() diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go new file mode 100644 index 000000000000..53df87f3b8fc --- /dev/null +++ b/types/bech32/legacybech32/pk.go @@ -0,0 +1,120 @@ +// Deprecated: The module provides legacy bech32 functions which will be removed in a future +// release. +package legacybech32 + +// nolint + +// TODO: remove Bech32 prefix, it's already in package + +import ( + "github.com/tendermint/tendermint/crypto" + tmed25519 "github.com/tendermint/tendermint/crypto/ed25519" + + "github.com/cosmos/cosmos-sdk/codec/legacy" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// Deprecated: Bech32PubKeyType defines a string type alias for a Bech32 public key type. +type Bech32PubKeyType string + +// Bech32 conversion constants +const ( + Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub" + Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub" + Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub" +) + +// Deprecated: Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate +// prefix based on the key type provided for a given PublicKey. +func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) (string, error) { + // This piece of code is to keep backwards-compatibility. + // For ed25519 keys, our own ed25519 is registered in Amino under a + // different name than TM's ed25519. But since users are already using + // TM's ed25519 bech32 encoding, we explicitly say to bech32-encode our own + // ed25519 the same way as TM's ed25519. + // TODO: Remove Bech32ifyPubKey and all usages (cosmos/cosmos-sdk/issues/#7357) + pkToMarshal := pubkey + if ed25519Pk, ok := pubkey.(*ed25519.PubKey); ok { + pkToMarshal = ed25519Pk.AsTmPubKey() + } + + bech32Prefix := getPrefix(pkt) + return bech32.ConvertAndEncode(bech32Prefix, legacy.Cdc.MustMarshalBinaryBare(pkToMarshal)) +} + +// Deprecated: MustBech32ifyPubKey calls Bech32ifyPubKey and panics on error. +func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey crypto.PubKey) string { + res, err := Bech32ifyPubKey(pkt, pubkey) + if err != nil { + panic(err) + } + + return res +} + +func getPrefix(pkt Bech32PubKeyType) string { + cfg := sdk.GetConfig() + switch pkt { + case Bech32PubKeyTypeAccPub: + return cfg.GetBech32AccountPubPrefix() + + case Bech32PubKeyTypeValPub: + return cfg.GetBech32ValidatorPubPrefix() + case Bech32PubKeyTypeConsPub: + return cfg.GetBech32ConsensusPubPrefix() + } + + return "" +} + +// Deprecated: GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with +// a given key type. +func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (crypto.PubKey, error) { + bech32Prefix := getPrefix(pkt) + bz, err := sdk.GetFromBech32(pubkeyStr, bech32Prefix) + if err != nil { + return nil, err + } + + aminoPk, err := cryptocodec.PubKeyFromBytes(bz) + if err != nil { + return nil, err + } + + var protoPk crypto.PubKey + switch aminoPk.(type) { + + // We are bech32ifying some secp256k1 keys in tests. + case *secp256k1.PubKey: + protoPk = aminoPk + case *ed25519.PubKey: + protoPk = aminoPk + + // Real-life case. + case tmed25519.PubKey: + protoPk = &ed25519.PubKey{ + Key: aminoPk.Bytes(), + } + + default: + // We only allow ed25519 pubkeys to be bech32-ed right now. + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "bech32 pubkey does not support %T", aminoPk) + } + + return protoPk, nil +} + +// Deprecated: MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error. +func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) crypto.PubKey { + res, err := GetPubKeyFromBech32(pkt, pubkeyStr) + if err != nil { + panic(err) + } + + return res +} diff --git a/types/tx/signing/signing.pb.go b/types/tx/signing/signing.pb.go index 424e7395df30..377a60e3ebdf 100644 --- a/types/tx/signing/signing.pb.go +++ b/types/tx/signing/signing.pb.go @@ -5,12 +5,13 @@ package signing import ( fmt "fmt" - types "github.com/cosmos/cosmos-sdk/codec/types" - types1 "github.com/cosmos/cosmos-sdk/crypto/types" - proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" + + types "github.com/cosmos/cosmos-sdk/codec/types" + types1 "github.com/cosmos/cosmos-sdk/crypto/types" + proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. @@ -38,7 +39,7 @@ const ( // human-readable textual representation on top of the binary representation // from SIGN_MODE_DIRECT SignMode_SIGN_MODE_TEXTUAL SignMode = 2 - // SIGN_MODE_LEGACY_AMINO_JSON is a backwards compatibility mode which uses + // Deprecated: SIGN_MODE_LEGACY_AMINO_JSON is a backwards compatibility mode which uses // Amino JSON and will be removed in the future SignMode_SIGN_MODE_LEGACY_AMINO_JSON SignMode = 127 ) diff --git a/x/auth/legacy/v038/types.go b/x/auth/legacy/v038/types.go index aeb554eeeab5..035db9629c3f 100644 --- a/x/auth/legacy/v038/types.go +++ b/x/auth/legacy/v038/types.go @@ -15,6 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" v034auth "github.com/cosmos/cosmos-sdk/x/auth/legacy/v034" ) @@ -174,7 +175,7 @@ func (acc BaseAccount) MarshalJSON() ([]byte, error) { } if acc.PubKey != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, acc.PubKey) + pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, acc.PubKey) if err != nil { return nil, err } @@ -193,7 +194,7 @@ func (acc *BaseAccount) UnmarshalJSON(bz []byte) error { } if alias.PubKey != "" { - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) if err != nil { return err } @@ -240,7 +241,7 @@ func (bva BaseVestingAccount) MarshalJSON() ([]byte, error) { } if bva.PubKey != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, bva.PubKey) + pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, bva.PubKey) if err != nil { return nil, err } @@ -264,7 +265,7 @@ func (bva *BaseVestingAccount) UnmarshalJSON(bz []byte) error { ) if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) if err != nil { return err } @@ -309,7 +310,7 @@ func (cva ContinuousVestingAccount) MarshalJSON() ([]byte, error) { } if cva.PubKey != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, cva.PubKey) + pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, cva.PubKey) if err != nil { return nil, err } @@ -333,7 +334,7 @@ func (cva *ContinuousVestingAccount) UnmarshalJSON(bz []byte) error { ) if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) if err != nil { return err } @@ -375,7 +376,7 @@ func (dva DelayedVestingAccount) MarshalJSON() ([]byte, error) { } if dva.PubKey != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, dva.PubKey) + pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, dva.PubKey) if err != nil { return nil, err } @@ -399,7 +400,7 @@ func (dva *DelayedVestingAccount) UnmarshalJSON(bz []byte) error { ) if alias.PubKey != "" { - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) if err != nil { return err } diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index 36eb8cc24a68..0e1c68da145d 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -30,6 +30,7 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) { } // http request handler to query signing info +// [DEPRECATED] func signingInfoHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index ab486c5a9841..563ec6c49dbf 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -407,7 +407,7 @@ type TxCreateValidatorConfig struct { CommissionMaxChangeRate string MinSelfDelegation string - PubKey string + PubKey crypto.PubKey IP string Website string @@ -479,7 +479,7 @@ func PrepareConfigForTxCreateValidator(flagSet *flag.FlagSet, moniker, nodeID, c } c.NodeID = nodeID - c.PubKey = sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey) + c.PubKey = valPubKey c.Website = website c.SecurityContact = securityContact c.Details = details @@ -520,13 +520,6 @@ func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorC } valAddr := clientCtx.GetFromAddress() - pkStr := config.PubKey - - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr) - if err != nil { - return txBldr, nil, err - } - description := types.NewDescription( config.Moniker, config.Identity, @@ -554,7 +547,7 @@ func BuildCreateValidatorMsg(clientCtx client.Context, config TxCreateValidatorC } msg, err := types.NewMsgCreateValidator( - sdk.ValAddress(valAddr), pk, amount, description, commissionRates, minSelfDelegation, + sdk.ValAddress(valAddr), config.PubKey, amount, description, commissionRates, minSelfDelegation, ) if err != nil { return txBldr, msg, err From 2324c4ef4c0ff03a0a4f8f4089d2afac0bdbbe33 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 3 Nov 2020 21:57:02 +0100 Subject: [PATCH 02/91] update /server --- client/keys/add.go | 12 +++++------- codec/json.go | 15 +++++++++++++-- server/tm_cmds.go | 25 +++++++------------------ 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 885326416008..734ad24f8042 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -59,7 +59,7 @@ required through --multisig-threshold. The keys are sorted by address, unless the flag --nosort is set. `, Args: cobra.ExactArgs(1), - RunE: runAddCmd, + RunE: runAddCmdPrepare, } flags := cmd.Flags() flags.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") @@ -83,7 +83,7 @@ the flag --nosort is set. return cmd } -func runAddCmd(cmd *cobra.Command, args []string) error { +func runAddCmdPrepare(cmd *cobra.Command, args []string) error { buf := bufio.NewReader(cmd.InOrStdin()) clientCtx := client.GetClientContextFromCmd(cmd) @@ -104,7 +104,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error { return err } - return RunAddCmd(cmd, args, kr, buf) + return runAddCmd(clientCtx, cmd, args, kr, buf) } /* @@ -116,7 +116,7 @@ input output - armor encrypted private key (saved to file) */ -func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error { +func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error { var err error name := args[0] @@ -188,11 +188,9 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *buf if pubKey != "" { var pk crypto.PubKey // TODO: shall we use KeysCdc here (global from this module, = codec.NewLegacyAmino)? - marshaller := client.GetClientContextFromCmd(cmd).JSONMarshaler - if err := marshaller.UnmarshalJSON([]byte(pubKey), &pk); err != nil { + if err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pubKey), &pk); err != nil { return err } - _, err := kb.SavePubKey(name, pk, algo.Name()) return err } diff --git a/codec/json.go b/codec/json.go index db77365e0356..b0207cc9736a 100644 --- a/codec/json.go +++ b/codec/json.go @@ -3,10 +3,11 @@ package codec import ( "bytes" - "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/proto" + + "github.com/cosmos/cosmos-sdk/codec/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) // ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded @@ -28,3 +29,13 @@ func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, e return buf.Bytes(), nil } + +// ProtoMarshalJSONI same as ProtoMarshalJSON, but does msg type inspection to assert +// that it implements `proto.Message` and return an error if it doesn't. +func ProtoMarshalJSONI(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { + msgProto, ok := msg.(proto.Message) + if !ok { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", msgProto) + } + return ProtoMarshalJSON(msgProto, resolver) +} diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 0e43fa8c5f70..43eff54a51a7 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -12,7 +12,6 @@ import ( "github.com/tendermint/tendermint/p2p" pvm "github.com/tendermint/tendermint/privval" tversion "github.com/tendermint/tendermint/version" - "google.golang.org/protobuf/proto" yaml "gopkg.in/yaml.v2" "github.com/cosmos/cosmos-sdk/codec" @@ -30,11 +29,9 @@ func ShowNodeIDCmd() *cobra.Command { cfg := serverCtx.Config nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) - if err != nil { - return err + if err == nil { + fmt.Println(nodeKey.ID()) } - - fmt.Println(nodeKey.ID()) return nil }, } @@ -50,29 +47,19 @@ func ShowValidatorCmd() *cobra.Command { cfg := serverCtx.Config privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) - valPubKey, err := privValidator.GetPubKey() + pk, err := privValidator.GetPubKey() if err != nil { return err } - - output, _ := cmd.Flags().GetString(cli.OutputFlag) - if strings.ToLower(output) == "json" { - return printlnJSON(valPubKey) - } - - pk, ok := valPubKey.(proto.Message) - - pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey) + out, err := codec.ProtoMarshalJSONI(pk, nil) if err != nil { return err } - - fmt.Println(pubkey) + fmt.Println(out) return nil }, } - cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)") return &cmd } @@ -132,6 +119,8 @@ against which this app has been compiled. } } +// Deprecated: prints the content to the standard output using Legacy Amino +// TODO: add issue to trace it? func printlnJSON(v interface{}) error { cdc := codec.NewLegacyAmino() cryptocodec.RegisterCrypto(cdc) From 7df2182274df6f950ad9ade6ffcdee6f8a918aed Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 3 Nov 2020 23:52:31 +0100 Subject: [PATCH 03/91] wip --- client/keys/add.go | 30 +++---- client/keys/show.go | 12 +-- client/rpc/validators.go | 25 ++---- codec/json.go | 22 ++++- crypto/keyring/output.go | 11 ++- crypto/keys/ed25519/ed25519_test.go | 7 ++ crypto/ledger/ledger_test.go | 8 +- server/tm_cmds.go | 2 +- types/address.go | 1 - .../legacybech32/pk_bench_test.go} | 12 ++- types/bech32/legacybech32/pk_test.go | 26 ++++++ x/auth/legacy/legacytx/stdtx.go | 8 +- x/auth/vesting/types/vesting_account.go | 83 ++++++++----------- x/slashing/client/rest/query.go | 7 +- x/slashing/keeper/keeper.go | 37 +++------ x/slashing/simulation/decoder_test.go | 8 +- x/staking/legacy/v036/types.go | 3 +- 17 files changed, 158 insertions(+), 144 deletions(-) rename types/{address_bench_test.go => bech32/legacybech32/pk_bench_test.go} (74%) create mode 100644 types/bech32/legacybech32/pk_test.go diff --git a/client/keys/add.go b/client/keys/add.go index 734ad24f8042..685ae23b8d1e 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -61,21 +61,21 @@ the flag --nosort is set. Args: cobra.ExactArgs(1), RunE: runAddCmdPrepare, } - flags := cmd.Flags() - flags.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") - flags.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") - flags.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") - flags.String(FlagPublicKey, "", "Parse a public key in bech32 format and save it to disk") - flags.BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic") - flags.Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") - flags.Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating") - flags.Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)") - flags.Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore") - flags.String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)") - flags.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") - flags.Uint32(flagAccount, 0, "Account number for HD derivation") - flags.Uint32(flagIndex, 0, "Address index number for HD derivation") - flags.String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + f := cmd.Flags() + f.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") + f.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") + f.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") + f.String(FlagPublicKey, "", "Parse a public key in bech32 format and save it to disk") + f.BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic") + f.Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") + f.Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating") + f.Bool(flagNoBackup, false, "Don't print out seed phrase (if others are watching the terminal)") + f.Bool(flags.FlagDryRun, false, "Perform action, but don't add key to local keystore") + f.String(flagHDPath, "", "Manual HD Path derivation (overrides BIP44 config)") + f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") + f.Uint32(flagAccount, 0, "Account number for HD derivation") + f.Uint32(flagIndex, 0, "Address index number for HD derivation") + f.String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") cmd.SetOut(cmd.OutOrStdout()) cmd.SetErr(cmd.ErrOrStderr()) diff --git a/client/keys/show.go b/client/keys/show.go index 88611bfe9c32..4c705cf8a20f 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -41,12 +41,12 @@ consisting of all the keys provided by name and multisig threshold.`, Args: cobra.MinimumNArgs(1), RunE: runShowCmd, } - flags := cmd.Flags() - flags.String(FlagBechPrefix, sdk.PrefixAccount, "The Bech32 prefix encoding for a key (acc|val|cons)") - flags.BoolP(FlagAddress, "a", false, "Output the address only (overrides --output)") - flags.BoolP(FlagPublicKey, "p", false, "Output the public key only (overrides --output)") - flags.BoolP(FlagDevice, "d", false, "Output the address in a ledger device") - flags.Int(flagMultiSigThreshold, 1, "K out of N required signatures") + f := cmd.Flags() + f.String(FlagBechPrefix, sdk.PrefixAccount, "The Bech32 prefix encoding for a key (acc|val|cons)") + f.BoolP(FlagAddress, "a", false, "Output the address only (overrides --output)") + f.BoolP(FlagPublicKey, "p", false, "Output the public key only (overrides --output)") + f.BoolP(FlagDevice, "d", false, "Output the address in a ledger device") + f.Int(flagMultiSigThreshold, 1, "K out of N required signatures") return cmd } diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 591d3354c56c..0a8fdd1fc56c 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -10,6 +10,7 @@ import ( "github.com/gorilla/mux" "github.com/spf13/cobra" + "github.com/tendermint/tendermint/crypto" tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" @@ -63,10 +64,10 @@ func ValidatorCommand() *cobra.Command { return cmd } -// Validator output in bech32 format +// Validator output type ValidatorOutput struct { Address sdk.ConsAddress `json:"address"` - PubKey string `json:"pub_key"` + PubKey crypto.PubKey `json:"pub_key"` ProposerPriority int64 `json:"proposer_priority"` VotingPower int64 `json:"voting_power"` } @@ -98,18 +99,13 @@ func (rvo ResultValidatorsOutput) String() string { return b.String() } -func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) { - bechValPubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, validator.PubKey) - if err != nil { - return ValidatorOutput{}, err - } - +func validatorOutput(validator *tmtypes.Validator) ValidatorOutput { return ValidatorOutput{ Address: sdk.ConsAddress(validator.Address), - PubKey: bechValPubkey, + PubKey: validator.PubKey, ProposerPriority: validator.ProposerPriority, VotingPower: validator.VotingPower, - }, nil + } } // GetValidators from client @@ -125,19 +121,16 @@ func GetValidators(clientCtx client.Context, height *int64, page, limit *int) (R return ResultValidatorsOutput{}, err } - outputValidatorsRes := ResultValidatorsOutput{ + out := ResultValidatorsOutput{ BlockHeight: validatorsRes.BlockHeight, Validators: make([]ValidatorOutput, len(validatorsRes.Validators)), } for i := 0; i < len(validatorsRes.Validators); i++ { - outputValidatorsRes.Validators[i], err = bech32ValidatorOutput(validatorsRes.Validators[i]) - if err != nil { - return ResultValidatorsOutput{}, err - } + out.Validators[i] = validatorOutput(validatorsRes.Validators[i]) } - return outputValidatorsRes, nil + return out, nil } // REST diff --git a/codec/json.go b/codec/json.go index b0207cc9736a..0c8dda4b2596 100644 --- a/codec/json.go +++ b/codec/json.go @@ -10,12 +10,17 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) +var defaultJM = &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: nil} + // ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded // bytes of a message. func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, error) { // We use the OrigName because camel casing fields just doesn't make sense. // EmitDefaults is also often the more expected behavior for CLI users - jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: resolver} + jm := defaultJM + if resolver != nil { + jm = &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: resolver} + } err := types.UnpackInterfaces(msg, types.ProtoJSONPacker{JSONPBMarshaler: jm}) if err != nil { return nil, err @@ -33,9 +38,18 @@ func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, e // ProtoMarshalJSONI same as ProtoMarshalJSON, but does msg type inspection to assert // that it implements `proto.Message` and return an error if it doesn't. func ProtoMarshalJSONI(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { - msgProto, ok := msg.(proto.Message) - if !ok { - return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", msgProto) + msgProto, err := AssertProtoMsg(msg) + if err != nil { + return nil, err } return ProtoMarshalJSON(msgProto, resolver) } + +// AssertProtoMsg casts i to a proto.Message. Returns an error if it's not possible. +func AssertProtoMsg(i interface{}) (proto.Message, error) { + pm, ok := i.(proto.Message) + if !ok { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", i) + } + return pm, nil +} diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 5f76789caadf..fd3f9b8e8700 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -2,8 +2,11 @@ package keyring import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) +// TODO: Update this file and remove legacybech32 ? + // KeyOutput defines a structure wrapping around an Info object used for output // functionality. type KeyOutput struct { @@ -52,7 +55,7 @@ func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) { func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) { consAddr := sdk.ConsAddress(keyInfo.GetPubKey().Address().Bytes()) - bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, keyInfo.GetPubKey()) + bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, keyInfo.GetPubKey()) if err != nil { return KeyOutput{}, err } @@ -64,7 +67,7 @@ func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) { func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes()) - bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, keyInfo.GetPubKey()) + bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeValPub, keyInfo.GetPubKey()) if err != nil { return KeyOutput{}, err } @@ -77,7 +80,7 @@ func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { // public keys will be added. func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { accAddr := sdk.AccAddress(keyInfo.GetPubKey().Address().Bytes()) - bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, keyInfo.GetPubKey()) + bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, keyInfo.GetPubKey()) if err != nil { return KeyOutput{}, err } @@ -90,7 +93,7 @@ func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { for i, pk := range mInfo.PubKeys { accAddr := sdk.AccAddress(pk.PubKey.Address().Bytes()) - bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk.PubKey) + bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pk.PubKey) if err != nil { return KeyOutput{}, err } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 6b2ad36dc4f3..a95ae517f1f5 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -228,3 +228,10 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { }) } } + +// TODO - finish this test to show who the key will be presented in YAML +func TestMarshalYAML(t *testing.T) { + privKey := ed25519.GenPrivKey() + pubKey := privKey.PubKey() + +} diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index 9b21e464bcdb..5d463f635351 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -5,9 +5,9 @@ import ( "testing" "github.com/stretchr/testify/require" - tmcrypto "github.com/tendermint/tendermint/crypto" + "github.com/cosmos/cosmos-sdk/codec" cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/testutil" @@ -32,10 +32,10 @@ func TestPublicKeyUnsafe(t *testing.T) { fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey()) + out, err := codec.ProtoMarshalJSONI(pk, nil) require.NoError(t, err) - require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + // TODO: require.Equal(t, out, ...) + fmt.Println("TODO ledger_test.go", out) addr := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 43eff54a51a7..7846f7a939d3 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -55,7 +55,7 @@ func ShowValidatorCmd() *cobra.Command { if err != nil { return err } - fmt.Println(out) + fmt.Println(string(out)) return nil }, } diff --git a/types/address.go b/types/address.go index 0f92e8667b3f..e7df4839830a 100644 --- a/types/address.go +++ b/types/address.go @@ -233,7 +233,6 @@ func (aa AccAddress) String() string { } bech32PrefixAccAddr := GetConfig().GetBech32AccountAddrPrefix() - bech32Addr, err := bech32.ConvertAndEncode(bech32PrefixAccAddr, aa.Bytes()) if err != nil { panic(err) diff --git a/types/address_bench_test.go b/types/bech32/legacybech32/pk_bench_test.go similarity index 74% rename from types/address_bench_test.go rename to types/bech32/legacybech32/pk_bench_test.go index 59222dacf9b7..47a6b52bec02 100644 --- a/types/address_bench_test.go +++ b/types/bech32/legacybech32/pk_bench_test.go @@ -1,14 +1,12 @@ -package types_test +package legacybech32 import ( "math/rand" "testing" "time" - "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" ) func BenchmarkBech32ifyPubKey(b *testing.B) { @@ -23,7 +21,7 @@ func BenchmarkBech32ifyPubKey(b *testing.B) { rng.Read(pk.Key) b.StartTimer() - _, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) + _, err := Bech32ifyPubKey(Bech32PubKeyTypeConsPub, pk) require.NoError(b, err) } } @@ -39,11 +37,11 @@ func BenchmarkGetPubKeyFromBech32(b *testing.B) { b.StopTimer() rng.Read(pk.Key) - pkStr, err := types.Bech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pk) + pkStr, err := Bech32ifyPubKey(Bech32PubKeyTypeConsPub, pk) require.NoError(b, err) b.StartTimer() - pk2, err := types.GetPubKeyFromBech32(types.Bech32PubKeyTypeConsPub, pkStr) + pk2, err := GetPubKeyFromBech32(Bech32PubKeyTypeConsPub, pkStr) require.NoError(b, err) require.Equal(b, pk, pk2) } diff --git a/types/bech32/legacybech32/pk_test.go b/types/bech32/legacybech32/pk_test.go new file mode 100644 index 000000000000..0f2a63ad9f8e --- /dev/null +++ b/types/bech32/legacybech32/pk_test.go @@ -0,0 +1,26 @@ +package legacybech32 + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/ledger" + "github.com/cosmos/cosmos-sdk/testutil" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func TestBeach32ifPbKey(t *testing.T) { + require := require.New(t) + path := *hd.NewFundraiserParams(0, sdk.CoinType, 0) + priv, err := ledger.NewPrivKeySecp256k1Unsafe(path) + require.Nil(err, "%s", err) + require.NotNil(priv) + + pubKeyAddr, err := Bech32ifyPubKey(Bech32PubKeyTypeAccPub, priv.PubKey()) + require.NoError(err) + require.Equal("cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", + pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + +} diff --git a/x/auth/legacy/legacytx/stdtx.go b/x/auth/legacy/legacytx/stdtx.go index 270a8d727a21..20ce926e80d8 100644 --- a/x/auth/legacy/legacytx/stdtx.go +++ b/x/auth/legacy/legacytx/stdtx.go @@ -6,6 +6,7 @@ import ( "github.com/tendermint/tendermint/crypto" "gopkg.in/yaml.v2" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/legacy" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -92,13 +93,12 @@ func (ss StdSignature) GetPubKey() crypto.PubKey { func (ss StdSignature) MarshalYAML() (interface{}, error) { var ( bz []byte - pubkey string + pubkey []byte err error ) if ss.PubKey != nil { - pubkey, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, ss.GetPubKey()) - if err != nil { + if pubkey, err = codec.ProtoMarshalJSONI(ss.PubKey, nil); err != nil { return nil, err } } @@ -107,7 +107,7 @@ func (ss StdSignature) MarshalYAML() (interface{}, error) { PubKey string Signature string }{ - PubKey: pubkey, + PubKey: string(pubkey), Signature: fmt.Sprintf("%X", ss.Signature), }) if err != nil { diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index e011bd6efbbf..4eaa8030c5c4 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -6,9 +6,11 @@ import ( yaml "gopkg.in/yaml.v2" + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" + "github.com/tendermint/tendermint/crypto" ) // Compile-time type assertions @@ -188,7 +190,7 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) { return nil, err } - alias := vestingAccountYAML{ + out := vestingAccountYAML{ Address: accAddr, AccountNumber: bva.AccountNumber, Sequence: bva.Sequence, @@ -197,23 +199,10 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) { DelegatedVesting: bva.DelegatedVesting, EndTime: bva.EndTime, } - - pk := bva.GetPubKey() - if pk != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) - if err != nil { - return nil, err - } - - alias.PubKey = pks - } - - bz, err := yaml.Marshal(alias) - if err != nil { + if out.PubKey, err = getPKString(bva); err != nil { return nil, err } - - return string(bz), err + return marshalYaml(out) } //----------------------------------------------------------------------------- @@ -316,7 +305,7 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { return nil, err } - alias := vestingAccountYAML{ + out := vestingAccountYAML{ Address: accAddr, AccountNumber: cva.AccountNumber, Sequence: cva.Sequence, @@ -326,23 +315,10 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { EndTime: cva.EndTime, StartTime: cva.StartTime, } - - pk := cva.GetPubKey() - if pk != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) - if err != nil { - return nil, err - } - - alias.PubKey = pks - } - - bz, err := yaml.Marshal(alias) - if err != nil { + if out.PubKey, err = getPKString(cva); err != nil { return nil, err } - - return string(bz), err + return marshalYaml(out) } //----------------------------------------------------------------------------- @@ -474,7 +450,7 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { return nil, err } - alias := vestingAccountYAML{ + out := vestingAccountYAML{ Address: accAddr, AccountNumber: pva.AccountNumber, Sequence: pva.Sequence, @@ -485,23 +461,10 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { StartTime: pva.StartTime, VestingPeriods: pva.VestingPeriods, } - - pk := pva.GetPubKey() - if pk != nil { - pks, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk) - if err != nil { - return nil, err - } - - alias.PubKey = pks - } - - bz, err := yaml.Marshal(alias) - if err != nil { + if out.PubKey, err = getPKString(pva); err != nil { return nil, err } - - return string(bz), err + return marshalYaml(out) } //----------------------------------------------------------------------------- @@ -570,3 +533,27 @@ func (dva DelayedVestingAccount) String() string { out, _ := dva.MarshalYAML() return out.(string) } + +type getPK interface { + GetPubKey() crypto.PubKey +} + +func getPKString(g getPK) (string, err) { + if pk := g.GetPubKey(); pk != nil { + // TODO check if it's ok to change a type of ValidatorOutput.PubKey to crypto.PubKey + pk, err := codec.ProtoMarshalJSONI(pk, nil) + if err != nil { + return nil, err + } + return string(pk), err + } + return "", nil +} + +func marshalYaml(i interface{}) (interface{}, error) { + bz, err := yaml.Marshal(i) + if err != nil { + return nil, err + } + return string(bz), nil +} diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index 0e1c68da145d..fd9be86a0596 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -7,7 +7,7 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -29,12 +29,11 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) { ).Methods("GET") } -// http request handler to query signing info -// [DEPRECATED] +// Deprecated: http request handler to query signing info func signingInfoHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, vars["validatorPubKey"]) + pk, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, vars["validatorPubKey"]) if rest.CheckBadRequestError(w, err) { return } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index ee46915b745b..099889763c0d 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -3,8 +3,6 @@ package keeper import ( "fmt" - gogotypes "github.com/gogo/protobuf/types" - "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/log" @@ -42,33 +40,27 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // AddPubkey sets a address-pubkey relation -func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) { +func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) error { addr := pubkey.Address() - - pkStr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pubkey) + pkProto, err := codec.AssertProtoMsg(pubkey) if err != nil { - panic(fmt.Errorf("error while setting address-pubkey relation: %s", addr)) + return err } - - k.setAddrPubkeyRelation(ctx, addr, pkStr) + store := ctx.KVStore(k.storeKey) + bz := k.cdc.MustMarshalBinaryBare(pkProto) + store.Set(types.AddrPubkeyRelationKey(addr), bz) + return nil } // GetPubkey returns the pubkey from the adddress-pubkey relation func (k Keeper) GetPubkey(ctx sdk.Context, address crypto.Address) (crypto.PubKey, error) { store := ctx.KVStore(k.storeKey) - - var pubkey gogotypes.StringValue - err := k.cdc.UnmarshalBinaryBare(store.Get(types.AddrPubkeyRelationKey(address)), &pubkey) - if err != nil { + var pubkey crypto.PubKey + bz := store.Get(types.AddrPubkeyRelationKey(address)) + if bz == nil { return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(address)) } - - pkStr, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pubkey.Value) - if err != nil { - return pkStr, err - } - - return pkStr, nil + return pubkey, k.cdc.UnmarshalBinaryBare(bz, &pubkey) } // Slash attempts to slash a validator. The slash is delegated to the staking @@ -99,13 +91,6 @@ func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) { k.sk.Jail(ctx, consAddr) } -func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address, pubkey string) { - store := ctx.KVStore(k.storeKey) - - bz := k.cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: pubkey}) - store.Set(types.AddrPubkeyRelationKey(addr), bz) -} - func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address) { store := ctx.KVStore(k.storeKey) store.Delete(types.AddrPubkeyRelationKey(addr)) diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index e6122f0e0a98..d55bd7a28181 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -8,6 +8,7 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,15 +30,16 @@ func TestDecodeStore(t *testing.T) { dec := simulation.NewDecodeStore(cdc) info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) - bechPK := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, delPk1) missed := gogotypes.BoolValue{Value: true} + delPk1Proto, err := codec.AssertProtoMsg(delPk1) + require.NoError(t, err) kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.ValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(&info)}, {Key: types.ValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryBare(&missed)}, - {Key: types.AddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: bechPK})}, - {Key: []byte{0x99}, Value: []byte{0x99}}, + {Key: types.AddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(delPk1)}, + {Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic }, } diff --git a/x/staking/legacy/v036/types.go b/x/staking/legacy/v036/types.go index 6c110bbe57dc..258fd7057934 100644 --- a/x/staking/legacy/v036/types.go +++ b/x/staking/legacy/v036/types.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/legacy" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v034" ) @@ -89,7 +90,7 @@ func NewGenesisState( } func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, v.ConsPubKey) if err != nil { return nil, err } From 9198817f286b5f8a2803d2ed6bc5928278c90c10 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 5 Nov 2020 13:44:43 +0100 Subject: [PATCH 04/91] move proto json encoding helper functions to internal --- codec/json.go | 20 ------------------ crypto/ledger/ledger_test.go | 4 ++-- internal/protocdc/marshal.go | 27 +++++++++++++++++++++++++ server/tm_cmds.go | 3 ++- x/auth/legacy/legacytx/stdtx.go | 4 ++-- x/auth/vesting/types/vesting_account.go | 4 ++-- x/slashing/keeper/keeper.go | 2 +- x/slashing/simulation/decoder_test.go | 6 +++--- x/staking/legacy/v036/types.go | 4 ++-- 9 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 internal/protocdc/marshal.go diff --git a/codec/json.go b/codec/json.go index 0c8dda4b2596..b7325c67c10f 100644 --- a/codec/json.go +++ b/codec/json.go @@ -7,7 +7,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/cosmos/cosmos-sdk/codec/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) var defaultJM = &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: nil} @@ -34,22 +33,3 @@ func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, e return buf.Bytes(), nil } - -// ProtoMarshalJSONI same as ProtoMarshalJSON, but does msg type inspection to assert -// that it implements `proto.Message` and return an error if it doesn't. -func ProtoMarshalJSONI(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { - msgProto, err := AssertProtoMsg(msg) - if err != nil { - return nil, err - } - return ProtoMarshalJSON(msgProto, resolver) -} - -// AssertProtoMsg casts i to a proto.Message. Returns an error if it's not possible. -func AssertProtoMsg(i interface{}) (proto.Message, error) { - pm, ok := i.(proto.Message) - if !ok { - return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", i) - } - return pm, nil -} diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index 5d463f635351..ac09ea4d5daa 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -7,9 +7,9 @@ import ( "github.com/stretchr/testify/require" tmcrypto "github.com/tendermint/tendermint/crypto" - "github.com/cosmos/cosmos-sdk/codec" cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/internal/protocdc" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -32,7 +32,7 @@ func TestPublicKeyUnsafe(t *testing.T) { fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - out, err := codec.ProtoMarshalJSONI(pk, nil) + out, err := protocdc.ProtoMarshalJSONI(pk, nil) require.NoError(t, err) // TODO: require.Equal(t, out, ...) fmt.Println("TODO ledger_test.go", out) diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go new file mode 100644 index 000000000000..b64dce0b2356 --- /dev/null +++ b/internal/protocdc/marshal.go @@ -0,0 +1,27 @@ +package protocdc + +import ( + "github.com/cosmos/cosmos-sdk/codec" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/gogo/protobuf/jsonpb" + "google.golang.org/protobuf/proto" +) + +// MarshalJSONI same as codec.ProtoMarshalJSON, but does msg type inspection to assert +// that it implements `proto.Message` and return an error if it doesn't. +func MarshalJSONI(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { + msgProto, err := AssertMsg(msg) + if err != nil { + return nil, err + } + return codec.ProtoMarshalJSON(msgProto, resolver) +} + +// AssertMsg casts i to a proto.Message. Returns an error if it's not possible. +func AssertMsg(i interface{}) (proto.Message, error) { + pm, ok := i.(proto.Message) + if !ok { + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", i) + } + return pm, nil +} diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 7846f7a939d3..6cc4a935ea50 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -51,7 +52,7 @@ func ShowValidatorCmd() *cobra.Command { if err != nil { return err } - out, err := codec.ProtoMarshalJSONI(pk, nil) + out, err := protocdc.MarshalJSONI(pk, nil) if err != nil { return err } diff --git a/x/auth/legacy/legacytx/stdtx.go b/x/auth/legacy/legacytx/stdtx.go index 20ce926e80d8..2689bafdcc6f 100644 --- a/x/auth/legacy/legacytx/stdtx.go +++ b/x/auth/legacy/legacytx/stdtx.go @@ -6,9 +6,9 @@ import ( "github.com/tendermint/tendermint/crypto" "gopkg.in/yaml.v2" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/legacy" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" txtypes "github.com/cosmos/cosmos-sdk/types/tx" @@ -98,7 +98,7 @@ func (ss StdSignature) MarshalYAML() (interface{}, error) { ) if ss.PubKey != nil { - if pubkey, err = codec.ProtoMarshalJSONI(ss.PubKey, nil); err != nil { + if pubkey, err = protocdc.MarshalJSONI(ss.PubKey, nil); err != nil { return nil, err } } diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index 4eaa8030c5c4..0db4ece65905 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -6,7 +6,7 @@ import ( yaml "gopkg.in/yaml.v2" - "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" @@ -541,7 +541,7 @@ type getPK interface { func getPKString(g getPK) (string, err) { if pk := g.GetPubKey(); pk != nil { // TODO check if it's ok to change a type of ValidatorOutput.PubKey to crypto.PubKey - pk, err := codec.ProtoMarshalJSONI(pk, nil) + pk, err := protocdc.MarshalJSONI(pk, nil) if err != nil { return nil, err } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 099889763c0d..1d3abe879c6a 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -42,7 +42,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { // AddPubkey sets a address-pubkey relation func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) error { addr := pubkey.Address() - pkProto, err := codec.AssertProtoMsg(pubkey) + pkProto, err := protocdc.AssertMsg(pubkey) if err != nil { return err } diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index d55bd7a28181..3cb2153aa2b4 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -8,8 +8,8 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/internal/protocdc" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" @@ -31,14 +31,14 @@ func TestDecodeStore(t *testing.T) { info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) missed := gogotypes.BoolValue{Value: true} - delPk1Proto, err := codec.AssertProtoMsg(delPk1) + delPk1Proto, err := protocdc.AssertMsg(delPk1) require.NoError(t, err) kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.ValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(&info)}, {Key: types.ValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryBare(&missed)}, - {Key: types.AddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(delPk1)}, + {Key: types.AddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(delPk1Proto)}, {Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic }, } diff --git a/x/staking/legacy/v036/types.go b/x/staking/legacy/v036/types.go index 258fd7057934..753553856948 100644 --- a/x/staking/legacy/v036/types.go +++ b/x/staking/legacy/v036/types.go @@ -90,7 +90,7 @@ func NewGenesisState( } func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, v.ConsPubKey) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, bv.ConsPubKey) if err != nil { return err } From 412ccc43e9e815808084525b16820a17158732ca Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 9 Nov 2020 15:09:05 +0100 Subject: [PATCH 05/91] update internal/marshal --- client/keys/add.go | 7 ++++--- internal/protocdc/marshal.go | 4 ++-- server/tm_cmds.go | 2 +- x/auth/legacy/legacytx/stdtx.go | 2 +- x/auth/vesting/types/vesting_account.go | 4 ++-- x/slashing/keeper/keeper.go | 6 +++++- x/staking/client/cli/tx.go | 3 ++- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 685ae23b8d1e..99631b963dc8 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -18,6 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -153,7 +154,6 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring multisigKeys, _ := cmd.Flags().GetStringSlice(flagMultisig) if len(multisigKeys) != 0 { var pks []crypto.PubKey - multisigThreshold, _ := cmd.Flags().GetInt(flagMultiSigThreshold) if err := validateMultisigThreshold(multisigThreshold, len(multisigKeys)); err != nil { return err @@ -186,11 +186,12 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring pubKey, _ := cmd.Flags().GetString(FlagPublicKey) if pubKey != "" { - var pk crypto.PubKey + var pk cryptotypes.PubKey // TODO: shall we use KeysCdc here (global from this module, = codec.NewLegacyAmino)? - if err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pubKey), &pk); err != nil { + if err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pubKey), pk); err != nil { return err } + fmt.Println("TODO: Check", pk) _, err := kb.SavePubKey(name, pk, algo.Name()) return err } diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index b64dce0b2356..90fc526ccdb3 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -7,9 +7,9 @@ import ( "google.golang.org/protobuf/proto" ) -// MarshalJSONI same as codec.ProtoMarshalJSON, but does msg type inspection to assert +// MarshalJSON same as codec.ProtoMarshalJSON, but does msg type inspection to assert // that it implements `proto.Message` and return an error if it doesn't. -func MarshalJSONI(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { +func MarshalJSON(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { msgProto, err := AssertMsg(msg) if err != nil { return nil, err diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 6cc4a935ea50..1afe8dbebc74 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -52,7 +52,7 @@ func ShowValidatorCmd() *cobra.Command { if err != nil { return err } - out, err := protocdc.MarshalJSONI(pk, nil) + out, err := protocdc.MarshalJSON(pk, nil) if err != nil { return err } diff --git a/x/auth/legacy/legacytx/stdtx.go b/x/auth/legacy/legacytx/stdtx.go index 2689bafdcc6f..2a578e455c54 100644 --- a/x/auth/legacy/legacytx/stdtx.go +++ b/x/auth/legacy/legacytx/stdtx.go @@ -98,7 +98,7 @@ func (ss StdSignature) MarshalYAML() (interface{}, error) { ) if ss.PubKey != nil { - if pubkey, err = protocdc.MarshalJSONI(ss.PubKey, nil); err != nil { + if pubkey, err = protocdc.MarshalJSON(ss.PubKey, nil); err != nil { return nil, err } } diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index 0db4ece65905..44a1d1f9e07b 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -538,10 +538,10 @@ type getPK interface { GetPubKey() crypto.PubKey } -func getPKString(g getPK) (string, err) { +func getPKString(g getPK) (string, error) { if pk := g.GetPubKey(); pk != nil { // TODO check if it's ok to change a type of ValidatorOutput.PubKey to crypto.PubKey - pk, err := protocdc.MarshalJSONI(pk, nil) + pk, err := protocdc.MarshalJSON(pk, nil) if err != nil { return nil, err } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 1d3abe879c6a..0c5886955551 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -7,6 +7,7 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -47,7 +48,10 @@ func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) error { return err } store := ctx.KVStore(k.storeKey) - bz := k.cdc.MustMarshalBinaryBare(pkProto) + bz, err := k.cdc.MarshalBinaryBare(pkProto) + if err != nil { + return err + } store.Set(types.AddrPubkeyRelationKey(addr), bz) return nil } diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 563ec6c49dbf..374335f49ad7 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -307,7 +308,7 @@ func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl valAddr := clientCtx.GetFromAddress() pkStr, _ := fs.GetString(FlagPubKey) - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr) + pk, err := legacybech32.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr) if err != nil { return txf, nil, err } From 35275d76cb5ee03c0bc8cbd3e408b66f82a330bd Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 9 Nov 2020 15:20:30 +0100 Subject: [PATCH 06/91] wip --- client/debug/main.go | 17 +++++++++-------- crypto/keyring/output_test.go | 5 +++-- crypto/keyring/types_test.go | 3 ++- crypto/ledger/ledger_test.go | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index ab9ec0cf8a32..0ad934b5ae6c 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/version" ) @@ -49,17 +50,17 @@ func getPubKeyFromString(pkstr string) (crypto.PubKey, error) { } } - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, pkstr) + pk, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, pkstr) if err == nil { return pk, nil } - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeValPub, pkstr) + pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeValPub, pkstr) if err == nil { return pk, nil } - pk, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkstr) + pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, pkstr) if err == nil { return pk, nil } @@ -95,15 +96,15 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg if err != nil { return err } - accPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, edPK) + accPub, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, edPK) if err != nil { return err } - valPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, edPK) + valPub, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeValPub, edPK) if err != nil { return err } - consenusPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, edPK) + consenusPub, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, edPK) if err != nil { return err } @@ -125,7 +126,7 @@ func AddrCmd() *cobra.Command { Use: "addr [address]", Short: "Convert an address between hex and bech32", Long: fmt.Sprintf(`Convert an address between hex encoding and bech32. - + Example: $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg `, version.AppName), @@ -169,7 +170,7 @@ func RawBytesCmd() *cobra.Command { Use: "raw-bytes [raw-bytes]", Short: "Convert raw bytes output (eg. [10 21 13 255]) to hex", Long: fmt.Sprintf(`Convert raw-bytes to hex. - + Example: $ %s debug raw-bytes [72 101 108 108 111 44 32 112 108 97 121 103 114 111 117 110 100] `, version.AppName), diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index dc98ad62bc8a..e663a91e8a70 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -9,17 +9,18 @@ import ( kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func TestBech32KeysOutput(t *testing.T) { tmpKey := secp256k1.GenPrivKey().PubKey() - bechTmpKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, tmpKey) + bechTmpKey := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, tmpKey) tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes()) multisigPks := kmultisig.NewLegacyAminoPubKey(1, []crypto.PubKey{tmpKey}) multiInfo := NewMultiInfo("multisig", multisigPks) accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) - bechPubKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, multiInfo.GetPubKey()) + bechPubKey := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, multiInfo.GetPubKey()) expectedOutput := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType().String(), accAddr.String(), bechPubKey) expectedOutput.Threshold = 1 diff --git a/crypto/keyring/types_test.go b/crypto/keyring/types_test.go index ca99d9b7c55a..b80e2eabc095 100644 --- a/crypto/keyring/types_test.go +++ b/crypto/keyring/types_test.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func Test_writeReadLedgerInfo(t *testing.T) { @@ -24,7 +25,7 @@ func Test_writeReadLedgerInfo(t *testing.T) { assert.Equal(t, "44'/118'/5'/0/1", path.String()) assert.Equal(t, "cosmospub1addwnpepqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5wmxv8p", - sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, lInfo.GetPubKey())) + legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, lInfo.GetPubKey())) // Serialize and restore serialized := marshalInfo(lInfo) diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index ac09ea4d5daa..c66c28ece944 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -32,7 +32,7 @@ func TestPublicKeyUnsafe(t *testing.T) { fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - out, err := protocdc.ProtoMarshalJSONI(pk, nil) + out, err := protocdc.MarshalJSON(pk, nil) require.NoError(t, err) // TODO: require.Equal(t, out, ...) fmt.Println("TODO ledger_test.go", out) From dd6f93b842cd5d8e729a17598659b6292ee31363 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 16 Nov 2020 19:23:42 +0100 Subject: [PATCH 07/91] update sections which needs legacybech32 --- client/debug/main.go | 49 +++++-------------------- crypto/keyring/output.go | 25 +++++++++---- crypto/keyring/output_test.go | 9 +++-- crypto/keys/ed25519/ed25519_test.go | 1 - crypto/ledger/ledger_test.go | 9 +++-- types/address_test.go | 7 ++-- x/auth/vesting/types/vesting_account.go | 2 +- x/slashing/simulation/decoder_test.go | 2 +- x/staking/client/cli/tx.go | 12 ++++-- x/staking/legacy/v034/types.go | 5 ++- x/staking/legacy/v038/types.go | 5 ++- 11 files changed, 57 insertions(+), 69 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 0ad934b5ae6c..12b5ffb6a1f5 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -8,16 +8,16 @@ import ( "strings" "github.com/spf13/cobra" - "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/version" ) +// Cmd creats a main CLI command func Cmd() *cobra.Command { cmd := &cobra.Command{ Use: "debug", @@ -35,7 +35,8 @@ func Cmd() *cobra.Command { // getPubKeyFromString returns a Tendermint PubKey (PubKeyEd25519) by attempting // to decode the pubkey string from hex, base64, and finally bech32. If all // encodings fail, an error is returned. -func getPubKeyFromString(pkstr string) (crypto.PubKey, error) { +func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) { + // TODO: shall we clean it? Do we support HEX / base64 keys bz, err := hex.DecodeString(pkstr) if err == nil { if len(bz) == ed25519.PubKeySize { @@ -50,29 +51,16 @@ func getPubKeyFromString(pkstr string) (crypto.PubKey, error) { } } - pk, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, pkstr) - if err == nil { - return pk, nil - } - - pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeValPub, pkstr) - if err == nil { - return pk, nil - } - - pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, pkstr) - if err == nil { - return pk, nil - } - - return nil, fmt.Errorf("pubkey '%s' invalid; expected hex, base64, or bech32 of correct size", pkstr) + var pk cryptotypes.PubKey + err = ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) + return pk, err } func PubkeyCmd() *cobra.Command { return &cobra.Command{ Use: "pubkey [pubkey]", - Short: "Decode a ED25519 pubkey from hex, base64, or bech32", - Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32. + Short: "Decode a ED25519 pubkey from hex, base64", + Long: fmt.Sprintf(`Decode a pubkey from hex, base64. Example: $ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz @@ -82,11 +70,10 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg RunE: func(cmd *cobra.Command, args []string) error { clientCtx := client.GetClientContextFromCmd(cmd) - pk, err := getPubKeyFromString(args[0]) + pk, err := getPubKeyFromString(clientCtx, args[0]) if err != nil { return err } - edPK, ok := pk.(*ed25519.PubKey) if !ok { return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type; expected ED25519") @@ -96,26 +83,10 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg if err != nil { return err } - accPub, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, edPK) - if err != nil { - return err - } - valPub, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeValPub, edPK) - if err != nil { - return err - } - consenusPub, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, edPK) - if err != nil { - return err - } cmd.Println("Address:", edPK.Address()) cmd.Printf("Hex: %X\n", edPK.Key) cmd.Println("JSON (base64):", string(pubKeyJSONBytes)) - cmd.Println("Bech32 Acc:", accPub) - cmd.Println("Bech32 Validator Operator:", valPub) - cmd.Println("Bech32 Validator Consensus:", consenusPub) - return nil }, } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index fd3f9b8e8700..94f32e669d5b 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -1,6 +1,8 @@ package keyring import ( + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) @@ -20,13 +22,17 @@ type KeyOutput struct { } // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys -func NewKeyOutput(name, keyType, address, pubkey string) KeyOutput { +func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { + pkBytes, err := protocdc.MarshalJSON(pk, nil) + if err != nil { + return KeyOutput{}, err + } return KeyOutput{ Name: name, - Type: keyType, - Address: address, - PubKey: pubkey, - } + Type: keyType.String(), + Address: a.String(), + PubKey: string(pkBytes), + }, nil } type multisigPubKeyOutput struct { @@ -60,7 +66,7 @@ func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) { return KeyOutput{}, err } - return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType().String(), consAddr.String(), bechPubKey), nil + return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), consAddr, bechPubKey) } // Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes. @@ -72,7 +78,7 @@ func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { return KeyOutput{}, err } - return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType().String(), valAddr.String(), bechPubKey), nil + return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), valAddr, bechPubKey) } // Bech32KeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the @@ -85,7 +91,10 @@ func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { return KeyOutput{}, err } - ko := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType().String(), accAddr.String(), bechPubKey) + ko, err := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), accAddr, bechPubKey) + if err != nil { + return KeyOutput{}, err + } if mInfo, ok := keyInfo.(*multiInfo); ok { pubKeys := make([]multisigPubKeyOutput, len(mInfo.PubKeys)) diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index e663a91e8a70..82bd0d04a184 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -9,20 +9,21 @@ import ( kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func TestBech32KeysOutput(t *testing.T) { + // TODO - update this test to not use bech32 + tmpKey := secp256k1.GenPrivKey().PubKey() - bechTmpKey := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, tmpKey) tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes()) multisigPks := kmultisig.NewLegacyAminoPubKey(1, []crypto.PubKey{tmpKey}) multiInfo := NewMultiInfo("multisig", multisigPks) accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) - bechPubKey := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, multiInfo.GetPubKey()) + require.True(t, accAddr.Equals(tmpAddr)) - expectedOutput := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType().String(), accAddr.String(), bechPubKey) + expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, tmpKey) + require.NoError(t, err) expectedOutput.Threshold = 1 expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), bechTmpKey, 1}} diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index a95ae517f1f5..b4ad9be8cd76 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -233,5 +233,4 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { func TestMarshalYAML(t *testing.T) { privKey := ed25519.GenPrivKey() pubKey := privKey.PubKey() - } diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index c66c28ece944..fadd97e34a57 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -12,6 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/internal/protocdc" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func TestErrorHandling(t *testing.T) { @@ -32,7 +33,7 @@ func TestPublicKeyUnsafe(t *testing.T) { fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - out, err := protocdc.MarshalJSON(pk, nil) + out, err := protocdc.MarshalJSON(priv.PubKey(), nil) require.NoError(t, err) // TODO: require.Equal(t, out, ...) fmt.Println("TODO ledger_test.go", out) @@ -74,7 +75,7 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) { tmp := priv.(PrivKeyLedgerSecp256k1) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, priv.PubKey()) require.NoError(t, err) require.Equal(t, expectedAnswers[i], pubKeyAddr, @@ -110,7 +111,7 @@ func TestPublicKeySafe(t *testing.T) { fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, priv.PubKey()) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) @@ -174,7 +175,7 @@ func TestPublicKeyHDPath(t *testing.T) { tmp := priv.(PrivKeyLedgerSecp256k1) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, priv.PubKey()) require.NoError(t, err) require.Equal(t, expectedPubKeys[i], pubKeyAddr, diff --git a/types/address_test.go b/types/address_test.go index 720995f8a5d4..76b8eb93b481 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -16,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) type addressTestSuite struct { @@ -235,7 +236,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { acc.String(), prefix+types.PrefixAccount), acc.String()) - bech32Pub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeAccPub, pub) + bech32Pub := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pub) s.Require().True(strings.HasPrefix( bech32Pub, prefix+types.PrefixPublic)) @@ -249,7 +250,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { val.String(), prefix+types.PrefixValidator+types.PrefixAddress)) - bech32ValPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeValPub, pub) + bech32ValPub := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeValPub, pub) s.Require().True(strings.HasPrefix( bech32ValPub, prefix+types.PrefixValidator+types.PrefixPublic)) @@ -263,7 +264,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { cons.String(), prefix+types.PrefixConsensus+types.PrefixAddress)) - bech32ConsPub := types.MustBech32ifyPubKey(types.Bech32PubKeyTypeConsPub, pub) + bech32ConsPub := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, pub) s.Require().True(strings.HasPrefix( bech32ConsPub, prefix+types.PrefixConsensus+types.PrefixPublic)) diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index 44a1d1f9e07b..4dc4a08edaba 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -543,7 +543,7 @@ func getPKString(g getPK) (string, error) { // TODO check if it's ok to change a type of ValidatorOutput.PubKey to crypto.PubKey pk, err := protocdc.MarshalJSON(pk, nil) if err != nil { - return nil, err + return "", err } return string(pk), err } diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index 3cb2153aa2b4..59f82e72f21c 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -49,7 +49,7 @@ func TestDecodeStore(t *testing.T) { }{ {"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info)}, {"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed.Value, missed.Value)}, - {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", bechPK, bechPK)}, + {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1Proto, delPk1Proto)}, {"other", ""}, } for i, tt := range tests { diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 374335f49ad7..8bae3f9e37a5 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -12,8 +12,8 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -298,6 +298,7 @@ $ %s tx staking unbond %s1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhffj 100stake --from return cmd } +// NewBuildCreateValidatorMsg constructs a CreateValidatorMsg func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) { fAmount, _ := fs.GetString(FlagAmount) amount, err := sdk.ParseCoin(fAmount) @@ -306,13 +307,16 @@ func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl } valAddr := clientCtx.GetFromAddress() - pkStr, _ := fs.GetString(FlagPubKey) - - pk, err := legacybech32.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, pkStr) + pkStr, err := fs.GetString(FlagPubKey) if err != nil { return txf, nil, err } + var pk cryptotypes.PubKey + if err := clientCtx.JSONMarshaler.UnmarshalJSON([]byte(pkStr), pk); err != nil { + return txf, nil, err + } + moniker, _ := fs.GetString(FlagMoniker) identity, _ := fs.GetString(FlagIdentity) website, _ := fs.GetString(FlagWebsite) diff --git a/x/staking/legacy/v034/types.go b/x/staking/legacy/v034/types.go index 6e93b7986a52..37692de3b732 100644 --- a/x/staking/legacy/v034/types.go +++ b/x/staking/legacy/v034/types.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/legacy" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) const ( @@ -140,7 +141,7 @@ type ( ) func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, v.ConsPubKey) if err != nil { return nil, err } @@ -166,7 +167,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, bv.ConsPubKey) if err != nil { return err } diff --git a/x/staking/legacy/v038/types.go b/x/staking/legacy/v038/types.go index aff9a559d457..616696a04395 100644 --- a/x/staking/legacy/v038/types.go +++ b/x/staking/legacy/v038/types.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec/legacy" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" v034staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v034" v036staking "github.com/cosmos/cosmos-sdk/x/staking/legacy/v036" ) @@ -114,7 +115,7 @@ func NewGenesisState( // MarshalJSON marshals the validator to JSON using Bech32 func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, v.ConsPubKey) if err != nil { return nil, err } @@ -140,7 +141,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, bv.ConsPubKey) if err != nil { return err } From 7af7ab37125de5d0e802cba81eb2b29255ace159 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 16 Nov 2020 23:31:28 +0100 Subject: [PATCH 08/91] update validators output --- client/rpc/validators.go | 19 ++++++++++--------- internal/protocdc/marshal.go | 2 +- x/slashing/keeper/keeper.go | 12 ------------ 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/client/rpc/validators.go b/client/rpc/validators.go index a22e2bbaa48c..0c39cd1b2226 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -9,13 +9,12 @@ import ( "github.com/gorilla/mux" "github.com/spf13/cobra" - - "github.com/tendermint/tendermint/crypto" tmtypes "github.com/tendermint/tendermint/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/rest" ) @@ -67,10 +66,10 @@ func ValidatorCommand() *cobra.Command { // Validator output type ValidatorOutput struct { - Address sdk.ConsAddress `json:"address"` - PubKey crypto.PubKey `json:"pub_key"` - ProposerPriority int64 `json:"proposer_priority"` - VotingPower int64 `json:"voting_power"` + Address sdk.ConsAddress `json:"address"` + PubKey cryptotypes.PubKey `json:"pub_key"` + ProposerPriority int64 `json:"proposer_priority"` + VotingPower int64 `json:"voting_power"` } // Validators at a certain height output in bech32 format @@ -100,7 +99,7 @@ func (rvo ResultValidatorsOutput) String() string { return b.String() } -func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) { +func validatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) { pk, err := cryptocodec.FromTmPubKeyInterface(validator.PubKey) if err != nil { return ValidatorOutput{}, err @@ -131,9 +130,11 @@ func GetValidators(clientCtx client.Context, height *int64, page, limit *int) (R BlockHeight: validatorsRes.BlockHeight, Validators: make([]ValidatorOutput, len(validatorsRes.Validators)), } - for i := 0; i < len(validatorsRes.Validators); i++ { - out.Validators[i] = validatorOutput(validatorsRes.Validators[i]) + out.Validators[i], err = validatorOutput(validatorsRes.Validators[i]) + if err != nil { + return out, err + } } return out, nil diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index 90fc526ccdb3..78ec9db069b3 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -4,7 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/gogo/protobuf/jsonpb" - "google.golang.org/protobuf/proto" + "github.com/gogo/protobuf/proto" ) // MarshalJSON same as codec.ProtoMarshalJSON, but does msg type inspection to assert diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 70236f1922bf..d9a37c7ee060 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -3,25 +3,13 @@ package keeper import ( "fmt" -<<<<<<< HEAD - "github.com/tendermint/tendermint/crypto" -||||||| 4420fe2d5 - gogotypes "github.com/gogo/protobuf/types" - "github.com/tendermint/tendermint/crypto" -======= gogotypes "github.com/gogo/protobuf/types" - ->>>>>>> master "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" -<<<<<<< HEAD "github.com/cosmos/cosmos-sdk/internal/protocdc" -||||||| 4420fe2d5 -======= cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ->>>>>>> master sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) From 8331f33b8b150e97faf6556cc58a1ca370479256 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 16 Nov 2020 23:43:32 +0100 Subject: [PATCH 09/91] fix conflicts --- x/slashing/keeper/keeper.go | 25 +++---------------------- x/staking/client/cli/tx.go | 2 +- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index d9a37c7ee060..6c78a9c3c1ce 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -3,13 +3,13 @@ package keeper import ( "fmt" - "github.com/tendermint/tendermint/crypto" gogotypes "github.com/gogo/protobuf/types" + "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/internal/protocdc" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -43,13 +43,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { } // AddPubkey sets a address-pubkey relation -<<<<<<< HEAD -func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) error { -||||||| 4420fe2d5 -func (k Keeper) AddPubkey(ctx sdk.Context, pubkey crypto.PubKey) { -======= -func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) { ->>>>>>> master +func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error { addr := pubkey.Address() pkProto, err := protocdc.AssertMsg(pubkey) if err != nil { @@ -103,18 +97,6 @@ func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) { k.sk.Jail(ctx, consAddr) } -<<<<<<< HEAD -func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address) { -||||||| 4420fe2d5 -func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address, pubkey string) { - store := ctx.KVStore(k.storeKey) - - bz := k.cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: pubkey}) - store.Set(types.AddrPubkeyRelationKey(addr), bz) -} - -func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr crypto.Address) { -======= func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address, pubkey string) { store := ctx.KVStore(k.storeKey) @@ -123,7 +105,6 @@ func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address, } func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address) { ->>>>>>> master store := ctx.KVStore(k.storeKey) store.Delete(types.AddrPubkeyRelationKey(addr)) } diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index d9f90b5f8439..4086a4c3b9ae 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -411,7 +411,7 @@ type TxCreateValidatorConfig struct { CommissionMaxChangeRate string MinSelfDelegation string - PubKey crypto.PubKey + PubKey cryptotypes.PubKey IP string Website string From 41cbd9fbee7f15395d17bddfdd202aa5d36774bf Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 17 Nov 2020 01:51:52 +0100 Subject: [PATCH 10/91] slashing update --- client/debug/main.go | 18 +--------- crypto/keyring/output.go | 54 ++++++++++------------------- crypto/keys/ed25519/ed25519_test.go | 31 ++++++++++++++++- internal/protocdc/marshal.go | 3 +- types/address.go | 1 - x/genutil/client/cli/gentx.go | 10 +++--- x/slashing/client/cli/query.go | 10 +++--- x/slashing/keeper/keeper.go | 19 ++++------ 8 files changed, 68 insertions(+), 78 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 12b5ffb6a1f5..4bcdfa427994 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -1,7 +1,6 @@ package debug import ( - "encoding/base64" "encoding/hex" "fmt" "strconv" @@ -36,23 +35,8 @@ func Cmd() *cobra.Command { // to decode the pubkey string from hex, base64, and finally bech32. If all // encodings fail, an error is returned. func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) { - // TODO: shall we clean it? Do we support HEX / base64 keys - bz, err := hex.DecodeString(pkstr) - if err == nil { - if len(bz) == ed25519.PubKeySize { - return &ed25519.PubKey{Key: bz}, nil - } - } - - bz, err = base64.StdEncoding.DecodeString(pkstr) - if err == nil { - if len(bz) == ed25519.PubKeySize { - return &ed25519.PubKey{Key: bz}, nil - } - } - var pk cryptotypes.PubKey - err = ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) + err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) return pk, err } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 94f32e669d5b..d94fd367bc42 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -4,10 +4,9 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) -// TODO: Update this file and remove legacybech32 ? +// TODO: Move this file to client/keys // KeyOutput defines a structure wrapping around an Info object used for output // functionality. @@ -46,12 +45,12 @@ type multisigPubKeyOutput struct { // call to Bech32KeyOutput fails. func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) { kos := make([]KeyOutput, len(infos)) + var err error for i, info := range infos { - ko, err := Bech32KeyOutput(info) + kos[i], err = Bech32KeyOutput(info) if err != nil { return nil, err } - kos[i] = ko } return kos, nil @@ -59,55 +58,40 @@ func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) { // Bech32ConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes. func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) { - consAddr := sdk.ConsAddress(keyInfo.GetPubKey().Address().Bytes()) - - bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, keyInfo.GetPubKey()) - if err != nil { - return KeyOutput{}, err - } - - return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), consAddr, bechPubKey) + pk := keyInfo.GetPubKey() + addr := sdk.ConsAddress(pk.Address().Bytes()) + return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } // Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes. func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { - valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes()) - - bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeValPub, keyInfo.GetPubKey()) - if err != nil { - return KeyOutput{}, err - } - - return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), valAddr, bechPubKey) + pk := keyInfo.GetPubKey() + addr := sdk.ValAddress(pk.Address().Bytes()) + return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } // Bech32KeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the // public key is a multisig public key, then the threshold and constituent // public keys will be added. func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { - accAddr := sdk.AccAddress(keyInfo.GetPubKey().Address().Bytes()) - bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, keyInfo.GetPubKey()) - if err != nil { - return KeyOutput{}, err - } - - ko, err := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), accAddr, bechPubKey) + pk := keyInfo.GetPubKey() + addr := sdk.AccAddress(pk.Address().Bytes()) + ko, err := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) if err != nil { - return KeyOutput{}, err + return ko, err } if mInfo, ok := keyInfo.(*multiInfo); ok { pubKeys := make([]multisigPubKeyOutput, len(mInfo.PubKeys)) - for i, pk := range mInfo.PubKeys { - accAddr := sdk.AccAddress(pk.PubKey.Address().Bytes()) - - bechPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pk.PubKey) + for i, pkInfo := range mInfo.PubKeys { + pk = pkInfo.PubKey + addr = sdk.AccAddress(pk.Address().Bytes()) + pkBytes, err := protocdc.MarshalJSON(pk, nil) if err != nil { - return KeyOutput{}, err + return ko, err } - - pubKeys[i] = multisigPubKeyOutput{accAddr.String(), bechPubKey, pk.Weight} + pubKeys[i] = multisigPubKeyOutput{addr.String(), string(pkBytes), pkInfo.Weight} } ko.Threshold = mInfo.Threshold diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 111a9be1cc25..d5f86c32492d 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -3,8 +3,10 @@ package ed25519_test import ( stded25519 "crypto/ed25519" "encoding/base64" + "fmt" "testing" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" @@ -14,6 +16,7 @@ import ( ed25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/simapp" ) func TestSignAndValidateEd25519(t *testing.T) { @@ -230,7 +233,33 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { } // TODO - finish this test to show who the key will be presented in YAML -func TestMarshalYAML(t *testing.T) { +func TestMarshalProto(t *testing.T) { + require := require.New(t) + cdc, _ := simapp.MakeCodecs() + privKey := ed25519.GenPrivKey() pubKey := privKey.PubKey() + + bz, err := cdc.MarshalJSON(pubKey) + require.NoError(err) + var pubKey2 cryptotypes.PubKey + err = cdc.UnmarshalJSON(bz, pubKey2) + require.NoError(err) + + // pubKeyM := pubKey.(codec.ProtoMarshaler) + // bz, err := cdc.MarshalBinaryBare(pubKeyM) + bz, err = proto.Marshal(pubKey) + fmt.Println(bz) + require.NoError(err) + + // var pk cryptotypes.PubKey + // err = proto.Unmarshal(bz, pk) + // require.NoError(err) + + /* + var pk cryptotypes.PubKey + err = cdc.UnmarshalBinaryBare(bz, pk) + require.NoError(err) + require.True(pk.Equals(pubKey)) + */ } diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index 78ec9db069b3..f51f6af56293 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -1,10 +1,11 @@ package protocdc import ( - "github.com/cosmos/cosmos-sdk/codec" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/gogo/protobuf/jsonpb" "github.com/gogo/protobuf/proto" + + "github.com/cosmos/cosmos-sdk/codec" ) // MarshalJSON same as codec.ProtoMarshalJSON, but does msg type inspection to assert diff --git a/types/address.go b/types/address.go index 9c3358dcf90a..46c945eb77aa 100644 --- a/types/address.go +++ b/types/address.go @@ -387,7 +387,6 @@ func (va ValAddress) String() string { } bech32PrefixValAddr := GetConfig().GetBech32ValidatorAddrPrefix() - bech32Addr, err := bech32.ConvertAndEncode(bech32PrefixValAddr, va.Bytes()) if err != nil { panic(err) diff --git a/x/genutil/client/cli/gentx.go b/x/genutil/client/cli/gentx.go index 33a37760eda8..a79bf3ef7d3a 100644 --- a/x/genutil/client/cli/gentx.go +++ b/x/genutil/client/cli/gentx.go @@ -41,9 +41,9 @@ func GenTxCmd(mbm module.BasicManager, txEncCfg client.TxEncodingConfig, genBalI Long: fmt.Sprintf(`Generate a genesis transaction that creates a validator with a self-delegation, that is signed by the key in the Keyring referenced by a given name. A node ID and Bech32 consensus pubkey may optionally be provided. If they are omitted, they will be retrieved from the priv_validator.json -file. The following default parameters are included: +file. The following default parameters are included: %s - + Example: $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id=test-chain-1 \ --amount=1000000stake \ @@ -75,10 +75,10 @@ $ %s gentx my-key-name --home=/path/to/home/dir --keyring-backend=os --chain-id= } // read --pubkey, if empty take it from priv_validator.json - if valPubKeyString, _ := cmd.Flags().GetString(cli.FlagPubKey); valPubKeyString != "" { - valPubKey, err = sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, valPubKeyString) + if val, _ := cmd.Flags().GetString(cli.FlagPubKey); val != "" { + err = clientCtx.JSONMarshaler.UnmarshalJSON([]byte(val), valPubKey) if err != nil { - return errors.Wrap(err, "failed to get consensus node public key") + return errors.Wrap(err, "failed to unmarshal consensus node public key") } } diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index f74b0db96525..f719179acc5d 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -8,8 +8,8 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -51,16 +51,16 @@ $ query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdgexx return err } - queryClient := types.NewQueryClient(clientCtx) - - pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, args[0]) + var pk cryptotypes.PubKey + err = clientCtx.JSONMarshaler.UnmarshalJSON([]byte(args[0]), pk) if err != nil { return err } consAddr := sdk.ConsAddress(pk.Address()) params := &types.QuerySigningInfoRequest{ConsAddress: consAddr.String()} - res, err := queryClient.SigningInfo(context.Background(), params) + queryClient := types.NewQueryClient(clientCtx) + res, err := queryClient.SigningInfo(cmd.Context(), params) if err != nil { return err } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 6c78a9c3c1ce..39c941678505 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -3,8 +3,7 @@ package keeper import ( "fmt" - gogotypes "github.com/gogo/protobuf/types" - "github.com/tendermint/tendermint/crypto" + "github.com/gogo/protobuf/proto" "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" @@ -49,11 +48,11 @@ func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error { if err != nil { return err } - store := ctx.KVStore(k.storeKey) - bz, err := k.cdc.MarshalBinaryBare(pkProto) + bz, err := proto.Marshal(pkProto) if err != nil { return err } + store := ctx.KVStore(k.storeKey) store.Set(types.AddrPubkeyRelationKey(addr), bz) return nil } @@ -61,12 +60,13 @@ func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error { // GetPubkey returns the pubkey from the adddress-pubkey relation func (k Keeper) GetPubkey(ctx sdk.Context, address cryptotypes.Address) (cryptotypes.PubKey, error) { store := ctx.KVStore(k.storeKey) - var pubkey crypto.PubKey bz := store.Get(types.AddrPubkeyRelationKey(address)) if bz == nil { return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(address)) } - return pubkey, k.cdc.UnmarshalBinaryBare(bz, &pubkey) + // TODO Unmarshal Any? Do we need to use Any here? + var pk cryptotypes.PubKey + return pk, nil // TODO proto.Unmarshal(pk) } // Slash attempts to slash a validator. The slash is delegated to the staking @@ -97,13 +97,6 @@ func (k Keeper) Jail(ctx sdk.Context, consAddr sdk.ConsAddress) { k.sk.Jail(ctx, consAddr) } -func (k Keeper) setAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address, pubkey string) { - store := ctx.KVStore(k.storeKey) - - bz := k.cdc.MustMarshalBinaryBare(&gogotypes.StringValue{Value: pubkey}) - store.Set(types.AddrPubkeyRelationKey(addr), bz) -} - func (k Keeper) deleteAddrPubkeyRelation(ctx sdk.Context, addr cryptotypes.Address) { store := ctx.KVStore(k.storeKey) store.Delete(types.AddrPubkeyRelationKey(addr)) From 238e334836dcbb74aad114cd17f9cfa8a9321750 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 18 Nov 2020 14:41:29 +0100 Subject: [PATCH 11/91] add more tests and helper function for ANY JSON serialization --- client/debug/main.go | 2 +- codec/any.go | 32 ++++++++++-- codec/types/any.go | 1 - crypto/keys/ed25519/ed25519_test.go | 69 ++++++++++++++++++------- internal/protocdc/marshal.go | 1 + x/auth/vesting/types/vesting_account.go | 4 +- x/slashing/keeper/keeper.go | 12 ++--- 7 files changed, 88 insertions(+), 33 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 4bcdfa427994..8bfe93227cc7 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -35,7 +35,7 @@ func Cmd() *cobra.Command { // to decode the pubkey string from hex, base64, and finally bech32. If all // encodings fail, an error is returned. func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) { - var pk cryptotypes.PubKey + var pk cryptotypes.PubKey // FIXME, this won't work err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) return pk, err } diff --git a/codec/any.go b/codec/any.go index 075ad727e3e1..7cb42c260852 100644 --- a/codec/any.go +++ b/codec/any.go @@ -13,11 +13,9 @@ import ( func MarshalAny(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { - return nil, fmt.Errorf("can't proto marshal %T", x) + return nil, fmt.Errorf("can't proto marshal %T - expecting proto.Message", x) } - - any := &types.Any{} - err := any.Pack(msg) + any, err := types.NewAnyWithValue(msg) if err != nil { return nil, err } @@ -42,3 +40,29 @@ func UnmarshalAny(m BinaryMarshaler, iface interface{}, bz []byte) error { return m.UnpackAny(any, iface) } + +// MarshalAnyJSON is a convenience function for packing the provided value in an +// Any and then proto marshaling into JSON +func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { + any, err := types.NewAnyWithValue(x) + if err != nil { + return nil, err + } + return m.MarshalJSON(any) +} + +// UnmarshalAnyJSON is a convenience function for unmarshaling an Any from +// JSON bytes and then unpacking it to the `iface` pointer using the provided +// AnyUnpacker or returning an error +// +// Ex: +// var x MyInterface +// err := UnmarshalAny(unpacker, &x, bz) +func UnmarshalAnyJSON(m Marshaler, iface interface{}, bz []byte) error { + any := &types.Any{} + err := m.UnmarshalJSON(bz, any) + if err != nil { + return err + } + return m.UnpackAny(any, iface) +} diff --git a/codec/types/any.go b/codec/types/any.go index 38fe4b42aa72..731d2523ba2d 100644 --- a/codec/types/any.go +++ b/codec/types/any.go @@ -59,7 +59,6 @@ type Any struct { // unmarshaling func NewAnyWithValue(value proto.Message) (*Any, error) { any := &Any{} - err := any.Pack(value) if err != nil { return nil, err diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index d5f86c32492d..6406fd8b8620 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -6,16 +6,17 @@ import ( "fmt" "testing" - "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto" tmed25519 "github.com/tendermint/tendermint/crypto/ed25519" "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" ed25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/simapp" ) @@ -235,31 +236,61 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { // TODO - finish this test to show who the key will be presented in YAML func TestMarshalProto(t *testing.T) { require := require.New(t) - cdc, _ := simapp.MakeCodecs() - + ccfg := simapp.MakeTestEncodingConfig() privKey := ed25519.GenPrivKey() - pubKey := privKey.PubKey() + pk := privKey.PubKey() + + pkAny, err := codectypes.NewAnyWithValue(pk) + require.NoError(err) + bz, err := ccfg.Marshaler.MarshalJSON(pkAny) + require.NoError(err) - bz, err := cdc.MarshalJSON(pubKey) + var pkAny2 codectypes.Any + err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny2) require.NoError(err) - var pubKey2 cryptotypes.PubKey - err = cdc.UnmarshalJSON(bz, pubKey2) + // we before getting a cached value we need to unpack it. + // Normally this happens in in types which implement UnpackInterfaces + var pkI cryptotypes.PubKey + err = ccfg.InterfaceRegistry.UnpackAny(&pkAny2, &pkI) require.NoError(err) + var pk2 = pkAny2.GetCachedValue().(cryptotypes.PubKey) + require.True(pk2.Equals(pk)) + + // **** test binary serialization **** - // pubKeyM := pubKey.(codec.ProtoMarshaler) - // bz, err := cdc.MarshalBinaryBare(pubKeyM) - bz, err = proto.Marshal(pubKey) + // TODO - can we do it without packing into Any? + // pkM := pk.(codec.ProtoMarshaler) + bz, err = ccfg.Marshaler.MarshalBinaryBare(pkAny) fmt.Println(bz) require.NoError(err) - // var pk cryptotypes.PubKey - // err = proto.Unmarshal(bz, pk) - // require.NoError(err) + var pkAny3 codectypes.Any + err = ccfg.Marshaler.UnmarshalBinaryBare(bz, &pkAny3) + require.NoError(err) + err = ccfg.InterfaceRegistry.UnpackAny(&pkAny3, &pkI) + require.NoError(err) + var pk3 = pkAny3.GetCachedValue().(cryptotypes.PubKey) + require.True(pk3.Equals(pk)) +} - /* - var pk cryptotypes.PubKey - err = cdc.UnmarshalBinaryBare(bz, pk) - require.NoError(err) - require.True(pk.Equals(pubKey)) - */ +func TestMarshalProto2(t *testing.T) { + require := require.New(t) + ccfg := simapp.MakeTestEncodingConfig() + privKey := ed25519.GenPrivKey() + pk := privKey.PubKey() + + bz, err := codec.MarshalAny(ccfg.Marshaler, pk) + require.NoError(err) + + var pk2 cryptotypes.PubKey + err = codec.UnmarshalAny(ccfg.Marshaler, &pk2, bz) + require.NoError(err) + require.True(pk2.Equals(pk)) + + bz, err = codec.MarshalAnyJSON(ccfg.Marshaler, pk) + require.NoError(err) + var pk3 cryptotypes.PubKey + err = codec.UnmarshalAnyJSON(ccfg.Marshaler, &pk3, bz) + require.NoError(err) + require.True(pk3.Equals(pk)) } diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index f51f6af56293..4c781598e879 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -19,6 +19,7 @@ func MarshalJSON(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { } // AssertMsg casts i to a proto.Message. Returns an error if it's not possible. +// TODO remove func AssertMsg(i interface{}) (proto.Message, error) { pm, ok := i.(proto.Message) if !ok { diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index 4dc4a08edaba..cf42ccb21095 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -6,11 +6,11 @@ import ( yaml "gopkg.in/yaml.v2" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" - "github.com/tendermint/tendermint/crypto" ) // Compile-time type assertions @@ -535,7 +535,7 @@ func (dva DelayedVestingAccount) String() string { } type getPK interface { - GetPubKey() crypto.PubKey + GetPubKey() cryptotypes.PubKey } func getPKString(g getPK) (string, error) { diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 39c941678505..0ce88bd5e598 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -7,8 +7,8 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -44,11 +44,9 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { // AddPubkey sets a address-pubkey relation func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error { addr := pubkey.Address() - pkProto, err := protocdc.AssertMsg(pubkey) - if err != nil { - return err - } - bz, err := proto.Marshal(pkProto) + // TODO - wrap with ANY + bz, err := proto.Marshal(pubkey) + if err != nil { return err } @@ -64,6 +62,8 @@ func (k Keeper) GetPubkey(ctx sdk.Context, address cryptotypes.Address) (cryptot if bz == nil { return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(address)) } + var pkAny codectypes.Any + k.cdc.UnmarshalBinaryBare(bz, &pkAny) // TODO Unmarshal Any? Do we need to use Any here? var pk cryptotypes.PubKey return pk, nil // TODO proto.Unmarshal(pk) From ee70afbab96d442e120599be60c023d7f3550545 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 18 Nov 2020 15:53:51 +0100 Subject: [PATCH 12/91] update slashing --- client/debug/main.go | 38 +++++++++++------------------ codec/any.go | 2 +- codec/codec.go | 16 ++++++++++++ crypto/keys/ed25519/ed25519.go | 1 + crypto/keys/ed25519/ed25519_test.go | 3 ++- x/slashing/keeper/keeper.go | 22 ++++++----------- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 8bfe93227cc7..b02200ec0de4 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -9,10 +9,9 @@ import ( "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/version" ) @@ -35,16 +34,21 @@ func Cmd() *cobra.Command { // to decode the pubkey string from hex, base64, and finally bech32. If all // encodings fail, an error is returned. func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) { - var pk cryptotypes.PubKey // FIXME, this won't work - err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) + var pk cryptotypes.PubKey + // TODO: this won't work, where should we get an Any unpacker? + // err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) + am := codec.NewJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) + err := codec.UnmarshalAnyJSON(am, &pk, []byte(pkstr)) + return pk, err } func PubkeyCmd() *cobra.Command { return &cobra.Command{ Use: "pubkey [pubkey]", - Short: "Decode a ED25519 pubkey from hex, base64", - Long: fmt.Sprintf(`Decode a pubkey from hex, base64. + Short: "Decode a pubkey from proto JSON", + // TODO: update example + Long: fmt.Sprintf(`Decode a pubkey from proto JSON and display it's address. Example: $ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz @@ -53,24 +57,13 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx := client.GetClientContextFromCmd(cmd) - pk, err := getPubKeyFromString(clientCtx, args[0]) if err != nil { return err } - edPK, ok := pk.(*ed25519.PubKey) - if !ok { - return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type; expected ED25519") - } - - pubKeyJSONBytes, err := clientCtx.LegacyAmino.MarshalJSON(edPK) - if err != nil { - return err - } - cmd.Println("Address:", edPK.Address()) - cmd.Printf("Hex: %X\n", edPK.Key) - cmd.Println("JSON (base64):", string(pubKeyJSONBytes)) + cmd.Println("Address:", pk.Address()) + cmd.Println("Hex:", pk.String()) return nil }, } @@ -108,13 +101,10 @@ $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg } } - accAddr := sdk.AccAddress(addr) - valAddr := sdk.ValAddress(addr) - cmd.Println("Address:", addr) cmd.Printf("Address (hex): %X\n", addr) - cmd.Printf("Bech32 Acc: %s\n", accAddr) - cmd.Printf("Bech32 Val: %s\n", valAddr) + cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr)) + cmd.Printf("Bech32 Val: %s\n", sdk.ValAddress(addr)) return nil }, } diff --git a/codec/any.go b/codec/any.go index 7cb42c260852..ca2d3c17640c 100644 --- a/codec/any.go +++ b/codec/any.go @@ -58,7 +58,7 @@ func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { // Ex: // var x MyInterface // err := UnmarshalAny(unpacker, &x, bz) -func UnmarshalAnyJSON(m Marshaler, iface interface{}, bz []byte) error { +func UnmarshalAnyJSON(m JSONAnyMarshaler, iface interface{}, bz []byte) error { any := &types.Any{} err := m.UnmarshalJSON(bz, any) if err != nil { diff --git a/codec/codec.go b/codec/codec.go index 1cbc78b7fc0e..bd9970aa5af8 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -43,6 +43,11 @@ type ( MustUnmarshalJSON(bz []byte, ptr proto.Message) } + JSONAnyMarshaler interface { + types.AnyUnpacker + JSONMarshaler + } + // ProtoMarshaler defines an interface a type must implement as protocol buffer // defined message. ProtoMarshaler interface { @@ -64,3 +69,14 @@ type ( UnmarshalAminoJSON([]byte) error } ) + +type jsonAny struct { + JSONMarshaler + types.InterfaceRegistry +} + +// NewJSONAnyMarshaler creates a JSONAnyMarshaler using JSONMarshaler +// and InterfaceRegistry +func NewJSONAnyMarshaler(jm JSONMarshaler, ir types.InterfaceRegistry) JSONAnyMarshaler { + return jsonAny{jm, ir} +} diff --git a/crypto/keys/ed25519/ed25519.go b/crypto/keys/ed25519/ed25519.go index 17368c4b12ff..8e35d72bde1e 100644 --- a/crypto/keys/ed25519/ed25519.go +++ b/crypto/keys/ed25519/ed25519.go @@ -172,6 +172,7 @@ func (pubKey *PubKey) VerifySignature(msg []byte, sig []byte) bool { return ed25519.Verify(pubKey.Key, msg, sig) } +// String returns Hex representation of a pubkey with it's type func (pubKey *PubKey) String() string { return fmt.Sprintf("PubKeyEd25519{%X}", pubKey.Key) } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 6406fd8b8620..e157fc23ee4d 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -290,7 +290,8 @@ func TestMarshalProto2(t *testing.T) { bz, err = codec.MarshalAnyJSON(ccfg.Marshaler, pk) require.NoError(err) var pk3 cryptotypes.PubKey - err = codec.UnmarshalAnyJSON(ccfg.Marshaler, &pk3, bz) + am := codec.NewJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) + err = codec.UnmarshalAnyJSON(am, &pk3, bz) require.NoError(err) require.True(pk3.Equals(pk)) } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index 0ce88bd5e598..b5304a6c5e25 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -3,11 +3,9 @@ package keeper import ( "fmt" - "github.com/gogo/protobuf/proto" "github.com/tendermint/tendermint/libs/log" "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" @@ -43,30 +41,26 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { // AddPubkey sets a address-pubkey relation func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error { - addr := pubkey.Address() - // TODO - wrap with ANY - bz, err := proto.Marshal(pubkey) - + bz, err := codec.MarshalAny(k.cdc, pubkey) if err != nil { return err } store := ctx.KVStore(k.storeKey) - store.Set(types.AddrPubkeyRelationKey(addr), bz) + key := types.AddrPubkeyRelationKey(pubkey.Address()) + store.Set(key, bz) return nil } // GetPubkey returns the pubkey from the adddress-pubkey relation -func (k Keeper) GetPubkey(ctx sdk.Context, address cryptotypes.Address) (cryptotypes.PubKey, error) { +func (k Keeper) GetPubkey(ctx sdk.Context, a cryptotypes.Address) (cryptotypes.PubKey, error) { store := ctx.KVStore(k.storeKey) - bz := store.Get(types.AddrPubkeyRelationKey(address)) + bz := store.Get(types.AddrPubkeyRelationKey(a)) if bz == nil { - return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(address)) + return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a)) } - var pkAny codectypes.Any - k.cdc.UnmarshalBinaryBare(bz, &pkAny) - // TODO Unmarshal Any? Do we need to use Any here? var pk cryptotypes.PubKey - return pk, nil // TODO proto.Unmarshal(pk) + err := codec.UnmarshalAny(k.cdc, &pk, bz) + return pk, err } // Slash attempts to slash a validator. The slash is delegated to the staking From 084c306b8d47800eb58bf9c7325ae6b2b2342100 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 18 Nov 2020 20:47:27 +0100 Subject: [PATCH 13/91] Update function documentation --- codec/any.go | 24 ++++++++++++++---------- codec/proto_codec.go | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/codec/any.go b/codec/any.go index ca2d3c17640c..c137f2254f6a 100644 --- a/codec/any.go +++ b/codec/any.go @@ -8,8 +8,9 @@ import ( "github.com/cosmos/cosmos-sdk/codec/types" ) -// MarshalAny is a convenience function for packing the provided value in an -// Any and then proto marshaling it to bytes +// MarshalAny is a convenience function for proto marshalling interfaces. It +// packs the provided value in an Any and then marshals it to bytes. +// NOTE: if you use a concret type, then you should use BinaryMarshaler.MarshalBinaryBare directly func MarshalAny(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { @@ -23,9 +24,10 @@ func MarshalAny(m BinaryMarshaler, x interface{}) ([]byte, error) { return m.MarshalBinaryBare(any) } -// UnmarshalAny is a convenience function for proto unmarshaling an Any from -// bz and then unpacking it to the interface pointer passed in as iface using -// the provided AnyUnpacker or returning an error +// UnmarshalAny is a convenience function for proto unmarshaling interfaces. It +// unmarshals an Any from bz and then unpacks it to the `iface`, which must +// be a pointer to a non empty interface with registered implementations. +// NOTE: if you use a concret type, then you should use BinaryMarshaler.UnarshalBinaryBare directly // // Ex: // var x MyInterface @@ -41,8 +43,9 @@ func UnmarshalAny(m BinaryMarshaler, iface interface{}, bz []byte) error { return m.UnpackAny(any, iface) } -// MarshalAnyJSON is a convenience function for packing the provided value in an -// Any and then proto marshaling into JSON +// MarshalAnyJSON is a convenience function for proto marshalling interfaces. It +// packs the provided value in an Any and then marshals it to bytes. +// NOTE: if you use a concret type, then you should use JSONMarshaler.MarshalJSON directly func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { any, err := types.NewAnyWithValue(x) if err != nil { @@ -51,9 +54,10 @@ func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { return m.MarshalJSON(any) } -// UnmarshalAnyJSON is a convenience function for unmarshaling an Any from -// JSON bytes and then unpacking it to the `iface` pointer using the provided -// AnyUnpacker or returning an error +// UnmarshalAnyJSON is a convenience function for proto unmarshaling interfaces. +// It unmarshals an Any from bz and then unpacks it to the `iface`, which must +// be a pointer to a non empty interface with registered implementations. +// NOTE: if you use a concret type, then you should use JSONMarshaler.UnarshalJSON directly // // Ex: // var x MyInterface diff --git a/codec/proto_codec.go b/codec/proto_codec.go index e77409fe4829..f2737ba88246 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -33,11 +33,15 @@ func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec { } // MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.MarshalIfc func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) { return o.Marshal() } // MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.MarshalIfc func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte { bz, err := pc.MarshalBinaryBare(o) if err != nil { @@ -70,6 +74,8 @@ func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte { } // UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.UnmarshalIfc func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { err := ptr.Unmarshal(bz) if err != nil { @@ -83,6 +89,8 @@ func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { } // MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.UnmarshalIfc func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) { if err := pc.UnmarshalBinaryBare(bz, ptr); err != nil { panic(err) @@ -115,6 +123,8 @@ func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMars // MarshalJSON implements JSONMarshaler.MarshalJSON method, // it marshals to JSON using proto codec. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.MarshalIfcJSON func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { m, ok := o.(ProtoMarshaler) if !ok { @@ -126,6 +136,8 @@ func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { // MustMarshalJSON implements JSONMarshaler.MustMarshalJSON method, // it executes MarshalJSON except it panics upon failure. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.MarshalIfcJSON func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { bz, err := pc.MarshalJSON(o) if err != nil { @@ -137,6 +149,8 @@ func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { // UnmarshalJSON implements JSONMarshaler.UnmarshalJSON method, // it unmarshals from JSON using proto codec. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.UnmarshalIfcJSON func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { m, ok := ptr.(ProtoMarshaler) if !ok { @@ -154,6 +168,8 @@ func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { // MustUnmarshalJSON implements JSONMarshaler.MustUnmarshalJSON method, // it executes UnmarshalJSON except it panics upon failure. +// NOTE: this function must be used with a concret type which +// implements proto.Message. For interface please use the codec.UnmarshalIfcJSON func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) { if err := pc.UnmarshalJSON(bz, ptr); err != nil { panic(err) @@ -167,6 +183,7 @@ func (pc *ProtoCodec) UnpackAny(any *types.Any, iface interface{}) error { return pc.interfaceRegistry.UnpackAny(any, iface) } +// InterfaceRegistry returns InterfaceRegistry func (pc *ProtoCodec) InterfaceRegistry() types.InterfaceRegistry { return pc.interfaceRegistry } From 7974b42153c6c5d199b0b48d9a02917673aa58af Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 18 Nov 2020 20:53:03 +0100 Subject: [PATCH 14/91] Rename code any-marshal helper functions --- client/debug/main.go | 2 +- codec/any.go | 22 +++++++++++-------- codec/any_test.go | 12 +++++----- crypto/keys/ed25519/ed25519_test.go | 4 ++-- x/auth/keeper/keeper.go | 4 ++-- x/bank/keeper/keeper.go | 4 ++-- x/evidence/keeper/keeper.go | 4 ++-- x/ibc/core/02-client/types/encoding.go | 8 +++---- .../06-solomachine/types/solomachine_test.go | 2 +- .../07-tendermint/types/client_state.go | 4 ++-- .../07-tendermint/types/upgrade.go | 2 +- x/slashing/keeper/keeper.go | 4 ++-- x/staking/types/msg_test.go | 8 +++---- 13 files changed, 42 insertions(+), 38 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index b02200ec0de4..85ca58e847b9 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -38,7 +38,7 @@ func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, // TODO: this won't work, where should we get an Any unpacker? // err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) am := codec.NewJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) - err := codec.UnmarshalAnyJSON(am, &pk, []byte(pkstr)) + err := codec.UnmarshalIfcJSON(am, &pk, []byte(pkstr)) return pk, err } diff --git a/codec/any.go b/codec/any.go index c137f2254f6a..721fe5ae1b8f 100644 --- a/codec/any.go +++ b/codec/any.go @@ -8,10 +8,14 @@ import ( "github.com/cosmos/cosmos-sdk/codec/types" ) -// MarshalAny is a convenience function for proto marshalling interfaces. It +// TODO: For better UX this functions should be moved to the Marshaler interface +// - user should have a consisten way how to unpack and serialize data without thinking +// to use a helper function or an interface. + +// MarshalIfc is a convenience function for proto marshalling interfaces. It // packs the provided value in an Any and then marshals it to bytes. // NOTE: if you use a concret type, then you should use BinaryMarshaler.MarshalBinaryBare directly -func MarshalAny(m BinaryMarshaler, x interface{}) ([]byte, error) { +func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { return nil, fmt.Errorf("can't proto marshal %T - expecting proto.Message", x) @@ -24,15 +28,15 @@ func MarshalAny(m BinaryMarshaler, x interface{}) ([]byte, error) { return m.MarshalBinaryBare(any) } -// UnmarshalAny is a convenience function for proto unmarshaling interfaces. It +// UnmarshalIfc is a convenience function for proto unmarshaling interfaces. It // unmarshals an Any from bz and then unpacks it to the `iface`, which must // be a pointer to a non empty interface with registered implementations. // NOTE: if you use a concret type, then you should use BinaryMarshaler.UnarshalBinaryBare directly // // Ex: // var x MyInterface -// err := UnmarshalAny(unpacker, &x, bz) -func UnmarshalAny(m BinaryMarshaler, iface interface{}, bz []byte) error { +// err := UnmarshalIfc(unpacker, &x, bz) +func UnmarshalIfc(m BinaryMarshaler, iface interface{}, bz []byte) error { any := &types.Any{} err := m.UnmarshalBinaryBare(bz, any) @@ -43,10 +47,10 @@ func UnmarshalAny(m BinaryMarshaler, iface interface{}, bz []byte) error { return m.UnpackAny(any, iface) } -// MarshalAnyJSON is a convenience function for proto marshalling interfaces. It +// MarshalIfcJSON is a convenience function for proto marshalling interfaces. It // packs the provided value in an Any and then marshals it to bytes. // NOTE: if you use a concret type, then you should use JSONMarshaler.MarshalJSON directly -func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { +func MarshalIfcJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { any, err := types.NewAnyWithValue(x) if err != nil { return nil, err @@ -54,7 +58,7 @@ func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { return m.MarshalJSON(any) } -// UnmarshalAnyJSON is a convenience function for proto unmarshaling interfaces. +// UnmarshalIfcJSON is a convenience function for proto unmarshaling interfaces. // It unmarshals an Any from bz and then unpacks it to the `iface`, which must // be a pointer to a non empty interface with registered implementations. // NOTE: if you use a concret type, then you should use JSONMarshaler.UnarshalJSON directly @@ -62,7 +66,7 @@ func MarshalAnyJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { // Ex: // var x MyInterface // err := UnmarshalAny(unpacker, &x, bz) -func UnmarshalAnyJSON(m JSONAnyMarshaler, iface interface{}, bz []byte) error { +func UnmarshalIfcJSON(m JSONAnyMarshaler, iface interface{}, bz []byte) error { any := &types.Any{} err := m.UnmarshalJSON(bz, any) if err != nil { diff --git a/codec/any_test.go b/codec/any_test.go index d9ea888c49fb..6094215238f4 100644 --- a/codec/any_test.go +++ b/codec/any_test.go @@ -28,30 +28,30 @@ func TestMarshalAny(t *testing.T) { cdc := codec.NewProtoCodec(registry) kitty := &testdata.Cat{Moniker: "Kitty"} - bz, err := codec.MarshalAny(cdc, kitty) + bz, err := codec.MarshalIfc(cdc, kitty) require.NoError(t, err) var animal testdata.Animal // empty registry should fail - err = codec.UnmarshalAny(cdc, &animal, bz) + err = codec.UnmarshalIfc(cdc, &animal, bz) require.Error(t, err) // wrong type registration should fail registry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Dog{}) - err = codec.UnmarshalAny(cdc, &animal, bz) + err = codec.UnmarshalIfc(cdc, &animal, bz) require.Error(t, err) // should pass registry = NewTestInterfaceRegistry() cdc = codec.NewProtoCodec(registry) - err = codec.UnmarshalAny(cdc, &animal, bz) + err = codec.UnmarshalIfc(cdc, &animal, bz) require.NoError(t, err) require.Equal(t, kitty, animal) // nil should fail registry = NewTestInterfaceRegistry() - err = codec.UnmarshalAny(cdc, nil, bz) + err = codec.UnmarshalIfc(cdc, nil, bz) require.Error(t, err) } @@ -59,7 +59,7 @@ func TestMarshalAnyNonProtoErrors(t *testing.T) { registry := types.NewInterfaceRegistry() cdc := codec.NewProtoCodec(registry) - _, err := codec.MarshalAny(cdc, 29) + _, err := codec.MarshalIfc(cdc, 29) require.Error(t, err) require.Equal(t, err, errors.New("can't proto marshal int")) } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index e157fc23ee4d..ed91eea93ed8 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -279,11 +279,11 @@ func TestMarshalProto2(t *testing.T) { privKey := ed25519.GenPrivKey() pk := privKey.PubKey() - bz, err := codec.MarshalAny(ccfg.Marshaler, pk) + bz, err := codec.MarshalIfc(ccfg.Marshaler, pk) require.NoError(err) var pk2 cryptotypes.PubKey - err = codec.UnmarshalAny(ccfg.Marshaler, &pk2, bz) + err = codec.UnmarshalIfc(ccfg.Marshaler, &pk2, bz) require.NoError(err) require.True(pk2.Equals(pk)) diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index 75724f7d3c81..fc092a6cec06 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -219,7 +219,7 @@ func (ak AccountKeeper) decodeAccount(bz []byte) types.AccountI { // the Marshaler interface, it is treated as a Proto-defined message and // serialized that way. Otherwise, it falls back on the internal Amino codec. func (ak AccountKeeper) MarshalAccount(accountI types.AccountI) ([]byte, error) { - return codec.MarshalAny(ak.cdc, accountI) + return codec.MarshalIfc(ak.cdc, accountI) } // UnmarshalAccount returns an Account interface from raw encoded account @@ -227,7 +227,7 @@ func (ak AccountKeeper) MarshalAccount(accountI types.AccountI) ([]byte, error) // failure. func (ak AccountKeeper) UnmarshalAccount(bz []byte) (types.AccountI, error) { var acc types.AccountI - if err := codec.UnmarshalAny(ak.cdc, &acc, bz); err != nil { + if err := codec.UnmarshalIfc(ak.cdc, &acc, bz); err != nil { return nil, err } diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index 6594398aa513..9fb83d5a56c5 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -409,7 +409,7 @@ func (k BaseKeeper) trackUndelegation(ctx sdk.Context, addr sdk.AccAddress, amt // the Marshaler interface, it is treated as a Proto-defined message and // serialized that way. Otherwise, it falls back on the internal Amino codec. func (k BaseKeeper) MarshalSupply(supplyI exported.SupplyI) ([]byte, error) { - return codec.MarshalAny(k.cdc, supplyI) + return codec.MarshalIfc(k.cdc, supplyI) } // UnmarshalSupply returns a Supply interface from raw encoded supply @@ -417,7 +417,7 @@ func (k BaseKeeper) MarshalSupply(supplyI exported.SupplyI) ([]byte, error) { // failure. func (k BaseKeeper) UnmarshalSupply(bz []byte) (exported.SupplyI, error) { var evi exported.SupplyI - if err := codec.UnmarshalAny(k.cdc, &evi, bz); err != nil { + if err := codec.UnmarshalIfc(k.cdc, &evi, bz); err != nil { return nil, err } diff --git a/x/evidence/keeper/keeper.go b/x/evidence/keeper/keeper.go index e15cd74c8a07..7509a2a2690d 100644 --- a/x/evidence/keeper/keeper.go +++ b/x/evidence/keeper/keeper.go @@ -171,7 +171,7 @@ func (k Keeper) MustMarshalEvidence(evidence exported.Evidence) []byte { // the Marshaler interface, it is treated as a Proto-defined message and // serialized that way. Otherwise, it falls back on the internal Amino codec. func (k Keeper) MarshalEvidence(evidenceI exported.Evidence) ([]byte, error) { - return codec.MarshalAny(k.cdc, evidenceI) + return codec.MarshalIfc(k.cdc, evidenceI) } // UnmarshalEvidence returns an Evidence interface from raw encoded evidence @@ -179,7 +179,7 @@ func (k Keeper) MarshalEvidence(evidenceI exported.Evidence) ([]byte, error) { // failure. func (k Keeper) UnmarshalEvidence(bz []byte) (exported.Evidence, error) { var evi exported.Evidence - if err := codec.UnmarshalAny(k.cdc, &evi, bz); err != nil { + if err := codec.UnmarshalIfc(k.cdc, &evi, bz); err != nil { return nil, err } diff --git a/x/ibc/core/02-client/types/encoding.go b/x/ibc/core/02-client/types/encoding.go index 1a56ad9596bd..e16859681418 100644 --- a/x/ibc/core/02-client/types/encoding.go +++ b/x/ibc/core/02-client/types/encoding.go @@ -33,7 +33,7 @@ func MustMarshalClientState(cdc codec.BinaryMarshaler, clientState exported.Clie // the Marshaler interface, it is treated as a Proto-defined message and // serialized that way. func MarshalClientState(cdc codec.BinaryMarshaler, clientStateI exported.ClientState) ([]byte, error) { - return codec.MarshalAny(cdc, clientStateI) + return codec.MarshalIfc(cdc, clientStateI) } // UnmarshalClientState returns an ClientState interface from raw encoded clientState @@ -41,7 +41,7 @@ func MarshalClientState(cdc codec.BinaryMarshaler, clientStateI exported.ClientS // failure. func UnmarshalClientState(cdc codec.BinaryMarshaler, bz []byte) (exported.ClientState, error) { var clientState exported.ClientState - if err := codec.UnmarshalAny(cdc, &clientState, bz); err != nil { + if err := codec.UnmarshalIfc(cdc, &clientState, bz); err != nil { return nil, err } @@ -74,7 +74,7 @@ func MustMarshalConsensusState(cdc codec.BinaryMarshaler, consensusState exporte // the Marshaler interface, it is treated as a Proto-defined message and // serialized that way. func MarshalConsensusState(cdc codec.BinaryMarshaler, consensusStateI exported.ConsensusState) ([]byte, error) { - return codec.MarshalAny(cdc, consensusStateI) + return codec.MarshalIfc(cdc, consensusStateI) } // UnmarshalConsensusState returns an ConsensusState interface from raw encoded clientState @@ -82,7 +82,7 @@ func MarshalConsensusState(cdc codec.BinaryMarshaler, consensusStateI exported.C // failure. func UnmarshalConsensusState(cdc codec.BinaryMarshaler, bz []byte) (exported.ConsensusState, error) { var consensusState exported.ConsensusState - if err := codec.UnmarshalAny(cdc, &consensusState, bz); err != nil { + if err := codec.UnmarshalIfc(cdc, &consensusState, bz); err != nil { return nil, err } diff --git a/x/ibc/light-clients/06-solomachine/types/solomachine_test.go b/x/ibc/light-clients/06-solomachine/types/solomachine_test.go index b3b647d829ab..ac6d62f4b913 100644 --- a/x/ibc/light-clients/06-solomachine/types/solomachine_test.go +++ b/x/ibc/light-clients/06-solomachine/types/solomachine_test.go @@ -53,7 +53,7 @@ func (suite *SoloMachineTestSuite) GetSequenceFromStore() uint64 { suite.Require().NotNil(bz) var clientState exported.ClientState - err := codec.UnmarshalAny(suite.chainA.Codec, &clientState, bz) + err := codec.UnmarshalIfc(suite.chainA.Codec, &clientState, bz) suite.Require().NoError(err) return clientState.GetLatestHeight().GetVersionHeight() } diff --git a/x/ibc/light-clients/07-tendermint/types/client_state.go b/x/ibc/light-clients/07-tendermint/types/client_state.go index 8cfc849541bc..bb38d03124e5 100644 --- a/x/ibc/light-clients/07-tendermint/types/client_state.go +++ b/x/ibc/light-clients/07-tendermint/types/client_state.go @@ -173,7 +173,7 @@ func (cs ClientState) VerifyClientState( return sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "invalid client type %T, expected %T", clientState, &ClientState{}) } - bz, err := codec.MarshalAny(cdc, clientState) + bz, err := codec.MarshalIfc(cdc, clientState) if err != nil { return err } @@ -213,7 +213,7 @@ func (cs ClientState) VerifyClientConsensusState( return sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type %T, expected %T", consensusState, &ConsensusState{}) } - bz, err := codec.MarshalAny(cdc, consensusState) + bz, err := codec.MarshalIfc(cdc, consensusState) if err != nil { return err } diff --git a/x/ibc/light-clients/07-tendermint/types/upgrade.go b/x/ibc/light-clients/07-tendermint/types/upgrade.go index dcd3cd1a5f2e..4c2ef1d036f4 100644 --- a/x/ibc/light-clients/07-tendermint/types/upgrade.go +++ b/x/ibc/light-clients/07-tendermint/types/upgrade.go @@ -66,7 +66,7 @@ func (cs ClientState) VerifyUpgradeAndUpdateState( // counterparty chain must commit the upgraded client with all client-customizable fields zeroed out // at the upgrade path specified by current client committedClient := upgradedClient.ZeroCustomFields() - bz, err := codec.MarshalAny(cdc, committedClient) + bz, err := codec.MarshalIfc(cdc, committedClient) if err != nil { return nil, nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClient, "could not marshal client state: %v", err) } diff --git a/x/slashing/keeper/keeper.go b/x/slashing/keeper/keeper.go index b5304a6c5e25..14dc3d3d732c 100644 --- a/x/slashing/keeper/keeper.go +++ b/x/slashing/keeper/keeper.go @@ -41,7 +41,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { // AddPubkey sets a address-pubkey relation func (k Keeper) AddPubkey(ctx sdk.Context, pubkey cryptotypes.PubKey) error { - bz, err := codec.MarshalAny(k.cdc, pubkey) + bz, err := codec.MarshalIfc(k.cdc, pubkey) if err != nil { return err } @@ -59,7 +59,7 @@ func (k Keeper) GetPubkey(ctx sdk.Context, a cryptotypes.Address) (cryptotypes.P return nil, fmt.Errorf("address %s not found", sdk.ConsAddress(a)) } var pk cryptotypes.PubKey - err := codec.UnmarshalAny(k.cdc, &pk, bz) + err := codec.UnmarshalIfc(k.cdc, &pk, bz) return pk, err } diff --git a/x/staking/types/msg_test.go b/x/staking/types/msg_test.go index 3b858a64ddc3..095307b8f5e0 100644 --- a/x/staking/types/msg_test.go +++ b/x/staking/types/msg_test.go @@ -27,10 +27,10 @@ func TestMsgDecode(t *testing.T) { // firstly we start testing the pubkey serialization - pk1bz, err := codec.MarshalAny(cdc, pk1) + pk1bz, err := codec.MarshalIfc(cdc, pk1) require.NoError(t, err) var pkUnmarshaled cryptotypes.PubKey - err = codec.UnmarshalAny(cdc, &pkUnmarshaled, pk1bz) + err = codec.UnmarshalIfc(cdc, &pkUnmarshaled, pk1bz) require.NoError(t, err) require.True(t, pk1.Equals(pkUnmarshaled.(*ed25519.PubKey))) @@ -39,11 +39,11 @@ func TestMsgDecode(t *testing.T) { commission1 := types.NewCommissionRates(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()) msg, err := types.NewMsgCreateValidator(valAddr1, pk1, coinPos, types.Description{}, commission1, sdk.OneInt()) require.NoError(t, err) - msgSerialized, err := codec.MarshalAny(cdc, msg) + msgSerialized, err := codec.MarshalIfc(cdc, msg) require.NoError(t, err) var msgUnmarshaled sdk.Msg - err = codec.UnmarshalAny(cdc, &msgUnmarshaled, msgSerialized) + err = codec.UnmarshalIfc(cdc, &msgUnmarshaled, msgSerialized) require.NoError(t, err) msg2, ok := msgUnmarshaled.(*types.MsgCreateValidator) require.True(t, ok) From 943a4ff4c7044b8b1aca1f8a75d12e064250168c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 18 Nov 2020 21:13:54 +0100 Subject: [PATCH 15/91] Update pubkey unpacking test --- client/debug/main.go | 2 +- codec/any.go | 2 +- codec/codec.go | 6 +++--- crypto/keys/ed25519/ed25519_test.go | 23 +++++++++++++++++------ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 85ca58e847b9..e3f86ee80076 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -37,7 +37,7 @@ func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, var pk cryptotypes.PubKey // TODO: this won't work, where should we get an Any unpacker? // err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) - am := codec.NewJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) + am := codec.NewIfcJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) err := codec.UnmarshalIfcJSON(am, &pk, []byte(pkstr)) return pk, err diff --git a/codec/any.go b/codec/any.go index 721fe5ae1b8f..545638fe647e 100644 --- a/codec/any.go +++ b/codec/any.go @@ -66,7 +66,7 @@ func MarshalIfcJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { // Ex: // var x MyInterface // err := UnmarshalAny(unpacker, &x, bz) -func UnmarshalIfcJSON(m JSONAnyMarshaler, iface interface{}, bz []byte) error { +func UnmarshalIfcJSON(m IfcJSONMarshaler, iface interface{}, bz []byte) error { any := &types.Any{} err := m.UnmarshalJSON(bz, any) if err != nil { diff --git a/codec/codec.go b/codec/codec.go index bd9970aa5af8..756fbc509b94 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -43,7 +43,7 @@ type ( MustUnmarshalJSON(bz []byte, ptr proto.Message) } - JSONAnyMarshaler interface { + IfcJSONMarshaler interface { types.AnyUnpacker JSONMarshaler } @@ -75,8 +75,8 @@ type jsonAny struct { types.InterfaceRegistry } -// NewJSONAnyMarshaler creates a JSONAnyMarshaler using JSONMarshaler +// NewIfcJSONAnyMarshaler creates a JSONAnyMarshaler using JSONMarshaler // and InterfaceRegistry -func NewJSONAnyMarshaler(jm JSONMarshaler, ir types.InterfaceRegistry) JSONAnyMarshaler { +func NewIfcJSONAnyMarshaler(jm JSONMarshaler, ir types.InterfaceRegistry) IfcJSONMarshaler { return jsonAny{jm, ir} } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index ed91eea93ed8..7e0d8774dabb 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -233,7 +233,7 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { } } -// TODO - finish this test to show who the key will be presented in YAML +// TODO - maybe we should move the tests below to `codec_test` package, WDYT? func TestMarshalProto(t *testing.T) { require := require.New(t) ccfg := simapp.MakeTestEncodingConfig() @@ -258,8 +258,6 @@ func TestMarshalProto(t *testing.T) { // **** test binary serialization **** - // TODO - can we do it without packing into Any? - // pkM := pk.(codec.ProtoMarshaler) bz, err = ccfg.Marshaler.MarshalBinaryBare(pkAny) fmt.Println(bz) require.NoError(err) @@ -287,11 +285,24 @@ func TestMarshalProto2(t *testing.T) { require.NoError(err) require.True(pk2.Equals(pk)) - bz, err = codec.MarshalAnyJSON(ccfg.Marshaler, pk) + bz, err = codec.MarshalIfcJSON(ccfg.Marshaler, pk) + fmt.Println(string(bz)) + require.Empty(bz) require.NoError(err) var pk3 cryptotypes.PubKey - am := codec.NewJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) - err = codec.UnmarshalAnyJSON(am, &pk3, bz) + // TODO: make a task to integrate the coded.*Marshal helper functiosn (from any.go) to the codec.Marshaler interface + // and remove JSONAnyMarshaler + am := codec.NewIfcJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) + err = codec.UnmarshalIfcJSON(am, &pk3, bz) require.NoError(err) require.True(pk3.Equals(pk)) + + // Using JSONMarshaler for unpacking won't work + // -- Any type doesn't get automatically unpacked! + + var pkAny codectypes.Any + err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny) + require.NoError(err) + ifce := pkAny.GetCachedValue() + require.Nil(ifce) } From 3a0904b10d404fbe36f265da4256070b79dfcfe2 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 18 Nov 2020 23:00:10 +0100 Subject: [PATCH 16/91] Update test comments --- crypto/keys/ed25519/ed25519_test.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 7e0d8774dabb..991b4dabf6f3 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -286,8 +286,6 @@ func TestMarshalProto2(t *testing.T) { require.True(pk2.Equals(pk)) bz, err = codec.MarshalIfcJSON(ccfg.Marshaler, pk) - fmt.Println(string(bz)) - require.Empty(bz) require.NoError(err) var pk3 cryptotypes.PubKey // TODO: make a task to integrate the coded.*Marshal helper functiosn (from any.go) to the codec.Marshaler interface @@ -297,12 +295,12 @@ func TestMarshalProto2(t *testing.T) { require.NoError(err) require.True(pk3.Equals(pk)) - // Using JSONMarshaler for unpacking won't work - // -- Any type doesn't get automatically unpacked! - + // Using JSONMarshaler for unpacking won't work Any type + // doesn't get automatically unpacked, and we we can't do it + // because Any doesn't know interfaces into which it should unpack var pkAny codectypes.Any err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny) require.NoError(err) - ifce := pkAny.GetCachedValue() - require.Nil(ifce) + ifc := pkAny.GetCachedValue() + require.Nil(ifc) } From 46f85cea92a19618e9e7be91495d9faa681962b7 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 23 Nov 2020 13:20:10 +0100 Subject: [PATCH 17/91] solve TestDecodeStore --- crypto/keys/ed25519/ed25519_test.go | 22 ++++++++++++++-------- x/slashing/simulation/decoder.go | 13 +++++++++---- x/slashing/simulation/decoder_test.go | 13 ++++++------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 991b4dabf6f3..ccc4633219f7 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -240,6 +240,8 @@ func TestMarshalProto(t *testing.T) { privKey := ed25519.GenPrivKey() pk := privKey.PubKey() + // **** test JSON serialization **** + pkAny, err := codectypes.NewAnyWithValue(pk) require.NoError(err) bz, err := ccfg.Marshaler.MarshalJSON(pkAny) @@ -277,15 +279,9 @@ func TestMarshalProto2(t *testing.T) { privKey := ed25519.GenPrivKey() pk := privKey.PubKey() - bz, err := codec.MarshalIfc(ccfg.Marshaler, pk) - require.NoError(err) - - var pk2 cryptotypes.PubKey - err = codec.UnmarshalIfc(ccfg.Marshaler, &pk2, bz) - require.NoError(err) - require.True(pk2.Equals(pk)) + // **** test JSON serialization **** - bz, err = codec.MarshalIfcJSON(ccfg.Marshaler, pk) + bz, err := codec.MarshalIfcJSON(ccfg.Marshaler, pk) require.NoError(err) var pk3 cryptotypes.PubKey // TODO: make a task to integrate the coded.*Marshal helper functiosn (from any.go) to the codec.Marshaler interface @@ -303,4 +299,14 @@ func TestMarshalProto2(t *testing.T) { require.NoError(err) ifc := pkAny.GetCachedValue() require.Nil(ifc) + + // **** test binary serialization **** + + bz, err = codec.MarshalIfc(ccfg.Marshaler, pk) + require.NoError(err) + + var pk2 cryptotypes.PubKey + err = codec.UnmarshalIfc(ccfg.Marshaler, &pk2, bz) + require.NoError(err) + require.True(pk2.Equals(pk)) } diff --git a/x/slashing/simulation/decoder.go b/x/slashing/simulation/decoder.go index b0ebb64ef31a..a4c10329f998 100644 --- a/x/slashing/simulation/decoder.go +++ b/x/slashing/simulation/decoder.go @@ -7,6 +7,7 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/cosmos/cosmos-sdk/codec" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/types/kv" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) @@ -29,10 +30,14 @@ func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string { return fmt.Sprintf("missedA: %v\nmissedB: %v", missedA.Value, missedB.Value) case bytes.Equal(kvA.Key[:1], types.AddrPubkeyRelationKeyPrefix): - var pubKeyA, pubKeyB gogotypes.StringValue - cdc.MustUnmarshalBinaryBare(kvA.Value, &pubKeyA) - cdc.MustUnmarshalBinaryBare(kvB.Value, &pubKeyB) - return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA.Value, pubKeyB.Value) + var pubKeyA, pubKeyB cryptotypes.PubKey + if err := codec.UnmarshalIfc(cdc, &pubKeyA, kvA.Value); err != nil { + panic(fmt.Sprint("Can't unmarshal kvA; ", err)) + } + if err := codec.UnmarshalIfc(cdc, &pubKeyB, kvB.Value); err != nil { + panic(fmt.Sprint("Can't unmarshal kvB; ", err)) + } + return fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", pubKeyA, pubKeyB) default: panic(fmt.Sprintf("invalid slashing key prefix %X", kvA.Key[:1])) diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index 59f82e72f21c..c136306bd20b 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -8,8 +8,8 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/internal/protocdc" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/kv" @@ -31,14 +31,14 @@ func TestDecodeStore(t *testing.T) { info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) missed := gogotypes.BoolValue{Value: true} - delPk1Proto, err := protocdc.AssertMsg(delPk1) + bz, err := codec.MarshalIfc(cdc, delPk1) require.NoError(t, err) kvPairs := kv.Pairs{ Pairs: []kv.Pair{ {Key: types.ValidatorSigningInfoKey(consAddr1), Value: cdc.MustMarshalBinaryBare(&info)}, {Key: types.ValidatorMissedBlockBitArrayKey(consAddr1, 6), Value: cdc.MustMarshalBinaryBare(&missed)}, - {Key: types.AddrPubkeyRelationKey(delAddr1), Value: cdc.MustMarshalBinaryBare(delPk1Proto)}, + {Key: types.AddrPubkeyRelationKey(delAddr1), Value: bz}, {Key: []byte{0x99}, Value: []byte{0x99}}, // This test should panic }, } @@ -49,16 +49,15 @@ func TestDecodeStore(t *testing.T) { }{ {"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info)}, {"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed.Value, missed.Value)}, - {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1Proto, delPk1Proto)}, + {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1, delPk1)}, {"other", ""}, } for i, tt := range tests { i, tt := i, tt t.Run(tt.name, func(t *testing.T) { - switch i { - case len(tests) - 1: + if i == len(tests)-1 { require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name) - default: + } else { require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name) } }) From 2b350205cc7e38abaa25a8d0850b0948646690d9 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 23 Nov 2020 17:08:55 +0100 Subject: [PATCH 18/91] solve legacytx issues --- crypto/keys/ed25519/ed25519_test.go | 1 - x/auth/legacy/legacytx/stdsig_test.go | 42 ++++++++++ x/auth/legacy/legacytx/stdsign.go | 38 +++++++++ x/auth/legacy/legacytx/stdtx.go | 49 ------------ x/auth/legacy/legacytx/stdtx_test.go | 29 ------- x/staking/client/cli/tx_test.go | 110 +++++++------------------- 6 files changed, 108 insertions(+), 161 deletions(-) create mode 100644 x/auth/legacy/legacytx/stdsig_test.go diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index ccc4633219f7..0513c45c408a 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -16,7 +16,6 @@ import ( ed25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/simapp" ) diff --git a/x/auth/legacy/legacytx/stdsig_test.go b/x/auth/legacy/legacytx/stdsig_test.go new file mode 100644 index 000000000000..d3992e70f9f6 --- /dev/null +++ b/x/auth/legacy/legacytx/stdsig_test.go @@ -0,0 +1,42 @@ +package legacytx_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + yaml "gopkg.in/yaml.v2" + + "github.com/cosmos/cosmos-sdk/testutil/testdata" + "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" +) + +// TODO: is this test valuable? +func TestStdSignatureMarshalYAML(t *testing.T) { + _, pk, _ := testdata.KeyTestPubAddr() + pkStr := pk.String() + + testCases := []struct { + sig legacytx.StdSignature + expected string + }{ + { + legacytx.StdSignature{}, + "|\n pubkey: \"\"\n signature: \"\"\n", + }, + { + legacytx.StdSignature{PubKey: pk, Signature: []byte("dummySig")}, + fmt.Sprintf("|\n pubkey: %s\n signature: 64756D6D79536967\n", pkStr), + }, + { + legacytx.StdSignature{PubKey: pk, Signature: nil}, + fmt.Sprintf("|\n pubkey: %s\n signature: \"\"\n", pkStr), + }, + } + + for i, tc := range testCases { + bz, err := yaml.Marshal(tc.sig) + require.NoError(t, err) + require.Equal(t, tc.expected, string(bz), "test case #%d", i) + } +} diff --git a/x/auth/legacy/legacytx/stdsign.go b/x/auth/legacy/legacytx/stdsign.go index 2ba194dad843..6b73ff1f8a3a 100644 --- a/x/auth/legacy/legacytx/stdsign.go +++ b/x/auth/legacy/legacytx/stdsign.go @@ -2,6 +2,7 @@ package legacytx import ( "encoding/json" + "fmt" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/legacy" @@ -11,6 +12,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/tx/signing" + "gopkg.in/yaml.v2" ) // Interface implementation checks @@ -60,6 +62,42 @@ type StdSignature struct { Signature []byte `json:"signature" yaml:"signature"` } +// Deprecated +func NewStdSignature(pk cryptotypes.PubKey, sig []byte) StdSignature { + return StdSignature{PubKey: pk, Signature: sig} +} + +// GetSignature returns the raw signature bytes. +func (ss StdSignature) GetSignature() []byte { + return ss.Signature +} + +// GetPubKey returns the public key of a signature as a cryptotypes.PubKey using the +// Amino codec. +func (ss StdSignature) GetPubKey() cryptotypes.PubKey { + return ss.PubKey +} + +// MarshalYAML returns the YAML representation of the signature. +func (ss StdSignature) MarshalYAML() (interface{}, error) { + pk := "" + if ss.PubKey != nil { + pk = ss.PubKey.String() + } + bz, err := yaml.Marshal(struct { + PubKey string + Signature string + }{ + PubKey: pk, + Signature: fmt.Sprintf("%X", ss.Signature), + }) + if err != nil { + return nil, err + } + + return string(bz), err +} + // StdSignatureToSignatureV2 converts a StdSignature to a SignatureV2 func StdSignatureToSignatureV2(cdc *codec.LegacyAmino, sig StdSignature) (signing.SignatureV2, error) { pk := sig.GetPubKey() diff --git a/x/auth/legacy/legacytx/stdtx.go b/x/auth/legacy/legacytx/stdtx.go index 749e387373c8..a7a00de784fb 100644 --- a/x/auth/legacy/legacytx/stdtx.go +++ b/x/auth/legacy/legacytx/stdtx.go @@ -1,14 +1,9 @@ package legacytx import ( - "fmt" - - "gopkg.in/yaml.v2" - "github.com/cosmos/cosmos-sdk/codec/legacy" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" txtypes "github.com/cosmos/cosmos-sdk/types/tx" @@ -73,50 +68,6 @@ func (fee StdFee) GasPrices() sdk.DecCoins { return sdk.NewDecCoinsFromCoins(fee.Amount...).QuoDec(sdk.NewDec(int64(fee.Gas))) } -// Deprecated -func NewStdSignature(pk cryptotypes.PubKey, sig []byte) StdSignature { - return StdSignature{PubKey: pk, Signature: sig} -} - -// GetSignature returns the raw signature bytes. -func (ss StdSignature) GetSignature() []byte { - return ss.Signature -} - -// GetPubKey returns the public key of a signature as a cryptotypes.PubKey using the -// Amino codec. -func (ss StdSignature) GetPubKey() cryptotypes.PubKey { - return ss.PubKey -} - -// MarshalYAML returns the YAML representation of the signature. -func (ss StdSignature) MarshalYAML() (interface{}, error) { - var ( - bz []byte - pubkey []byte - err error - ) - - if ss.PubKey != nil { - if pubkey, err = protocdc.MarshalJSON(ss.PubKey, nil); err != nil { - return nil, err - } - } - - bz, err = yaml.Marshal(struct { - PubKey string - Signature string - }{ - PubKey: string(pubkey), - Signature: fmt.Sprintf("%X", ss.Signature), - }) - if err != nil { - return nil, err - } - - return string(bz), err -} - // StdTx is the legacy transaction format for wrapping a Msg with Fee and Signatures. // It only works with Amino, please prefer the new protobuf Tx in types/tx. // NOTE: the first signature is the fee payer (Signatures must not be nil). diff --git a/x/auth/legacy/legacytx/stdtx_test.go b/x/auth/legacy/legacytx/stdtx_test.go index a702a82841d0..f1e496ad49d6 100644 --- a/x/auth/legacy/legacytx/stdtx_test.go +++ b/x/auth/legacy/legacytx/stdtx_test.go @@ -7,7 +7,6 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/log" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" - yaml "gopkg.in/yaml.v2" "github.com/cosmos/cosmos-sdk/codec" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" @@ -182,34 +181,6 @@ func TestDefaultTxEncoder(t *testing.T) { require.Equal(t, cdcBytes, encoderBytes) } -func TestStdSignatureMarshalYAML(t *testing.T) { - _, pubKey, _ := testdata.KeyTestPubAddr() - - testCases := []struct { - sig StdSignature - output string - }{ - { - StdSignature{}, - "|\n pubkey: \"\"\n signature: \"\"\n", - }, - { - StdSignature{PubKey: pubKey, Signature: []byte("dummySig")}, - fmt.Sprintf("|\n pubkey: %s\n signature: 64756D6D79536967\n", sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey)), - }, - { - StdSignature{PubKey: pubKey, Signature: nil}, - fmt.Sprintf("|\n pubkey: %s\n signature: \"\"\n", sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey)), - }, - } - - for i, tc := range testCases { - bz, err := yaml.Marshal(tc.sig) - require.NoError(t, err) - require.Equal(t, tc.output, string(bz), "test case #%d", i) - } -} - func TestSignatureV2Conversions(t *testing.T) { _, pubKey, _ := testdata.KeyTestPubAddr() cdc := codec.NewLegacyAmino() diff --git a/x/staking/client/cli/tx_test.go b/x/staking/client/cli/tx_test.go index 7a28aac96447..cf0e9fd232db 100644 --- a/x/staking/client/cli/tx_test.go +++ b/x/staking/client/cli/tx_test.go @@ -7,15 +7,31 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/client/flags" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" ) func TestPrepareConfigForTxCreateValidator(t *testing.T) { chainID := "chainID" ip := "1.1.1.1" nodeID := "nodeID" - valPubKey, _ := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, "cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a") + privKey := ed25519.GenPrivKey() + valPubKey := privKey.PubKey() + // valPubKey:= sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, "cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a") moniker := "DefaultMoniker" + mkTxValCfg := func(amount, commission, commissionMax, commissionMaxChange, minSelfDelegation string) TxCreateValidatorConfig { + return TxCreateValidatorConfig{ + IP: ip, + ChainID: chainID, + NodeID: nodeID, + PubKey: valPubKey, + Moniker: moniker, + Amount: amount, + CommissionRate: commission, + CommissionMaxRate: commissionMax, + CommissionMaxChangeRate: commissionMaxChange, + MinSelfDelegation: minSelfDelegation, + } + } tests := []struct { name string @@ -27,108 +43,38 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) { fsModify: func(fs *pflag.FlagSet) { return }, - expectedCfg: TxCreateValidatorConfig{ - IP: ip, - ChainID: chainID, - NodeID: nodeID, - PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey), - Moniker: moniker, - Amount: defaultAmount, - CommissionRate: "0.1", - CommissionMaxRate: "0.2", - CommissionMaxChangeRate: "0.01", - MinSelfDelegation: "1", - }, - }, - { + expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.2", "0.01", "1"), + }, { name: "Custom amount", fsModify: func(fs *pflag.FlagSet) { fs.Set(FlagAmount, "2000stake") }, - expectedCfg: TxCreateValidatorConfig{ - IP: ip, - Moniker: moniker, - ChainID: chainID, - NodeID: nodeID, - PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey), - Amount: "2000stake", - CommissionRate: "0.1", - CommissionMaxRate: "0.2", - CommissionMaxChangeRate: "0.01", - MinSelfDelegation: "1", - }, - }, - { + expectedCfg: mkTxValCfg("2000stake", "0.1", "0.2", "0.01", "1"), + }, { name: "Custom commission rate", fsModify: func(fs *pflag.FlagSet) { fs.Set(FlagCommissionRate, "0.54") }, - expectedCfg: TxCreateValidatorConfig{ - IP: ip, - Moniker: moniker, - ChainID: chainID, - NodeID: nodeID, - PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey), - Amount: defaultAmount, - CommissionRate: "0.54", - CommissionMaxRate: "0.2", - CommissionMaxChangeRate: "0.01", - MinSelfDelegation: "1", - }, - }, - { + expectedCfg: mkTxValCfg(defaultAmount, "0.54", "0.2", "0.01", "1"), + }, { name: "Custom commission max rate", fsModify: func(fs *pflag.FlagSet) { fs.Set(FlagCommissionMaxRate, "0.89") }, - expectedCfg: TxCreateValidatorConfig{ - IP: ip, - Moniker: moniker, - ChainID: chainID, - NodeID: nodeID, - PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey), - Amount: defaultAmount, - CommissionRate: "0.1", - CommissionMaxRate: "0.89", - CommissionMaxChangeRate: "0.01", - MinSelfDelegation: "1", - }, - }, - { + expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.89", "0.01", "1"), + }, { name: "Custom commission max change rate", fsModify: func(fs *pflag.FlagSet) { fs.Set(FlagCommissionMaxChangeRate, "0.55") }, - expectedCfg: TxCreateValidatorConfig{ - IP: ip, - Moniker: moniker, - ChainID: chainID, - NodeID: nodeID, - PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey), - Amount: defaultAmount, - CommissionRate: "0.1", - CommissionMaxRate: "0.2", - CommissionMaxChangeRate: "0.55", - MinSelfDelegation: "1", - }, + expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.2", "0.55", "1"), }, { name: "Custom min self delegations", fsModify: func(fs *pflag.FlagSet) { fs.Set(FlagMinSelfDelegation, "0.33") }, - expectedCfg: TxCreateValidatorConfig{ - IP: ip, - Moniker: moniker, - ChainID: chainID, - NodeID: nodeID, - PubKey: sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey), - Amount: defaultAmount, - CommissionRate: "0.1", - CommissionMaxRate: "0.2", - CommissionMaxChangeRate: "0.01", - MinSelfDelegation: "0.33", - }, + expectedCfg: mkTxValCfg(defaultAmount, "0.1", "0.2", "0.01", "0.33"), }, } From f7411dd26aeb35989b13a6a1a448834cb59972d9 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 23 Nov 2020 22:41:03 +0100 Subject: [PATCH 19/91] all code compiles --- client/keys/add.go | 1 + crypto/keyring/output_test.go | 6 ++---- crypto/keys/ed25519/ed25519_test.go | 10 +++++----- internal/protocdc/marshal.go | 6 ++++-- x/auth/vesting/types/vesting_account.go | 24 ++++++------------------ 5 files changed, 18 insertions(+), 29 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 4a04951ccd73..a24ece256d6e 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -295,6 +295,7 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemonic string) error { output, _ := cmd.Flags().GetString(cli.OutputFlag) + // TODO: remove Bech32 from here switch output { case OutputFormatText: diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index 89385748c98b..5e4b197c4443 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -12,20 +12,18 @@ import ( ) func TestBech32KeysOutput(t *testing.T) { - // TODO - update this test to not use bech32 - tmpKey := secp256k1.GenPrivKey().PubKey() tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes()) multisigPks := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey}) multiInfo := NewMultiInfo("multisig", multisigPks) accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) - require.True(t, accAddr.Equals(tmpAddr)) + require.True(t, accAddr.Equals(tmpAddr), "- %s\n+ %s", accAddr, tmpAddr) expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, tmpKey) require.NoError(t, err) expectedOutput.Threshold = 1 - expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), bechTmpKey, 1}} + expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), expectedOutput.PubKey, 1}} outputs, err := Bech32KeysOutput([]Info{multiInfo}) require.NoError(t, err) diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 0513c45c408a..4531c7fa96ba 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -282,17 +282,17 @@ func TestMarshalProto2(t *testing.T) { bz, err := codec.MarshalIfcJSON(ccfg.Marshaler, pk) require.NoError(err) + var pk3 cryptotypes.PubKey - // TODO: make a task to integrate the coded.*Marshal helper functiosn (from any.go) to the codec.Marshaler interface - // and remove JSONAnyMarshaler am := codec.NewIfcJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) err = codec.UnmarshalIfcJSON(am, &pk3, bz) require.NoError(err) require.True(pk3.Equals(pk)) - // Using JSONMarshaler for unpacking won't work Any type - // doesn't get automatically unpacked, and we we can't do it - // because Any doesn't know interfaces into which it should unpack + // ** Check unmarshal using JSONMarshaler ** + // Unpacking won't work straightforward s Any type + // Any can't implement UnpackInterfacesMessage insterface. So Any is not + // automatically unpacked and we won't get a value. var pkAny codectypes.Any err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny) require.NoError(err) diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index 4c781598e879..bdfd93569b4d 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -8,8 +8,10 @@ import ( "github.com/cosmos/cosmos-sdk/codec" ) -// MarshalJSON same as codec.ProtoMarshalJSON, but does msg type inspection to assert -// that it implements `proto.Message` and return an error if it doesn't. +// MarshalJSON is a wrapper for codec.ProtoMarshalJSON. It asserts that msg +// implements `proto.Message` and calls codec.ProtoMarshalJSON. +// This function should be used only with concrete types. For interface serialization +// you need to wrap the interface into Any or generally use MarshalIfcJSON. func MarshalJSON(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { msgProto, err := AssertMsg(msg) if err != nil { diff --git a/x/auth/vesting/types/vesting_account.go b/x/auth/vesting/types/vesting_account.go index cf42ccb21095..8387426507e2 100644 --- a/x/auth/vesting/types/vesting_account.go +++ b/x/auth/vesting/types/vesting_account.go @@ -7,7 +7,6 @@ import ( yaml "gopkg.in/yaml.v2" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" @@ -193,15 +192,13 @@ func (bva BaseVestingAccount) MarshalYAML() (interface{}, error) { out := vestingAccountYAML{ Address: accAddr, AccountNumber: bva.AccountNumber, + PubKey: getPKString(bva), Sequence: bva.Sequence, OriginalVesting: bva.OriginalVesting, DelegatedFree: bva.DelegatedFree, DelegatedVesting: bva.DelegatedVesting, EndTime: bva.EndTime, } - if out.PubKey, err = getPKString(bva); err != nil { - return nil, err - } return marshalYaml(out) } @@ -308,6 +305,7 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { out := vestingAccountYAML{ Address: accAddr, AccountNumber: cva.AccountNumber, + PubKey: getPKString(cva), Sequence: cva.Sequence, OriginalVesting: cva.OriginalVesting, DelegatedFree: cva.DelegatedFree, @@ -315,9 +313,6 @@ func (cva ContinuousVestingAccount) MarshalYAML() (interface{}, error) { EndTime: cva.EndTime, StartTime: cva.StartTime, } - if out.PubKey, err = getPKString(cva); err != nil { - return nil, err - } return marshalYaml(out) } @@ -453,6 +448,7 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { out := vestingAccountYAML{ Address: accAddr, AccountNumber: pva.AccountNumber, + PubKey: getPKString(pva), Sequence: pva.Sequence, OriginalVesting: pva.OriginalVesting, DelegatedFree: pva.DelegatedFree, @@ -461,9 +457,6 @@ func (pva PeriodicVestingAccount) MarshalYAML() (interface{}, error) { StartTime: pva.StartTime, VestingPeriods: pva.VestingPeriods, } - if out.PubKey, err = getPKString(pva); err != nil { - return nil, err - } return marshalYaml(out) } @@ -538,16 +531,11 @@ type getPK interface { GetPubKey() cryptotypes.PubKey } -func getPKString(g getPK) (string, error) { +func getPKString(g getPK) string { if pk := g.GetPubKey(); pk != nil { - // TODO check if it's ok to change a type of ValidatorOutput.PubKey to crypto.PubKey - pk, err := protocdc.MarshalJSON(pk, nil) - if err != nil { - return "", err - } - return string(pk), err + return pk.String() } - return "", nil + return "" } func marshalYaml(i interface{}) (interface{}, error) { From c906d74889c797ed4051454d5e89764db5606707 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 24 Nov 2020 01:10:19 +0100 Subject: [PATCH 20/91] keyring tests --- crypto/keyring/output.go | 25 +++++++++++++------------ crypto/keyring/output_test.go | 8 +++++++- crypto/keys/multisig/multisig.go | 8 ++++---- x/auth/keeper/account.go | 3 ++- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index d94fd367bc42..2ed6bf1c7a1f 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -1,6 +1,8 @@ package keyring import ( + "fmt" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,16 +23,23 @@ type KeyOutput struct { } // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys +// TODO remove error func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { - pkBytes, err := protocdc.MarshalJSON(pk, nil) + bz, err := protocdc.MarshalJSON(pk, nil) if err != nil { return KeyOutput{}, err } + fmt.Println(">>>> bz", string(bz)) + /* if pk != nil { + fmt.Printf(">>>>>> %t\n", pk) + pkStr = pk.String() + } + */ return KeyOutput{ Name: name, Type: keyType.String(), Address: a.String(), - PubKey: string(pkBytes), + PubKey: string(bz), }, nil } @@ -76,22 +85,14 @@ func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.AccAddress(pk.Address().Bytes()) - ko, err := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) - if err != nil { - return ko, err - } + ko, _ := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) if mInfo, ok := keyInfo.(*multiInfo); ok { pubKeys := make([]multisigPubKeyOutput, len(mInfo.PubKeys)) for i, pkInfo := range mInfo.PubKeys { pk = pkInfo.PubKey - addr = sdk.AccAddress(pk.Address().Bytes()) - pkBytes, err := protocdc.MarshalJSON(pk, nil) - if err != nil { - return ko, err - } - pubKeys[i] = multisigPubKeyOutput{addr.String(), string(pkBytes), pkInfo.Weight} + pubKeys[i] = multisigPubKeyOutput{addr.String(), pk.String(), pkInfo.Weight} } ko.Threshold = mInfo.Threshold diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index 5e4b197c4443..eae1be7ef72b 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -1,6 +1,7 @@ package keyring import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -18,11 +19,16 @@ func TestBech32KeysOutput(t *testing.T) { multisigPks := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey}) multiInfo := NewMultiInfo("multisig", multisigPks) accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) - require.True(t, accAddr.Equals(tmpAddr), "- %s\n+ %s", accAddr, tmpAddr) + + fmt.Println(multiInfo) + // TODO: + // require.True(t, accAddr.Equals(tmpAddr), "- %s\n+ %s", tmpAddr, accAddr) expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, tmpKey) require.NoError(t, err) expectedOutput.Threshold = 1 + + // TODO: check: pkStr = tmpKey.String() expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), expectedOutput.PubKey, 1}} outputs, err := Bech32KeysOutput([]Info{multiInfo}) diff --git a/crypto/keys/multisig/multisig.go b/crypto/keys/multisig/multisig.go index 590e5ba11001..cb11fbd4ddb5 100644 --- a/crypto/keys/multisig/multisig.go +++ b/crypto/keys/multisig/multisig.go @@ -16,18 +16,18 @@ var _ types.UnpackInterfacesMessage = &LegacyAminoPubKey{} // NewLegacyAminoPubKey returns a new LegacyAminoPubKey. // Panics if len(pubKeys) < k or 0 >= k. -func NewLegacyAminoPubKey(k int, pubKeys []cryptotypes.PubKey) *LegacyAminoPubKey { - if k <= 0 { +func NewLegacyAminoPubKey(treshold int, pubKeys []cryptotypes.PubKey) *LegacyAminoPubKey { + if treshold <= 0 { panic("threshold k of n multisignature: k <= 0") } - if len(pubKeys) < k { + if len(pubKeys) < treshold { panic("threshold k of n multisignature: len(pubKeys) < k") } anyPubKeys, err := packPubKeys(pubKeys) if err != nil { panic(err) } - return &LegacyAminoPubKey{Threshold: uint32(k), PubKeys: anyPubKeys} + return &LegacyAminoPubKey{Threshold: uint32(treshold), PubKeys: anyPubKeys} } // Address implements cryptotypes.PubKey Address method diff --git a/x/auth/keeper/account.go b/x/auth/keeper/account.go index 9921bb96f945..a26ea6427bc4 100644 --- a/x/auth/keeper/account.go +++ b/x/auth/keeper/account.go @@ -67,7 +67,8 @@ func (ak AccountKeeper) RemoveAccount(ctx sdk.Context, acc types.AccountI) { store.Delete(types.AddressStoreKey(addr)) } -// IterateAccounts iterates over all the stored accounts and performs a callback function +// IterateAccounts iterates over all the stored accounts and performs a callback function. +// Stops iteration when callback returns true. func (ak AccountKeeper) IterateAccounts(ctx sdk.Context, cb func(account types.AccountI) (stop bool)) { store := ctx.KVStore(ak.key) iterator := sdk.KVStorePrefixIterator(store, types.AddressStoreKeyPrefix) From 0632000d2f3809971d15fc976ea30856741d82ad Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 24 Nov 2020 21:50:15 +0100 Subject: [PATCH 21/91] keyring cleanup --- codec/types/compat.go | 2 +- crypto/keyring/output.go | 16 +++++++--------- crypto/keyring/output_test.go | 22 ++++++++++------------ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/codec/types/compat.go b/codec/types/compat.go index 3aa024126000..6716545932eb 100644 --- a/codec/types/compat.go +++ b/codec/types/compat.go @@ -37,7 +37,7 @@ func anyCompatError(errType string, x interface{}) error { func (any Any) MarshalAmino() ([]byte, error) { ac := any.compat if ac == nil { - return nil, anyCompatError("amino binary unmarshal", any) + return nil, anyCompatError("amino binary marshal", any) } return ac.aminoBz, ac.err } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 2ed6bf1c7a1f..cd7c6c4875a0 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -1,8 +1,6 @@ package keyring import ( - "fmt" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" @@ -29,12 +27,6 @@ func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.Pu if err != nil { return KeyOutput{}, err } - fmt.Println(">>>> bz", string(bz)) - /* if pk != nil { - fmt.Printf(">>>>>> %t\n", pk) - pkStr = pk.String() - } - */ return KeyOutput{ Name: name, Type: keyType.String(), @@ -92,7 +84,13 @@ func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { for i, pkInfo := range mInfo.PubKeys { pk = pkInfo.PubKey - pubKeys[i] = multisigPubKeyOutput{addr.String(), pk.String(), pkInfo.Weight} + addr = sdk.AccAddress(pk.Address()) + bz, err := protocdc.MarshalJSON(pk, nil) + if err != nil { + return ko, err + } + pubKeys[i] = multisigPubKeyOutput{ + addr.String(), string(bz), pkInfo.Weight} } ko.Threshold = mInfo.Threshold diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index eae1be7ef72b..d2cc24518322 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -1,7 +1,6 @@ package keyring import ( - "fmt" "testing" "github.com/stretchr/testify/require" @@ -9,29 +8,28 @@ import ( kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" ) func TestBech32KeysOutput(t *testing.T) { tmpKey := secp256k1.GenPrivKey().PubKey() tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes()) + multisigPk := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey}) - multisigPks := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey}) - multiInfo := NewMultiInfo("multisig", multisigPks) + multiInfo := NewMultiInfo("multisig", multisigPk) accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) - fmt.Println(multiInfo) - // TODO: - // require.True(t, accAddr.Equals(tmpAddr), "- %s\n+ %s", tmpAddr, accAddr) - - expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, tmpKey) + expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, multisigPk) require.NoError(t, err) expectedOutput.Threshold = 1 - - // TODO: check: pkStr = tmpKey.String() - expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), expectedOutput.PubKey, 1}} + tmpKeyBz, err := protocdc.MarshalJSON(tmpKey, nil) + expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), string(tmpKeyBz), 1}} outputs, err := Bech32KeysOutput([]Info{multiInfo}) require.NoError(t, err) - require.Equal(t, expectedOutput, outputs[0]) + require.Equal(t, expectedOutput.PubKeys, outputs[0].PubKeys) + + // expectedOutput is: + // {"name":"multisig","type":"multi","address":"cosmos1dutd6e06gv4tfdypzyvdfuwct8cdtctsjr0nl3","pubkey":"{\"threshold\":1,\"public_keys\":[{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"A5edrlIT2MWsp4SYh+KvGfjxmKw7NNxga7K23faEEmL/\"}]}","threshold":1,"pubkeys":[{"address":"cosmos13533lkpyxrgttefm34f35c89v2yrv3ugkns5p6","pubkey":"{\"key\":\"A5edrlIT2MWsp4SYh+KvGfjxmKw7NNxga7K23faEEmL/\"}","weight":1}]} } From 7151b25a8dc49988f3a185cd7a70ecd37fb5b088 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 24 Nov 2020 21:52:26 +0100 Subject: [PATCH 22/91] remove AssertMsg --- internal/protocdc/marshal.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index bdfd93569b4d..aad57629b9ff 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -13,19 +13,9 @@ import ( // This function should be used only with concrete types. For interface serialization // you need to wrap the interface into Any or generally use MarshalIfcJSON. func MarshalJSON(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { - msgProto, err := AssertMsg(msg) - if err != nil { - return nil, err - } - return codec.ProtoMarshalJSON(msgProto, resolver) -} - -// AssertMsg casts i to a proto.Message. Returns an error if it's not possible. -// TODO remove -func AssertMsg(i interface{}) (proto.Message, error) { - pm, ok := i.(proto.Message) + msgProto, ok := i.(proto.Message) if !ok { return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", i) } - return pm, nil + return codec.ProtoMarshalJSON(msgProto, resolver) } From 9a12242b298269b6b2426b41df3848fdc0cf90c5 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 24 Nov 2020 21:58:13 +0100 Subject: [PATCH 23/91] fix some tests --- codec/any.go | 2 +- codec/any_test.go | 2 +- internal/protocdc/marshal.go | 4 ++-- types/address_test.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/codec/any.go b/codec/any.go index 545638fe647e..e5b8fdd8ee1c 100644 --- a/codec/any.go +++ b/codec/any.go @@ -18,7 +18,7 @@ import ( func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { - return nil, fmt.Errorf("can't proto marshal %T - expecting proto.Message", x) + return nil, fmt.Errorf("can't proto marshal %T; expecting proto.Message", x) } any, err := types.NewAnyWithValue(msg) if err != nil { diff --git a/codec/any_test.go b/codec/any_test.go index 6094215238f4..20c5136f2b00 100644 --- a/codec/any_test.go +++ b/codec/any_test.go @@ -61,5 +61,5 @@ func TestMarshalAnyNonProtoErrors(t *testing.T) { _, err := codec.MarshalIfc(cdc, 29) require.Error(t, err) - require.Equal(t, err, errors.New("can't proto marshal int")) + require.Equal(t, err, errors.New("can't proto marshal int; expecting proto.Message")) } diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go index aad57629b9ff..02609b3fe924 100644 --- a/internal/protocdc/marshal.go +++ b/internal/protocdc/marshal.go @@ -13,9 +13,9 @@ import ( // This function should be used only with concrete types. For interface serialization // you need to wrap the interface into Any or generally use MarshalIfcJSON. func MarshalJSON(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { - msgProto, ok := i.(proto.Message) + msgProto, ok := msg.(proto.Message) if !ok { - return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", i) + return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", msg) } return codec.ProtoMarshalJSON(msgProto, resolver) } diff --git a/types/address_test.go b/types/address_test.go index f5f3e8039d4a..b7938fca822b 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -474,7 +474,7 @@ func (s *addressTestSuite) TestGetConsAddress() { func (s *addressTestSuite) TestGetFromBech32() { _, err := types.GetFromBech32("", "prefix") s.Require().Error(err) - s.Require().Equal("decoding Bech32 address failed: must provide an address", err.Error()) + s.Require().Equal("decoding Bech32 address failed: must provide a non empty address", err.Error()) _, err = types.GetFromBech32("cosmos1qqqsyqcyq5rqwzqfys8f67", "x") s.Require().Error(err) s.Require().Equal("invalid Bech32 prefix; expected x, got cosmos", err.Error()) From f4a585bd14cfe9d5ccd415fbf54ab78d99bfe7cf Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 24 Nov 2020 23:32:39 +0100 Subject: [PATCH 24/91] fix add_ledger_test.go --- client/keys/add_ledger_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index b8ba1ec7f59e..f477a1be2041 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client" @@ -17,6 +16,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { @@ -75,7 +75,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, "terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6", - sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, key1.GetPubKey())) + legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, key1.GetPubKey())) config.SetCoinType(118) config.SetFullFundraiserPath("44'/118'/0'/0/0") @@ -123,5 +123,5 @@ func Test_runAddCmdLedger(t *testing.T) { require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, key1.GetPubKey())) + legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, key1.GetPubKey())) } From b6e5582d0aeba56165ae74c69c82a6c0bd2c1221 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 24 Nov 2020 23:57:45 +0100 Subject: [PATCH 25/91] update cli tests --- crypto/keyring/keyring_ledger_test.go | 7 ++++--- x/slashing/client/cli/cli_test.go | 4 +++- x/staking/client/cli/cli_test.go | 4 +++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index eb7e85f9e89c..9241eedb2289 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func TestInMemoryCreateLedger(t *testing.T) { @@ -28,7 +29,7 @@ func TestInMemoryCreateLedger(t *testing.T) { // The mock is available, check that the address is correct pubKey := ledger.GetPubKey() - pk, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey) + pk, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -39,7 +40,7 @@ func TestInMemoryCreateLedger(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey) + pk, err = legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -106,7 +107,7 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { // The mock is available, check that the address is correct require.Equal(t, "some_account", ledger.GetName()) pubKey := ledger.GetPubKey() - pk, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey) + pk, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) diff --git a/x/slashing/client/cli/cli_test.go b/x/slashing/client/cli/cli_test.go index 41fac414c7af..6c6a4d2fb3b8 100644 --- a/x/slashing/client/cli/cli_test.go +++ b/x/slashing/client/cli/cli_test.go @@ -15,6 +15,7 @@ import ( clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" ) @@ -50,7 +51,8 @@ func (s *IntegrationTestSuite) TearDownSuite() { func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { val := s.network.Validators[0] - valConsPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, val.PubKey) + // TODO: don't use bech32 here + valConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, val.PubKey) s.Require().NoError(err) testCases := []struct { diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 12b4084a4e8f..8c5de4b23d0e 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -21,6 +21,7 @@ import ( clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/types/query" banktestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil" "github.com/cosmos/cosmos-sdk/x/staking/client/cli" @@ -79,7 +80,8 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { val := s.network.Validators[0] consPrivKey := ed25519.GenPrivKey() - consPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, consPrivKey.PubKey()) + // TODO: check if we can change this + consPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, consPrivKey.PubKey()) s.Require().NoError(err) info, _, err := val.ClientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, hd.Secp256k1) From c5be087e8ff61c719605a2007875e34c97e16845 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 25 Nov 2020 00:30:33 +0100 Subject: [PATCH 26/91] debug cli test --- x/staking/client/cli/cli_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 8c5de4b23d0e..f8b94146f514 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -1,4 +1,5 @@ -// +build norace +// TODO: change this flag +// xbuild norace package cli_test @@ -18,10 +19,10 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/internal/protocdc" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/types/query" banktestutil "github.com/cosmos/cosmos-sdk/x/bank/client/testutil" "github.com/cosmos/cosmos-sdk/x/staking/client/cli" @@ -80,8 +81,10 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { val := s.network.Validators[0] consPrivKey := ed25519.GenPrivKey() - // TODO: check if we can change this - consPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, consPrivKey.PubKey()) + // TODO: fix this + //consPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, consPrivKey.PubKey()) + //s.Require().NoError(err) + consPubKey, err := protocdc.MarshalJSON(consPrivKey.PubKey(), nil) s.Require().NoError(err) info, _, err := val.ClientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, hd.Secp256k1) From f6fd51ee6a934140961b65135c72de65dd0a2952 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 25 Nov 2020 00:42:38 +0100 Subject: [PATCH 27/91] rename clashed bech32 names --- crypto/keyring/types_test.go | 3 +- crypto/ledger/ledger_test.go | 6 ++-- types/address_test.go | 6 ++-- types/bech32/legacybech32/pk.go | 37 ++++++++-------------- types/bech32/legacybech32/pk_bench_test.go | 6 ++-- types/bech32/legacybech32/pk_test.go | 2 +- x/auth/legacy/v038/types.go | 16 +++++----- x/slashing/client/rest/query.go | 2 +- x/staking/legacy/v034/types.go | 4 +-- x/staking/legacy/v036/types.go | 4 +-- x/staking/legacy/v038/types.go | 4 +-- 11 files changed, 41 insertions(+), 49 deletions(-) diff --git a/crypto/keyring/types_test.go b/crypto/keyring/types_test.go index 24bc7dec1302..880a3901cb3f 100644 --- a/crypto/keyring/types_test.go +++ b/crypto/keyring/types_test.go @@ -19,12 +19,13 @@ func Test_writeReadLedgerInfo(t *testing.T) { lInfo := newLedgerInfo("some_name", &secp256k1.PubKey{Key: tmpKey}, *hd.NewFundraiserParams(5, sdk.CoinType, 1), hd.Secp256k1Type) require.Equal(t, TypeLedger, lInfo.GetType()) + // TODO: update the test - shouldn't use bech32 path, err := lInfo.GetPath() require.NoError(t, err) require.Equal(t, "m/44'/118'/5'/0/1", path.String()) require.Equal(t, "cosmospub1addwnpepqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5wmxv8p", - legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, lInfo.GetPubKey())) + legacybech32.MustMarshalPubKey(legacybech32.AccPK, lInfo.GetPubKey())) // Serialize and restore serialized := marshalInfo(lInfo) diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index a4f82d3e84a0..c691974004a4 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -75,7 +75,7 @@ func TestPublicKeyUnsafeHDPath(t *testing.T) { require.NoError(t, tmp.ValidateKey()) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := legacybech32.MarshalPubKey(legacybech32.AccPK, priv.PubKey()) require.NoError(t, err) require.Equal(t, expectedAnswers[i], pubKeyAddr, @@ -111,7 +111,7 @@ func TestPublicKeySafe(t *testing.T) { fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - pubKeyAddr, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := legacybech32.MarshalPubKey(legacybech32.AccPK, priv.PubKey()) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) @@ -175,7 +175,7 @@ func TestPublicKeyHDPath(t *testing.T) { require.NoError(t, tmp.ValidateKey()) (&tmp).AssertIsPrivKeyInner() - pubKeyAddr, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := legacybech32.MarshalPubKey(legacybech32.AccPK, priv.PubKey()) require.NoError(t, err) require.Equal(t, expectedPubKeys[i], pubKeyAddr, diff --git a/types/address_test.go b/types/address_test.go index b7938fca822b..44fa5987f9b7 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -236,7 +236,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { acc.String(), prefix+types.PrefixAccount), acc.String()) - bech32Pub := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pub) + bech32Pub := legacybech32.MustMarshalPubKey(legacybech32.AccPK, pub) s.Require().True(strings.HasPrefix( bech32Pub, prefix+types.PrefixPublic)) @@ -250,7 +250,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { val.String(), prefix+types.PrefixValidator+types.PrefixAddress)) - bech32ValPub := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeValPub, pub) + bech32ValPub := legacybech32.MustMarshalPubKey(legacybech32.ValPub, pub) s.Require().True(strings.HasPrefix( bech32ValPub, prefix+types.PrefixValidator+types.PrefixPublic)) @@ -264,7 +264,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { cons.String(), prefix+types.PrefixConsensus+types.PrefixAddress)) - bech32ConsPub := legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, pub) + bech32ConsPub := legacybech32.MustMarshalPubKey(legacybech32.ConsPub, pub) s.Require().True(strings.HasPrefix( bech32ConsPub, prefix+types.PrefixConsensus+types.PrefixPublic)) diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index dc6a84baf5a2..072d7741234e 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -18,22 +18,23 @@ import ( type Bech32PubKeyType string // Bech32 conversion constants +// TODO: check where we can remove this const ( - Bech32PubKeyTypeAccPub Bech32PubKeyType = "accpub" - Bech32PubKeyTypeValPub Bech32PubKeyType = "valpub" - Bech32PubKeyTypeConsPub Bech32PubKeyType = "conspub" + AccPK Bech32PubKeyType = "accpub" + ValPub Bech32PubKeyType = "valpub" + ConsPub Bech32PubKeyType = "conspub" ) -// Deprecated: Bech32ifyPubKey returns a Bech32 encoded string containing the appropriate +// Deprecated: MarshalPubKey returns a Bech32 encoded string containing the appropriate // prefix based on the key type provided for a given PublicKey. -func Bech32ifyPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) (string, error) { +func MarshalPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) (string, error) { bech32Prefix := getPrefix(pkt) return bech32.ConvertAndEncode(bech32Prefix, legacy.Cdc.MustMarshalBinaryBare(pubkey)) } -// Deprecated: MustBech32ifyPubKey calls Bech32ifyPubKey and panics on error. -func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) string { - res, err := Bech32ifyPubKey(pkt, pubkey) +// Deprecated: MustMarshalPubKey calls Bech32ifyPubKey and panics on error. +func MustMarshalPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) string { + res, err := MarshalPubKey(pkt, pubkey) if err != nil { panic(err) } @@ -44,21 +45,21 @@ func MustBech32ifyPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) string func getPrefix(pkt Bech32PubKeyType) string { cfg := sdk.GetConfig() switch pkt { - case Bech32PubKeyTypeAccPub: + case AccPK: return cfg.GetBech32AccountPubPrefix() - case Bech32PubKeyTypeValPub: + case ValPub: return cfg.GetBech32ValidatorPubPrefix() - case Bech32PubKeyTypeConsPub: + case ConsPub: return cfg.GetBech32ConsensusPubPrefix() } return "" } -// Deprecated: GetPubKeyFromBech32 returns a PublicKey from a bech32-encoded PublicKey with +// Deprecated: UnmarshalPubKey returns a PublicKey from a bech32-encoded PublicKey with // a given key type. -func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.PubKey, error) { +func UnmarshalPubKey(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.PubKey, error) { bech32Prefix := getPrefix(pkt) bz, err := sdk.GetFromBech32(pubkeyStr, bech32Prefix) @@ -68,13 +69,3 @@ func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.Pu return cryptocodec.PubKeyFromBytes(bz) } - -// Deprecated: MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error. -func MustGetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) cryptotypes.PubKey { - res, err := GetPubKeyFromBech32(pkt, pubkeyStr) - if err != nil { - panic(err) - } - - return res -} diff --git a/types/bech32/legacybech32/pk_bench_test.go b/types/bech32/legacybech32/pk_bench_test.go index 47a6b52bec02..48848394b5ff 100644 --- a/types/bech32/legacybech32/pk_bench_test.go +++ b/types/bech32/legacybech32/pk_bench_test.go @@ -21,7 +21,7 @@ func BenchmarkBech32ifyPubKey(b *testing.B) { rng.Read(pk.Key) b.StartTimer() - _, err := Bech32ifyPubKey(Bech32PubKeyTypeConsPub, pk) + _, err := MarshalPubKey(ConsPub, pk) require.NoError(b, err) } } @@ -37,11 +37,11 @@ func BenchmarkGetPubKeyFromBech32(b *testing.B) { b.StopTimer() rng.Read(pk.Key) - pkStr, err := Bech32ifyPubKey(Bech32PubKeyTypeConsPub, pk) + pkStr, err := MarshalPubKey(ConsPub, pk) require.NoError(b, err) b.StartTimer() - pk2, err := GetPubKeyFromBech32(Bech32PubKeyTypeConsPub, pkStr) + pk2, err := UnmarshalPubKey(ConsPub, pkStr) require.NoError(b, err) require.Equal(b, pk, pk2) } diff --git a/types/bech32/legacybech32/pk_test.go b/types/bech32/legacybech32/pk_test.go index 0f2a63ad9f8e..262784c0428a 100644 --- a/types/bech32/legacybech32/pk_test.go +++ b/types/bech32/legacybech32/pk_test.go @@ -18,7 +18,7 @@ func TestBeach32ifPbKey(t *testing.T) { require.Nil(err, "%s", err) require.NotNil(priv) - pubKeyAddr, err := Bech32ifyPubKey(Bech32PubKeyTypeAccPub, priv.PubKey()) + pubKeyAddr, err := MarshalPubKey(AccPK, priv.PubKey()) require.NoError(err) require.Equal("cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) diff --git a/x/auth/legacy/v038/types.go b/x/auth/legacy/v038/types.go index e0dc7be4b6c7..8d109fc1e863 100644 --- a/x/auth/legacy/v038/types.go +++ b/x/auth/legacy/v038/types.go @@ -177,7 +177,7 @@ func (acc BaseAccount) MarshalJSON() ([]byte, error) { } if acc.PubKey != nil { - pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, acc.PubKey) + pks, err := legacybech32.MarshalPubKey(legacybech32.AccPK, acc.PubKey) if err != nil { return nil, err } @@ -196,7 +196,7 @@ func (acc *BaseAccount) UnmarshalJSON(bz []byte) error { } if alias.PubKey != "" { - pk, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err := legacybech32.UnmarshalPubKey(legacybech32.AccPK, alias.PubKey) if err != nil { return err } @@ -243,7 +243,7 @@ func (bva BaseVestingAccount) MarshalJSON() ([]byte, error) { } if bva.PubKey != nil { - pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, bva.PubKey) + pks, err := legacybech32.MarshalPubKey(legacybech32.AccPK, bva.PubKey) if err != nil { return nil, err } @@ -267,7 +267,7 @@ func (bva *BaseVestingAccount) UnmarshalJSON(bz []byte) error { ) if alias.PubKey != "" { - pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err = legacybech32.UnmarshalPubKey(legacybech32.AccPK, alias.PubKey) if err != nil { return err } @@ -312,7 +312,7 @@ func (cva ContinuousVestingAccount) MarshalJSON() ([]byte, error) { } if cva.PubKey != nil { - pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, cva.PubKey) + pks, err := legacybech32.MarshalPubKey(legacybech32.AccPK, cva.PubKey) if err != nil { return nil, err } @@ -336,7 +336,7 @@ func (cva *ContinuousVestingAccount) UnmarshalJSON(bz []byte) error { ) if alias.PubKey != "" { - pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err = legacybech32.UnmarshalPubKey(legacybech32.AccPK, alias.PubKey) if err != nil { return err } @@ -378,7 +378,7 @@ func (dva DelayedVestingAccount) MarshalJSON() ([]byte, error) { } if dva.PubKey != nil { - pks, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, dva.PubKey) + pks, err := legacybech32.MarshalPubKey(legacybech32.AccPK, dva.PubKey) if err != nil { return nil, err } @@ -402,7 +402,7 @@ func (dva *DelayedVestingAccount) UnmarshalJSON(bz []byte) error { ) if alias.PubKey != "" { - pk, err = legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeAccPub, alias.PubKey) + pk, err = legacybech32.UnmarshalPubKey(legacybech32.AccPK, alias.PubKey) if err != nil { return err } diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index fd9be86a0596..8818c3bc7d44 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -33,7 +33,7 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) { func signingInfoHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - pk, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, vars["validatorPubKey"]) + pk, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, vars["validatorPubKey"]) if rest.CheckBadRequestError(w, err) { return } diff --git a/x/staking/legacy/v034/types.go b/x/staking/legacy/v034/types.go index 1bd7439b9e48..7817388e862e 100644 --- a/x/staking/legacy/v034/types.go +++ b/x/staking/legacy/v034/types.go @@ -140,7 +140,7 @@ type ( ) func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, v.ConsPubKey) if err != nil { return nil, err } @@ -166,7 +166,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, bv.ConsPubKey) if err != nil { return err } diff --git a/x/staking/legacy/v036/types.go b/x/staking/legacy/v036/types.go index 323db01ce8f0..32c83f2c990a 100644 --- a/x/staking/legacy/v036/types.go +++ b/x/staking/legacy/v036/types.go @@ -89,7 +89,7 @@ func NewGenesisState( } func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, v.ConsPubKey) if err != nil { return nil, err } @@ -114,7 +114,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, bv.ConsPubKey) if err != nil { return err } diff --git a/x/staking/legacy/v038/types.go b/x/staking/legacy/v038/types.go index 80df4c5142e3..fd92a83622e9 100644 --- a/x/staking/legacy/v038/types.go +++ b/x/staking/legacy/v038/types.go @@ -114,7 +114,7 @@ func NewGenesisState( // MarshalJSON marshals the validator to JSON using Bech32 func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, v.ConsPubKey) if err != nil { return nil, err } @@ -140,7 +140,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := legacybech32.GetPubKeyFromBech32(legacybech32.Bech32PubKeyTypeConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, bv.ConsPubKey) if err != nil { return err } From 21e3dc65df61f84f632265e3ad1b577786445227 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 25 Nov 2020 00:53:12 +0100 Subject: [PATCH 28/91] linter fixes --- codec/any.go | 8 ++++---- codec/proto_codec.go | 16 ++++++++-------- types/bech32/legacybech32/pk.go | 4 ---- x/slashing/simulation/decoder.go | 2 +- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/codec/any.go b/codec/any.go index e5b8fdd8ee1c..314e7937938a 100644 --- a/codec/any.go +++ b/codec/any.go @@ -14,7 +14,7 @@ import ( // MarshalIfc is a convenience function for proto marshalling interfaces. It // packs the provided value in an Any and then marshals it to bytes. -// NOTE: if you use a concret type, then you should use BinaryMarshaler.MarshalBinaryBare directly +// NOTE: if you use a concert type, then you should use BinaryMarshaler.MarshalBinaryBare directly func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { @@ -31,7 +31,7 @@ func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { // UnmarshalIfc is a convenience function for proto unmarshaling interfaces. It // unmarshals an Any from bz and then unpacks it to the `iface`, which must // be a pointer to a non empty interface with registered implementations. -// NOTE: if you use a concret type, then you should use BinaryMarshaler.UnarshalBinaryBare directly +// NOTE: if you use a concert type, then you should use BinaryMarshaler.UnarshalBinaryBare directly // // Ex: // var x MyInterface @@ -49,7 +49,7 @@ func UnmarshalIfc(m BinaryMarshaler, iface interface{}, bz []byte) error { // MarshalIfcJSON is a convenience function for proto marshalling interfaces. It // packs the provided value in an Any and then marshals it to bytes. -// NOTE: if you use a concret type, then you should use JSONMarshaler.MarshalJSON directly +// NOTE: if you use a concert type, then you should use JSONMarshaler.MarshalJSON directly func MarshalIfcJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { any, err := types.NewAnyWithValue(x) if err != nil { @@ -61,7 +61,7 @@ func MarshalIfcJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { // UnmarshalIfcJSON is a convenience function for proto unmarshaling interfaces. // It unmarshals an Any from bz and then unpacks it to the `iface`, which must // be a pointer to a non empty interface with registered implementations. -// NOTE: if you use a concret type, then you should use JSONMarshaler.UnarshalJSON directly +// NOTE: if you use a concert type, then you should use JSONMarshaler.UnarshalJSON directly // // Ex: // var x MyInterface diff --git a/codec/proto_codec.go b/codec/proto_codec.go index f2737ba88246..20feef56cc6d 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -33,14 +33,14 @@ func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec { } // MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.MarshalIfc func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) { return o.Marshal() } // MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.MarshalIfc func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte { bz, err := pc.MarshalBinaryBare(o) @@ -74,7 +74,7 @@ func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte { } // UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.UnmarshalIfc func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { err := ptr.Unmarshal(bz) @@ -89,7 +89,7 @@ func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { } // MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.UnmarshalIfc func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) { if err := pc.UnmarshalBinaryBare(bz, ptr); err != nil { @@ -123,7 +123,7 @@ func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMars // MarshalJSON implements JSONMarshaler.MarshalJSON method, // it marshals to JSON using proto codec. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.MarshalIfcJSON func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { m, ok := o.(ProtoMarshaler) @@ -136,7 +136,7 @@ func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { // MustMarshalJSON implements JSONMarshaler.MustMarshalJSON method, // it executes MarshalJSON except it panics upon failure. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.MarshalIfcJSON func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { bz, err := pc.MarshalJSON(o) @@ -149,7 +149,7 @@ func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { // UnmarshalJSON implements JSONMarshaler.UnmarshalJSON method, // it unmarshals from JSON using proto codec. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.UnmarshalIfcJSON func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { m, ok := ptr.(ProtoMarshaler) @@ -168,7 +168,7 @@ func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { // MustUnmarshalJSON implements JSONMarshaler.MustUnmarshalJSON method, // it executes UnmarshalJSON except it panics upon failure. -// NOTE: this function must be used with a concret type which +// NOTE: this function must be used with a concert type which // implements proto.Message. For interface please use the codec.UnmarshalIfcJSON func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) { if err := pc.UnmarshalJSON(bz, ptr); err != nil { diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index 072d7741234e..b4fa2d8163a5 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -2,10 +2,6 @@ // release. package legacybech32 -// nolint - -// TODO: remove Bech32 prefix, it's already in package - import ( "github.com/cosmos/cosmos-sdk/codec/legacy" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" diff --git a/x/slashing/simulation/decoder.go b/x/slashing/simulation/decoder.go index a4c10329f998..5da4fdd547e8 100644 --- a/x/slashing/simulation/decoder.go +++ b/x/slashing/simulation/decoder.go @@ -14,7 +14,7 @@ import ( // NewDecodeStore returns a decoder function closure that unmarshals the KVPair's // Value to the corresponding slashing type. -func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string { +func NewDecodeStore(cdc codec.BinaryMarshaler) func(kvA, kvB kv.Pair) string { return func(kvA, kvB kv.Pair) string { switch { case bytes.Equal(kvA.Key[:1], types.ValidatorSigningInfoKeyPrefix): From 688e3ca30858afc0f249142e81f781024d440f2d Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 20:09:01 +0100 Subject: [PATCH 29/91] update tmservice tests --- client/grpc/tmservice/service.go | 57 +++++++++++---------------- crypto/keys/multisig/multisig_test.go | 36 ++++++++++++++--- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/client/grpc/tmservice/service.go b/client/grpc/tmservice/service.go index b1198eb67331..4a15729ad670 100644 --- a/client/grpc/tmservice/service.go +++ b/client/grpc/tmservice/service.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/rpc" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/internal/protocdc" qtypes "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/version" ) @@ -94,25 +95,9 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *qtypes.GetL return nil, err } - validatorsRes, err := rpc.GetValidators(s.clientCtx, nil, &page, &limit) - if err != nil { - return nil, err - } - - outputValidatorsRes := &qtypes.GetLatestValidatorSetResponse{ - BlockHeight: validatorsRes.BlockHeight, - Validators: make([]*qtypes.Validator, len(validatorsRes.Validators)), - } - - for i, validator := range validatorsRes.Validators { - outputValidatorsRes.Validators[i] = &qtypes.Validator{ - Address: validator.Address, - ProposerPriority: validator.ProposerPriority, - PubKey: validator.PubKey, - VotingPower: validator.VotingPower, - } - } - return outputValidatorsRes, nil + r := &qtypes.GetLatestValidatorSetResponse{} + r.BlockHeight, r.Validators, err = validatorsOutput(s.clientCtx, nil, page, limit) + return r, err } // GetValidatorSetByHeight implements ServiceServer.GetValidatorSetByHeight @@ -129,27 +114,31 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *qtypes.Ge if req.Height > chainHeight { return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length") } + r := &qtypes.GetValidatorSetByHeightResponse{} + r.BlockHeight, r.Validators, err = validatorsOutput(s.clientCtx, &req.Height, page, limit) + return r, err +} - validatorsRes, err := rpc.GetValidators(s.clientCtx, &req.Height, &page, &limit) - +func validatorsOutput(ctx client.Context, height *int64, page, limit int) (int64, []*qtypes.Validator, error) { + vsRes, err := rpc.GetValidators(ctx, height, &page, &limit) if err != nil { - return nil, err + return 0, nil, err } - outputValidatorsRes := &qtypes.GetValidatorSetByHeightResponse{ - BlockHeight: validatorsRes.BlockHeight, - Validators: make([]*qtypes.Validator, len(validatorsRes.Validators)), - } - - for i, validator := range validatorsRes.Validators { - outputValidatorsRes.Validators[i] = &qtypes.Validator{ - Address: validator.Address, - ProposerPriority: validator.ProposerPriority, - PubKey: validator.PubKey, - VotingPower: validator.VotingPower, + out := make([]*qtypes.Validator, len(vsRes.Validators)) + for i, v := range vsRes.Validators { + pkBz, err := protocdc.MarshalJSON(v.PubKey, nil) + if err != nil { + return 0, nil, err + } + out[i] = &qtypes.Validator{ + Address: v.Address, + ProposerPriority: v.ProposerPriority, + PubKey: string(pkBz), + VotingPower: v.VotingPower, } } - return outputValidatorsRes, nil + return vsRes.BlockHeight, out, nil } // GetNodeInfo implements ServiceServer.GetNodeInfo diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 2b91e74eb6c5..4e9f3c0f068b 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -11,13 +11,13 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" + "github.com/cosmos/cosmos-sdk/internal/protocdc" "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" ) func TestAddress(t *testing.T) { - msg := []byte{1, 2, 3, 4} - pubKeys, _ := generatePubKeysAndSignatures(5, msg) + pubKeys := generatePubKeys(5) multisigKey := kmultisig.NewLegacyAminoPubKey(2, pubKeys) require.Len(t, multisigKey.Address().Bytes(), 20) @@ -100,7 +100,7 @@ func TestVerifyMultisignature(t *testing.T) { { "wrong size for sig bit array", func() { - pubKeys, _ := generatePubKeysAndSignatures(3, msg) + pubKeys := generatePubKeys(3) pk = kmultisig.NewLegacyAminoPubKey(3, pubKeys) sig = multisig.NewMultisig(1) }, @@ -177,7 +177,7 @@ func TestVerifyMultisignature(t *testing.T) { { "unable to verify signature", func() { - pubKeys, _ := generatePubKeysAndSignatures(2, msg) + pubKeys := generatePubKeys(2) _, sigs := generatePubKeysAndSignatures(2, msg) pk = kmultisig.NewLegacyAminoPubKey(2, pubKeys) sig = multisig.NewMultisig(2) @@ -246,8 +246,7 @@ func TestMultiSigMigration(t *testing.T) { } func TestPubKeyMultisigThresholdAminoToIface(t *testing.T) { - msg := []byte{1, 2, 3, 4} - pubkeys, _ := generatePubKeysAndSignatures(5, msg) + pubkeys := generatePubKeys(5) multisigKey := kmultisig.NewLegacyAminoPubKey(2, pubkeys) ab, err := kmultisig.AminoCdc.MarshalBinaryLengthPrefixed(multisigKey) @@ -261,6 +260,14 @@ func TestPubKeyMultisigThresholdAminoToIface(t *testing.T) { require.Equal(t, multisigKey.Equals(&pubKey), true) } +func generatePubKeys(n int) []cryptotypes.PubKey { + pks := make([]cryptotypes.PubKey, n) + for i := 0; i < n; i++ { + pks[i] = secp256k1.GenPrivKey().PubKey() + } + return pks +} + func generatePubKeysAndSignatures(n int, msg []byte) (pubKeys []cryptotypes.PubKey, signatures []signing.SignatureData) { pubKeys = make([]cryptotypes.PubKey, n) signatures = make([]signing.SignatureData, n) @@ -307,3 +314,20 @@ func reorderPubKey(pk *kmultisig.LegacyAminoPubKey) (other *kmultisig.LegacyAmin other = &kmultisig.LegacyAminoPubKey{Threshold: 2, PubKeys: pubkeysCpy} return } + +func TestDisplay(t *testing.T) { + require := require.New(t) + pubKeys := generatePubKeys(3) + msig := kmultisig.NewLegacyAminoPubKey(2, pubKeys) + + // LegacyAminoPubKey wraps PubKeys into Amino (for serialization) and Any String method doesn't work. + require.PanicsWithValue("reflect.Value.Interface: cannot return value obtained from unexported field or method", + func() { require.Empty(msig.String()) }, + ) + msigBz, err := protocdc.MarshalJSON(msig, nil) + require.NoError(err) + msigStr := string(msigBz) + require.Contains(msigStr, `"threshold":2`) + require.Contains(msigStr, "cosmos.crypto.secp256k1.PubKey") + // example output: {"threshold":2,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AoiL9HXu/WDndVCrDptys9fx4NFEbBquhs3SubQG5QMC"},{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Am8adwKmNfz6klSx6e2gessjP7FznW8Y56NyrUtpTW4n"},{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtWLgEf+eQXd4ZudD/kjppK1pWRrXJH55joRyghB8WWc"}]} +} From 14bfa7b1a35440f03944b979fa1de940ac3adfd5 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 20:36:40 +0100 Subject: [PATCH 30/91] linter: update legacy deprecated checks --- .golangci.yml | 5 +++++ crypto/keyring/output.go | 2 +- x/slashing/client/rest/query.go | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 34738ccf7e6f..a79f7d7e44a6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -56,6 +56,11 @@ issues: - text: "ST1016:" linters: - stylecheck + - path: "legacy" + text: "SA1019:" + linters: + - staticcheck + max-issues-per-linter: 10000 max-same-issues: 10000 diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index cd7c6c4875a0..e9d2e257f2b2 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -22,7 +22,7 @@ type KeyOutput struct { // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys // TODO remove error -func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { +func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { // nolint:interfacer bz, err := protocdc.MarshalJSON(pk, nil) if err != nil { return KeyOutput{}, err diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index 8818c3bc7d44..c97c7124bfdb 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -7,7 +7,7 @@ import ( "github.com/gorilla/mux" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" + "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" //nolint:staticcheck "github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/x/slashing/types" ) From 218f54bdc377a96ebe1aba66bfe6e9756b99dfd5 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 20:46:41 +0100 Subject: [PATCH 31/91] fix names --- client/keys/add_ledger_test.go | 4 ++-- crypto/keyring/keyring_ledger_test.go | 9 ++++----- types/bech32/legacybech32/pk.go | 2 +- types/bech32/legacybech32/pk_bench_test.go | 2 +- x/slashing/client/cli/cli_test.go | 2 +- x/staking/client/cli/cli_test.go | 2 +- x/staking/client/cli/tx_test.go | 1 - 7 files changed, 10 insertions(+), 12 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index f477a1be2041..def02d699d53 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -75,7 +75,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, "terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6", - legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, key1.GetPubKey())) + legacybech32.MustMarshalPubKey(legacybech32.AccPub, key1.GetPubKey())) config.SetCoinType(118) config.SetFullFundraiserPath("44'/118'/0'/0/0") @@ -123,5 +123,5 @@ func Test_runAddCmdLedger(t *testing.T) { require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - legacybech32.MustBech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, key1.GetPubKey())) + legacybech32.MustMarshalPubKey(legacybech32.AccPub, key1.GetPubKey())) } diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index 9241eedb2289..cf761828138f 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/types" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) @@ -29,7 +28,7 @@ func TestInMemoryCreateLedger(t *testing.T) { // The mock is available, check that the address is correct pubKey := ledger.GetPubKey() - pk, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pubKey) + pk, err := legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -40,7 +39,7 @@ func TestInMemoryCreateLedger(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pubKey) + pk, err = legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -107,7 +106,7 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { // The mock is available, check that the address is correct require.Equal(t, "some_account", ledger.GetName()) pubKey := ledger.GetPubKey() - pk, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeAccPub, pubKey) + pk, err := legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -118,7 +117,7 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pubKey) + pk, err = legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index b4fa2d8163a5..98974df48af4 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -28,7 +28,7 @@ func MarshalPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) (string, err return bech32.ConvertAndEncode(bech32Prefix, legacy.Cdc.MustMarshalBinaryBare(pubkey)) } -// Deprecated: MustMarshalPubKey calls Bech32ifyPubKey and panics on error. +// Deprecated: MustMarshalPubKey calls MarshalPubKey and panics on error. func MustMarshalPubKey(pkt Bech32PubKeyType, pubkey cryptotypes.PubKey) string { res, err := MarshalPubKey(pkt, pubkey) if err != nil { diff --git a/types/bech32/legacybech32/pk_bench_test.go b/types/bech32/legacybech32/pk_bench_test.go index 48848394b5ff..31d184fa2c7f 100644 --- a/types/bech32/legacybech32/pk_bench_test.go +++ b/types/bech32/legacybech32/pk_bench_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/require" ) -func BenchmarkBech32ifyPubKey(b *testing.B) { +func BenchmarkMarshalPubKey(b *testing.B) { pkBz := make([]byte, ed25519.PubKeySize) pk := &ed25519.PubKey{Key: pkBz} rng := rand.New(rand.NewSource(time.Now().Unix())) diff --git a/x/slashing/client/cli/cli_test.go b/x/slashing/client/cli/cli_test.go index 6c6a4d2fb3b8..69b52184e31b 100644 --- a/x/slashing/client/cli/cli_test.go +++ b/x/slashing/client/cli/cli_test.go @@ -52,7 +52,7 @@ func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { val := s.network.Validators[0] // TODO: don't use bech32 here - valConsPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, val.PubKey) + valConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, val.PubKey) s.Require().NoError(err) testCases := []struct { diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index f8b94146f514..6acfd473de7a 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -82,7 +82,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { consPrivKey := ed25519.GenPrivKey() // TODO: fix this - //consPubKey, err := legacybech32.Bech32ifyPubKey(legacybech32.Bech32PubKeyTypeConsPub, consPrivKey.PubKey()) + //consPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, consPrivKey.PubKey()) //s.Require().NoError(err) consPubKey, err := protocdc.MarshalJSON(consPrivKey.PubKey(), nil) s.Require().NoError(err) diff --git a/x/staking/client/cli/tx_test.go b/x/staking/client/cli/tx_test.go index cf0e9fd232db..a7eb30fde3c2 100644 --- a/x/staking/client/cli/tx_test.go +++ b/x/staking/client/cli/tx_test.go @@ -16,7 +16,6 @@ func TestPrepareConfigForTxCreateValidator(t *testing.T) { nodeID := "nodeID" privKey := ed25519.GenPrivKey() valPubKey := privKey.PubKey() - // valPubKey:= sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeConsPub, "cosmosvalconspub1zcjduepq7jsrkl9fgqk0wj3ahmfr8pgxj6vakj2wzn656s8pehh0zhv2w5as5gd80a") moniker := "DefaultMoniker" mkTxValCfg := func(amount, commission, commissionMax, commissionMaxChange, minSelfDelegation string) TxCreateValidatorConfig { return TxCreateValidatorConfig{ From 829db7d681553d52985862c9ab6970ae80c84993 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 20:53:36 +0100 Subject: [PATCH 32/91] linting --- codec/any.go | 2 +- types/bech32/legacybech32/pk_bench_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/codec/any.go b/codec/any.go index 314e7937938a..72a36b58a6fa 100644 --- a/codec/any.go +++ b/codec/any.go @@ -14,7 +14,7 @@ import ( // MarshalIfc is a convenience function for proto marshalling interfaces. It // packs the provided value in an Any and then marshals it to bytes. -// NOTE: if you use a concert type, then you should use BinaryMarshaler.MarshalBinaryBare directly +// NOTE: if you use a concret type, then you should use BinaryMarshaler.MarshalBinaryBare directly func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { diff --git a/types/bech32/legacybech32/pk_bench_test.go b/types/bech32/legacybech32/pk_bench_test.go index 31d184fa2c7f..0d48c28245ef 100644 --- a/types/bech32/legacybech32/pk_bench_test.go +++ b/types/bech32/legacybech32/pk_bench_test.go @@ -5,8 +5,9 @@ import ( "testing" "time" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" ) func BenchmarkMarshalPubKey(b *testing.B) { From 452e406b6e804ee24556dbb687ac88fdb4d3d50f Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 21:02:48 +0100 Subject: [PATCH 33/91] legacybech32 pubkey type rename --- client/keys/add_ledger_test.go | 4 ++-- codec/any.go | 2 +- crypto/keyring/keyring_ledger_test.go | 8 ++++---- types/address_test.go | 4 ++-- types/bech32/legacybech32/pk.go | 10 +++++----- types/bech32/legacybech32/pk_bench_test.go | 6 +++--- x/slashing/client/cli/cli_test.go | 2 +- x/slashing/client/rest/query.go | 2 +- x/staking/client/cli/cli_test.go | 2 +- x/staking/legacy/v034/types.go | 4 ++-- x/staking/legacy/v036/types.go | 4 ++-- x/staking/legacy/v038/types.go | 4 ++-- 12 files changed, 26 insertions(+), 26 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index def02d699d53..65d26c648620 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -75,7 +75,7 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, "terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6", - legacybech32.MustMarshalPubKey(legacybech32.AccPub, key1.GetPubKey())) + legacybech32.MustMarshalPubKey(legacybech32.AccPK, key1.GetPubKey())) config.SetCoinType(118) config.SetFullFundraiserPath("44'/118'/0'/0/0") @@ -123,5 +123,5 @@ func Test_runAddCmdLedger(t *testing.T) { require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - legacybech32.MustMarshalPubKey(legacybech32.AccPub, key1.GetPubKey())) + legacybech32.MustMarshalPubKey(legacybech32.AccPK, key1.GetPubKey())) } diff --git a/codec/any.go b/codec/any.go index 72a36b58a6fa..314e7937938a 100644 --- a/codec/any.go +++ b/codec/any.go @@ -14,7 +14,7 @@ import ( // MarshalIfc is a convenience function for proto marshalling interfaces. It // packs the provided value in an Any and then marshals it to bytes. -// NOTE: if you use a concret type, then you should use BinaryMarshaler.MarshalBinaryBare directly +// NOTE: if you use a concert type, then you should use BinaryMarshaler.MarshalBinaryBare directly func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) if !ok { diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index cf761828138f..1b220c295178 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -28,7 +28,7 @@ func TestInMemoryCreateLedger(t *testing.T) { // The mock is available, check that the address is correct pubKey := ledger.GetPubKey() - pk, err := legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) + pk, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -39,7 +39,7 @@ func TestInMemoryCreateLedger(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) + pk, err = legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -106,7 +106,7 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { // The mock is available, check that the address is correct require.Equal(t, "some_account", ledger.GetName()) pubKey := ledger.GetPubKey() - pk, err := legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) + pk, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) @@ -117,7 +117,7 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = legacybech32.MarshalPubKey(legacybech32.AccPub, pubKey) + pk, err = legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) require.NoError(t, err) require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) diff --git a/types/address_test.go b/types/address_test.go index 44fa5987f9b7..2d4c22ecd34e 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -250,7 +250,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { val.String(), prefix+types.PrefixValidator+types.PrefixAddress)) - bech32ValPub := legacybech32.MustMarshalPubKey(legacybech32.ValPub, pub) + bech32ValPub := legacybech32.MustMarshalPubKey(legacybech32.ValPK, pub) s.Require().True(strings.HasPrefix( bech32ValPub, prefix+types.PrefixValidator+types.PrefixPublic)) @@ -264,7 +264,7 @@ func (s *addressTestSuite) TestConfiguredPrefix() { cons.String(), prefix+types.PrefixConsensus+types.PrefixAddress)) - bech32ConsPub := legacybech32.MustMarshalPubKey(legacybech32.ConsPub, pub) + bech32ConsPub := legacybech32.MustMarshalPubKey(legacybech32.ConsPK, pub) s.Require().True(strings.HasPrefix( bech32ConsPub, prefix+types.PrefixConsensus+types.PrefixPublic)) diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index 98974df48af4..209871f66077 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -16,9 +16,9 @@ type Bech32PubKeyType string // Bech32 conversion constants // TODO: check where we can remove this const ( - AccPK Bech32PubKeyType = "accpub" - ValPub Bech32PubKeyType = "valpub" - ConsPub Bech32PubKeyType = "conspub" + AccPK Bech32PubKeyType = "accpub" + ValPK Bech32PubKeyType = "valpub" + ConsPK Bech32PubKeyType = "conspub" ) // Deprecated: MarshalPubKey returns a Bech32 encoded string containing the appropriate @@ -44,9 +44,9 @@ func getPrefix(pkt Bech32PubKeyType) string { case AccPK: return cfg.GetBech32AccountPubPrefix() - case ValPub: + case ValPK: return cfg.GetBech32ValidatorPubPrefix() - case ConsPub: + case ConsPK: return cfg.GetBech32ConsensusPubPrefix() } diff --git a/types/bech32/legacybech32/pk_bench_test.go b/types/bech32/legacybech32/pk_bench_test.go index 0d48c28245ef..ea44c063b3dc 100644 --- a/types/bech32/legacybech32/pk_bench_test.go +++ b/types/bech32/legacybech32/pk_bench_test.go @@ -22,7 +22,7 @@ func BenchmarkMarshalPubKey(b *testing.B) { rng.Read(pk.Key) b.StartTimer() - _, err := MarshalPubKey(ConsPub, pk) + _, err := MarshalPubKey(ConsPK, pk) require.NoError(b, err) } } @@ -38,11 +38,11 @@ func BenchmarkGetPubKeyFromBech32(b *testing.B) { b.StopTimer() rng.Read(pk.Key) - pkStr, err := MarshalPubKey(ConsPub, pk) + pkStr, err := MarshalPubKey(ConsPK, pk) require.NoError(b, err) b.StartTimer() - pk2, err := UnmarshalPubKey(ConsPub, pkStr) + pk2, err := UnmarshalPubKey(ConsPK, pkStr) require.NoError(b, err) require.Equal(b, pk, pk2) } diff --git a/x/slashing/client/cli/cli_test.go b/x/slashing/client/cli/cli_test.go index 69b52184e31b..3f7f60a8f627 100644 --- a/x/slashing/client/cli/cli_test.go +++ b/x/slashing/client/cli/cli_test.go @@ -52,7 +52,7 @@ func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { val := s.network.Validators[0] // TODO: don't use bech32 here - valConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, val.PubKey) + valConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, val.PubKey) s.Require().NoError(err) testCases := []struct { diff --git a/x/slashing/client/rest/query.go b/x/slashing/client/rest/query.go index c97c7124bfdb..d5d4a3a187f1 100644 --- a/x/slashing/client/rest/query.go +++ b/x/slashing/client/rest/query.go @@ -33,7 +33,7 @@ func registerQueryRoutes(clientCtx client.Context, r *mux.Router) { func signingInfoHandlerFn(clientCtx client.Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - pk, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, vars["validatorPubKey"]) + pk, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPK, vars["validatorPubKey"]) if rest.CheckBadRequestError(w, err) { return } diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 6acfd473de7a..1b27e9f793b9 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -82,7 +82,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { consPrivKey := ed25519.GenPrivKey() // TODO: fix this - //consPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, consPrivKey.PubKey()) + //consPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, consPrivKey.PubKey()) //s.Require().NoError(err) consPubKey, err := protocdc.MarshalJSON(consPrivKey.PubKey(), nil) s.Require().NoError(err) diff --git a/x/staking/legacy/v034/types.go b/x/staking/legacy/v034/types.go index 7817388e862e..ef3b34cd495d 100644 --- a/x/staking/legacy/v034/types.go +++ b/x/staking/legacy/v034/types.go @@ -140,7 +140,7 @@ type ( ) func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, v.ConsPubKey) if err != nil { return nil, err } @@ -166,7 +166,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPK, bv.ConsPubKey) if err != nil { return err } diff --git a/x/staking/legacy/v036/types.go b/x/staking/legacy/v036/types.go index 32c83f2c990a..01e88e588900 100644 --- a/x/staking/legacy/v036/types.go +++ b/x/staking/legacy/v036/types.go @@ -89,7 +89,7 @@ func NewGenesisState( } func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, v.ConsPubKey) if err != nil { return nil, err } @@ -114,7 +114,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPK, bv.ConsPubKey) if err != nil { return err } diff --git a/x/staking/legacy/v038/types.go b/x/staking/legacy/v038/types.go index fd92a83622e9..18d0ef903cda 100644 --- a/x/staking/legacy/v038/types.go +++ b/x/staking/legacy/v038/types.go @@ -114,7 +114,7 @@ func NewGenesisState( // MarshalJSON marshals the validator to JSON using Bech32 func (v Validator) MarshalJSON() ([]byte, error) { - bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPub, v.ConsPubKey) + bechConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, v.ConsPubKey) if err != nil { return nil, err } @@ -140,7 +140,7 @@ func (v *Validator) UnmarshalJSON(data []byte) error { if err := legacy.Cdc.UnmarshalJSON(data, bv); err != nil { return err } - consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPub, bv.ConsPubKey) + consPubKey, err := legacybech32.UnmarshalPubKey(legacybech32.ConsPK, bv.ConsPubKey) if err != nil { return err } From 81aab57cc293d58a7ff1d0ede59a9d2a8ecd574d Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 22:30:55 +0100 Subject: [PATCH 34/91] fix staking client --- x/staking/client/cli/cli_test.go | 38 +++++++++++++++----------------- x/staking/client/cli/tx.go | 7 ++++-- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 1b27e9f793b9..4c18e9b1535f 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -1,5 +1,4 @@ -// TODO: change this flag -// xbuild norace +// build norace package cli_test @@ -16,10 +15,10 @@ import ( "github.com/tendermint/tendermint/rpc/client/http" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - "github.com/cosmos/cosmos-sdk/internal/protocdc" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" @@ -44,10 +43,8 @@ func (s *IntegrationTestSuite) SetupSuite() { s.T().Skip("skipping test in unit-tests mode.") } - cfg := network.DefaultConfig() - cfg.NumValidators = 2 - - s.cfg = cfg + s.cfg = network.DefaultConfig() + s.cfg.NumValidators = 2 s.network = network.New(s.T(), cfg) _, err := s.network.WaitForHeight(1) @@ -78,17 +75,16 @@ func (s *IntegrationTestSuite) TearDownSuite() { } func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { + require := s.Require() val := s.network.Validators[0] consPrivKey := ed25519.GenPrivKey() - // TODO: fix this - //consPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, consPrivKey.PubKey()) - //s.Require().NoError(err) - consPubKey, err := protocdc.MarshalJSON(consPrivKey.PubKey(), nil) - s.Require().NoError(err) + consPubKeyBz, err := codec.MarshalIfcJSON(s.cfg.Codec, consPrivKey.PubKey()) + require.NoError(err) + require.NotNil(consPubKeyBz) info, _, err := val.ClientCtx.Keyring.NewMnemonic("NewValidator", keyring.English, sdk.FullFundraiserPath, hd.Secp256k1) - s.Require().NoError(err) + require.NoError(err) newAddr := sdk.AccAddress(info.GetPubKey().Address()) @@ -100,7 +96,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), ) - s.Require().NoError(err) + require.NoError(err) testCases := []struct { name string @@ -149,7 +145,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { { "invalid transaction (missing moniker)", []string{ - fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKey), + fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKeyBz), fmt.Sprintf("--%s=100stake", cli.FlagAmount), fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity), fmt.Sprintf("--%s=https://newvalidator.io", cli.FlagWebsite), @@ -169,7 +165,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { { "valid transaction", []string{ - fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKey), + fmt.Sprintf("--%s=%s", cli.FlagPubKey, consPubKeyBz), fmt.Sprintf("--%s=100stake", cli.FlagAmount), fmt.Sprintf("--%s=NewValidator", cli.FlagMoniker), fmt.Sprintf("--%s=AFAF00C4", cli.FlagIdentity), @@ -198,13 +194,15 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) if tc.expectErr { - s.Require().Error(err) + require.Error(err) } else { - s.Require().NoError(err, out.String()) - s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String()) + require.NoError(err, "test: %s\noutput: %s", tc.name, out.String()) + err = clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType) + require.NoError(err, out.String(), "test: %s, output\n:", tc.name, out.String()) txResp := tc.respType.(*sdk.TxResponse) - s.Require().Equal(tc.expectedCode, txResp.Code, out.String()) + require.Equal(tc.expectedCode, txResp.Code, + "test: %s, output\n:", tc.name, out.String()) } }) } diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 489ad546855f..71cf0908acc3 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -11,6 +11,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" @@ -59,7 +60,8 @@ func NewCreateValidatorCmd() *cobra.Command { return err } - txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()). + WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) txf, msg, err := NewBuildCreateValidatorMsg(clientCtx, txf, cmd.Flags()) if err != nil { @@ -313,7 +315,8 @@ func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl } var pk cryptotypes.PubKey - if err := clientCtx.JSONMarshaler.UnmarshalJSON([]byte(pkStr), pk); err != nil { + am := codec.NewIfcJSONAnyMarshaler(clientCtx.JSONMarshaler, clientCtx.InterfaceRegistry) + if err := codec.UnmarshalIfcJSON(am, &pk, []byte(pkStr)); err != nil { return txf, nil, err } From ae91f0f94dcea70e862c26d48debc67a8e83b116 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 27 Nov 2020 22:32:46 +0100 Subject: [PATCH 35/91] fix test compilation --- x/staking/client/cli/cli_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 4c18e9b1535f..33e1285e1e29 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -45,7 +45,7 @@ func (s *IntegrationTestSuite) SetupSuite() { s.cfg = network.DefaultConfig() s.cfg.NumValidators = 2 - s.network = network.New(s.T(), cfg) + s.network = network.New(s.T(), s.cfg) _, err := s.network.WaitForHeight(1) s.Require().NoError(err) From 69572c67481306f3be9d7c594c85b079b64ab7a4 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Sat, 28 Nov 2020 10:49:31 +0100 Subject: [PATCH 36/91] fix TestGetCmdQuerySigningInfo --- x/slashing/client/cli/cli_test.go | 11 +++++------ x/slashing/client/cli/query.go | 5 +++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/x/slashing/client/cli/cli_test.go b/x/slashing/client/cli/cli_test.go index 3f7f60a8f627..08198778473c 100644 --- a/x/slashing/client/cli/cli_test.go +++ b/x/slashing/client/cli/cli_test.go @@ -12,10 +12,10 @@ import ( tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/cosmos/cosmos-sdk/x/slashing/client/cli" ) @@ -50,10 +50,9 @@ func (s *IntegrationTestSuite) TearDownSuite() { func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { val := s.network.Validators[0] - - // TODO: don't use bech32 here - valConsPubKey, err := legacybech32.MarshalPubKey(legacybech32.ConsPK, val.PubKey) + pubKeyBz, err := codec.MarshalIfcJSON(s.cfg.Codec, val.PubKey) s.Require().NoError(err) + pubKeyStr := string(pubKeyBz) testCases := []struct { name string @@ -65,7 +64,7 @@ func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { { "valid address (json output)", []string{ - valConsPubKey, + pubKeyStr, fmt.Sprintf("--%s=json", tmcli.OutputFlag), fmt.Sprintf("--%s=1", flags.FlagHeight), }, @@ -75,7 +74,7 @@ func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { { "valid address (text output)", []string{ - valConsPubKey, + pubKeyStr, fmt.Sprintf("--%s=text", tmcli.OutputFlag), fmt.Sprintf("--%s=1", flags.FlagHeight), }, diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index f719179acc5d..6ce5992ccbcc 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" @@ -52,8 +53,8 @@ $ query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdgexx } var pk cryptotypes.PubKey - err = clientCtx.JSONMarshaler.UnmarshalJSON([]byte(args[0]), pk) - if err != nil { + am := codec.NewIfcJSONAnyMarshaler(clientCtx.JSONMarshaler, clientCtx.InterfaceRegistry) + if err := codec.UnmarshalIfcJSON(am, &pk, []byte(args[0])); err != nil { return err } From 288a080b8e267e7d3a94098c1244ddf6810abddb Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Sat, 28 Nov 2020 10:50:08 +0100 Subject: [PATCH 37/91] rename NewIfcJSONAnyMarshaler --- client/debug/main.go | 2 +- codec/codec.go | 4 ++-- crypto/keys/ed25519/ed25519_test.go | 2 +- x/slashing/client/cli/query.go | 2 +- x/staking/client/cli/tx.go | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index e3f86ee80076..85ca58e847b9 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -37,7 +37,7 @@ func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, var pk cryptotypes.PubKey // TODO: this won't work, where should we get an Any unpacker? // err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) - am := codec.NewIfcJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) + am := codec.NewJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) err := codec.UnmarshalIfcJSON(am, &pk, []byte(pkstr)) return pk, err diff --git a/codec/codec.go b/codec/codec.go index 756fbc509b94..3393f3c4e7a7 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -75,8 +75,8 @@ type jsonAny struct { types.InterfaceRegistry } -// NewIfcJSONAnyMarshaler creates a JSONAnyMarshaler using JSONMarshaler +// NewJSONAnyMarshaler creates a JSONAnyMarshaler using JSONMarshaler // and InterfaceRegistry -func NewIfcJSONAnyMarshaler(jm JSONMarshaler, ir types.InterfaceRegistry) IfcJSONMarshaler { +func NewJSONAnyMarshaler(jm JSONMarshaler, ir types.InterfaceRegistry) IfcJSONMarshaler { return jsonAny{jm, ir} } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 4531c7fa96ba..78f76541b027 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -284,7 +284,7 @@ func TestMarshalProto2(t *testing.T) { require.NoError(err) var pk3 cryptotypes.PubKey - am := codec.NewIfcJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) + am := codec.NewJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) err = codec.UnmarshalIfcJSON(am, &pk3, bz) require.NoError(err) require.True(pk3.Equals(pk)) diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index 6ce5992ccbcc..2e4729e5d715 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -53,7 +53,7 @@ $ query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdgexx } var pk cryptotypes.PubKey - am := codec.NewIfcJSONAnyMarshaler(clientCtx.JSONMarshaler, clientCtx.InterfaceRegistry) + am := codec.NewJSONAnyMarshaler(clientCtx.JSONMarshaler, clientCtx.InterfaceRegistry) if err := codec.UnmarshalIfcJSON(am, &pk, []byte(args[0])); err != nil { return err } diff --git a/x/staking/client/cli/tx.go b/x/staking/client/cli/tx.go index 71cf0908acc3..2ab16421b6aa 100644 --- a/x/staking/client/cli/tx.go +++ b/x/staking/client/cli/tx.go @@ -315,7 +315,7 @@ func NewBuildCreateValidatorMsg(clientCtx client.Context, txf tx.Factory, fs *fl } var pk cryptotypes.PubKey - am := codec.NewIfcJSONAnyMarshaler(clientCtx.JSONMarshaler, clientCtx.InterfaceRegistry) + am := codec.NewJSONAnyMarshaler(clientCtx.JSONMarshaler, clientCtx.InterfaceRegistry) if err := codec.UnmarshalIfcJSON(am, &pk, []byte(pkStr)); err != nil { return txf, nil, err } From 773d0c62b4f4fe03aaec727ea971c1b816585aa5 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 15:44:37 +0100 Subject: [PATCH 38/91] keyring: remove duplicated information from multinfo structure --- client/keys/codec_test.go | 8 ++++---- client/keys/show.go | 5 ++++- client/keys/show_test.go | 20 ++++++------------ crypto/keyring/info.go | 31 +++++++++------------------- crypto/keyring/keyring.go | 6 ++++-- crypto/keyring/output.go | 38 +++++------------------------------ crypto/keyring/output_test.go | 19 ++++++++---------- 7 files changed, 40 insertions(+), 87 deletions(-) diff --git a/client/keys/codec_test.go b/client/keys/codec_test.go index 33f6103d1888..f61792e600df 100644 --- a/client/keys/codec_test.go +++ b/client/keys/codec_test.go @@ -20,10 +20,10 @@ func getTestCases() testCases { return testCases{ // nolint:govet []keyring.KeyOutput{ - {"A", "B", "C", "D", "E", 0, nil}, - {"A", "B", "C", "D", "", 0, nil}, - {"", "B", "C", "D", "", 0, nil}, - {"", "", "", "", "", 0, nil}, + {"A", "B", "C", "D", "E"}, + {"A", "B", "C", "D", ""}, + {"", "B", "C", "D", ""}, + {"", "", "", "", ""}, }, make([]keyring.KeyOutput, 4), [][]byte{ diff --git a/client/keys/show.go b/client/keys/show.go index 5b2ed09ded0e..307db6897779 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -78,7 +78,10 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { } multikey := multisig.NewLegacyAminoPubKey(multisigThreshold, pks) - info = keyring.NewMultiInfo(defaultMultiSigKeyName, multikey) + info, err = keyring.NewMultiInfo(defaultMultiSigKeyName, multikey) + if err != nil { + return err + } } isShowAddr, _ := cmd.Flags().GetBool(FlagAddress) diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 882a80683a18..2c8cfd2f170c 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -24,8 +24,8 @@ func Test_multiSigKey_Properties(t *testing.T) { 1, []cryptotypes.PubKey{tmpKey1.PubKey()}, ) - tmp := keyring.NewMultiInfo("myMultisig", pk) - + tmp, err := keyring.NewMultiInfo("myMultisig", pk) + require.NoError(t, err) require.Equal(t, "myMultisig", tmp.GetName()) require.Equal(t, keyring.TypeMulti, tmp.GetType()) require.Equal(t, "D3923267FA8A3DD367BB768FA8BDC8FF7F89DA3F", tmp.GetPubKey().Address().String()) @@ -202,20 +202,12 @@ func Test_getBechKeyOut(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { got, err := getBechKeyOut(tt.args.bechPrefix) - if (err != nil) != tt.wantErr { - t.Errorf("getBechKeyOut() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if !tt.wantErr { + if tt.wantErr { + require.Error(t, err) + } else { + require.NoError(t, err) require.NotNil(t, got) } - - // TODO: Still not possible to compare functions - // Maybe in next release: https://github.com/stretchr/testify/issues/182 - //if &got != &tt.want { - // t.Errorf("getBechKeyOut() = %v, want %v", got, tt.want) - //} }) } } diff --git a/crypto/keyring/info.go b/crypto/keyring/info.go index f05e2f0ddef6..17708efefdc6 100644 --- a/crypto/keyring/info.go +++ b/crypto/keyring/info.go @@ -176,35 +176,21 @@ func (i offlineInfo) GetPath() (*hd.BIP44Params, error) { return nil, fmt.Errorf("BIP44 Paths are not available for this type") } -type multisigPubKeyInfo struct { - PubKey cryptotypes.PubKey `json:"pubkey"` - Weight uint `json:"weight"` -} - // multiInfo is the public information about a multisig key type multiInfo struct { - Name string `json:"name"` - PubKey cryptotypes.PubKey `json:"pubkey"` - Threshold uint `json:"threshold"` - PubKeys []multisigPubKeyInfo `json:"pubkeys"` + Name string `json:"name"` + PubKey cryptotypes.PubKey `json:"pubkey"` } // NewMultiInfo creates a new multiInfo instance -func NewMultiInfo(name string, pub cryptotypes.PubKey) Info { - multiPK := pub.(*multisig.LegacyAminoPubKey) - - pubKeys := make([]multisigPubKeyInfo, len(multiPK.PubKeys)) - for i, pk := range multiPK.GetPubKeys() { - // TODO: Recursively check pk for total weight? - pubKeys[i] = multisigPubKeyInfo{pk, 1} +func NewMultiInfo(name string, pub cryptotypes.PubKey) (Info, error) { + if _, ok := pub.(*multisig.LegacyAminoPubKey); !ok { + return nil, fmt.Errorf("MultiInfo supports only LegacyAminoPubKey, got %T", pub) } - return &multiInfo{ - Name: name, - PubKey: pub, - Threshold: uint(multiPK.Threshold), - PubKeys: pubKeys, - } + Name: name, + PubKey: pub, + }, nil } // GetType implements Info interface @@ -246,6 +232,7 @@ func (i multiInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { // encoding info func marshalInfo(i Info) []byte { + // TODO: Why do we use Legacy Amino marshaling here? return CryptoCdc.MustMarshalBinaryLengthPrefixed(i) } diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index d3220b5b43ef..049929c8f23d 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -765,11 +765,13 @@ func (ks keystore) writeOfflineKey(name string, pub types.PubKey, algo hd.PubKey } func (ks keystore) writeMultisigKey(name string, pub types.PubKey) (Info, error) { - info := NewMultiInfo(name, pub) - err := ks.writeInfo(info) + info, err := NewMultiInfo(name, pub) if err != nil { return nil, err } + if err = ks.writeInfo(info); err != nil { + return nil, err + } return info, nil } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index e9d2e257f2b2..51470d4492c6 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -11,17 +11,14 @@ import ( // KeyOutput defines a structure wrapping around an Info object used for output // functionality. type KeyOutput struct { - Name string `json:"name" yaml:"name"` - Type string `json:"type" yaml:"type"` - Address string `json:"address" yaml:"address"` - PubKey string `json:"pubkey" yaml:"pubkey"` - Mnemonic string `json:"mnemonic,omitempty" yaml:"mnemonic"` - Threshold uint `json:"threshold,omitempty" yaml:"threshold"` - PubKeys []multisigPubKeyOutput `json:"pubkeys,omitempty" yaml:"pubkeys"` + Name string `json:"name" yaml:"name"` + Type string `json:"type" yaml:"type"` + Address string `json:"address" yaml:"address"` + PubKey string `json:"pubkey" yaml:"pubkey"` + Mnemonic string `json:"mnemonic,omitempty" yaml:"mnemonic"` } // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys -// TODO remove error func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { // nolint:interfacer bz, err := protocdc.MarshalJSON(pk, nil) if err != nil { @@ -35,12 +32,6 @@ func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.Pu }, nil } -type multisigPubKeyOutput struct { - Address string `json:"address" yaml:"address"` - PubKey string `json:"pubkey" yaml:"pubkey"` - Weight uint `json:"weight" yaml:"weight"` -} - // Bech32KeysOutput returns a slice of KeyOutput objects, each with the "acc" // Bech32 prefixes, given a slice of Info objects. It returns an error if any // call to Bech32KeyOutput fails. @@ -78,24 +69,5 @@ func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.AccAddress(pk.Address().Bytes()) ko, _ := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) - - if mInfo, ok := keyInfo.(*multiInfo); ok { - pubKeys := make([]multisigPubKeyOutput, len(mInfo.PubKeys)) - - for i, pkInfo := range mInfo.PubKeys { - pk = pkInfo.PubKey - addr = sdk.AccAddress(pk.Address()) - bz, err := protocdc.MarshalJSON(pk, nil) - if err != nil { - return ko, err - } - pubKeys[i] = multisigPubKeyOutput{ - addr.String(), string(bz), pkInfo.Weight} - } - - ko.Threshold = mInfo.Threshold - ko.PubKeys = pubKeys - } - return ko, nil } diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index d2cc24518322..c6eda5a97cfc 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -1,6 +1,7 @@ package keyring import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -8,28 +9,24 @@ import ( kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" ) func TestBech32KeysOutput(t *testing.T) { - tmpKey := secp256k1.GenPrivKey().PubKey() - tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes()) + sk := secp256k1.PrivKey{Key: []byte{154, 49, 3, 117, 55, 232, 249, 20, 205, 216, 102, 7, 136, 72, 177, 2, 131, 202, 234, 81, 31, 208, 46, 244, 179, 192, 167, 163, 142, 117, 246, 13}} + tmpKey := sk.PubKey() multisigPk := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey}) - multiInfo := NewMultiInfo("multisig", multisigPk) + multiInfo, err := NewMultiInfo("multisig", multisigPk) + require.NoError(t, err) accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, multisigPk) require.NoError(t, err) - expectedOutput.Threshold = 1 - tmpKeyBz, err := protocdc.MarshalJSON(tmpKey, nil) - expectedOutput.PubKeys = []multisigPubKeyOutput{{tmpAddr.String(), string(tmpKeyBz), 1}} outputs, err := Bech32KeysOutput([]Info{multiInfo}) require.NoError(t, err) - require.Equal(t, expectedOutput.PubKeys, outputs[0].PubKeys) - - // expectedOutput is: - // {"name":"multisig","type":"multi","address":"cosmos1dutd6e06gv4tfdypzyvdfuwct8cdtctsjr0nl3","pubkey":"{\"threshold\":1,\"public_keys\":[{\"@type\":\"/cosmos.crypto.secp256k1.PubKey\",\"key\":\"A5edrlIT2MWsp4SYh+KvGfjxmKw7NNxga7K23faEEmL/\"}]}","threshold":1,"pubkeys":[{"address":"cosmos13533lkpyxrgttefm34f35c89v2yrv3ugkns5p6","pubkey":"{\"key\":\"A5edrlIT2MWsp4SYh+KvGfjxmKw7NNxga7K23faEEmL/\"}","weight":1}]} + require.Equal(t, expectedOutput, outputs[0]) + require.Len(t, outputs, 1) + require.Equal(t, `{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{"threshold":1,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}]} Mnemonic:}`, fmt.Sprintf("%+v", outputs[0])) } From 8f86bf43bec9ba34e0d799a4676468e23595ab80 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 17:01:29 +0100 Subject: [PATCH 39/91] todo cleanups --- client/debug/main.go | 11 +++-------- client/keys/add.go | 7 ++++--- client/keys/codec.go | 2 ++ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 85ca58e847b9..fe149fc145e8 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -35,8 +35,6 @@ func Cmd() *cobra.Command { // encodings fail, an error is returned. func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) { var pk cryptotypes.PubKey - // TODO: this won't work, where should we get an Any unpacker? - // err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pkstr), pk) am := codec.NewJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) err := codec.UnmarshalIfcJSON(am, &pk, []byte(pkstr)) @@ -47,13 +45,11 @@ func PubkeyCmd() *cobra.Command { return &cobra.Command{ Use: "pubkey [pubkey]", Short: "Decode a pubkey from proto JSON", - // TODO: update example Long: fmt.Sprintf(`Decode a pubkey from proto JSON and display it's address. Example: -$ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz -$ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg - `, version.AppName, version.AppName), +$ %s debug pubkey '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}' + `, version.AppName), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { clientCtx := client.GetClientContextFromCmd(cmd) @@ -61,9 +57,8 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg if err != nil { return err } - cmd.Println("Address:", pk.Address()) - cmd.Println("Hex:", pk.String()) + cmd.Println("PubKey Hex:", hex.EncodeToString(pk.Bytes())) return nil }, } diff --git a/client/keys/add.go b/client/keys/add.go index a24ece256d6e..0370b6415afd 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -14,6 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/input" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -186,11 +187,11 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring pubKey, _ := cmd.Flags().GetString(FlagPublicKey) if pubKey != "" { var pk cryptotypes.PubKey - // TODO: shall we use KeysCdc here (global from this module, = codec.NewLegacyAmino)? - if err := ctx.JSONMarshaler.UnmarshalJSON([]byte(pubKey), pk); err != nil { + am := codec.NewJSONAnyMarshaler(ctx.JSONMarshaler, ctx.InterfaceRegistry) + err = codec.UnmarshalIfcJSON(am, &pk, []byte(pubKey)) + if err != nil { return err } - fmt.Println("TODO: Check", pk) _, err := kb.SavePubKey(name, pk, algo.Name()) return err } diff --git a/client/keys/codec.go b/client/keys/codec.go index 2fb7bd94397e..ae8f641b81de 100644 --- a/client/keys/codec.go +++ b/client/keys/codec.go @@ -5,6 +5,8 @@ import ( cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" ) +// TOOD: remove this file #8047 + // KeysCdc defines codec to be used with key operations var KeysCdc *codec.LegacyAmino From 904129e197b5a207af93d135cf433a85b9041e45 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 18:40:09 +0100 Subject: [PATCH 40/91] Update Changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b2c97295b0f..e3f19b5503d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,9 +54,17 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Client Breaking * (x/staking) [\#7419](https://github.com/cosmos/cosmos-sdk/pull/7419) The `TmConsPubKey` method on ValidatorI has been removed and replaced instead by `ConsPubKey` (which returns a SDK `cryptotypes.PubKey`) and `TmConsPublicKey` (which returns a Tendermint proto PublicKey). +* [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) Changed Bech32 Public Key serialization in the client facing functionality (CLI, MsgServer, QueryServer): + * updated the keyring display structure (it uses protobuf JSON serialization) - the output is more verbose. + * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalIfc` and `UnmarshalIfc` respectively. These functions must take interface as a parameter (not a concrete type nor `Any` object). Underneeth they use `Any` wrapping for correct protobuf serialization. + ### Improvements * (tendermint) [\#7828](https://github.com/cosmos/cosmos-sdk/pull/7828) Update tendermint dependency to v0.34.0-rc6 +* [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) + * Created `IfcJSONMarshaler` interface and `NewJSONAnyMarshaler` - a helper method to use codec.(Un)marsjalJSON withing a client context. + * Added `codec.(Un)marsjalJSON` helper functions (we are planning to move them into `codec.Marshaler` interface). + ## [v0.40.0-rc2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.40.0-rc2) - 2020-11-02 From c6f003c99b6e6ba39a9740a6ac953da3e716b35a Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 22:19:09 +0100 Subject: [PATCH 41/91] remove some legacybech32 from tests --- client/keys/add.go | 2 -- crypto/keyring/keyring_ledger_test.go | 19 ++++++------------- crypto/keyring/types_test.go | 11 ++++++----- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 0370b6415afd..0c6dae66e620 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -296,8 +296,6 @@ func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemonic string) error { output, _ := cmd.Flags().GetString(cli.OutputFlag) - // TODO: remove Bech32 from here - switch output { case OutputFormatText: cmd.PrintErrln() diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index 1b220c295178..f57ddc83a5e1 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -10,7 +10,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func TestInMemoryCreateLedger(t *testing.T) { @@ -28,9 +27,8 @@ func TestInMemoryCreateLedger(t *testing.T) { // The mock is available, check that the address is correct pubKey := ledger.GetPubKey() - pk, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) - require.NoError(t, err) - require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) + expectedPkStr := "PubKeySecp256k1{03602C0CB4D8C0081FEE794BDE96E7B95FA16F2B5283B764AC070584327B2C7202}" + require.Equal(t, expectedPkStr, pubKey.String()) // Check that restoring the key gets the same results restoredKey, err := kb.Key("some_account") @@ -39,9 +37,7 @@ func TestInMemoryCreateLedger(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) - require.NoError(t, err) - require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) + require.Equal(t, expectedPkStr, pubKey.String()) path, err := restoredKey.GetPath() require.NoError(t, err) @@ -106,9 +102,8 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { // The mock is available, check that the address is correct require.Equal(t, "some_account", ledger.GetName()) pubKey := ledger.GetPubKey() - pk, err := legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) - require.NoError(t, err) - require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) + expectedPkStr := "PubKeySecp256k1{03602C0CB4D8C0081FEE794BDE96E7B95FA16F2B5283B764AC070584327B2C7202}" + require.Equal(t, expectedPkStr, pubKey.String()) // Check that restoring the key gets the same results restoredKey, err := keyring.Key("some_account") @@ -117,9 +112,7 @@ func TestAltKeyring_SaveLedgerKey(t *testing.T) { require.Equal(t, "some_account", restoredKey.GetName()) require.Equal(t, TypeLedger, restoredKey.GetType()) pubKey = restoredKey.GetPubKey() - pk, err = legacybech32.MarshalPubKey(legacybech32.AccPK, pubKey) - require.NoError(t, err) - require.Equal(t, "cosmospub1addwnpepqdszcr95mrqqs8lw099aa9h8h906zmet22pmwe9vquzcgvnm93eqygufdlv", pk) + require.Equal(t, expectedPkStr, pubKey.String()) path, err := restoredKey.GetPath() require.NoError(t, err) diff --git a/crypto/keyring/types_test.go b/crypto/keyring/types_test.go index 880a3901cb3f..b5b19ae882cc 100644 --- a/crypto/keyring/types_test.go +++ b/crypto/keyring/types_test.go @@ -2,30 +2,31 @@ package keyring import ( "encoding/hex" + "fmt" "testing" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" "github.com/stretchr/testify/require" ) func Test_writeReadLedgerInfo(t *testing.T) { tmpKey := make([]byte, secp256k1.PubKeySize) - bz, _ := hex.DecodeString("035AD6810A47F073553FF30D2FCC7E0D3B1C0B74B61A1AAA2582344037151E143A") + hexPK := "035AD6810A47F073553FF30D2FCC7E0D3B1C0B74B61A1AAA2582344037151E143A" + bz, err := hex.DecodeString(hexPK) + require.NoError(t, err) copy(tmpKey[:], bz) lInfo := newLedgerInfo("some_name", &secp256k1.PubKey{Key: tmpKey}, *hd.NewFundraiserParams(5, sdk.CoinType, 1), hd.Secp256k1Type) require.Equal(t, TypeLedger, lInfo.GetType()) - // TODO: update the test - shouldn't use bech32 path, err := lInfo.GetPath() require.NoError(t, err) require.Equal(t, "m/44'/118'/5'/0/1", path.String()) require.Equal(t, - "cosmospub1addwnpepqddddqg2glc8x4fl7vxjlnr7p5a3czm5kcdp4239sg6yqdc4rc2r5wmxv8p", - legacybech32.MustMarshalPubKey(legacybech32.AccPK, lInfo.GetPubKey())) + fmt.Sprintf("PubKeySecp256k1{%s}", hexPK), + lInfo.GetPubKey().String()) // Serialize and restore serialized := marshalInfo(lInfo) From 47eceb6dc865b38589805c30078938d7dcac8156 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 22:54:14 +0100 Subject: [PATCH 42/91] remove todos --- crypto/ledger/ledger_test.go | 29 ++++++++------------------- server/tm_cmds.go | 1 - types/bech32/legacybech32/pk.go | 1 - x/auth/legacy/legacytx/stdsig_test.go | 1 - 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index c691974004a4..e8f55b4a66db 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -9,7 +9,6 @@ import ( cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" @@ -27,17 +26,16 @@ func TestPublicKeyUnsafe(t *testing.T) { path := *hd.NewFundraiserParams(0, sdk.CoinType, 0) priv, err := NewPrivKeySecp256k1Unsafe(path) require.NoError(t, err) - require.NotNil(t, priv) + checkDefaultPubKey(t, priv) +} +func checkDefaultPubKey(t *testing.T, priv types.LedgerPrivKey) { + require.NotNil(t, priv) + expectedPkStr := "PubKeySecp256k1{034FEF9CD7C4C63588D3B03FEB5281B9D232CBA34D6F3D71AEE59211FFBFE1FE87}" require.Equal(t, "eb5ae98721034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - - out, err := protocdc.MarshalJSON(priv.PubKey(), nil) - require.NoError(t, err) - // TODO: require.Equal(t, out, ...) - fmt.Println("TODO ledger_test.go", out) - + require.Equal(t, expectedPkStr, priv.PubKey()) addr := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) @@ -104,26 +102,15 @@ func TestPublicKeySafe(t *testing.T) { require.NoError(t, err) require.NotNil(t, priv) - require.Nil(t, ShowAddress(path, priv.PubKey(), sdk.GetConfig().GetBech32AccountAddrPrefix())) - - require.Equal(t, "eb5ae98721034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", - fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), - "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - - pubKeyAddr, err := legacybech32.MarshalPubKey(legacybech32.AccPK, priv.PubKey()) - require.NoError(t, err) - require.Equal(t, "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - - require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", - addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) + checkDefaultPubKey(t, priv) addr2 := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, addr, addr2) } func TestPublicKeyHDPath(t *testing.T) { + // TODO: convert this tests to not use bech32 expectedPubKeys := []string{ "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", "cosmospub1addwnpepqfsdqjr68h7wjg5wacksmqaypasnra232fkgu5sxdlnlu8j22ztxvlqvd65", diff --git a/server/tm_cmds.go b/server/tm_cmds.go index f2e26482ef45..b3d5ba65316e 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -125,7 +125,6 @@ against which this app has been compiled. } // Deprecated: prints the content to the standard output using Legacy Amino -// TODO: add issue to trace it? func printlnJSON(v interface{}) error { cdc := codec.NewLegacyAmino() cryptocodec.RegisterCrypto(cdc) diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index 209871f66077..1529955a920b 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -14,7 +14,6 @@ import ( type Bech32PubKeyType string // Bech32 conversion constants -// TODO: check where we can remove this const ( AccPK Bech32PubKeyType = "accpub" ValPK Bech32PubKeyType = "valpub" diff --git a/x/auth/legacy/legacytx/stdsig_test.go b/x/auth/legacy/legacytx/stdsig_test.go index d3992e70f9f6..3e1662ba9960 100644 --- a/x/auth/legacy/legacytx/stdsig_test.go +++ b/x/auth/legacy/legacytx/stdsig_test.go @@ -11,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" ) -// TODO: is this test valuable? func TestStdSignatureMarshalYAML(t *testing.T) { _, pk, _ := testdata.KeyTestPubAddr() pkStr := pk.String() From 3a94d65ef7ed3524d099ca5240c3782377d1c628 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 23:23:58 +0100 Subject: [PATCH 43/91] remove printlnJSON from /server CLI and amino encoding --- server/tm_cmds.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index b3d5ba65316e..3c782dcd247c 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -14,9 +14,9 @@ import ( tversion "github.com/tendermint/tendermint/version" yaml "gopkg.in/yaml.v2" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -56,11 +56,12 @@ func ShowValidatorCmd() *cobra.Command { if err != nil { return err } - out, err := protocdc.MarshalJSON(sdkPK, nil) + clientCtx := client.GetClientContextFromCmd(cmd) + bz, err := codec.MarshalIfcJSON(clientCtx.JSONMarshaler, sdkPK) if err != nil { return err } - fmt.Println(string(out)) + fmt.Println(string(bz)) return nil }, } @@ -82,7 +83,8 @@ func ShowAddressCmd() *cobra.Command { output, _ := cmd.Flags().GetString(cli.OutputFlag) if strings.ToLower(output) == "json" { - return printlnJSON(valConsAddr) + fmt.Printf("{\"address\": %q}\n", valConsAddr.String()) + return nil } fmt.Println(valConsAddr.String()) @@ -124,20 +126,6 @@ against which this app has been compiled. } } -// Deprecated: prints the content to the standard output using Legacy Amino -func printlnJSON(v interface{}) error { - cdc := codec.NewLegacyAmino() - cryptocodec.RegisterCrypto(cdc) - - marshalled, err := cdc.MarshalJSON(v) - if err != nil { - return err - } - - fmt.Println(string(marshalled)) - return nil -} - // UnsafeResetAllCmd - extension of the tendermint command, resets initialization func UnsafeResetAllCmd() *cobra.Command { return &cobra.Command{ From dc9f743529210049d7c41b0b34c4e094125bb199 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 30 Nov 2020 23:44:49 +0100 Subject: [PATCH 44/91] remove protocdc.MarshalJSON --- client/grpc/tmservice/service.go | 4 ++-- crypto/keyring/output.go | 3 +-- crypto/keys/multisig/multisig_test.go | 14 ++++++++------ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/client/grpc/tmservice/service.go b/client/grpc/tmservice/service.go index 4a15729ad670..7b102f14806d 100644 --- a/client/grpc/tmservice/service.go +++ b/client/grpc/tmservice/service.go @@ -10,8 +10,8 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/rpc" + "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" qtypes "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/version" ) @@ -127,7 +127,7 @@ func validatorsOutput(ctx client.Context, height *int64, page, limit int) (int64 out := make([]*qtypes.Validator, len(vsRes.Validators)) for i, v := range vsRes.Validators { - pkBz, err := protocdc.MarshalJSON(v.PubKey, nil) + pkBz, err := codec.MarshalIfcJSON(ctx.JSONMarshaler, v.PubKey) if err != nil { return 0, nil, err } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 51470d4492c6..59f04cc3d956 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -68,6 +68,5 @@ func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.AccAddress(pk.Address().Bytes()) - ko, _ := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) - return ko, nil + return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 4e9f3c0f068b..32a64d5d2533 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -1,6 +1,7 @@ package multisig_test import ( + "strings" "testing" "github.com/stretchr/testify/require" @@ -11,7 +12,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" - "github.com/cosmos/cosmos-sdk/internal/protocdc" + "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" ) @@ -324,10 +325,11 @@ func TestDisplay(t *testing.T) { require.PanicsWithValue("reflect.Value.Interface: cannot return value obtained from unexported field or method", func() { require.Empty(msig.String()) }, ) - msigBz, err := protocdc.MarshalJSON(msig, nil) + ccfg := simapp.MakeTestEncodingConfig() + bz, err := codec.MarshalIfcJSON(ccfg.Marshaler, msig) require.NoError(err) - msigStr := string(msigBz) - require.Contains(msigStr, `"threshold":2`) - require.Contains(msigStr, "cosmos.crypto.secp256k1.PubKey") - // example output: {"threshold":2,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AoiL9HXu/WDndVCrDptys9fx4NFEbBquhs3SubQG5QMC"},{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Am8adwKmNfz6klSx6e2gessjP7FznW8Y56NyrUtpTW4n"},{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtWLgEf+eQXd4ZudD/kjppK1pWRrXJH55joRyghB8WWc"}]} + expectedPrefix := `{"@type":"/cosmos.crypto.multisig.LegacyAminoPubKey","threshold":2,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey"` + require.True(strings.HasPrefix(string(bz), expectedPrefix)) + // Example output: + // {"@type":"/cosmos.crypto.multisig.LegacyAminoPubKey","threshold":2,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AymUY3J2HKIyy9cbpGKcBFUTuDQsRH9NO/orKF/0WQ76"},{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AkvnCDzSYF+tQV/FoI217V7CDIRPzjJj7zBE2nw7x3xT"},{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A0yiqgcM5EB1i0h79+sQp+C0jLPFnT3+dFmdZmGa+H1s"}]} } From da247734746a518650d87a720e03eda336e1d69c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 1 Dec 2020 00:05:05 +0100 Subject: [PATCH 45/91] client/show remove duplicated function --- client/keys/show.go | 6 ++---- client/keys/utils.go | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/client/keys/show.go b/client/keys/show.go index 307db6897779..72a4fe462723 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -111,10 +111,8 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { output, _ := cmd.Flags().GetString(cli.OutputFlag) switch { - case isShowAddr: - printKeyAddress(cmd.OutOrStdout(), info, bechKeyOut) - case isShowPubKey: - printPubKey(cmd.OutOrStdout(), info, bechKeyOut) + case isShowAddr, isShowPubKey: + printInfo(cmd.OutOrStdout(), info, bechKeyOut) default: printKeyInfo(cmd.OutOrStdout(), info, bechKeyOut, output) } diff --git a/client/keys/utils.go b/client/keys/utils.go index 7490914d1108..acc586b9c66d 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -79,7 +79,7 @@ func printTextInfos(w io.Writer, kos []cryptokeyring.KeyOutput) { fmt.Fprintln(w, string(out)) } -func printKeyAddress(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) { +func printInfo(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) { ko, err := bechKeyOut(info) if err != nil { panic(err) From a965f3a5ed8ba81dcd1af5d92fa1ca42e75bae7f Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 1 Dec 2020 00:11:15 +0100 Subject: [PATCH 46/91] remove protocdc package --- crypto/keyring/output.go | 5 +++-- internal/protocdc/marshal.go | 21 --------------------- 2 files changed, 3 insertions(+), 23 deletions(-) delete mode 100644 internal/protocdc/marshal.go diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 59f04cc3d956..c2af424da405 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -1,12 +1,13 @@ package keyring import ( + "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/internal/protocdc" sdk "github.com/cosmos/cosmos-sdk/types" ) // TODO: Move this file to client/keys +// Use protobuf interface marshaler rather then generic JSON // KeyOutput defines a structure wrapping around an Info object used for output // functionality. @@ -20,7 +21,7 @@ type KeyOutput struct { // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { // nolint:interfacer - bz, err := protocdc.MarshalJSON(pk, nil) + bz, err := codec.ProtoMarshalJSON(pk, nil) if err != nil { return KeyOutput{}, err } diff --git a/internal/protocdc/marshal.go b/internal/protocdc/marshal.go deleted file mode 100644 index 02609b3fe924..000000000000 --- a/internal/protocdc/marshal.go +++ /dev/null @@ -1,21 +0,0 @@ -package protocdc - -import ( - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/gogo/protobuf/jsonpb" - "github.com/gogo/protobuf/proto" - - "github.com/cosmos/cosmos-sdk/codec" -) - -// MarshalJSON is a wrapper for codec.ProtoMarshalJSON. It asserts that msg -// implements `proto.Message` and calls codec.ProtoMarshalJSON. -// This function should be used only with concrete types. For interface serialization -// you need to wrap the interface into Any or generally use MarshalIfcJSON. -func MarshalJSON(msg interface{}, resolver jsonpb.AnyResolver) ([]byte, error) { - msgProto, ok := msg.(proto.Message) - if !ok { - return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "Expecting protobuf Message type, got %T", msg) - } - return codec.ProtoMarshalJSON(msgProto, resolver) -} From 2e95ae8716ddc32a8cac98a7fefe0404f0715729 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 1 Dec 2020 00:24:20 +0100 Subject: [PATCH 47/91] comment update --- codec/any.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/codec/any.go b/codec/any.go index 314e7937938a..25f97f7cb7de 100644 --- a/codec/any.go +++ b/codec/any.go @@ -13,7 +13,8 @@ import ( // to use a helper function or an interface. // MarshalIfc is a convenience function for proto marshalling interfaces. It -// packs the provided value in an Any and then marshals it to bytes. +// packs the provided value, which must implemenet proto.Message, +// in an Any and then marshals it to bytes. // NOTE: if you use a concert type, then you should use BinaryMarshaler.MarshalBinaryBare directly func MarshalIfc(m BinaryMarshaler, x interface{}) ([]byte, error) { msg, ok := x.(proto.Message) @@ -60,11 +61,11 @@ func MarshalIfcJSON(m JSONMarshaler, x proto.Message) ([]byte, error) { // UnmarshalIfcJSON is a convenience function for proto unmarshaling interfaces. // It unmarshals an Any from bz and then unpacks it to the `iface`, which must -// be a pointer to a non empty interface with registered implementations. +// be a pointer to a non empty interface, implementing proto.Message with registered implementations. // NOTE: if you use a concert type, then you should use JSONMarshaler.UnarshalJSON directly // // Ex: -// var x MyInterface +// var x MyInterface // must implement proto.Message // err := UnmarshalAny(unpacker, &x, bz) func UnmarshalIfcJSON(m IfcJSONMarshaler, iface interface{}, bz []byte) error { any := &types.Any{} From 937f11d3b293b40ea3296085d13e71e11c80ff4e Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 1 Dec 2020 00:50:12 +0100 Subject: [PATCH 48/91] remove legacybech32.MustMarshalPubKey from a test --- client/keys/add_ledger_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 65d26c648620..89f26dd436ef 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -122,6 +122,6 @@ func Test_runAddCmdLedger(t *testing.T) { require.Equal(t, "keyname1", key1.GetName()) require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, - "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", - legacybech32.MustMarshalPubKey(legacybech32.AccPK, key1.GetPubKey())) + "PubKeySecp256k1{034FEF9CD7C4C63588D3B03FEB5281B9D232CBA34D6F3D71AEE59211FFBFE1FE87}", + key1.GetPubKey().String()) } From 18613c5ddcaf802fd5cddf525369986d3fe08c12 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 1 Dec 2020 01:00:48 +0100 Subject: [PATCH 49/91] add todo --- types/bech32/legacybech32/pk.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index 1529955a920b..f7bc8643ed16 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -10,6 +10,10 @@ import ( "github.com/cosmos/cosmos-sdk/types/bech32" ) +// TODO: when removing this package remove: +// + sdk:config.GetBech32AccountPubPrefix (and other related functions) +// + Bech32PrefixAccAddr and other related constants + // Deprecated: Bech32PubKeyType defines a string type alias for a Bech32 public key type. type Bech32PubKeyType string From f60c9b7937d7044239ee10912b88735cd3f6cefd Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 1 Dec 2020 02:04:03 +0100 Subject: [PATCH 50/91] fix TestPublicKeyUnsafe test --- crypto/ledger/ledger_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index e8f55b4a66db..918032c3627f 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -35,7 +35,7 @@ func checkDefaultPubKey(t *testing.T, priv types.LedgerPrivKey) { require.Equal(t, "eb5ae98721034fef9cd7c4c63588d3b03feb5281b9d232cba34d6f3d71aee59211ffbfe1fe87", fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())), "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) - require.Equal(t, expectedPkStr, priv.PubKey()) + require.Equal(t, expectedPkStr, priv.PubKey().String()) addr := sdk.AccAddress(priv.PubKey().Address()).String() require.Equal(t, "cosmos1w34k53py5v5xyluazqpq65agyajavep2rflq6h", addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic) From 7401c1a59339e56e8a9cb8ef620b137160a9896b Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 2 Dec 2020 21:37:28 +0100 Subject: [PATCH 51/91] review update --- crypto/keyring/info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/keyring/info.go b/crypto/keyring/info.go index 17708efefdc6..ad8292eea3ab 100644 --- a/crypto/keyring/info.go +++ b/crypto/keyring/info.go @@ -232,7 +232,7 @@ func (i multiInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { // encoding info func marshalInfo(i Info) []byte { - // TODO: Why do we use Legacy Amino marshaling here? + // Legacy. #7108 return CryptoCdc.MustMarshalBinaryLengthPrefixed(i) } From eb682681e2a20be77b8a00eab2c5c781dba5fe26 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 11 Jan 2021 21:24:21 +0100 Subject: [PATCH 52/91] fix bech32 UnmarshalPubKey --- types/bech32/legacybech32/pk.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/types/bech32/legacybech32/pk.go b/types/bech32/legacybech32/pk.go index f7bc8643ed16..d87053190f40 100644 --- a/types/bech32/legacybech32/pk.go +++ b/types/bech32/legacybech32/pk.go @@ -4,7 +4,6 @@ package legacybech32 import ( "github.com/cosmos/cosmos-sdk/codec/legacy" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/bech32" @@ -65,6 +64,5 @@ func UnmarshalPubKey(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.PubKey if err != nil { return nil, err } - - return cryptocodec.PubKeyFromBytes(bz) + return legacy.PubKeyFromBytes(bz) } From 4a28e7a6220d21a507b0f958d9a906e47ce7478c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 11 Jan 2021 23:50:37 +0100 Subject: [PATCH 53/91] Use codec.MarshalIfcJSON --- client/keys/codec.go | 2 +- codec/proto_codec.go | 4 ++-- crypto/keys/ed25519/ed25519_test.go | 2 +- crypto/keys/multisig/multisig_test.go | 2 +- x/slashing/client/cli/cli_test.go | 3 +-- x/staking/client/cli/cli_test.go | 3 +-- 6 files changed, 7 insertions(+), 9 deletions(-) diff --git a/client/keys/codec.go b/client/keys/codec.go index ae8f641b81de..30ebcea40226 100644 --- a/client/keys/codec.go +++ b/client/keys/codec.go @@ -5,7 +5,7 @@ import ( cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" ) -// TOOD: remove this file #8047 +// TODO: remove this file #8047 // KeysCdc defines codec to be used with key operations var KeysCdc *codec.LegacyAmino diff --git a/codec/proto_codec.go b/codec/proto_codec.go index 801fc0253607..e01bf1dcd804 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -125,7 +125,7 @@ func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMars // MarshalJSON implements JSONMarshaler.MarshalJSON method, // it marshals to JSON using proto codec. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.MarshalIfcJSON +// implements proto.Message. For interface please use the codec.MarshalInterfaceJSON func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { m, ok := o.(ProtoMarshaler) if !ok { @@ -138,7 +138,7 @@ func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { // MustMarshalJSON implements JSONMarshaler.MustMarshalJSON method, // it executes MarshalJSON except it panics upon failure. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.MarshalIfcJSON +// implements proto.Message. For interface please use the codec.MarshalInterfaceJSON func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { bz, err := pc.MarshalJSON(o) if err != nil { diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 78f76541b027..a5c11bedd0d1 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -280,7 +280,7 @@ func TestMarshalProto2(t *testing.T) { // **** test JSON serialization **** - bz, err := codec.MarshalIfcJSON(ccfg.Marshaler, pk) + bz, err := ccfg.Marshaler.MarshalInterfaceJSON(pk) require.NoError(err) var pk3 cryptotypes.PubKey diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 32a64d5d2533..3cd2160a1b0e 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -326,7 +326,7 @@ func TestDisplay(t *testing.T) { func() { require.Empty(msig.String()) }, ) ccfg := simapp.MakeTestEncodingConfig() - bz, err := codec.MarshalIfcJSON(ccfg.Marshaler, msig) + bz, err := ccfg.Marshaler.MarshalInterfaceJSON(msig) require.NoError(err) expectedPrefix := `{"@type":"/cosmos.crypto.multisig.LegacyAminoPubKey","threshold":2,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey"` require.True(strings.HasPrefix(string(bz), expectedPrefix)) diff --git a/x/slashing/client/cli/cli_test.go b/x/slashing/client/cli/cli_test.go index 08198778473c..a2a9eb4fa198 100644 --- a/x/slashing/client/cli/cli_test.go +++ b/x/slashing/client/cli/cli_test.go @@ -12,7 +12,6 @@ import ( tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/codec" clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" "github.com/cosmos/cosmos-sdk/testutil/network" sdk "github.com/cosmos/cosmos-sdk/types" @@ -50,7 +49,7 @@ func (s *IntegrationTestSuite) TearDownSuite() { func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() { val := s.network.Validators[0] - pubKeyBz, err := codec.MarshalIfcJSON(s.cfg.Codec, val.PubKey) + pubKeyBz, err := s.cfg.Codec.MarshalInterfaceJSON(val.PubKey) s.Require().NoError(err) pubKeyStr := string(pubKeyBz) diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 960c6e0ac4b5..86ce427b2401 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -15,7 +15,6 @@ import ( "github.com/tendermint/tendermint/rpc/client/http" "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" @@ -79,7 +78,7 @@ func (s *IntegrationTestSuite) TestNewCreateValidatorCmd() { val := s.network.Validators[0] consPrivKey := ed25519.GenPrivKey() - consPubKeyBz, err := codec.MarshalIfcJSON(s.cfg.Codec, consPrivKey.PubKey()) + consPubKeyBz, err := s.cfg.Codec.MarshalInterfaceJSON(consPrivKey.PubKey()) require.NoError(err) require.NotNil(consPubKeyBz) From 801984b4e52a24c15f8e123dd8fef319aebcd716 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 11 Jan 2021 23:56:33 +0100 Subject: [PATCH 54/91] fix linter issues --- client/keys/utils.go | 9 --------- client/tx/factory.go | 2 +- x/auth/ante/sigverify.go | 2 +- x/auth/client/cli/tx_multisign.go | 2 +- x/auth/client/cli/tx_sign.go | 4 ++-- x/auth/testutil/suite.go | 2 +- x/auth/tx/mode_handler.go | 4 ++-- 7 files changed, 8 insertions(+), 17 deletions(-) diff --git a/client/keys/utils.go b/client/keys/utils.go index acc586b9c66d..a9dcb9669597 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -87,12 +87,3 @@ func printInfo(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) { fmt.Fprintln(w, ko.Address) } - -func printPubKey(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) { - ko, err := bechKeyOut(info) - if err != nil { - panic(err) - } - - fmt.Fprintln(w, ko.PubKey) -} diff --git a/client/tx/factory.go b/client/tx/factory.go index b10d728d6b87..8510b39c00c9 100644 --- a/client/tx/factory.go +++ b/client/tx/factory.go @@ -38,7 +38,7 @@ func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) Factory { case flags.SignModeDirect: signMode = signing.SignMode_SIGN_MODE_DIRECT case flags.SignModeLegacyAminoJSON: - signMode = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON + signMode = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON //nolint:staticcheck } accNum, _ := flagSet.GetUint64(flags.FlagAccountNumber) diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index 79fbbcdc676a..72bde23c58fd 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -182,7 +182,7 @@ func NewSigVerificationDecorator(ak AccountKeeper, signModeHandler authsigning.S func OnlyLegacyAminoSigners(sigData signing.SignatureData) bool { switch v := sigData.(type) { case *signing.SingleSignatureData: - return v.SignMode == signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON + return v.SignMode == signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON //nolint:staticcheck case *signing.MultiSignatureData: for _, s := range v.Signatures { if !OnlyLegacyAminoSigners(s) { diff --git a/x/auth/client/cli/tx_multisign.go b/x/auth/client/cli/tx_multisign.go index 2910244f6bf0..73f01b05c905 100644 --- a/x/auth/client/cli/tx_multisign.go +++ b/x/auth/client/cli/tx_multisign.go @@ -73,7 +73,7 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) (err error) { txFactory := tx.NewFactoryCLI(clientCtx, cmd.Flags()) if txFactory.SignMode() == signingtypes.SignMode_SIGN_MODE_UNSPECIFIED { - txFactory = txFactory.WithSignMode(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) + txFactory = txFactory.WithSignMode(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) //nolint:staticcheck } txCfg := clientCtx.TxConfig diff --git a/x/auth/client/cli/tx_sign.go b/x/auth/client/cli/tx_sign.go index 251282bb8e0c..3706486335e2 100644 --- a/x/auth/client/cli/tx_sign.go +++ b/x/auth/client/cli/tx_sign.go @@ -114,7 +114,7 @@ func makeSignBatchCmd() func(cmd *cobra.Command, args []string) error { } } else { if txFactory.SignMode() == signing.SignMode_SIGN_MODE_UNSPECIFIED { - txFactory = txFactory.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) + txFactory = txFactory.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) //nolint:staticcheck } err = authclient.SignTxWithSignerAddress( txFactory, clientCtx, multisigAddr, clientCtx.GetFromName(), txBuilder, clientCtx.Offline, true) @@ -216,7 +216,7 @@ func makeSignCmd() func(cmd *cobra.Command, args []string) error { return err } if txF.SignMode() == signing.SignMode_SIGN_MODE_UNSPECIFIED { - txF = txF.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) + txF = txF.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) //nolint:staticcheck } txCfg := clientCtx.TxConfig txBuilder, err := txCfg.WrapTxBuilder(newTx) diff --git a/x/auth/testutil/suite.go b/x/auth/testutil/suite.go index f8002e68e4e6..a33af5e5c304 100644 --- a/x/auth/testutil/suite.go +++ b/x/auth/testutil/suite.go @@ -236,7 +236,7 @@ func (s *TxConfigTestSuite) TestTxEncodeDecode() { sig := signingtypes.SignatureV2{ PubKey: pubkey, Data: &signingtypes.SingleSignatureData{ - SignMode: signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + SignMode: signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, //nolint:staticcheck Signature: dummySig, }, } diff --git a/x/auth/tx/mode_handler.go b/x/auth/tx/mode_handler.go index f49ee16198d7..861ab777322a 100644 --- a/x/auth/tx/mode_handler.go +++ b/x/auth/tx/mode_handler.go @@ -10,7 +10,7 @@ import ( // DefaultSignModes are the default sign modes enabled for protobuf transactions. var DefaultSignModes = []signingtypes.SignMode{ signingtypes.SignMode_SIGN_MODE_DIRECT, - signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, //nolint:staticcheck } // makeSignModeHandler returns the default protobuf SignModeHandler supporting @@ -26,7 +26,7 @@ func makeSignModeHandler(modes []signingtypes.SignMode) signing.SignModeHandler switch mode { case signingtypes.SignMode_SIGN_MODE_DIRECT: handlers[i] = signModeDirectHandler{} - case signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON: + case signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON: //nolint:staticcheck handlers[i] = signModeLegacyAminoJSONHandler{} default: panic(fmt.Errorf("unsupported sign mode %+v", mode)) From a05c1b20e8cfbfdb94ce0e4903aa8f02f69a5360 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 12 Jan 2021 00:58:36 +0100 Subject: [PATCH 55/91] merging conflict: fix codec.Unmarshal calls --- codec/proto_codec.go | 12 ++++++------ crypto/keys/ed25519/ed25519_test.go | 7 +++---- x/slashing/simulation/decoder_test.go | 3 +-- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/codec/proto_codec.go b/codec/proto_codec.go index e01bf1dcd804..818d7b9802ea 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -35,14 +35,14 @@ func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec { // MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.MarshalIfc +// implements proto.Message. For interface please use the codec.MarshalInterface func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) { return o.Marshal() } // MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.MarshalIfc +// implements proto.Message. For interface please use the codec.MarshalInterface func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte { bz, err := pc.MarshalBinaryBare(o) if err != nil { @@ -76,7 +76,7 @@ func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte { // UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.UnmarshalIfc +// implements proto.Message. For interface please use the codec.UnmarshalInterface func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { err := ptr.Unmarshal(bz) if err != nil { @@ -91,7 +91,7 @@ func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { // MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.UnmarshalIfc +// implements proto.Message. For interface please use the codec.UnmarshalInterface func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) { if err := pc.UnmarshalBinaryBare(bz, ptr); err != nil { panic(err) @@ -151,7 +151,7 @@ func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { // UnmarshalJSON implements JSONMarshaler.UnmarshalJSON method, // it unmarshals from JSON using proto codec. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.UnmarshalIfcJSON +// implements proto.Message. For interface please use the codec.UnmarshalInterfaceJSON func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { m, ok := ptr.(ProtoMarshaler) if !ok { @@ -170,7 +170,7 @@ func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { // MustUnmarshalJSON implements JSONMarshaler.MustUnmarshalJSON method, // it executes UnmarshalJSON except it panics upon failure. // NOTE: this function must be used with a concert type which -// implements proto.Message. For interface please use the codec.UnmarshalIfcJSON +// implements proto.Message. For interface please use the codec.UnmarshalInterfaceJSON func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) { if err := pc.UnmarshalJSON(bz, ptr); err != nil { panic(err) diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index a5c11bedd0d1..7b7300617e9a 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -284,8 +284,7 @@ func TestMarshalProto2(t *testing.T) { require.NoError(err) var pk3 cryptotypes.PubKey - am := codec.NewJSONAnyMarshaler(ccfg.Marshaler, ccfg.InterfaceRegistry) - err = codec.UnmarshalIfcJSON(am, &pk3, bz) + err = ccfg.Marshaler.UnmarshalInterfaceJSON(bz, &pk3) require.NoError(err) require.True(pk3.Equals(pk)) @@ -301,11 +300,11 @@ func TestMarshalProto2(t *testing.T) { // **** test binary serialization **** - bz, err = codec.MarshalIfc(ccfg.Marshaler, pk) + bz, err = ccfg.Marshaler.MarshalInterface(pk) require.NoError(err) var pk2 cryptotypes.PubKey - err = codec.UnmarshalIfc(ccfg.Marshaler, &pk2, bz) + err = ccfg.Marshaler.UnmarshalInterface(bz, &pk2) require.NoError(err) require.True(pk2.Equals(pk)) } diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index ae7612539f73..f36077d816d7 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -8,7 +8,6 @@ import ( gogotypes "github.com/gogo/protobuf/types" "github.com/stretchr/testify/require" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/simapp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -31,7 +30,7 @@ func TestDecodeStore(t *testing.T) { info := types.NewValidatorSigningInfo(consAddr1, 0, 1, time.Now().UTC(), false, 0) missed := gogotypes.BoolValue{Value: true} - bz, err := codec.MarshalIfc(cdc, delPk1) + bz, err := cdc.MarshalInterface(delPk1) require.NoError(t, err) kvPairs := kv.Pairs{ From e6292e1a2b94094d4907556b9a37b97a0a84c3a3 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 12 Jan 2021 20:08:42 +0100 Subject: [PATCH 56/91] cleanups --- client/debug/main.go | 2 +- client/keys/add.go | 4 ++-- codec/codec.go | 16 ---------------- crypto/keyring/info.go | 2 +- 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index dbe7a5a1732d..672b3aaef898 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/version" ) -// Cmd creats a main CLI command +// Cmd creates a main CLI command func Cmd() *cobra.Command { cmd := &cobra.Command{ Use: "debug", diff --git a/client/keys/add.go b/client/keys/add.go index f36094dfa7cd..1b548888be7d 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -40,7 +40,7 @@ const ( func AddKeyCommand() *cobra.Command { cmd := &cobra.Command{ Use: "add ", - Short: "Add an encrypted private key (either newly generated or recovered), encrypt it, and save to disk", + Short: "Add an encrypted private key (either newly generated or recovered), encrypt it, and save to file", Long: `Derive a new private key and encrypt to disk. Optionally specify a BIP39 mnemonic, a BIP39 passphrase to further secure the mnemonic, and a bip32 HD path to derive a specific account. The key will be stored under the given name @@ -65,7 +65,7 @@ the flag --nosort is set. f.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") f.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") f.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") - f.String(FlagPublicKey, "", "Parse a public key in bech32 format and save it to disk") + f.String(FlagPublicKey, "", "Parse a public key in JSON format and saves key info to file.") f.BoolP(flagInteractive, "i", false, "Interactively prompt user for BIP39 passphrase and mnemonic") f.Bool(flags.FlagUseLedger, false, "Store a local reference to a private key on a Ledger device") f.Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating") diff --git a/codec/codec.go b/codec/codec.go index 74f48bccdadc..4746c58b6fae 100644 --- a/codec/codec.go +++ b/codec/codec.go @@ -48,11 +48,6 @@ type ( MustUnmarshalJSON(bz []byte, ptr proto.Message) } - IfcJSONMarshaler interface { - types.AnyUnpacker - JSONMarshaler - } - // ProtoMarshaler defines an interface a type must implement as protocol buffer // defined message. ProtoMarshaler interface { @@ -74,14 +69,3 @@ type ( UnmarshalAminoJSON([]byte) error } ) - -type jsonAny struct { - JSONMarshaler - types.InterfaceRegistry -} - -// NewJSONAnyMarshaler creates a JSONAnyMarshaler using JSONMarshaler -// and InterfaceRegistry -func NewJSONAnyMarshaler(jm JSONMarshaler, ir types.InterfaceRegistry) IfcJSONMarshaler { - return jsonAny{jm, ir} -} diff --git a/crypto/keyring/info.go b/crypto/keyring/info.go index 3744a67400a8..6d7b9aad6e9b 100644 --- a/crypto/keyring/info.go +++ b/crypto/keyring/info.go @@ -186,7 +186,7 @@ type multiInfo struct { // NewMultiInfo creates a new multiInfo instance func NewMultiInfo(name string, pub cryptotypes.PubKey) (Info, error) { if _, ok := pub.(*multisig.LegacyAminoPubKey); !ok { - return nil, fmt.Errorf("MultiInfo supports only LegacyAminoPubKey, got %T", pub) + return nil, fmt.Errorf("MultiInfo supports only multisig.LegacyAminoPubKey, got %T", pub) } return &multiInfo{ Name: name, From d55f19e22ca5a28a7775514f724966bbd15eb633 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 12 Jan 2021 21:21:12 +0100 Subject: [PATCH 57/91] Update CHANGELOG.md Co-authored-by: Aaron Craelius --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcfae9de0808..d55f2957a0cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) Changed Bech32 Public Key serialization in the client facing functionality (CLI, MsgServer, QueryServer): * updated the keyring display structure (it uses protobuf JSON serialization) - the output is more verbose. - * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalIfc` and `UnmarshalIfc` respectively. These functions must take interface as a parameter (not a concrete type nor `Any` object). Underneeth they use `Any` wrapping for correct protobuf serialization. + * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take interface as a parameter (not a concrete type nor `Any` object). Underneeth they use `Any` wrapping for correct protobuf serialization. ### API Breaking From 4be2229f6a4bea68600c0c8389ad683ab9e02ea7 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 12 Jan 2021 21:27:41 +0100 Subject: [PATCH 58/91] Reword changelog updates --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d55f2957a0cf..436a268cf901 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,8 +45,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### API Breaking * (x/staking/types) [\#7447](https://github.com/cosmos/cosmos-sdk/issues/7447) Remove bech32 PubKey support: - * `ValidatorI` interface update. `GetConsPubKey` renamed to `TmConsPubKey` (consensus public key must be a tendermint key). `TmConsPubKey`, `GetConsAddr` methods return error. - * `Validator` update. Methods changed in `ValidatorI` (as described above) and `ToTmValidator` return error. + * `ValidatorI` interface update: `GetConsPubKey` renamed to `TmConsPubKey` (this is to clarify the return type: consensus public key must be a tendermint key); `TmConsPubKey`, `GetConsAddr` methods return error. + * `Validator` updated according to the `ValidatorI` changes described above. + * `ToTmValidator` function: added `error` to return values. * `Validator.ConsensusPubkey` type changed from `string` to `codectypes.Any`. * `MsgCreateValidator.Pubkey` type changed from `string` to `codectypes.Any`. From 83fcfd0d05fcae65b5a59510637d4015f9fb5c8a Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 12 Jan 2021 21:38:27 +0100 Subject: [PATCH 59/91] use pubkey.String for comparison in Test_runAddCmdLedgerWithCustomCoinType --- client/debug/main.go | 4 +--- client/keys/add_ledger_test.go | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/client/debug/main.go b/client/debug/main.go index 672b3aaef898..8a3fbe910bee 100644 --- a/client/debug/main.go +++ b/client/debug/main.go @@ -29,9 +29,7 @@ func Cmd() *cobra.Command { return cmd } -// getPubKeyFromString returns a Tendermint PubKey (PubKeyEd25519) by attempting -// to decode the pubkey string from hex, base64, and finally bech32. If all -// encodings fail, an error is returned. +// getPubKeyFromString decodes SDK PubKey using JSON marshaler. func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) { var pk cryptotypes.PubKey err := ctx.JSONMarshaler.UnmarshalInterfaceJSON([]byte(pkstr), &pk) diff --git a/client/keys/add_ledger_test.go b/client/keys/add_ledger_test.go index 89f26dd436ef..cb55fd485914 100644 --- a/client/keys/add_ledger_test.go +++ b/client/keys/add_ledger_test.go @@ -16,7 +16,6 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" ) func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { @@ -74,8 +73,8 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) { require.Equal(t, "keyname1", key1.GetName()) require.Equal(t, keyring.TypeLedger, key1.GetType()) require.Equal(t, - "terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6", - legacybech32.MustMarshalPubKey(legacybech32.AccPK, key1.GetPubKey())) + "PubKeySecp256k1{03028F0D5A9FD41600191CDEFDEA05E77A68DFBCE286241C0190805B9346667D07}", + key1.GetPubKey().String()) config.SetCoinType(118) config.SetFullFundraiserPath("44'/118'/0'/0/0") From 84a0c210b0f327c8da0dad033775df8616f73e88 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 12 Jan 2021 23:41:38 +0100 Subject: [PATCH 60/91] Update GetCmdQuerySigningInfo example --- crypto/keys/ed25519/ed25519_test.go | 6 +++--- x/slashing/client/cli/query.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 7b7300617e9a..55e3c88cbe88 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -3,7 +3,6 @@ package ed25519_test import ( stded25519 "crypto/ed25519" "encoding/base64" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -260,7 +259,6 @@ func TestMarshalProto(t *testing.T) { // **** test binary serialization **** bz, err = ccfg.Marshaler.MarshalBinaryBare(pkAny) - fmt.Println(bz) require.NoError(err) var pkAny3 codectypes.Any @@ -272,7 +270,9 @@ func TestMarshalProto(t *testing.T) { require.True(pk3.Equals(pk)) } -func TestMarshalProto2(t *testing.T) { +// TestMarshalProtoInterface tests PubKey marshaling using (Un)marshalInterface helper +// functions +func TestMarshalProtoInterface(t *testing.T) { require := require.New(t) ccfg := simapp.MakeTestEncodingConfig() privKey := ed25519.GenPrivKey() diff --git a/x/slashing/client/cli/query.go b/x/slashing/client/cli/query.go index 130edf72b827..cfa26ab5a7c4 100644 --- a/x/slashing/client/cli/query.go +++ b/x/slashing/client/cli/query.go @@ -41,7 +41,7 @@ func GetCmdQuerySigningInfo() *cobra.Command { Short: "Query a validator's signing information", Long: strings.TrimSpace(`Use a validators' consensus public key to find the signing-info for that validator: -$ query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdgexxhmz0l8c7sgswl7ulv7aulk364x4g5xsw7sr0k2g5 +$ query slashing signing-info '{"@type":"/cosmos.crypto.ed25519.PubKey","key":"OauFcTKbN5Lx3fJL689cikXBqe+hcp6Y+x0rYUdR9Jk="}' `), Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { From 4e60fadaad3f8c46dc8f67680a95092f55685246 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 13 Jan 2021 12:56:33 +0100 Subject: [PATCH 61/91] cli: update keys add docs --- client/keys/add.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 1b548888be7d..e3028894934c 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -53,16 +53,18 @@ local keystore. Use the --pubkey flag to add arbitrary public keys to the keystore for constructing multisig transactions. -You can add a multisig key by passing the list of key names you want the public -key to be composed of to the --multisig flag and the minimum number of signatures -required through --multisig-threshold. The keys are sorted by address, unless -the flag --nosort is set. +You can create and store a multisig key by passing the list of key names stored in a keyring +and the minimum number of signatures required through --multisig-threshold. The keys are +sorted by address, unless the flag --nosort is set. +Example: + + keys add mymultisig --multisig "keyname1,keyname2,keyname3" --multisig-threshold 2 `, Args: cobra.ExactArgs(1), RunE: runAddCmdPrepare, } f := cmd.Flags() - f.StringSlice(flagMultisig, nil, "Construct and store a multisig public key (implies --pubkey)") + f.StringSlice(flagMultisig, nil, "List of key names stored in keyring to construct a public legacy multisig key") f.Int(flagMultiSigThreshold, 1, "K out of N required signatures. For use in conjunction with --multisig") f.Bool(flagNoSort, false, "Keys passed to --multisig are taken in the order they're supplied") f.String(FlagPublicKey, "", "Parse a public key in JSON format and saves key info to file.") From 8eba4b021b93aceb0a501187af9e7cfd1b013524 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 13 Jan 2021 14:01:40 +0100 Subject: [PATCH 62/91] Add errors AsOf and errors.ErrIO type --- types/errors/errors.go | 15 +++++++++++++++ types/errors/errors_test.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/types/errors/errors.go b/types/errors/errors.go index d08412524c40..c978998a4163 100644 --- a/types/errors/errors.go +++ b/types/errors/errors.go @@ -135,6 +135,10 @@ var ( // supported. ErrNotSupported = Register(RootCodespace, 37, "feature not supported") + // ErrIO should be used to wrap internal errors caused by external operation. + // Examples: not DB domain error, file writing etc... + ErrIO = Register(RootCodespace, 38, "Internal IO error") + // ErrPanic is only set when we recover from a panic, so we know to // redact potentially sensitive system info ErrPanic = Register(UndefinedCodespace, 111222, "panic") @@ -358,6 +362,17 @@ func WithType(err error, obj interface{}) error { return Wrap(err, fmt.Sprintf("%T", obj)) } +// AsOf checks if a received error is caused by one of the target errors. +// It extends the errors.As functionality to a list of errors. +func AsOf(received error, targets ...error) bool { + for _, t := range targets { + if errors.As(received, t) { + return true + } + } + return false +} + // causer is an interface implemented by an error that supports wrapping. Use // it to test if an error wraps another error instance. type causer interface { diff --git a/types/errors/errors_test.go b/types/errors/errors_test.go index 7852f8168a7e..7b92c66a975b 100644 --- a/types/errors/errors_test.go +++ b/types/errors/errors_test.go @@ -149,6 +149,27 @@ func (s *errorsTestSuite) TestErrorIs() { } } +func (s *errorsTestSuite) TestIsOf() { + require := s.Require() + + var errNil error + var err = errors.New("some error") + var errW = Wrap(ErrLogic, "more info") + + require.False(AsOf(errNil), "nil error should always have no causer") + require.False(AsOf(errNil, err), "nil error should always have no causer") + require.False(AsOf(errNil, err), "nil error should always have no causer") + + require.False(AsOf(err)) + require.False(AsOf(err, nil)) + require.False(AsOf(err, ErrLogic)) + require.False(AsOf(err, errW)) + + require.True(AsOf(errW, ErrLogic)) + require.True(AsOf(errW, err, ErrLogic)) + require.True(AsOf(errW, nil, errW), "error should much itsel") +} + type customError struct { } From d0beb741a4eb237968f8f3977f883786ef7d3b0c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 13 Jan 2021 14:28:58 +0100 Subject: [PATCH 63/91] restore multisigPubKeyInfo structure bring it back to multiInfo struct --- client/keys/show.go | 20 ++++++++++---------- crypto/keyring/info.go | 15 +++++++++++++-- crypto/keyring/keyring.go | 2 -- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/client/keys/show.go b/client/keys/show.go index 28a9b489ee53..112614e7339d 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -13,6 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/ledger" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerr "github.com/cosmos/cosmos-sdk/types/errors" ) const ( @@ -145,19 +146,18 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { } func fetchKey(kb keyring.Keyring, keyref string) (keyring.Info, error) { + // firstly check if the keyref is a key name of a key registered in a keyring. info, err := kb.Key(keyref) + if err == nil || !sdkerr.AsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) { + return info, err + } + accAddr, err := sdk.AccAddressFromBech32(keyref) if err != nil { - accAddr, err := sdk.AccAddressFromBech32(keyref) - if err != nil { - return info, err - } - - info, err = kb.KeyByAddress(accAddr) - if err != nil { - return info, errors.New("key not found") - } + return info, err } - return info, nil + + info, err = kb.KeyByAddress(accAddr) + return info, sdkerr.Wrap(err, "Invalid key") } func validateMultisigThreshold(k, nKeys int) error { diff --git a/crypto/keyring/info.go b/crypto/keyring/info.go index 6d7b9aad6e9b..aa205eec019a 100644 --- a/crypto/keyring/info.go +++ b/crypto/keyring/info.go @@ -177,10 +177,21 @@ func (i offlineInfo) GetPath() (*hd.BIP44Params, error) { return nil, fmt.Errorf("BIP44 Paths are not available for this type") } +// Deprecated: this structure is not used anymore and it's here only to allow +// decoding old multiInfo records from keyring. +// The problem with legacy.Cdc.UnmarshalBinaryLengthPrefixed - the legacy codec doesn't +// tolerate extensibility. +type multisigPubKeyInfo struct { + PubKey cryptotypes.PubKey `json:"pubkey"` + Weight uint `json:"weight"` +} + // multiInfo is the public information about a multisig key type multiInfo struct { - Name string `json:"name"` - PubKey cryptotypes.PubKey `json:"pubkey"` + Name string `json:"name"` + PubKey cryptotypes.PubKey `json:"pubkey"` + Threshold uint `json:"threshold"` + PubKeys []multisigPubKeyInfo `json:"pubkeys"` } // NewMultiInfo creates a new multiInfo instance diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 4942e88c8ec5..d1b84c65ff9b 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -532,11 +532,9 @@ func (ks keystore) Key(uid string) (Info, error) { if err != nil { return nil, err } - if len(bs.Data) == 0 { return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, uid) } - return unmarshalInfo(bs.Data) } From 707f05410b8d4517dd92399d4e154e4dfc2c5fc5 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Jan 2021 22:30:41 +0100 Subject: [PATCH 64/91] Update codec/proto_codec.go Co-authored-by: Marie Gauthier --- codec/proto_codec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec/proto_codec.go b/codec/proto_codec.go index 818d7b9802ea..fe152b7e2faa 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -75,7 +75,7 @@ func (pc *ProtoCodec) MustMarshalBinaryLengthPrefixed(o ProtoMarshaler) []byte { } // UnmarshalBinaryBare implements BinaryMarshaler.UnmarshalBinaryBare method. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.UnmarshalInterface func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { err := ptr.Unmarshal(bz) From f5b060c0ae990602863d3c78c37f0589a7fc75de Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Jan 2021 22:31:08 +0100 Subject: [PATCH 65/91] Update crypto/keys/ed25519/ed25519_test.go Co-authored-by: Marie Gauthier --- crypto/keys/ed25519/ed25519_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 55e3c88cbe88..61dc2e09a0bb 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -290,7 +290,7 @@ func TestMarshalProtoInterface(t *testing.T) { // ** Check unmarshal using JSONMarshaler ** // Unpacking won't work straightforward s Any type - // Any can't implement UnpackInterfacesMessage insterface. So Any is not + // Any can't implement UnpackInterfacesMessage interface. So Any is not // automatically unpacked and we won't get a value. var pkAny codectypes.Any err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny) From a0a028593007592735b6c5f6d081d2de5453f016 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Jan 2021 22:31:51 +0100 Subject: [PATCH 66/91] Update codec/proto_codec.go Co-authored-by: Marie Gauthier --- codec/proto_codec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec/proto_codec.go b/codec/proto_codec.go index fe152b7e2faa..09df0166c2b0 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -90,7 +90,7 @@ func (pc *ProtoCodec) UnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) error { } // MustUnmarshalBinaryBare implements BinaryMarshaler.MustUnmarshalBinaryBare method. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.UnmarshalInterface func (pc *ProtoCodec) MustUnmarshalBinaryBare(bz []byte, ptr ProtoMarshaler) { if err := pc.UnmarshalBinaryBare(bz, ptr); err != nil { From 75fdcbdabb6e0e3e11340716d61301d7ba521fd6 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Jan 2021 23:13:45 +0100 Subject: [PATCH 67/91] move pubkey any marshaling tests --- codec/any_test.go | 81 ++++++++++++++++++++++++++++ crypto/keys/ed25519/ed25519_test.go | 82 +---------------------------- 2 files changed, 82 insertions(+), 81 deletions(-) diff --git a/codec/any_test.go b/codec/any_test.go index 16e1b7b7788d..60a14bfd8a8e 100644 --- a/codec/any_test.go +++ b/codec/any_test.go @@ -7,6 +7,10 @@ import ( "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/testutil/testdata" ) @@ -53,3 +57,80 @@ func TestMarshalAny(t *testing.T) { err = cdc.UnmarshalInterface(bz, nil) require.Error(t, err) } + +func TestMarshalProtoPubKey(t *testing.T) { + require := require.New(t) + ccfg := simapp.MakeTestEncodingConfig() + privKey := ed25519.GenPrivKey() + pk := privKey.PubKey() + + // **** test JSON serialization **** + + pkAny, err := codectypes.NewAnyWithValue(pk) + require.NoError(err) + bz, err := ccfg.Marshaler.MarshalJSON(pkAny) + require.NoError(err) + + var pkAny2 codectypes.Any + err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny2) + require.NoError(err) + // we before getting a cached value we need to unpack it. + // Normally this happens in in types which implement UnpackInterfaces + var pkI cryptotypes.PubKey + err = ccfg.InterfaceRegistry.UnpackAny(&pkAny2, &pkI) + require.NoError(err) + var pk2 = pkAny2.GetCachedValue().(cryptotypes.PubKey) + require.True(pk2.Equals(pk)) + + // **** test binary serialization **** + + bz, err = ccfg.Marshaler.MarshalBinaryBare(pkAny) + require.NoError(err) + + var pkAny3 codectypes.Any + err = ccfg.Marshaler.UnmarshalBinaryBare(bz, &pkAny3) + require.NoError(err) + err = ccfg.InterfaceRegistry.UnpackAny(&pkAny3, &pkI) + require.NoError(err) + var pk3 = pkAny3.GetCachedValue().(cryptotypes.PubKey) + require.True(pk3.Equals(pk)) +} + +// TestMarshalProtoInterfacePubKey tests PubKey marshaling using (Un)marshalInterface +// helper functions +func TestMarshalProtoInterfacePubKey(t *testing.T) { + require := require.New(t) + ccfg := simapp.MakeTestEncodingConfig() + privKey := ed25519.GenPrivKey() + pk := privKey.PubKey() + + // **** test JSON serialization **** + + bz, err := ccfg.Marshaler.MarshalInterfaceJSON(pk) + require.NoError(err) + + var pk3 cryptotypes.PubKey + err = ccfg.Marshaler.UnmarshalInterfaceJSON(bz, &pk3) + require.NoError(err) + require.True(pk3.Equals(pk)) + + // ** Check unmarshal using JSONMarshaler ** + // Unpacking won't work straightforward s Any type + // Any can't implement UnpackInterfacesMessage interface. So Any is not + // automatically unpacked and we won't get a value. + var pkAny codectypes.Any + err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny) + require.NoError(err) + ifc := pkAny.GetCachedValue() + require.Nil(ifc) + + // **** test binary serialization **** + + bz, err = ccfg.Marshaler.MarshalInterface(pk) + require.NoError(err) + + var pk2 cryptotypes.PubKey + err = ccfg.Marshaler.UnmarshalInterface(bz, &pk2) + require.NoError(err) + require.True(pk2.Equals(pk)) +} diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 61dc2e09a0bb..84083949b089 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -11,11 +11,9 @@ import ( tmed25519 "github.com/tendermint/tendermint/crypto/ed25519" "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - ed25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/simapp" ) func TestSignAndValidateEd25519(t *testing.T) { @@ -230,81 +228,3 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { }) } } - -// TODO - maybe we should move the tests below to `codec_test` package, WDYT? -func TestMarshalProto(t *testing.T) { - require := require.New(t) - ccfg := simapp.MakeTestEncodingConfig() - privKey := ed25519.GenPrivKey() - pk := privKey.PubKey() - - // **** test JSON serialization **** - - pkAny, err := codectypes.NewAnyWithValue(pk) - require.NoError(err) - bz, err := ccfg.Marshaler.MarshalJSON(pkAny) - require.NoError(err) - - var pkAny2 codectypes.Any - err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny2) - require.NoError(err) - // we before getting a cached value we need to unpack it. - // Normally this happens in in types which implement UnpackInterfaces - var pkI cryptotypes.PubKey - err = ccfg.InterfaceRegistry.UnpackAny(&pkAny2, &pkI) - require.NoError(err) - var pk2 = pkAny2.GetCachedValue().(cryptotypes.PubKey) - require.True(pk2.Equals(pk)) - - // **** test binary serialization **** - - bz, err = ccfg.Marshaler.MarshalBinaryBare(pkAny) - require.NoError(err) - - var pkAny3 codectypes.Any - err = ccfg.Marshaler.UnmarshalBinaryBare(bz, &pkAny3) - require.NoError(err) - err = ccfg.InterfaceRegistry.UnpackAny(&pkAny3, &pkI) - require.NoError(err) - var pk3 = pkAny3.GetCachedValue().(cryptotypes.PubKey) - require.True(pk3.Equals(pk)) -} - -// TestMarshalProtoInterface tests PubKey marshaling using (Un)marshalInterface helper -// functions -func TestMarshalProtoInterface(t *testing.T) { - require := require.New(t) - ccfg := simapp.MakeTestEncodingConfig() - privKey := ed25519.GenPrivKey() - pk := privKey.PubKey() - - // **** test JSON serialization **** - - bz, err := ccfg.Marshaler.MarshalInterfaceJSON(pk) - require.NoError(err) - - var pk3 cryptotypes.PubKey - err = ccfg.Marshaler.UnmarshalInterfaceJSON(bz, &pk3) - require.NoError(err) - require.True(pk3.Equals(pk)) - - // ** Check unmarshal using JSONMarshaler ** - // Unpacking won't work straightforward s Any type - // Any can't implement UnpackInterfacesMessage interface. So Any is not - // automatically unpacked and we won't get a value. - var pkAny codectypes.Any - err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny) - require.NoError(err) - ifc := pkAny.GetCachedValue() - require.Nil(ifc) - - // **** test binary serialization **** - - bz, err = ccfg.Marshaler.MarshalInterface(pk) - require.NoError(err) - - var pk2 cryptotypes.PubKey - err = ccfg.Marshaler.UnmarshalInterface(bz, &pk2) - require.NoError(err) - require.True(pk2.Equals(pk)) -} From 7ebc098566fba795f57bc857775d42d24934a30f Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Jan 2021 23:16:55 +0100 Subject: [PATCH 68/91] Apply suggestions from code review Co-authored-by: Marie Gauthier --- CHANGELOG.md | 2 +- codec/proto_codec.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f883cc26a12..6d3481402d05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,7 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) Changed Bech32 Public Key serialization in the client facing functionality (CLI, MsgServer, QueryServer): * updated the keyring display structure (it uses protobuf JSON serialization) - the output is more verbose. - * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take interface as a parameter (not a concrete type nor `Any` object). Underneeth they use `Any` wrapping for correct protobuf serialization. + * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take an interface as parameter (not a concrete type nor `Any` object). Underneath they use `Any` wrapping for correct protobuf serialization. ### API Breaking diff --git a/codec/proto_codec.go b/codec/proto_codec.go index 09df0166c2b0..7afc99c69075 100644 --- a/codec/proto_codec.go +++ b/codec/proto_codec.go @@ -34,14 +34,14 @@ func NewProtoCodec(interfaceRegistry types.InterfaceRegistry) *ProtoCodec { } // MarshalBinaryBare implements BinaryMarshaler.MarshalBinaryBare method. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.MarshalInterface func (pc *ProtoCodec) MarshalBinaryBare(o ProtoMarshaler) ([]byte, error) { return o.Marshal() } // MustMarshalBinaryBare implements BinaryMarshaler.MustMarshalBinaryBare method. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.MarshalInterface func (pc *ProtoCodec) MustMarshalBinaryBare(o ProtoMarshaler) []byte { bz, err := pc.MarshalBinaryBare(o) @@ -124,7 +124,7 @@ func (pc *ProtoCodec) MustUnmarshalBinaryLengthPrefixed(bz []byte, ptr ProtoMars // MarshalJSON implements JSONMarshaler.MarshalJSON method, // it marshals to JSON using proto codec. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.MarshalInterfaceJSON func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { m, ok := o.(ProtoMarshaler) @@ -137,7 +137,7 @@ func (pc *ProtoCodec) MarshalJSON(o proto.Message) ([]byte, error) { // MustMarshalJSON implements JSONMarshaler.MustMarshalJSON method, // it executes MarshalJSON except it panics upon failure. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.MarshalInterfaceJSON func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { bz, err := pc.MarshalJSON(o) @@ -150,7 +150,7 @@ func (pc *ProtoCodec) MustMarshalJSON(o proto.Message) []byte { // UnmarshalJSON implements JSONMarshaler.UnmarshalJSON method, // it unmarshals from JSON using proto codec. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.UnmarshalInterfaceJSON func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { m, ok := ptr.(ProtoMarshaler) @@ -169,7 +169,7 @@ func (pc *ProtoCodec) UnmarshalJSON(bz []byte, ptr proto.Message) error { // MustUnmarshalJSON implements JSONMarshaler.MustUnmarshalJSON method, // it executes UnmarshalJSON except it panics upon failure. -// NOTE: this function must be used with a concert type which +// NOTE: this function must be used with a concrete type which // implements proto.Message. For interface please use the codec.UnmarshalInterfaceJSON func (pc *ProtoCodec) MustUnmarshalJSON(bz []byte, ptr proto.Message) { if err := pc.UnmarshalJSON(bz, ptr); err != nil { From 12b22b59f53209d19a4b74bf753c369936b190c2 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 15 Jan 2021 23:53:48 +0100 Subject: [PATCH 69/91] review updates --- CHANGELOG.md | 2 +- client/keys/show.go | 2 ++ crypto/keys/multisig/multisig.go | 8 ++++---- server/tm_cmds.go | 16 +++------------- x/slashing/simulation/decoder_test.go | 11 ++++++----- x/staking/client/cli/cli_test.go | 2 +- 6 files changed, 17 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d3481402d05..7ad9a60d9341 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) Changed Bech32 Public Key serialization in the client facing functionality (CLI, MsgServer, QueryServer): * updated the keyring display structure (it uses protobuf JSON serialization) - the output is more verbose. * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take an interface as parameter (not a concrete type nor `Any` object). Underneath they use `Any` wrapping for correct protobuf serialization. - + * CLI: removed `--text` flag from `show-node-id` command; the text format for public keys is not used any more - instead we use ProtoJSON. ### API Breaking diff --git a/client/keys/show.go b/client/keys/show.go index 112614e7339d..c9cbb9ec3d0a 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -148,6 +148,8 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { func fetchKey(kb keyring.Keyring, keyref string) (keyring.Info, error) { // firstly check if the keyref is a key name of a key registered in a keyring. info, err := kb.Key(keyref) + // if the key is not there or if we have a problem with a keyring itself then we move to a + // fallback: searching for key by address. if err == nil || !sdkerr.AsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) { return info, err } diff --git a/crypto/keys/multisig/multisig.go b/crypto/keys/multisig/multisig.go index cb11fbd4ddb5..dd90b5fd9769 100644 --- a/crypto/keys/multisig/multisig.go +++ b/crypto/keys/multisig/multisig.go @@ -16,18 +16,18 @@ var _ types.UnpackInterfacesMessage = &LegacyAminoPubKey{} // NewLegacyAminoPubKey returns a new LegacyAminoPubKey. // Panics if len(pubKeys) < k or 0 >= k. -func NewLegacyAminoPubKey(treshold int, pubKeys []cryptotypes.PubKey) *LegacyAminoPubKey { - if treshold <= 0 { +func NewLegacyAminoPubKey(threshold int, pubKeys []cryptotypes.PubKey) *LegacyAminoPubKey { + if threshold <= 0 { panic("threshold k of n multisignature: k <= 0") } - if len(pubKeys) < treshold { + if len(pubKeys) < threshold { panic("threshold k of n multisignature: len(pubKeys) < k") } anyPubKeys, err := packPubKeys(pubKeys) if err != nil { panic(err) } - return &LegacyAminoPubKey{Threshold: uint32(treshold), PubKeys: anyPubKeys} + return &LegacyAminoPubKey{Threshold: uint32(threshold), PubKeys: anyPubKeys} } // Address implements cryptotypes.PubKey Address method diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 81481f6a968a..ef2cca9eecd4 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -4,11 +4,9 @@ package server import ( "fmt" - "strings" "github.com/spf13/cobra" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/p2p" pvm "github.com/tendermint/tendermint/privval" tversion "github.com/tendermint/tendermint/version" @@ -29,10 +27,10 @@ func ShowNodeIDCmd() *cobra.Command { cfg := serverCtx.Config nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) - if err == nil { - fmt.Println(nodeKey.ID()) + if err != nil { + return err } - return nil + fmt.Println(nodeKey.ID()) }, } } @@ -79,19 +77,11 @@ func ShowAddressCmd() *cobra.Command { privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile()) valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress()) - - output, _ := cmd.Flags().GetString(cli.OutputFlag) - if strings.ToLower(output) == "json" { - fmt.Printf("{\"address\": %q}\n", valConsAddr.String()) - return nil - } - fmt.Println(valConsAddr.String()) return nil }, } - cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)") return cmd } diff --git a/x/slashing/simulation/decoder_test.go b/x/slashing/simulation/decoder_test.go index f36077d816d7..271995d9bd95 100644 --- a/x/slashing/simulation/decoder_test.go +++ b/x/slashing/simulation/decoder_test.go @@ -45,16 +45,17 @@ func TestDecodeStore(t *testing.T) { tests := []struct { name string expectedLog string + panics bool }{ - {"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info)}, - {"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed.Value, missed.Value)}, - {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1, delPk1)}, - {"other", ""}, + {"ValidatorSigningInfo", fmt.Sprintf("%v\n%v", info, info), false}, + {"ValidatorMissedBlockBitArray", fmt.Sprintf("missedA: %v\nmissedB: %v", missed.Value, missed.Value), false}, + {"AddrPubkeyRelation", fmt.Sprintf("PubKeyA: %s\nPubKeyB: %s", delPk1, delPk1), false}, + {"other", "", true}, } for i, tt := range tests { i, tt := i, tt t.Run(tt.name, func(t *testing.T) { - if i == len(tests)-1 { + if tt.panics { require.Panics(t, func() { dec(kvPairs.Pairs[i], kvPairs.Pairs[i]) }, tt.name) } else { require.Equal(t, tt.expectedLog, dec(kvPairs.Pairs[i], kvPairs.Pairs[i]), tt.name) diff --git a/x/staking/client/cli/cli_test.go b/x/staking/client/cli/cli_test.go index 86ce427b2401..83fcfadd02a5 100644 --- a/x/staking/client/cli/cli_test.go +++ b/x/staking/client/cli/cli_test.go @@ -1,4 +1,4 @@ -// build norace +// +build norace package cli_test From 6848e6309e34ebbbc997cea2914616caf554c377 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Sat, 16 Jan 2021 00:07:15 +0100 Subject: [PATCH 70/91] adding missing return --- server/tm_cmds.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index ef2cca9eecd4..4dd41aac6342 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -31,6 +31,7 @@ func ShowNodeIDCmd() *cobra.Command { return err } fmt.Println(nodeKey.ID()) + return nil }, } } From d37b1c5ce2eb6a196a6f2c9fb20f80e78acde362 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Sat, 16 Jan 2021 11:57:25 +0100 Subject: [PATCH 71/91] errors: use IsOf instead of AsOf --- client/keys/show.go | 2 +- types/errors/errors.go | 8 ++++---- types/errors/errors_test.go | 24 ++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/client/keys/show.go b/client/keys/show.go index c9cbb9ec3d0a..07aa35d5d128 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -150,7 +150,7 @@ func fetchKey(kb keyring.Keyring, keyref string) (keyring.Info, error) { info, err := kb.Key(keyref) // if the key is not there or if we have a problem with a keyring itself then we move to a // fallback: searching for key by address. - if err == nil || !sdkerr.AsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) { + if err == nil || !sdkerr.IsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) { return info, err } accAddr, err := sdk.AccAddressFromBech32(keyref) diff --git a/types/errors/errors.go b/types/errors/errors.go index c978998a4163..a491911d9335 100644 --- a/types/errors/errors.go +++ b/types/errors/errors.go @@ -362,11 +362,11 @@ func WithType(err error, obj interface{}) error { return Wrap(err, fmt.Sprintf("%T", obj)) } -// AsOf checks if a received error is caused by one of the target errors. -// It extends the errors.As functionality to a list of errors. -func AsOf(received error, targets ...error) bool { +// IsOf checks if a received error is caused by one of the target errors. +// It extends the errors.Is functionality to a list of errors. +func IsOf(received error, targets ...error) bool { for _, t := range targets { - if errors.As(received, t) { + if errors.Is(received, t) { return true } } diff --git a/types/errors/errors_test.go b/types/errors/errors_test.go index 7b92c66a975b..b9d3c2afcaa0 100644 --- a/types/errors/errors_test.go +++ b/types/errors/errors_test.go @@ -152,22 +152,22 @@ func (s *errorsTestSuite) TestErrorIs() { func (s *errorsTestSuite) TestIsOf() { require := s.Require() - var errNil error - var err = errors.New("some error") + var errNil *Error + var err = ErrInvalidAddress var errW = Wrap(ErrLogic, "more info") - require.False(AsOf(errNil), "nil error should always have no causer") - require.False(AsOf(errNil, err), "nil error should always have no causer") - require.False(AsOf(errNil, err), "nil error should always have no causer") + require.False(IsOf(errNil), "nil error should always have no causer") + require.False(IsOf(errNil, err), "nil error should always have no causer") - require.False(AsOf(err)) - require.False(AsOf(err, nil)) - require.False(AsOf(err, ErrLogic)) - require.False(AsOf(err, errW)) + require.False(IsOf(err)) + require.False(IsOf(err, nil)) + require.False(IsOf(err, ErrLogic)) - require.True(AsOf(errW, ErrLogic)) - require.True(AsOf(errW, err, ErrLogic)) - require.True(AsOf(errW, nil, errW), "error should much itsel") + require.True(IsOf(errW, ErrLogic)) + require.True(IsOf(errW, err, ErrLogic)) + require.True(IsOf(errW, nil, errW), "error should much itsel") + var err2 = errors.New("other error") + require.True(IsOf(err2, nil, err2), "error should much itsel") } type customError struct { From f667fe482a897ad95916930d76c9d98929fe61eb Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Sat, 16 Jan 2021 12:42:02 +0100 Subject: [PATCH 72/91] keyring: add a correct check for key not found in keyring.Get --- crypto/keyring/keyring.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index d1b84c65ff9b..0f54166049f0 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -530,6 +530,9 @@ func (ks keystore) Key(uid string) (Info, error) { bs, err := ks.db.Get(string(key)) if err != nil { + if err == keyring.ErrKeyNotFound { + return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, uid) + } return nil, err } if len(bs.Data) == 0 { From 15648453e6eca8df79a26e86cf402c87c1a5f475 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 18 Jan 2021 17:13:47 +0100 Subject: [PATCH 73/91] add checkKeyNotFound --- crypto/keyring/keyring.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 0f54166049f0..512b3e905c45 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -428,19 +428,21 @@ func (ks keystore) Delete(uid string) error { func (ks keystore) KeyByAddress(address sdk.Address) (Info, error) { ik, err := ks.db.Get(addrHexKeyAsString(address)) if err != nil { - return nil, err + return nil, checkKeyNotFound(err, fmt.Sprint("key with address", address, "not found")) } if len(ik.Data) == 0 { - return nil, fmt.Errorf("key with address %s not found", address) + return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprint("key with address", address, "not found")) } - bs, err := ks.db.Get(string(ik.Data)) - if err != nil { - return nil, err - } + return ks.Key(string(ik.Data)) +} - return unmarshalInfo(bs.Data) +func checkKeyNotFound(err error, msg string) error { + if err == keyring.ErrKeyNotFound { + return sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, msg) + } + return err } func (ks keystore) List() ([]Info, error) { @@ -530,10 +532,7 @@ func (ks keystore) Key(uid string) (Info, error) { bs, err := ks.db.Get(string(key)) if err != nil { - if err == keyring.ErrKeyNotFound { - return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, uid) - } - return nil, err + return nil, checkKeyNotFound(err, uid) } if len(bs.Data) == 0 { return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, uid) @@ -756,13 +755,13 @@ func (ks keystore) writeInfo(info Info) error { func (ks keystore) existsInDb(info Info) (bool, error) { if _, err := ks.db.Get(addrHexKeyAsString(info.GetAddress())); err == nil { return true, nil // address lookup succeeds - info exists - } else if err != keyring.ErrKeyNotFound { + } else if !sdkerrors.ErrKeyNotFound.Is(err) { return false, err // received unexpected error - returns error } if _, err := ks.db.Get(string(infoKey(info.GetName()))); err == nil { return true, nil // uid lookup succeeds - info exists - } else if err != keyring.ErrKeyNotFound { + } else if !sdkerrors.ErrKeyNotFound.Is(err) { return false, err // received unexpected error - returns } From a7cc7944ec4a039252b182c322be080e62db75aa Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 9 Mar 2021 14:18:56 +0100 Subject: [PATCH 74/91] fix linter issues --- types/errors/errors_test.go | 4 ++-- x/auth/legacy/legacytx/amino_signing.go | 1 + x/auth/legacy/legacytx/stdsign.go | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/types/errors/errors_test.go b/types/errors/errors_test.go index 8dceef6fd2f5..c72344e4f1e9 100644 --- a/types/errors/errors_test.go +++ b/types/errors/errors_test.go @@ -165,9 +165,9 @@ func (s *errorsTestSuite) TestIsOf() { require.True(IsOf(errW, ErrLogic)) require.True(IsOf(errW, err, ErrLogic)) - require.True(IsOf(errW, nil, errW), "error should much itsel") + require.True(IsOf(errW, nil, errW), "error should much itself") var err2 = errors.New("other error") - require.True(IsOf(err2, nil, err2), "error should much itsel") + require.True(IsOf(err2, nil, err2), "error should much itself") } type customError struct { diff --git a/x/auth/legacy/legacytx/amino_signing.go b/x/auth/legacy/legacytx/amino_signing.go index d09b1b30ac47..dc5998b0cff3 100644 --- a/x/auth/legacy/legacytx/amino_signing.go +++ b/x/auth/legacy/legacytx/amino_signing.go @@ -1,3 +1,4 @@ +// nolint: staticcheck package legacytx import ( diff --git a/x/auth/legacy/legacytx/stdsign.go b/x/auth/legacy/legacytx/stdsign.go index ba753683f9ec..149a9aa2fd83 100644 --- a/x/auth/legacy/legacytx/stdsign.go +++ b/x/auth/legacy/legacytx/stdsign.go @@ -121,7 +121,7 @@ func pubKeySigToSigData(cdc *codec.LegacyAmino, key cryptotypes.PubKey, sig []by multiPK, ok := key.(multisig.PubKey) if !ok { return &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, // nolint: staticcheck Signature: sig, }, nil } From 705143eb2d13a4ccc64a01deef28812ef40ec7df Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 9 Mar 2021 17:53:47 +0100 Subject: [PATCH 75/91] fix: keyring key not found check --- crypto/keyring/keyring.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index c9d01d699830..5a4b76546352 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -448,7 +448,7 @@ func (ks keystore) Delete(uid string) error { func (ks keystore) KeyByAddress(address sdk.Address) (Info, error) { ik, err := ks.db.Get(addrHexKeyAsString(address)) if err != nil { - return nil, checkKeyNotFound(err, fmt.Sprint("key with address", address, "not found")) + return nil, wrapKeyNotFound(err, fmt.Sprint("key with address", address, "not found")) } if len(ik.Data) == 0 { @@ -458,7 +458,7 @@ func (ks keystore) KeyByAddress(address sdk.Address) (Info, error) { return ks.Key(string(ik.Data)) } -func checkKeyNotFound(err error, msg string) error { +func wrapKeyNotFound(err error, msg string) error { if err == keyring.ErrKeyNotFound { return sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, msg) } @@ -563,7 +563,7 @@ func (ks keystore) Key(uid string) (Info, error) { bs, err := ks.db.Get(string(key)) if err != nil { - return nil, checkKeyNotFound(err, uid) + return nil, wrapKeyNotFound(err, uid) } if len(bs.Data) == 0 { return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, uid) @@ -741,7 +741,6 @@ func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) { func (ks keystore) writeLocalKey(name string, priv types.PrivKey, algo hd.PubKeyType) (Info, error) { // encrypt private key using keyring pub := priv.PubKey() - info := newLocalInfo(name, pub, string(legacy.Cdc.MustMarshalBinaryBare(priv)), algo) if err := ks.writeInfo(info); err != nil { return nil, err @@ -759,7 +758,6 @@ func (ks keystore) writeInfo(info Info) error { if exists { return errors.New("public key already exist in keybase") } - if err != nil { return err } @@ -786,13 +784,13 @@ func (ks keystore) writeInfo(info Info) error { func (ks keystore) existsInDb(info Info) (bool, error) { if _, err := ks.db.Get(addrHexKeyAsString(info.GetAddress())); err == nil { return true, nil // address lookup succeeds - info exists - } else if !sdkerrors.ErrKeyNotFound.Is(err) { + } else if err != keyring.ErrKeyNotFound { return false, err // received unexpected error - returns error } if _, err := ks.db.Get(string(infoKey(info.GetName()))); err == nil { return true, nil // uid lookup succeeds - info exists - } else if !sdkerrors.ErrKeyNotFound.Is(err) { + } else if err != keyring.ErrKeyNotFound { return false, err // received unexpected error - returns } From e3ef9b83f01966af256eb61bbbca711fc5656a5c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 10 Mar 2021 18:08:15 +0100 Subject: [PATCH 76/91] fix keyring tests --- crypto/keyring/keyring.go | 36 ++++++++++++++++--------------- crypto/keyring/legacy.go | 9 ++++---- x/slashing/client/cli/cli_test.go | 1 - 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 5a4b76546352..a54ba767846b 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -437,7 +437,7 @@ func (ks keystore) Delete(uid string) error { return err } - err = ks.db.Remove(string(infoKey(uid))) + err = ks.db.Remove(infoKey(uid)) if err != nil { return err } @@ -452,10 +452,9 @@ func (ks keystore) KeyByAddress(address sdk.Address) (Info, error) { } if len(ik.Data) == 0 { - return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprint("key with address", address, "not found")) + return nil, wrapKeyNotFound(err, fmt.Sprint("key with address", address, "not found")) } - - return ks.Key(string(ik.Data)) + return ks.key(string(ik.Data)) } func wrapKeyNotFound(err error, msg string) error { @@ -531,7 +530,7 @@ func (ks keystore) NewMnemonic(uid string, language Language, hdPath, bip39Passp return info, mnemonic, nil } -func (ks keystore) NewAccount(uid string, mnemonic string, bip39Passphrase string, hdPath string, algo SignatureAlgo) (Info, error) { +func (ks keystore) NewAccount(name string, mnemonic string, bip39Passphrase string, hdPath string, algo SignatureAlgo) (Info, error) { if !ks.isSupportedSigningAlgo(algo) { return nil, ErrUnsupportedSigningAlgo } @@ -551,26 +550,28 @@ func (ks keystore) NewAccount(uid string, mnemonic string, bip39Passphrase strin return nil, fmt.Errorf("account with address %s already exists in keyring, delete the key first if you want to recreate it", address) } - return ks.writeLocalKey(uid, privKey, algo.Name()) + return ks.writeLocalKey(name, privKey, algo.Name()) } func (ks keystore) isSupportedSigningAlgo(algo SignatureAlgo) bool { return ks.options.SupportedAlgos.Contains(algo) } -func (ks keystore) Key(uid string) (Info, error) { - key := infoKey(uid) - - bs, err := ks.db.Get(string(key)) +func (ks keystore) key(infoKey string) (Info, error) { + bs, err := ks.db.Get(infoKey) if err != nil { - return nil, wrapKeyNotFound(err, uid) + return nil, wrapKeyNotFound(err, infoKey) } if len(bs.Data) == 0 { - return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, uid) + return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, infoKey) } return unmarshalInfo(bs.Data) } +func (ks keystore) Key(uid string) (Info, error) { + return ks.key(infoKey(uid)) +} + // SupportedAlgorithms returns the keystore Options' supported signing algorithm. // for the keyring and Ledger. func (ks keystore) SupportedAlgorithms() (SigningAlgoList, SigningAlgoList) { @@ -750,17 +751,16 @@ func (ks keystore) writeLocalKey(name string, priv types.PrivKey, algo hd.PubKey } func (ks keystore) writeInfo(info Info) error { - // write the info by key - key := infoKey(info.GetName()) + key := infoKeyBz(info.GetName()) serializedInfo := marshalInfo(info) exists, err := ks.existsInDb(info) - if exists { - return errors.New("public key already exist in keybase") - } if err != nil { return err } + if exists { + return errors.New("public key already exist in keybase") + } err = ks.db.Set(keyring.Item{ Key: string(key), @@ -781,6 +781,8 @@ func (ks keystore) writeInfo(info Info) error { return nil } +// existsInDb returns true if key is in DB. Error is returned only when we have error +// different thant ErrKeyNotFound func (ks keystore) existsInDb(info Info) (bool, error) { if _, err := ks.db.Get(addrHexKeyAsString(info.GetAddress())); err == nil { return true, nil // address lookup succeeds - info exists diff --git a/crypto/keyring/legacy.go b/crypto/keyring/legacy.go index 94bff8232f42..00ca8bfcc3df 100644 --- a/crypto/keyring/legacy.go +++ b/crypto/keyring/legacy.go @@ -85,7 +85,7 @@ func (kb dbKeybase) List() ([]Info, error) { // Get returns the public information about one key. func (kb dbKeybase) Get(name string) (Info, error) { - bs, err := kb.db.Get(infoKey(name)) + bs, err := kb.db.Get(infoKeyBz(name)) if err != nil { return nil, err } @@ -129,7 +129,7 @@ func (kb dbKeybase) ExportPrivateKeyObject(name string, passphrase string) (type } func (kb dbKeybase) Export(name string) (armor string, err error) { - bz, err := kb.db.Get(infoKey(name)) + bz, err := kb.db.Get(infoKeyBz(name)) if err != nil { return "", err } @@ -144,7 +144,7 @@ func (kb dbKeybase) Export(name string) (armor string, err error) { // 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) { - bz, err := kb.db.Get(infoKey(name)) + bz, err := kb.db.Get(infoKeyBz(name)) if err != nil { return "", err } @@ -182,7 +182,8 @@ func (kb dbKeybase) ExportPrivKey(name string, decryptPassphrase string, // Close the underlying storage. func (kb dbKeybase) Close() error { return kb.db.Close() } -func infoKey(name string) []byte { return []byte(fmt.Sprintf("%s.%s", name, infoSuffix)) } +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) diff --git a/x/slashing/client/cli/cli_test.go b/x/slashing/client/cli/cli_test.go index a2a9eb4fa198..84be17b76987 100644 --- a/x/slashing/client/cli/cli_test.go +++ b/x/slashing/client/cli/cli_test.go @@ -145,7 +145,6 @@ slash_fraction_downtime: "0.010000000000000000"`, func (s *IntegrationTestSuite) TestNewUnjailTxCmd() { val := s.network.Validators[0] - testCases := []struct { name string args []string From 51f764db4a2ae0b2cf64676f3cc5aa952ce18fdf Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 10 Mar 2021 20:53:49 +0100 Subject: [PATCH 77/91] fix linting issues --- crypto/keyring/keyring.go | 2 +- server/rosetta/client_offline.go | 4 ++-- x/auth/client/cli/tx_multisign.go | 2 +- x/auth/legacy/legacytx/amino_signing.go | 1 - x/auth/legacy/legacytx/stdsign.go | 2 +- x/bank/legacy/v042/store.go | 2 +- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index a54ba767846b..329a4ad7f148 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -790,7 +790,7 @@ func (ks keystore) existsInDb(info Info) (bool, error) { return false, err // received unexpected error - returns error } - if _, err := ks.db.Get(string(infoKey(info.GetName()))); err == nil { + if _, err := ks.db.Get(infoKey(info.GetName())); err == nil { return true, nil // uid lookup succeeds - info exists } else if err != keyring.ErrKeyNotFound { return false, err // received unexpected error - returns diff --git a/server/rosetta/client_offline.go b/server/rosetta/client_offline.go index f619bfc6d2cf..4d5f1734278d 100644 --- a/server/rosetta/client_offline.go +++ b/server/rosetta/client_offline.go @@ -87,7 +87,7 @@ func (c *Client) SignedTx(ctx context.Context, txBytes []byte, signatures []*typ sig := signing.SignatureV2{ PubKey: pubKey, Data: &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, //nolint:staticcheck Signature: signature.Bytes, }, Sequence: accountInfo.GetSequence(), @@ -139,7 +139,7 @@ func (c *Client) ConstructionPayload(_ context.Context, request *types.Construct // needs the signer infos to be set before hand but rosetta doesn't have a way // to do this yet. To be revisited in future versions of sdk and rosetta if txFactory.SignMode() == signing.SignMode_SIGN_MODE_UNSPECIFIED { - txFactory = txFactory.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) + txFactory = txFactory.WithSignMode(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) //nolint:staticcheck } signerData := authsigning.SignerData{ diff --git a/x/auth/client/cli/tx_multisign.go b/x/auth/client/cli/tx_multisign.go index f606a28bee19..395cc089aed5 100644 --- a/x/auth/client/cli/tx_multisign.go +++ b/x/auth/client/cli/tx_multisign.go @@ -234,7 +234,7 @@ func makeBatchMultisignCmd() func(cmd *cobra.Command, args []string) error { txCfg := clientCtx.TxConfig txFactory := tx.NewFactoryCLI(clientCtx, cmd.Flags()) if txFactory.SignMode() == signingtypes.SignMode_SIGN_MODE_UNSPECIFIED { - txFactory = txFactory.WithSignMode(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) + txFactory = txFactory.WithSignMode(signingtypes.SignMode_SIGN_MODE_LEGACY_AMINO_JSON) //nolint:staticcheck } var infile = os.Stdin diff --git a/x/auth/legacy/legacytx/amino_signing.go b/x/auth/legacy/legacytx/amino_signing.go index dc5998b0cff3..d09b1b30ac47 100644 --- a/x/auth/legacy/legacytx/amino_signing.go +++ b/x/auth/legacy/legacytx/amino_signing.go @@ -1,4 +1,3 @@ -// nolint: staticcheck package legacytx import ( diff --git a/x/auth/legacy/legacytx/stdsign.go b/x/auth/legacy/legacytx/stdsign.go index 149a9aa2fd83..ba753683f9ec 100644 --- a/x/auth/legacy/legacytx/stdsign.go +++ b/x/auth/legacy/legacytx/stdsign.go @@ -121,7 +121,7 @@ func pubKeySigToSigData(cdc *codec.LegacyAmino, key cryptotypes.PubKey, sig []by multiPK, ok := key.(multisig.PubKey) if !ok { return &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, // nolint: staticcheck + SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, Signature: sig, }, nil } diff --git a/x/bank/legacy/v042/store.go b/x/bank/legacy/v042/store.go index cc0d9d6f5ae9..20c5b988111e 100644 --- a/x/bank/legacy/v042/store.go +++ b/x/bank/legacy/v042/store.go @@ -14,7 +14,7 @@ import ( // ref: https://github.com/cosmos/cosmos-sdk/issues/7092 func migrateSupply(store sdk.KVStore, cdc codec.BinaryMarshaler) error { // Old supply was stored as a single blob under the SupplyKey. - var oldSupply types.Supply // nolint:staticcheck + var oldSupply types.Supply err := cdc.UnmarshalBinaryBare(store.Get(v040bank.SupplyKey), &oldSupply) if err != nil { return err From 17a47a274ecd14f077a725a9152d880c3838fe98 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 10 Mar 2021 21:14:53 +0100 Subject: [PATCH 78/91] cli tests --- client/keys/delete_test.go | 2 +- crypto/keyring/keyring_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/keys/delete_test.go b/client/keys/delete_test.go index 76f8c211f264..d5f8c1df30b5 100644 --- a/client/keys/delete_test.go +++ b/client/keys/delete_test.go @@ -49,7 +49,7 @@ func Test_runDeleteCmd(t *testing.T) { err = cmd.ExecuteContext(ctx) require.Error(t, err) - require.Equal(t, "The specified item could not be found in the keyring", err.Error()) + require.EqualError(t, err, "blah.info: key not found") // User confirmation missing cmd.SetArgs([]string{ diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index c9f387e78e41..2f8e928e7a7e 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -367,7 +367,7 @@ func TestKeyringKeybaseExportImportPrivKey(t *testing.T) { // try export non existing key _, err = kb.ExportPrivKeyArmor("john3", "wrongpassword") - require.Equal(t, "The specified item could not be found in the keyring", err.Error()) + require.EqualError(t, err, "john3.info: key not found") } func TestInMemoryLanguage(t *testing.T) { From 61cba59ec7499cb1fc94918b3460763d5dd2610a Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 10 Mar 2021 21:48:06 +0100 Subject: [PATCH 79/91] fix: 'simd keys show -p' --- client/keys/show.go | 10 +++++++++- client/keys/utils.go | 9 --------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/client/keys/show.go b/client/keys/show.go index 07aa35d5d128..85d1ac4eab7a 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -116,7 +116,15 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) { switch { case isShowAddr, isShowPubKey: - printInfo(cmd.OutOrStdout(), info, bechKeyOut) + ko, err := bechKeyOut(info) + if err != nil { + return err + } + out := ko.Address + if isShowPubKey { + out = ko.PubKey + } + fmt.Fprintln(cmd.OutOrStdout(), out) default: printKeyInfo(cmd.OutOrStdout(), info, bechKeyOut, output) } diff --git a/client/keys/utils.go b/client/keys/utils.go index a9dcb9669597..46a7327f6ce9 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -78,12 +78,3 @@ func printTextInfos(w io.Writer, kos []cryptokeyring.KeyOutput) { } fmt.Fprintln(w, string(out)) } - -func printInfo(w io.Writer, info cryptokeyring.Info, bechKeyOut bechKeyOutFn) { - ko, err := bechKeyOut(info) - if err != nil { - panic(err) - } - - fmt.Fprintln(w, ko.Address) -} From 74e2b130b4e53ce81f0f2a0f5ed25709a7c12724 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 10 Mar 2021 22:38:08 +0100 Subject: [PATCH 80/91] fix: TestVerifyMultisignature --- crypto/keys/multisig/multisig_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index d875937d07a3..9d7f7d605acc 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -109,7 +109,7 @@ func TestVerifyMultisignature(t *testing.T) { }, { "wrong size for sig bit array", func(require *require.Assertions) { - pubKeys := generatePubKeys(3, msg) + pubKeys := generatePubKeys(3) pk = kmultisig.NewLegacyAminoPubKey(3, pubKeys) sig = multisig.NewMultisig(1) }, @@ -200,7 +200,7 @@ func TestVerifyMultisignature(t *testing.T) { }, { "unable to verify signature", func(require *require.Assertions) { - pubKeys := generatePubKeys(2, msg) + pubKeys := generatePubKeys(2) _, sigs := generatePubKeysAndSignatures(2, msg) pk = kmultisig.NewLegacyAminoPubKey(2, pubKeys) sig = multisig.NewMultisig(2) From bccf755c7aa80fd84f3ef2aa829969dfafc0b255 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 11 Mar 2021 01:39:15 +0100 Subject: [PATCH 81/91] rename keyring Bech32... functions to Mk... --- client/keys/add.go | 4 ++-- client/keys/show.go | 6 +++--- client/keys/show_test.go | 6 +++--- client/keys/utils.go | 2 +- crypto/keyring/output.go | 20 ++++++++++---------- crypto/keyring/output_test.go | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index e3028894934c..423747fb3528 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -299,7 +299,7 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo switch output { case OutputFormatText: cmd.PrintErrln() - printKeyInfo(cmd.OutOrStdout(), info, keyring.Bech32KeyOutput, output) + printKeyInfo(cmd.OutOrStdout(), info, keyring.MkKeyOutput, output) // print mnemonic unless requested not to. if showMnemonic { @@ -309,7 +309,7 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo fmt.Fprintln(cmd.ErrOrStderr(), mnemonic) } case OutputFormatJSON: - out, err := keyring.Bech32KeyOutput(info) + out, err := keyring.MkKeyOutput(info) if err != nil { return err } diff --git a/client/keys/show.go b/client/keys/show.go index 85d1ac4eab7a..3627bcd1bfab 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -184,11 +184,11 @@ func validateMultisigThreshold(k, nKeys int) error { func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) { switch bechPrefix { case sdk.PrefixAccount: - return keyring.Bech32KeyOutput, nil + return keyring.MkKeyOutput, nil case sdk.PrefixValidator: - return keyring.Bech32ValKeyOutput, nil + return keyring.MkValKeyOutput, nil case sdk.PrefixConsensus: - return keyring.Bech32ConsKeyOutput, nil + return keyring.MkConsKeyOutput, nil } return nil, fmt.Errorf("invalid Bech32 prefix encoding provided: %s", bechPrefix) diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 2c8cfd2f170c..687d04b4e4c0 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -194,9 +194,9 @@ func Test_getBechKeyOut(t *testing.T) { }{ {"empty", args{""}, nil, true}, {"wrong", args{"???"}, nil, true}, - {"acc", args{sdk.PrefixAccount}, keyring.Bech32KeyOutput, false}, - {"val", args{sdk.PrefixValidator}, keyring.Bech32ValKeyOutput, false}, - {"cons", args{sdk.PrefixConsensus}, keyring.Bech32ConsKeyOutput, false}, + {"acc", args{sdk.PrefixAccount}, keyring.MkKeyOutput, false}, + {"val", args{sdk.PrefixValidator}, keyring.MkValKeyOutput, false}, + {"cons", args{sdk.PrefixConsensus}, keyring.MkConsKeyOutput, false}, } for _, tt := range tests { tt := tt diff --git a/client/keys/utils.go b/client/keys/utils.go index 46a7327f6ce9..5d8d34e6c8b3 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -52,7 +52,7 @@ func printKeyInfo(w io.Writer, keyInfo cryptokeyring.Info, bechKeyOut bechKeyOut } func printInfos(w io.Writer, infos []cryptokeyring.Info, output string) { - kos, err := cryptokeyring.Bech32KeysOutput(infos) + kos, err := cryptokeyring.MkKeysOutput(infos) if err != nil { panic(err) } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index c2af424da405..1bbda15bdd69 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -33,14 +33,14 @@ func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.Pu }, nil } -// Bech32KeysOutput returns a slice of KeyOutput objects, each with the "acc" +// MkKeysOutput returns a slice of KeyOutput objects, each with the "acc" // Bech32 prefixes, given a slice of Info objects. It returns an error if any -// call to Bech32KeyOutput fails. -func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) { +// call to MkKeyOutput fails. +func MkKeysOutput(infos []Info) ([]KeyOutput, error) { kos := make([]KeyOutput, len(infos)) var err error for i, info := range infos { - kos[i], err = Bech32KeyOutput(info) + kos[i], err = MkKeyOutput(info) if err != nil { return nil, err } @@ -49,24 +49,24 @@ func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) { return kos, nil } -// Bech32ConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes. -func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) { +// MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes. +func MkConsKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.ConsAddress(pk.Address().Bytes()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } -// Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes. -func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) { +// MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes. +func MkValKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.ValAddress(pk.Address().Bytes()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } -// Bech32KeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the +// MkKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the // public key is a multisig public key, then the threshold and constituent // public keys will be added. -func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) { +func MkKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.AccAddress(pk.Address().Bytes()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index c6eda5a97cfc..4183314448bf 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -25,7 +25,7 @@ func TestBech32KeysOutput(t *testing.T) { require.NoError(t, err) outputs, err := Bech32KeysOutput([]Info{multiInfo}) - require.NoError(t, err) + require.NoError(MkKeysOutput require.Equal(t, expectedOutput, outputs[0]) require.Len(t, outputs, 1) require.Equal(t, `{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{"threshold":1,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}]} Mnemonic:}`, fmt.Sprintf("%+v", outputs[0])) From e58f6a8f5b4465d472c7229cabcadb46112ad135 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 23 Mar 2021 10:48:06 +0100 Subject: [PATCH 82/91] fix RunAddCmd --- client/keys/add.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index d2e19eb2f342..6b1b2a47a605 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -92,7 +92,7 @@ func runAddCmdPrepare(cmd *cobra.Command, args []string) error { return err } - return RunAddCmd(cmd, args, clientCtx.Keyring, buf) + return RunAddCmd(clientCtx, cmd, args, buf) } /* @@ -104,13 +104,15 @@ input output - armor encrypted private key (saved to file) */ -func runAddCmd(ctx client.Context, cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error { +func RunAddCmd(ctx client.Context, cmd *cobra.Command, args []string, inBuf *bufio.Reader) error { + // func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *bufio.Reader) error { var err error name := args[0] interactive, _ := cmd.Flags().GetBool(flagInteractive) noBackup, _ := cmd.Flags().GetBool(flagNoBackup) showMnemonic := !noBackup + kb := ctx.Keyring keyringAlgos, _ := kb.SupportedAlgorithms() algoStr, _ := cmd.Flags().GetString(flags.FlagKeyAlgorithm) From a9a0a55f794be248dd3868499dd2ac3276e65a52 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 23 Mar 2021 11:04:34 +0100 Subject: [PATCH 83/91] Update pubkey display --- codec/any_test.go | 4 ++-- crypto/keyring/output.go | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/codec/any_test.go b/codec/any_test.go index 60a14bfd8a8e..ff80d8ee427c 100644 --- a/codec/any_test.go +++ b/codec/any_test.go @@ -74,8 +74,8 @@ func TestMarshalProtoPubKey(t *testing.T) { var pkAny2 codectypes.Any err = ccfg.Marshaler.UnmarshalJSON(bz, &pkAny2) require.NoError(err) - // we before getting a cached value we need to unpack it. - // Normally this happens in in types which implement UnpackInterfaces + // Before getting a cached value we need to unpack it. + // Normally this happens in types which implement UnpackInterfaces var pkI cryptotypes.PubKey err = ccfg.InterfaceRegistry.UnpackAny(&pkAny2, &pkI) require.NoError(err) diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 1bbda15bdd69..5872ba5b797e 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -2,6 +2,7 @@ package keyring import ( "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -21,7 +22,11 @@ type KeyOutput struct { // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { // nolint:interfacer - bz, err := codec.ProtoMarshalJSON(pk, nil) + apk, err := codectypes.NewAnyWithValue(pk) + if err != nil { + return KeyOutput{}, err + } + bz, err := codec.ProtoMarshalJSON(apk, nil) if err != nil { return KeyOutput{}, err } From 65485a5aecc06734600d30bd52bda129038a7134 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 23 Mar 2021 11:14:06 +0100 Subject: [PATCH 84/91] wip --- codec/json.go | 1 - crypto/keys/multisig/multisig_test.go | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/codec/json.go b/codec/json.go index b7325c67c10f..e01caa259936 100644 --- a/codec/json.go +++ b/codec/json.go @@ -26,7 +26,6 @@ func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, e } buf := new(bytes.Buffer) - if err := jm.Marshal(buf, msg); err != nil { return nil, err } diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 26b010c3e188..3cf62c795fd4 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -427,5 +427,7 @@ func TestAminoUnmarshalJSON(t *testing.T) { var pk cryptotypes.PubKey err := cdc.UnmarshalJSON([]byte(pkJSON), &pk) require.NoError(t, err) - require.Equal(t, uint32(3), pk.(*kmultisig.LegacyAminoPubKey).Threshold) + lpk := pk.(*kmultisig.LegacyAminoPubKey) + require.Equal(t, uint32(3), lpk.Threshold) + } From eaea2f43514135c4156a91a539d03e181b51b63e Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 23 Mar 2021 13:56:33 +0100 Subject: [PATCH 85/91] add more tests --- crypto/keyring/output.go | 6 +-- crypto/keys/ed25519/ed25519_test.go | 20 ++++++++ crypto/keys/multisig/multisig_test.go | 47 ++++++++++++++----- .../keys/secp256r1/privkey_internal_test.go | 4 +- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 5872ba5b797e..3238958d8dec 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -57,14 +57,14 @@ func MkKeysOutput(infos []Info) ([]KeyOutput, error) { // MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes. func MkConsKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() - addr := sdk.ConsAddress(pk.Address().Bytes()) + addr := sdk.ConsAddress(pk.Address()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } // MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes. func MkValKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() - addr := sdk.ValAddress(pk.Address().Bytes()) + addr := sdk.ValAddress(pk.Address()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } @@ -73,6 +73,6 @@ func MkValKeyOutput(keyInfo Info) (KeyOutput, error) { // public keys will be added. func MkKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() - addr := sdk.AccAddress(pk.Address().Bytes()) + addr := sdk.AccAddress(pk.Address()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 4647f379f4f6..44b003fbefe4 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -11,6 +11,8 @@ import ( tmed25519 "github.com/tendermint/tendermint/crypto/ed25519" "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -234,3 +236,21 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { }) } } + +func TestMarshalJSON(t *testing.T) { + require := require.New(t) + privKey := ed25519.GenPrivKey() + pk := privKey.PubKey() + + registry := types.NewInterfaceRegistry() + cryptocodec.RegisterInterfaces(registry) + cdc := codec.NewProtoCodec(registry) + + bz, err := cdc.MarshalInterfaceJSON(pk) + require.NoError(err) + + var pk2 cryptotypes.PubKey + err = cdc.UnmarshalInterfaceJSON(bz, &pk2) + require.NoError(err) + require.True(pk2.Equals(pk)) +} diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 3cf62c795fd4..69c6a0334b9d 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -10,11 +10,13 @@ import ( "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/crypto/keyring" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" "github.com/cosmos/cosmos-sdk/simapp" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx" ) @@ -359,24 +361,21 @@ func TestDisplay(t *testing.T) { } func TestAminoBinary(t *testing.T) { - pubKey1 := secp256k1.GenPrivKey().PubKey() - pubKey2 := secp256k1.GenPrivKey().PubKey() - multisigKey := kmultisig.NewLegacyAminoPubKey(2, []cryptotypes.PubKey{pubKey1, pubKey2}) + pubkeys := generatePubKeys(2) + msig := kmultisig.NewLegacyAminoPubKey(2, pubkeys) // Do a round-trip key->bytes->key. - bz, err := legacy.Cdc.MarshalBinaryBare(multisigKey) + bz, err := legacy.Cdc.MarshalBinaryBare(msig) require.NoError(t, err) - var newMultisigKey cryptotypes.PubKey - err = legacy.Cdc.UnmarshalBinaryBare(bz, &newMultisigKey) + var newMsig cryptotypes.PubKey + err = legacy.Cdc.UnmarshalBinaryBare(bz, &newMsig) require.NoError(t, err) - require.Equal(t, multisigKey.Threshold, newMultisigKey.(*kmultisig.LegacyAminoPubKey).Threshold) + require.Equal(t, msig.Threshold, newMsig.(*kmultisig.LegacyAminoPubKey).Threshold) } func TestAminoMarshalJSON(t *testing.T) { - pubKey1 := secp256k1.GenPrivKey().PubKey() - pubKey2 := secp256k1.GenPrivKey().PubKey() - multisigKey := kmultisig.NewLegacyAminoPubKey(2, []cryptotypes.PubKey{pubKey1, pubKey2}) - + pubkeys := generatePubKeys(2) + multisigKey := kmultisig.NewLegacyAminoPubKey(2, pubkeys) bz, err := legacy.Cdc.MarshalJSON(multisigKey) require.NoError(t, err) @@ -429,5 +428,31 @@ func TestAminoUnmarshalJSON(t *testing.T) { require.NoError(t, err) lpk := pk.(*kmultisig.LegacyAminoPubKey) require.Equal(t, uint32(3), lpk.Threshold) +} + +func TestProtoMarshalJSON(t *testing.T) { + require := require.New(t) + pubkeys := generatePubKeys(3) + msig := kmultisig.NewLegacyAminoPubKey(2, pubkeys) + registry := types.NewInterfaceRegistry() + cryptocodec.RegisterInterfaces(registry) + cdc := codec.NewProtoCodec(registry) + + bz, err := cdc.MarshalInterfaceJSON(msig) + require.NoError(err) + + var pk2 cryptotypes.PubKey + err = cdc.UnmarshalInterfaceJSON(bz, &pk2) + require.NoError(err) + require.True(pk2.Equals(msig)) + + // Test that we can correctly unmarshal key from keyring output + + info, err := keyring.NewMultiInfo("my multisig", msig) + require.NoError(err) + ko, err := keyring.MkKeyOutput(info) + require.NoError(err) + require.Equal(ko.Address, sdk.AccAddress(pk2.Address()).String()) + require.Equal(ko.PubKey, string(bz)) } diff --git a/crypto/keys/secp256r1/privkey_internal_test.go b/crypto/keys/secp256r1/privkey_internal_test.go index ae48b47e3d68..d00040f7aa56 100644 --- a/crypto/keys/secp256r1/privkey_internal_test.go +++ b/crypto/keys/secp256r1/privkey_internal_test.go @@ -3,13 +3,13 @@ package secp256r1 import ( "testing" + proto "github.com/gogo/protobuf/proto" + "github.com/stretchr/testify/suite" "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - proto "github.com/gogo/protobuf/proto" - "github.com/stretchr/testify/suite" ) var _ cryptotypes.PrivKey = &PrivKey{} From ba700c4cf82ea82836524b97b80aee72dd5641af Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 23 Mar 2021 14:58:38 +0100 Subject: [PATCH 86/91] udate keyring output tests --- crypto/keyring/output.go | 32 ++++++++++++++++---------------- crypto/keyring/output_test.go | 16 +++++++--------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 3238958d8dec..8a283e502e95 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -38,22 +38,6 @@ func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.Pu }, nil } -// MkKeysOutput returns a slice of KeyOutput objects, each with the "acc" -// Bech32 prefixes, given a slice of Info objects. It returns an error if any -// call to MkKeyOutput fails. -func MkKeysOutput(infos []Info) ([]KeyOutput, error) { - kos := make([]KeyOutput, len(infos)) - var err error - for i, info := range infos { - kos[i], err = MkKeyOutput(info) - if err != nil { - return nil, err - } - } - - return kos, nil -} - // MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes. func MkConsKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() @@ -76,3 +60,19 @@ func MkKeyOutput(keyInfo Info) (KeyOutput, error) { addr := sdk.AccAddress(pk.Address()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } + +// MkKeysOutput returns a slice of KeyOutput objects, each with the "acc" +// Bech32 prefixes, given a slice of Info objects. It returns an error if any +// call to MkKeyOutput fails. +func MkKeysOutput(infos []Info) ([]KeyOutput, error) { + kos := make([]KeyOutput, len(infos)) + var err error + for i, info := range infos { + kos[i], err = MkKeyOutput(info) + if err != nil { + return nil, err + } + } + + return kos, nil +} diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index 4183314448bf..dbe47de351c6 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -17,16 +17,14 @@ func TestBech32KeysOutput(t *testing.T) { tmpKey := sk.PubKey() multisigPk := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey}) - multiInfo, err := NewMultiInfo("multisig", multisigPk) + info, err := NewMultiInfo("multisig", multisigPk) require.NoError(t, err) - accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes()) - - expectedOutput, err := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType(), accAddr, multisigPk) + accAddr := sdk.AccAddress(info.GetPubKey().Address()) + expectedOutput, err := NewKeyOutput(info.GetName(), info.GetType(), accAddr, multisigPk) require.NoError(t, err) - outputs, err := Bech32KeysOutput([]Info{multiInfo}) - require.NoError(MkKeysOutput - require.Equal(t, expectedOutput, outputs[0]) - require.Len(t, outputs, 1) - require.Equal(t, `{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{"threshold":1,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}]} Mnemonic:}`, fmt.Sprintf("%+v", outputs[0])) + out, err := MkKeyOutput(info) + require.NoError(t, err) + require.Equal(t, expectedOutput, out) + require.Equal(t, `{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{"@type":"/cosmos.crypto.multisig.LegacyAminoPubKey","threshold":1,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}]} Mnemonic:}`, fmt.Sprintf("%+v", out)) } From d6e76d78df4c0c123b03d63f5c34f7b3901cec54 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 23 Mar 2021 18:47:29 +0100 Subject: [PATCH 87/91] remove todo from ledger tests --- crypto/ledger/ledger_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index b33aebd2b25b..6583484ff06d 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -110,7 +110,6 @@ func TestPublicKeySafe(t *testing.T) { } func TestPublicKeyHDPath(t *testing.T) { - // TODO: convert this tests to not use bech32 expectedPubKeys := []string{ "cosmospub1addwnpepqd87l8xhcnrrtzxnkql7k55ph8fr9jarf4hn6udwukfprlalu8lgw0urza0", "cosmospub1addwnpepqfsdqjr68h7wjg5wacksmqaypasnra232fkgu5sxdlnlu8j22ztxvlqvd65", @@ -142,8 +141,8 @@ func TestPublicKeyHDPath(t *testing.T) { privKeys := make([]types.LedgerPrivKey, numIters) // Check with device - for i := uint32(0); i < 10; i++ { - path := *hd.NewFundraiserParams(0, sdk.CoinType, i) + for i := 0; i < len(expectedAddrs); i++ { + path := *hd.NewFundraiserParams(0, sdk.CoinType, uint32(i)) t.Logf("Checking keys at %s\n", path) priv, addr, err := NewPrivKeySecp256k1(path, "cosmos") From 192999e135ba0b59dd36accc8ddc74c4e612b3c1 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 24 Mar 2021 14:13:44 +0100 Subject: [PATCH 88/91] rename MkKeyOutput --- client/keys/add.go | 4 ++-- client/keys/show.go | 2 +- client/keys/show_test.go | 2 +- client/keys/utils.go | 2 +- crypto/keyring/output.go | 10 +++++----- crypto/keyring/output_test.go | 2 +- crypto/keys/multisig/multisig_test.go | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/client/keys/add.go b/client/keys/add.go index 6b1b2a47a605..4176da632ddb 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -287,7 +287,7 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo switch output { case OutputFormatText: cmd.PrintErrln() - printKeyInfo(cmd.OutOrStdout(), info, keyring.MkKeyOutput, output) + printKeyInfo(cmd.OutOrStdout(), info, keyring.MkAccKeyOutput, output) // print mnemonic unless requested not to. if showMnemonic { @@ -297,7 +297,7 @@ func printCreate(cmd *cobra.Command, info keyring.Info, showMnemonic bool, mnemo fmt.Fprintln(cmd.ErrOrStderr(), mnemonic) } case OutputFormatJSON: - out, err := keyring.MkKeyOutput(info) + out, err := keyring.MkAccKeyOutput(info) if err != nil { return err } diff --git a/client/keys/show.go b/client/keys/show.go index 3627bcd1bfab..8c19f8797679 100644 --- a/client/keys/show.go +++ b/client/keys/show.go @@ -184,7 +184,7 @@ func validateMultisigThreshold(k, nKeys int) error { func getBechKeyOut(bechPrefix string) (bechKeyOutFn, error) { switch bechPrefix { case sdk.PrefixAccount: - return keyring.MkKeyOutput, nil + return keyring.MkAccKeyOutput, nil case sdk.PrefixValidator: return keyring.MkValKeyOutput, nil case sdk.PrefixConsensus: diff --git a/client/keys/show_test.go b/client/keys/show_test.go index 687d04b4e4c0..9405a6f84b6f 100644 --- a/client/keys/show_test.go +++ b/client/keys/show_test.go @@ -194,7 +194,7 @@ func Test_getBechKeyOut(t *testing.T) { }{ {"empty", args{""}, nil, true}, {"wrong", args{"???"}, nil, true}, - {"acc", args{sdk.PrefixAccount}, keyring.MkKeyOutput, false}, + {"acc", args{sdk.PrefixAccount}, keyring.MkAccKeyOutput, false}, {"val", args{sdk.PrefixValidator}, keyring.MkValKeyOutput, false}, {"cons", args{sdk.PrefixConsensus}, keyring.MkConsKeyOutput, false}, } diff --git a/client/keys/utils.go b/client/keys/utils.go index 5d8d34e6c8b3..5e1daf1c4346 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -52,7 +52,7 @@ func printKeyInfo(w io.Writer, keyInfo cryptokeyring.Info, bechKeyOut bechKeyOut } func printInfos(w io.Writer, infos []cryptokeyring.Info, output string) { - kos, err := cryptokeyring.MkKeysOutput(infos) + kos, err := cryptokeyring.MkAccKeysOutput(infos) if err != nil { panic(err) } diff --git a/crypto/keyring/output.go b/crypto/keyring/output.go index 8a283e502e95..91588e31379f 100644 --- a/crypto/keyring/output.go +++ b/crypto/keyring/output.go @@ -52,23 +52,23 @@ func MkValKeyOutput(keyInfo Info) (KeyOutput, error) { return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } -// MkKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the +// MkAccKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the // public key is a multisig public key, then the threshold and constituent // public keys will be added. -func MkKeyOutput(keyInfo Info) (KeyOutput, error) { +func MkAccKeyOutput(keyInfo Info) (KeyOutput, error) { pk := keyInfo.GetPubKey() addr := sdk.AccAddress(pk.Address()) return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk) } -// MkKeysOutput returns a slice of KeyOutput objects, each with the "acc" +// MkAccKeysOutput returns a slice of KeyOutput objects, each with the "acc" // Bech32 prefixes, given a slice of Info objects. It returns an error if any // call to MkKeyOutput fails. -func MkKeysOutput(infos []Info) ([]KeyOutput, error) { +func MkAccKeysOutput(infos []Info) ([]KeyOutput, error) { kos := make([]KeyOutput, len(infos)) var err error for i, info := range infos { - kos[i], err = MkKeyOutput(info) + kos[i], err = MkAccKeyOutput(info) if err != nil { return nil, err } diff --git a/crypto/keyring/output_test.go b/crypto/keyring/output_test.go index dbe47de351c6..8f28e8cd1f9f 100644 --- a/crypto/keyring/output_test.go +++ b/crypto/keyring/output_test.go @@ -23,7 +23,7 @@ func TestBech32KeysOutput(t *testing.T) { expectedOutput, err := NewKeyOutput(info.GetName(), info.GetType(), accAddr, multisigPk) require.NoError(t, err) - out, err := MkKeyOutput(info) + out, err := MkAccKeyOutput(info) require.NoError(t, err) require.Equal(t, expectedOutput, out) require.Equal(t, `{Name:multisig Type:multi Address:cosmos1nf8lf6n4wa43rzmdzwe6hkrnw5guekhqt595cw PubKey:{"@type":"/cosmos.crypto.multisig.LegacyAminoPubKey","threshold":1,"public_keys":[{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}]} Mnemonic:}`, fmt.Sprintf("%+v", out)) diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 69c6a0334b9d..03b4f7eb6703 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -451,7 +451,7 @@ func TestProtoMarshalJSON(t *testing.T) { info, err := keyring.NewMultiInfo("my multisig", msig) require.NoError(err) - ko, err := keyring.MkKeyOutput(info) + ko, err := keyring.MkAccKeyOutput(info) require.NoError(err) require.Equal(ko.Address, sdk.AccAddress(pk2.Address()).String()) require.Equal(ko.PubKey, string(bz)) From d0875005ac57c06e4545b7aa073fcb9b6390a109 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 24 Mar 2021 14:16:01 +0100 Subject: [PATCH 89/91] Changelog update --- CHANGELOG.md | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59c5c7d91716..ca6ed12ae5de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,8 +48,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#8363](https://github.com/cosmos/cosmos-sdk/pull/8363) Addresses no longer have a fixed 20-byte length. From the SDK modules' point of view, any 1-255 bytes-long byte array is a valid address. * [\#8346](https://github.com/cosmos/cosmos-sdk/pull/8346) All CLI `tx` commands generate ServiceMsgs by default. Graceful Amino support has been added to ServiceMsgs to support signing legacy Msgs. * (crypto/ed25519) [\#8690] Adopt zip1215 ed2559 verification rules. -* [\#8849](https://github.com/cosmos/cosmos-sdk/pull/8849) Upgrade module no longer supports time based upgrades. +* [\#8849](https://github.com/cosmos/cosmos-sdk/pull/8849) Upgrade module no longer supports time based upgrades. * [\#8880](https://github.com/cosmos/cosmos-sdk/pull/8880) The CLI `simd migrate v0.40 ...` command has been renamed to `simd migrate v0.42`. +* [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) Changed Bech32 Public Key serialization in the client facing functionality (CLI, MsgServer, QueryServer): + * updated the keyring display structure (it uses protobuf JSON serialization) - the output is more verbose. + * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take an interface as parameter (not a concrete type nor `Any` object). Underneath they use `Any` wrapping for correct protobuf serialization. + * CLI: removed `--text` flag from `show-node-id` command; the text format for public keys is not used any more - instead we use ProtoJSON. ### API Breaking Changes @@ -66,6 +70,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/upgrade) [\#8743](https://github.com/cosmos/cosmos-sdk/pull/8743) `UpgradeHandler` includes a new argument `VersionMap` which helps facilitate in-place migrations. * (x/auth) [\#8129](https://github.com/cosmos/cosmos-sdk/pull/8828) Updated `SigVerifiableTx.GetPubKeys` method signature to return error. * [\#8682](https://github.com/cosmos/cosmos-sdk/pull/8682) `ante.NewAnteHandler` updated to receive all positional params as `ante.HandlerOptions` struct. If required fields aren't set, throws error accordingly. +* (x/staking/types) [\#7447](https://github.com/cosmos/cosmos-sdk/issues/7447) Remove bech32 PubKey support: + * `ValidatorI` interface update: `GetConsPubKey` renamed to `TmConsPubKey` (this is to clarify the return type: consensus public key must be a tendermint key); `TmConsPubKey`, `GetConsAddr` methods return error. + * `Validator` updated according to the `ValidatorI` changes described above. + * `ToTmValidator` function: added `error` to return values. + * `Validator.ConsensusPubkey` type changed from `string` to `codectypes.Any`. + * `MsgCreateValidator.Pubkey` type changed from `string` to `codectypes.Any`. ### State Machine Breaking @@ -79,7 +89,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements -* (x/staking) [\#8909](https://github.com/cosmos/cosmos-sdk/pull/8909) Require self delegation in `MsgCreateValidator` to be at least one consensus power. +* (x/staking) [\#8909](https://github.com/cosmos/cosmos-sdk/pull/8909) Require self delegation in `MsgCreateValidator` to be at least one consensus power. * (x/bank) [\#8614](https://github.com/cosmos/cosmos-sdk/issues/8614) Add `Name` and `Symbol` fields to denom metadata * (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts * (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method. @@ -194,24 +204,6 @@ he Cosmos Hub) should not use this release or any release in the v0.41.x series. * (x/bank) [\#8317](https://github.com/cosmos/cosmos-sdk/pull/8317) Fix panic when querying for a not found client denomination metadata. - -### Client Breaking - -* [\#7477](https://github.com/cosmos/cosmos-sdk/pull/7477) Changed Bech32 Public Key serialization in the client facing functionality (CLI, MsgServer, QueryServer): - * updated the keyring display structure (it uses protobuf JSON serialization) - the output is more verbose. - * Renamed `MarshalAny` and `UnmarshalAny` to `MarshalInterface` and `UnmarshalInterface` respectively. These functions must take an interface as parameter (not a concrete type nor `Any` object). Underneath they use `Any` wrapping for correct protobuf serialization. - * CLI: removed `--text` flag from `show-node-id` command; the text format for public keys is not used any more - instead we use ProtoJSON. - -### API Breaking - -* (x/staking/types) [\#7447](https://github.com/cosmos/cosmos-sdk/issues/7447) Remove bech32 PubKey support: - * `ValidatorI` interface update: `GetConsPubKey` renamed to `TmConsPubKey` (this is to clarify the return type: consensus public key must be a tendermint key); `TmConsPubKey`, `GetConsAddr` methods return error. - * `Validator` updated according to the `ValidatorI` changes described above. - * `ToTmValidator` function: added `error` to return values. - * `Validator.ConsensusPubkey` type changed from `string` to `codectypes.Any`. - * `MsgCreateValidator.Pubkey` type changed from `string` to `codectypes.Any`. - - ## [v0.40.0](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.40.0) - 2021-01-08 v0.40.0, known as the Stargate release of the Cosmos SDK, is one of the largest releases From 3ff91ccdaed28beeed5f32b6e18c52383aa4abbb Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 24 Mar 2021 14:24:20 +0100 Subject: [PATCH 90/91] solve liner issues --- server/rosetta/converter.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/rosetta/converter.go b/server/rosetta/converter.go index 43c9460014fc..2c7282e4851f 100644 --- a/server/rosetta/converter.go +++ b/server/rosetta/converter.go @@ -118,7 +118,7 @@ func NewConverter(cdc *codec.ProtoCodec, ir codectypes.InterfaceRegistry, cfg sd txDecode: cfg.TxDecoder(), txEncode: cfg.TxEncoder(), bytesToSign: func(tx authsigning.Tx, signerData authsigning.SignerData) (b []byte, err error) { - bytesToSign, err := cfg.SignModeHandler().GetSignBytes(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx) + bytesToSign, err := cfg.SignModeHandler().GetSignBytes(signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, signerData, tx) //nolint:staticcheck if err != nil { return nil, err } @@ -662,7 +662,7 @@ func (c converter) SignedTx(txBytes []byte, signatures []*rosettatypes.Signature signedSigs[i] = signing.SignatureV2{ PubKey: notSignedSigs[i].PubKey, Data: &signing.SingleSignatureData{ - SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, + SignMode: signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON, //nolint:staticcheck Signature: signature.Bytes, }, Sequence: notSignedSigs[i].Sequence, From b0b8f59e07d02cda98f00c7f4f40268f9794f586 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 24 Mar 2021 15:19:23 +0100 Subject: [PATCH 91/91] add link to github issue --- client/keys/codec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/keys/codec.go b/client/keys/codec.go index 30ebcea40226..3b1f6fc38b3c 100644 --- a/client/keys/codec.go +++ b/client/keys/codec.go @@ -5,7 +5,7 @@ import ( cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" ) -// TODO: remove this file #8047 +// TODO: remove this file https://github.com/cosmos/cosmos-sdk/issues/8047 // KeysCdc defines codec to be used with key operations var KeysCdc *codec.LegacyAmino