From b2e8feb61f036fdb747f7937685a22c9b0fa30ff Mon Sep 17 00:00:00 2001 From: Qt Date: Tue, 5 Mar 2024 21:36:10 +0800 Subject: [PATCH] refactor(crypto): unify the error handling methods in the crypto package that are different from the project style (#19650) --- crypto/armor.go | 2 +- crypto/hd/hdpath.go | 3 ++- crypto/keyring/errors.go | 8 ++++---- crypto/keyring/keyring.go | 10 +++++----- crypto/keyring/keyring_ledger_test.go | 2 +- crypto/keyring/keyring_test.go | 4 ++-- crypto/keyring/legacy_info.go | 9 +++++---- crypto/keyring/record.go | 2 +- crypto/keyring/signing_algorithms.go | 4 ++-- crypto/keys/ed25519/ed25519.go | 7 ++++--- crypto/keys/secp256k1/secp256k1.go | 7 ++++--- x/tx/signing/textual/any.go | 2 +- 12 files changed, 32 insertions(+), 28 deletions(-) diff --git a/crypto/armor.go b/crypto/armor.go index 47eba69d8a..d7bebe96e1 100644 --- a/crypto/armor.go +++ b/crypto/armor.go @@ -241,7 +241,7 @@ func decryptPrivKey(saltBytes, encBytes []byte, passphrase, kdf string) (privKey key = crypto.Sha256(key) // Get 32 bytes privKeyBytes, err = xsalsa20symmetric.DecryptSymmetric(encBytes, key) - if err == xsalsa20symmetric.ErrCiphertextDecrypt { + if errors.Is(err, xsalsa20symmetric.ErrCiphertextDecrypt) { return privKey, sdkerrors.ErrWrongPassword } default: diff --git a/crypto/hd/hdpath.go b/crypto/hd/hdpath.go index 4c0529e832..9ef9961d12 100644 --- a/crypto/hd/hdpath.go +++ b/crypto/hd/hdpath.go @@ -4,6 +4,7 @@ import ( "crypto/hmac" "crypto/sha512" "encoding/binary" + "errors" "fmt" "math/big" "path/filepath" @@ -88,7 +89,7 @@ func NewParamsFromPath(path string) (*BIP44Params, error) { } if !(change == 0 || change == 1) { - return nil, fmt.Errorf("change field can only be 0 or 1") + return nil, errors.New("change field can only be 0 or 1") } return &BIP44Params{ diff --git a/crypto/keyring/errors.go b/crypto/keyring/errors.go index 3ebbb24094..13ea72356c 100644 --- a/crypto/keyring/errors.go +++ b/crypto/keyring/errors.go @@ -1,6 +1,6 @@ package keyring -import "github.com/cockroachdb/errors" +import "errors" var ( // ErrUnsupportedSigningAlgo is raised when the caller tries to use a @@ -14,8 +14,8 @@ var ( // ErrOverwriteKey is raised when a key cannot be overwritten ErrOverwriteKey = errors.New("cannot overwrite key") // ErrKeyAlreadyExists is raised when creating a key that already exists - ErrKeyAlreadyExists = errors.Newf("key already exists") - // ErrInvalidSignMode is raised when trying to sign with an invaled method + ErrKeyAlreadyExists = errors.New("key already exists") + // ErrInvalidSignMode is raised when trying to sign with an invalid method ErrInvalidSignMode = errors.New("invalid sign mode, expected LEGACY_AMINO_JSON or TEXTUAL") // ErrMaxPassPhraseAttempts is raised when the maxPassphraseEntryAttempts is reached ErrMaxPassPhraseAttempts = errors.New("too many failed passphrase attempts") @@ -30,7 +30,7 @@ var ( // ErrNotLedgerObj is raised when record.GetLedger() returns nil. ErrNotLedgerObj = errors.New("not a ledger object") // ErrLedgerInvalidSignature is raised when ledger generates an invalid signature. - ErrLedgerInvalidSignature = errors.New("Ledger generated an invalid signature. Perhaps you have multiple ledgers and need to try another one") + ErrLedgerInvalidSignature = errors.New("ledger generated an invalid signature. Perhaps you have multiple ledgers and need to try another one") // ErrLegacyToRecord is raised when cannot be converted to a Record ErrLegacyToRecord = errors.New("unable to convert LegacyInfo to Record") // ErrUnknownLegacyType is raised when a LegacyInfo type is unknown. diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 22673e06fa..e46db74693 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -3,6 +3,7 @@ package keyring import ( "bufio" "encoding/hex" + "errors" "fmt" "io" "os" @@ -11,7 +12,6 @@ import ( "strings" "github.com/99designs/keyring" - "github.com/cockroachdb/errors" "github.com/cosmos/go-bip39" "golang.org/x/crypto/bcrypt" @@ -435,7 +435,7 @@ func (ks keystore) SaveLedgerKey(uid string, algo SignatureAlgo, hrp string, coi priv, _, err := ledger.NewPrivKeySecp256k1(*hdPath, hrp) if err != nil { - return nil, errors.CombineErrors(ErrLedgerGenerateKey, err) + return nil, errorsmod.Wrap(ErrLedgerGenerateKey, err.Error()) } return ks.writeLedgerKey(uid, priv.PubKey(), hdPath) @@ -534,7 +534,7 @@ func (ks keystore) KeyByAddress(address []byte) (*Record, error) { } func wrapKeyNotFound(err error, msg string) error { - if err == keyring.ErrKeyNotFound { + if errors.Is(err, keyring.ErrKeyNotFound) { return errorsmod.Wrap(sdkerrors.ErrKeyNotFound, msg) } return err @@ -822,7 +822,7 @@ func (ks keystore) writeRecord(k *Record) error { serializedRecord, err := ks.cdc.Marshal(k) if err != nil { - return errors.CombineErrors(ErrUnableToSerialize, err) + return errorsmod.Wrap(ErrUnableToSerialize, err.Error()) } item := keyring.Item{ @@ -977,7 +977,7 @@ func (ks keystore) migrate(key string) (*Record, error) { serializedRecord, err := ks.cdc.Marshal(k) if err != nil { - return nil, errors.CombineErrors(ErrUnableToSerialize, err) + return nil, errorsmod.Wrap(ErrUnableToSerialize, err.Error()) } item = keyring.Item{ diff --git a/crypto/keyring/keyring_ledger_test.go b/crypto/keyring/keyring_ledger_test.go index ca1b79bfbf..89f1013b64 100644 --- a/crypto/keyring/keyring_ledger_test.go +++ b/crypto/keyring/keyring_ledger_test.go @@ -5,9 +5,9 @@ package keyring import ( "bytes" + "errors" "testing" - "github.com/cockroachdb/errors" "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/crypto/hd" diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index acd4c6d7f3..142360dd7f 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -1119,7 +1119,7 @@ func TestNewAccount(t *testing.T) { bip39Passphrease: "", algo: hd.Secp256k1, mnemonic: "fresh enact fresh ski large bicycle marine abandon motor end pact mixture annual elite bind fan write warrior adapt common manual cool happy dutch", - expectedErr: fmt.Errorf("Invalid byte at position"), + expectedErr: errors.New("invalid byte at position"), }, { name: "in memory invalid mnemonic", @@ -1129,7 +1129,7 @@ func TestNewAccount(t *testing.T) { bip39Passphrease: "", algo: hd.Secp256k1, mnemonic: "malarkey pair crucial catch public canyon evil outer stage ten gym tornado", - expectedErr: fmt.Errorf("Invalid mnemonic"), + expectedErr: errors.New("invalid mnemonic"), }, } for _, tt := range tests { diff --git a/crypto/keyring/legacy_info.go b/crypto/keyring/legacy_info.go index 7bdaad0e5b..47cbe3e9a9 100644 --- a/crypto/keyring/legacy_info.go +++ b/crypto/keyring/legacy_info.go @@ -1,6 +1,7 @@ package keyring import ( + "errors" "fmt" "github.com/cosmos/cosmos-sdk/codec/legacy" @@ -77,7 +78,7 @@ func (i legacyLocalInfo) GetAlgo() hd.PubKeyType { // GetPath returns bip44 path, but not available for this type func (i legacyLocalInfo) GetPath() (*hd.BIP44Params, error) { - return nil, fmt.Errorf("BIP44 Paths are not available for this type") + return nil, errors.New("BIP44 Paths are not available for this type") } // legacyLedgerInfo is the public information about a Ledger key @@ -155,7 +156,7 @@ func (i legacyOfflineInfo) GetAddress() sdk.AccAddress { // GetPath returns bip44 path, but not available for this type func (i legacyOfflineInfo) GetPath() (*hd.BIP44Params, error) { - return nil, fmt.Errorf("BIP44 Paths are not available for this type") + return nil, errors.New("BIP44 Paths are not available for this type") } // Deprecated: this structure is not used anymore and it's here only to allow @@ -213,7 +214,7 @@ func (i LegacyMultiInfo) GetAlgo() hd.PubKeyType { // GetPath returns bip44 path, but not available for this type func (i LegacyMultiInfo) GetPath() (*hd.BIP44Params, error) { - return nil, fmt.Errorf("BIP44 Paths are not available for this type") + return nil, errors.New("BIP44 Paths are not available for this type") } // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces @@ -258,7 +259,7 @@ func privKeyFromLegacyInfo(info LegacyInfo) (cryptotypes.PrivKey, error) { switch linfo := info.(type) { case legacyLocalInfo: if linfo.PrivKeyArmor == "" { - return nil, fmt.Errorf("private key not available") + return nil, errors.New("private key not available") } priv, err := legacy.PrivKeyFromBytes([]byte(linfo.PrivKeyArmor)) if err != nil { diff --git a/crypto/keyring/record.go b/crypto/keyring/record.go index c461f7f6a4..96141e4c90 100644 --- a/crypto/keyring/record.go +++ b/crypto/keyring/record.go @@ -1,7 +1,7 @@ package keyring import ( - "github.com/cockroachdb/errors" + "errors" errorsmod "cosmossdk.io/errors" diff --git a/crypto/keyring/signing_algorithms.go b/crypto/keyring/signing_algorithms.go index 4a22a98a8e..eb5e5d43ae 100644 --- a/crypto/keyring/signing_algorithms.go +++ b/crypto/keyring/signing_algorithms.go @@ -3,7 +3,7 @@ package keyring import ( "strings" - "github.com/cockroachdb/errors" + errorsmod "cosmossdk.io/errors" "github.com/cosmos/cosmos-sdk/crypto/hd" ) @@ -22,7 +22,7 @@ func NewSigningAlgoFromString(str string, algoList SigningAlgoList) (SignatureAl return algo, nil } } - return nil, errors.Wrap(ErrUnsupportedSigningAlgo, str) + return nil, errorsmod.Wrap(ErrUnsupportedSigningAlgo, str) } // SigningAlgoList is a slice of signature algorithms diff --git a/crypto/keys/ed25519/ed25519.go b/crypto/keys/ed25519/ed25519.go index b0c28052a8..8ded87ec3f 100644 --- a/crypto/keys/ed25519/ed25519.go +++ b/crypto/keys/ed25519/ed25519.go @@ -3,6 +3,7 @@ package ed25519 import ( "crypto/ed25519" "crypto/subtle" + "errors" "fmt" "io" @@ -14,7 +15,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) //------------------------------------- @@ -102,7 +103,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) { // UnmarshalAmino overrides Amino binary marshaling. func (privKey *PrivKey) UnmarshalAmino(bz []byte) error { if len(bz) != PrivKeySize { - return fmt.Errorf("invalid privkey size") + return errors.New("invalid privkey size") } privKey.Key = bz @@ -211,7 +212,7 @@ func (pubKey PubKey) MarshalAmino() ([]byte, error) { // UnmarshalAmino overrides Amino binary marshaling. func (pubKey *PubKey) UnmarshalAmino(bz []byte) error { if len(bz) != PubKeySize { - return errorsmod.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size") + return errorsmod.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size") } pubKey.Key = bz diff --git a/crypto/keys/secp256k1/secp256k1.go b/crypto/keys/secp256k1/secp256k1.go index b995eb6334..4073684076 100644 --- a/crypto/keys/secp256k1/secp256k1.go +++ b/crypto/keys/secp256k1/secp256k1.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/sha256" "crypto/subtle" + "errors" "fmt" "io" "math/big" @@ -17,7 +18,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) var ( @@ -66,7 +67,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) { // UnmarshalAmino overrides Amino binary marshaling. func (privKey *PrivKey) UnmarshalAmino(bz []byte) error { if len(bz) != PrivKeySize { - return fmt.Errorf("invalid privkey size") + return errors.New("invalid privkey size") } privKey.Key = bz @@ -202,7 +203,7 @@ func (pubKey PubKey) MarshalAmino() ([]byte, error) { // UnmarshalAmino overrides Amino binary marshaling. func (pubKey *PubKey) UnmarshalAmino(bz []byte) error { if len(bz) != PubKeySize { - return errorsmod.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size") + return errorsmod.Wrap(sdkerrors.ErrInvalidPubKey, "invalid pubkey size") } pubKey.Key = bz diff --git a/x/tx/signing/textual/any.go b/x/tx/signing/textual/any.go index 5f187f3033..3770969dc5 100644 --- a/x/tx/signing/textual/any.go +++ b/x/tx/signing/textual/any.go @@ -83,7 +83,7 @@ func (ar anyValueRenderer) Parse(ctx context.Context, screens []Screen) (protore typeURL := screens[0].Content msgType, err := ar.tr.typeResolver.FindMessageByURL(typeURL) - if err == protoregistry.NotFound { + if errors.Is(err, protoregistry.NotFound) { // If the proto v2 registry doesn't have this message, then we use // protoFiles (which can e.g. be initialized to gogo's MergedRegistry) // to retrieve the message descriptor, and then use dynamicpb on that