github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/crypto/keys/keyerror/errors.go (about)

     1  package keyerror
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  const (
     8  	codeKeyNotFound   = 1
     9  	codeWrongPassword = 2
    10  )
    11  
    12  type keybaseError interface {
    13  	error
    14  	Code() int
    15  }
    16  
    17  type errKeyNotFound struct {
    18  	code int
    19  	name string
    20  }
    21  
    22  func (e errKeyNotFound) Code() int {
    23  	return e.code
    24  }
    25  
    26  func (e errKeyNotFound) Error() string {
    27  	return fmt.Sprintf("Key %s not found", e.name)
    28  }
    29  
    30  // NewErrKeyNotFound returns a standardized error reflecting that the specified key doesn't exist
    31  func NewErrKeyNotFound(name string) error {
    32  	return errKeyNotFound{
    33  		code: codeKeyNotFound,
    34  		name: name,
    35  	}
    36  }
    37  
    38  // IsErrKeyNotFound returns true if the given error is errKeyNotFound
    39  func IsErrKeyNotFound(err error) bool {
    40  	if err == nil {
    41  		return false
    42  	}
    43  	if keyErr, ok := err.(keybaseError); ok {
    44  		if keyErr.Code() == codeKeyNotFound {
    45  			return true
    46  		}
    47  	}
    48  	return false
    49  }
    50  
    51  type errWrongPassword struct {
    52  	code int
    53  }
    54  
    55  func (e errWrongPassword) Code() int {
    56  	return e.code
    57  }
    58  
    59  func (e errWrongPassword) Error() string {
    60  	return "invalid account password"
    61  }
    62  
    63  // NewErrWrongPassword returns a standardized error reflecting that the specified password is wrong
    64  func NewErrWrongPassword() error {
    65  	return errWrongPassword{
    66  		code: codeWrongPassword,
    67  	}
    68  }
    69  
    70  // IsErrWrongPassword returns true if the given error is errWrongPassword
    71  func IsErrWrongPassword(err error) bool {
    72  	if err == nil {
    73  		return false
    74  	}
    75  	if keyErr, ok := err.(keybaseError); ok {
    76  		if keyErr.Code() == codeWrongPassword {
    77  			return true
    78  		}
    79  	}
    80  	return false
    81  }