From dee6335c8b5850d26f2ae6e27b2f6cc50a95a7d2 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Sat, 4 Feb 2023 13:23:04 -0500 Subject: [PATCH] Errors are now in one place --- address.go | 13 ++++++------- errors.go | 33 +++++++++++++++++++++++++++++++++ pubkey.go | 3 +-- script.go | 4 +--- sign.go | 3 +-- transaction.go | 5 ++--- 6 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 errors.go diff --git a/address.go b/address.go index 2977cdf..1349c97 100644 --- a/address.go +++ b/address.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/sha256" "encoding/hex" - "errors" "fmt" "github.com/libsv/go-bk/bec" @@ -28,12 +27,12 @@ func (a *A25) doubleSHA256() []byte { return h.Sum(d[:0]) } -// Version returns the version byte of a A25 address +// Version returns the version byte of an A25 address func (a *A25) Version() byte { return a[0] } -// EmbeddedChecksum returns the 4 checksum bytes of a A25 address +// EmbeddedChecksum returns the 4 checksum bytes of an A25 address func (a *A25) EmbeddedChecksum() (c [4]byte) { copy(c[:], a[21:]) return @@ -51,7 +50,7 @@ func (a *A25) Set58(s []byte) error { for _, s1 := range s { c := bytes.IndexByte(tmpl, s1) if c < 0 { - return errors.New("bad char") + return ErrBadCharacter } for j := 24; j >= 0; j-- { c += 58 * int(a[j]) @@ -59,7 +58,7 @@ func (a *A25) Set58(s []byte) error { c /= 256 } if c > 0 { - return errors.New("too long") + return ErrTooLong } } return nil @@ -83,7 +82,7 @@ func ValidA58(a58 []byte) (bool, error) { return false, err } if a.Version() != 0 { - return false, errors.New("not version 0") + return false, ErrNotVersion0 } return a.EmbeddedChecksum() == a.ComputeChecksum(), nil } @@ -148,7 +147,7 @@ func GetAddressFromScript(script string) (string, error) { // No script? if len(script) == 0 { - return "", errors.New("missing script") + return "", ErrMissingScript } // Decode the hex string into bytes diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..662b11c --- /dev/null +++ b/errors.go @@ -0,0 +1,33 @@ +package bitcoin + +import "errors" + +// ErrPrivateKeyMissing is returned when a private key is missing +var ErrPrivateKeyMissing = errors.New("private key is missing") + +// ErrWifMissing is returned when a wif is missing +var ErrWifMissing = errors.New("wif is missing") + +// ErrBadCharacter is returned when a bad character is found +var ErrBadCharacter = errors.New("bad char") + +// ErrTooLong is returned when a string is too long +var ErrTooLong = errors.New("too long") + +// ErrNotVersion0 is returned when a string is not version 0 +var ErrNotVersion0 = errors.New("not version 0") + +// ErrMissingScript is returned when a script is missing +var ErrMissingScript = errors.New("missing script") + +// ErrMissingPubKey is returned when a pubkey is missing +var ErrMissingPubKey = errors.New("missing pubkey") + +// ErrMissingAddress is returned when an address is missing +var ErrMissingAddress = errors.New("missing address") + +// ErrUtxosRequired is returned when utxos are required to create a tx +var ErrUtxosRequired = errors.New("utxo(s) are required to create a tx") + +// ErrChangeAddressRequired is returned when a change address is required to create a tx +var ErrChangeAddressRequired = errors.New("change address is required") diff --git a/pubkey.go b/pubkey.go index 3789ee8..d51185b 100644 --- a/pubkey.go +++ b/pubkey.go @@ -2,7 +2,6 @@ package bitcoin import ( "encoding/hex" - "errors" "github.com/libsv/go-bk/bec" ) @@ -31,7 +30,7 @@ func PubKeyFromString(pubKey string) (*bec.PublicKey, error) { // Invalid pubKey if len(pubKey) == 0 { - return nil, errors.New("missing pubkey") + return nil, ErrMissingPubKey } // Decode from hex string diff --git a/script.go b/script.go index d6548b7..308b25d 100644 --- a/script.go +++ b/script.go @@ -1,8 +1,6 @@ package bitcoin import ( - "errors" - "github.com/libsv/go-bt/v2/bscript" ) @@ -10,7 +8,7 @@ import ( func ScriptFromAddress(address string) (string, error) { // Missing address? if len(address) == 0 { - return "", errors.New("missing address") + return "", ErrMissingAddress } // Generate a script from address diff --git a/sign.go b/sign.go index cfba058..b2128d8 100644 --- a/sign.go +++ b/sign.go @@ -3,7 +3,6 @@ package bitcoin import ( "bytes" "encoding/base64" - "errors" "github.com/bitcoinsv/bsvd/chaincfg/chainhash" "github.com/bitcoinsv/bsvd/wire" @@ -15,7 +14,7 @@ import ( // Spec: https://docs.moneybutton.com/docs/bsv-message.html func SignMessage(privateKey string, message string, sigRefCompressedKey bool) (string, error) { if len(privateKey) == 0 { - return "", errors.New("privateKey is empty") + return "", ErrPrivateKeyMissing } var buf bytes.Buffer diff --git a/transaction.go b/transaction.go index 2602cad..4f57f80 100644 --- a/transaction.go +++ b/transaction.go @@ -2,7 +2,6 @@ package bitcoin import ( "context" - "errors" "fmt" "strings" @@ -63,9 +62,9 @@ func CreateTxWithChange(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns // Missing utxo(s) or change address if len(utxos) == 0 { - return nil, errors.New("utxo(s) are required to create a tx") + return nil, ErrUtxosRequired } else if len(changeAddress) == 0 { - return nil, errors.New("change address is required") + return nil, ErrChangeAddressRequired } // Accumulate the total satoshis from all utxo(s)