diff --git a/errors.go b/errors.go index eb0bd5d4..b9eb5740 100644 --- a/errors.go +++ b/errors.go @@ -16,7 +16,7 @@ var ErrMissingXpriv = models.SPVError{Message: "xpriv missing", StatusCode: 401, var ErrCouldNotFindDraftTransaction = models.SPVError{Message: "could not find draft transaction", StatusCode: 404, Code: "error-draft-transaction-not-found"} // WrapError wraps an error into SPVError -func WrapError(err error) *models.SPVError { +func WrapError(err error) error { if err == nil { return nil } @@ -29,7 +29,7 @@ func WrapError(err error) *models.SPVError { } // WrapResponseError wraps a http response into SPVError -func WrapResponseError(res *http.Response) *models.SPVError { +func WrapResponseError(res *http.Response) error { if res == nil { return nil } @@ -48,7 +48,7 @@ func WrapResponseError(res *http.Response) *models.SPVError { } } -func CreateErrorResponse(code string, message string) *models.SPVError { +func CreateErrorResponse(code string, message string) error { return &models.SPVError{ StatusCode: http.StatusInternalServerError, Code: code, diff --git a/examples/go.mod b/examples/go.mod index cc54b458..7b7b0bd8 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -6,7 +6,7 @@ replace github.com/bitcoin-sv/spv-wallet-go-client => ../ require ( github.com/bitcoin-sv/spv-wallet-go-client v0.0.0-00010101000000-000000000000 - github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6 + github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15 ) require ( diff --git a/examples/go.sum b/examples/go.sum index 76d7b5ab..99f6fce5 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -2,6 +2,8 @@ github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 h1:rBscs3Gbz0RWY03eI3Z9Aw github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6 h1:ZTEHuSNbXszs+5TKN0uiW6DY7JdWIM5m6NQpBJZyre4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6/go.mod h1:u3gnRDS3uHWZNM2qbYATTpN+mAphyozCJrYIKGwBX7k= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15 h1:Qjp9gSe1XlBwADgDlkaIGuzqNoQwktu1DuB6tzurdQI= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15/go.mod h1:Ni6SFkmMjV39Bg4FtlgPAsnsiJUfRDVEPlbzTZa8z40= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= diff --git a/examples/handle_exceptions/handle_exceptions.go b/examples/handle_exceptions/handle_exceptions.go index 62437a87..29fcfeea 100644 --- a/examples/handle_exceptions/handle_exceptions.go +++ b/examples/handle_exceptions/handle_exceptions.go @@ -5,11 +5,13 @@ package main import ( "context" + "errors" "fmt" "os" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" "github.com/bitcoin-sv/spv-wallet-go-client/examples" + "github.com/bitcoin-sv/spv-wallet/models" ) func main() { @@ -24,8 +26,12 @@ func main() { status, err := client.AdminGetStatus(ctx) if err != nil { - fmt.Println("Response status: ", err.GetStatusCode()) - fmt.Println("Content: ", err.Error()) + var extendedErr models.ExtendedError + if errors.As(err, &extendedErr) { + fmt.Printf("Extended error: [%d] '%s': %s\n", extendedErr.GetStatusCode(), extendedErr.GetCode(), extendedErr.GetMessage()) + } else { + fmt.Println("Error: ", err.Error()) + } os.Exit(1) } diff --git a/go.mod b/go.mod index 6bef1b16..523c5d7f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bitcoin-sv/spv-wallet-go-client go 1.22.4 require ( - github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6 + github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15 github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 github.com/libsv/go-bk v0.1.6 github.com/libsv/go-bt/v2 v2.2.5 @@ -47,5 +47,3 @@ require ( google.golang.org/protobuf v1.34.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) - -replace github.com/bitcoin-sv/spv-wallet/models => ../spv-wallet/models diff --git a/go.sum b/go.sum index 5054e71a..bc4ae1f6 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13 h1:rBscs3Gbz0RWY03eI3Z9Aw github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.13/go.mod h1:i3txysriHpprqYd3u97wEQsC4/jn+KHcyFOmuFYMw8M= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6 h1:ZTEHuSNbXszs+5TKN0uiW6DY7JdWIM5m6NQpBJZyre4= github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.14.0.20240626082725-2c073c5330a6/go.mod h1:u3gnRDS3uHWZNM2qbYATTpN+mAphyozCJrYIKGwBX7k= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15 h1:Qjp9gSe1XlBwADgDlkaIGuzqNoQwktu1DuB6tzurdQI= +github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.15/go.mod h1:Ni6SFkmMjV39Bg4FtlgPAsnsiJUfRDVEPlbzTZa8z40= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck= github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA= github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo= diff --git a/http.go b/http.go index 565ec953..4b0b66de 100644 --- a/http.go +++ b/http.go @@ -7,13 +7,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/bitcoinschema/go-bitcoin/v2" "net/http" "strconv" "github.com/bitcoin-sv/spv-wallet-go-client/utils" "github.com/bitcoin-sv/spv-wallet/models" "github.com/bitcoin-sv/spv-wallet/models/filter" + "github.com/bitcoinschema/go-bitcoin/v2" "github.com/libsv/go-bk/bec" "github.com/libsv/go-bk/bip32" ) @@ -565,6 +565,9 @@ func (wc *WalletClient) authenticateWithXpriv(sign bool, req *http.Request, xPri } func (wc *WalletClient) authenticateWithAccessKey(req *http.Request, rawJSON []byte) error { + if wc.accessKey == nil { + return WrapError(errors.New("access key is missing")) + } return SetSignatureFromAccessKey(&req.Header, hex.EncodeToString(wc.accessKey.Serialise()), string(rawJSON)) }