Skip to content

Commit

Permalink
refactor(crypto): unify the error handling methods in the crypto pack…
Browse files Browse the repository at this point in the history
…age that are different from the project style (#19650)
  • Loading branch information
0x2d3c authored Mar 5, 2024
1 parent d37871a commit b2e8feb
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion crypto/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion crypto/hd/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/hmac"
"crypto/sha512"
"encoding/binary"
"errors"
"fmt"
"math/big"
"path/filepath"
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions crypto/keyring/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package keyring

import "github.com/cockroachdb/errors"
import "errors"

var (
// ErrUnsupportedSigningAlgo is raised when the caller tries to use a
Expand All @@ -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")
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keyring
import (
"bufio"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
Expand All @@ -11,7 +12,6 @@ import (
"strings"

"github.com/99designs/keyring"
"github.com/cockroachdb/errors"
"github.com/cosmos/go-bip39"
"golang.org/x/crypto/bcrypt"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion crypto/keyring/keyring_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions crypto/keyring/legacy_info.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keyring

import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec/legacy"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crypto/keyring/record.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package keyring

import (
"github.com/cockroachdb/errors"
"errors"

errorsmod "cosmossdk.io/errors"

Expand Down
4 changes: 2 additions & 2 deletions crypto/keyring/signing_algorithms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keyring
import (
"strings"

"github.com/cockroachdb/errors"
errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/crypto/hd"
)
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions crypto/keys/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ed25519
import (
"crypto/ed25519"
"crypto/subtle"
"errors"
"fmt"
"io"

Expand All @@ -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"
)

//-------------------------------------
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions crypto/keys/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/sha256"
"crypto/subtle"
"errors"
"fmt"
"io"
"math/big"
Expand All @@ -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 (
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion x/tx/signing/textual/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b2e8feb

Please sign in to comment.