Skip to content

Commit

Permalink
Merge branch 'main' into feat-896-regression-tests-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski authored Aug 11, 2024
2 parents 4aad98d + f3b5441 commit bd63bec
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 188 deletions.
22 changes: 17 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,30 @@ import (
var ErrAdminKey = models.SPVError{Message: "an admin key must be set to be able to create an xpub", StatusCode: 401, Code: "error-unauthorized-admin-key-not-set"}

// ErrMissingXpriv is when xpriv is missing
var ErrMissingXpriv = models.SPVError{Message: "xpriv missing", StatusCode: 401, Code: "error-unauthorized-xpriv-missing"}
var ErrMissingXpriv = models.SPVError{Message: "xpriv is missing", StatusCode: 401, Code: "error-unauthorized-xpriv-missing"}

// ErrMissingKey is when neither xPriv nor adminXPriv is provided
var ErrMissingKey = models.SPVError{Message: "neither xPriv nor adminXPriv is provided", StatusCode: 404, Code: "error-shared-config-key-missing"}

// ErrMissingAccessKey is when access key is missing
var ErrMissingAccessKey = models.SPVError{Message: "access key is missing", StatusCode: 401, Code: "error-unauthorized-access-key-missing"}

// ErrCouldNotFindDraftTransaction is when draft transaction is not found
var ErrCouldNotFindDraftTransaction = models.SPVError{Message: "could not find draft transaction", StatusCode: 404, Code: "error-draft-transaction-not-found"}

// ErrTotpInvalid is when totp is invalid
var ErrTotpInvalid = models.SPVError{Message: "totp is invalid", StatusCode: 400, Code: "error-totp-invalid"}

// ErrContactPubKeyInvalid is when contact's PubKey is invalid
var ErrContactPubKeyInvalid = models.SPVError{Message: "contact's PubKey is invalid", StatusCode: 400, Code: "error-contact-pubkey-invalid"}

// WrapError wraps an error into SPVError
func WrapError(err error) error {
if err == nil {
return nil
}

return &models.SPVError{
return models.SPVError{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
Code: models.UnknownErrorCode,
Expand All @@ -34,22 +46,22 @@ func WrapResponseError(res *http.Response) error {
return nil
}

var resError *models.ResponseError
var resError models.ResponseError

err := json.NewDecoder(res.Body).Decode(&resError)
if err != nil {
return WrapError(err)
}

return &models.SPVError{
return models.SPVError{
StatusCode: res.StatusCode,
Code: resError.Code,
Message: resError.Message,
}
}

func CreateErrorResponse(code string, message string) error {
return &models.SPVError{
return models.SPVError{
StatusCode: http.StatusInternalServerError,
Code: code,
Message: message,
Expand Down
1 change: 0 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ In this directory you can find examples of how to use the `spv-wallet-go-client`
In addition to the above, there are additional examples showing how to use the client from a developer perspective:

- `handle_exceptions` - presents how to "catch" exceptions which the client can throw
- `custom_logger` - shows different ways you can configure (or disable) internal logger

## Util examples

Expand Down
9 changes: 6 additions & 3 deletions examples/admin_add_user/admin_add_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ func main() {

metadata := map[string]any{"some_metadata": "example"}

newXPubRes := adminClient.AdminNewXpub(ctx, examples.ExampleXPub, metadata)
fmt.Println("AdminNewXpub response: ", newXPubRes)
err := adminClient.AdminNewXpub(ctx, examples.ExampleXPub, metadata)
if err != nil {
examples.GetFullErrorMessage(err)
os.Exit(1)
}

createPaymailRes, err := adminClient.AdminCreatePaymail(ctx, examples.ExampleXPub, examples.ExamplePaymail, "Some public name", "")
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("AdminCreatePaymail response: ", createPaymailRes)
Expand Down
3 changes: 1 addition & 2 deletions examples/admin_remove_user/admin_remove_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"context"
"fmt"
"os"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
Expand All @@ -24,7 +23,7 @@ func main() {

err := adminClient.AdminDeletePaymail(ctx, examples.ExamplePaymail)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
}
6 changes: 3 additions & 3 deletions examples/create_transaction/create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ func main() {
client := walletclient.NewWithXPriv(server, examples.ExampleXPriv)
ctx := context.Background()

recipient := walletclient.Recipients{To: "[email protected]", Satoshis: 1}
recipient := walletclient.Recipients{To: "[email protected]", Satoshis: 1}
recipients := []*walletclient.Recipients{&recipient}
metadata := map[string]any{"some_metadata": "example"}

newTransaction, err := client.SendToRecipients(ctx, recipients, metadata)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("SendToRecipients response: ", newTransaction)

tx, err := client.GetTransaction(ctx, newTransaction.ID)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("GetTransaction response: ", tx)
Expand Down
21 changes: 21 additions & 0 deletions examples/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package examples

import (
"errors"
"fmt"

"github.com/bitcoin-sv/spv-wallet/models"
)

// GetFullErrorMessage prints detailed info about the error
func GetFullErrorMessage(err error) {
var errMsg string

var spvError models.SPVError
if errors.As(err, &spvError) {
errMsg = fmt.Sprintf("Error, Message: %s, Code: %s, HTTP status code: %d", spvError.GetMessage(), spvError.GetCode(), spvError.GetStatusCode())
} else {
errMsg = fmt.Sprintf("Error, Message: %s, Code: %s, HTTP status code: %d", err.Error(), models.UnknownErrorCode, 500)
}
fmt.Println(errMsg)
}
2 changes: 1 addition & 1 deletion examples/get_balance/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

xpubInfo, err := client.GetXPub(ctx)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("Current balance: ", xpubInfo.CurrentBalance)
Expand Down
8 changes: 4 additions & 4 deletions examples/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 6 additions & 14 deletions examples/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions examples/handle_exceptions/handle_exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ 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() {
defer examples.HandlePanic()

fmt.Println("Handle exceptions example")

examples.CheckIfXPubExists()

fmt.Println("XPub exists")

const server = "http://localhost:3003/v1"

client := walletclient.NewWithXPub(server, examples.ExampleXPub)
ctx := context.Background()

fmt.Println("Client created")

status, err := client.AdminGetStatus(ctx)
if err != nil {
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())
}

fmt.Println("Error: ", err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}

Expand Down
7 changes: 4 additions & 3 deletions examples/list_transactions/list_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ func main() {

txs, err := client.GetTransactions(ctx, &conditions, metadata, &queryParams)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("GetTransactions response: ", txs)

conditions = filter.TransactionFilter{BlockHeight: func(i uint64) *uint64 { return &i }(839228)}
targetBlockHeight := uint64(839228)
conditions = filter.TransactionFilter{BlockHeight: &targetBlockHeight}
queryParams = filter.QueryParams{PageSize: 100, Page: 1}

txsFiltered, err := client.GetTransactions(ctx, &conditions, metadata, &queryParams)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("Filtered GetTransactions response: ", txsFiltered)
Expand Down
6 changes: 3 additions & 3 deletions examples/send_op_return/send_op_return.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ func main() {

draftTransaction, err := client.DraftTransaction(ctx, &transactionConfig, metadata)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("DraftTransaction response: ", draftTransaction)

finalized, err := client.FinalizeTransaction(draftTransaction)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
transaction, err := client.RecordTransaction(ctx, finalized, draftTransaction.ID, metadata)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}
fmt.Println("Transaction with OP_RETURN: ", transaction)
Expand Down
3 changes: 2 additions & 1 deletion examples/xpriv_from_mnemonic/xpriv_from_mnemonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"fmt"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"os"

"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
Expand All @@ -16,7 +17,7 @@ func main() {

keys, err := xpriv.FromMnemonic(mnemonicPhrase)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}

Expand Down
3 changes: 2 additions & 1 deletion examples/xpub_from_xpriv/xpub_from_xpriv.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"fmt"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
"os"

"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
Expand All @@ -16,7 +17,7 @@ func main() {

keys, err := xpriv.FromString(xPriv)
if err != nil {
fmt.Println(err)
examples.GetFullErrorMessage(err)
os.Exit(1)
}

Expand Down
35 changes: 4 additions & 31 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bd63bec

Please sign in to comment.