Skip to content

Commit

Permalink
Errors are now in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Feb 4, 2023
1 parent 10a1276 commit dee6335
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
13 changes: 6 additions & 7 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"

"github.com/libsv/go-bk/bec"
Expand All @@ -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
Expand All @@ -51,15 +50,15 @@ 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])
a[j] = byte(c % 256)
c /= 256
}
if c > 0 {
return errors.New("too long")
return ErrTooLong
}
}
return nil
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 1 addition & 2 deletions pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bitcoin

import (
"encoding/hex"
"errors"

"github.com/libsv/go-bk/bec"
)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions script.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package bitcoin

import (
"errors"

"github.com/libsv/go-bt/v2/bscript"
)

// ScriptFromAddress will create an output P2PKH script from an address string
func ScriptFromAddress(address string) (string, error) {
// Missing address?
if len(address) == 0 {
return "", errors.New("missing address")
return "", ErrMissingAddress
}

// Generate a script from address
Expand Down
3 changes: 1 addition & 2 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bitcoin
import (
"bytes"
"encoding/base64"
"errors"

"github.com/bitcoinsv/bsvd/chaincfg/chainhash"
"github.com/bitcoinsv/bsvd/wire"
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bitcoin

import (
"context"
"errors"
"fmt"
"strings"

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

0 comments on commit dee6335

Please sign in to comment.