refactor(crypto): unify the error handling methods in the crypto package that are different from the project style (#19650)

This commit is contained in:
Qt 2024-03-05 21:36:10 +08:00 committed by GitHub
parent d37871af50
commit b2e8feb61f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 32 additions and 28 deletions

View File

@ -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:

View File

@ -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{

View File

@ -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.

View File

@ -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{

View File

@ -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"

View File

@ -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 {

View File

@ -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 {

View File

@ -1,7 +1,7 @@
package keyring
import (
"github.com/cockroachdb/errors"
"errors"
errorsmod "cosmossdk.io/errors"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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