Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SPV-904) update endpoint use cases #247

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@ import (
"testing"

"github.com/bitcoin-sv/spv-wallet-go-client/fixtures"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/response"
"github.com/stretchr/testify/require"
)

// TestAccessKeys will test the AccessKey methods
func TestAccessKeys(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/v1/access-key":
case "/api/v1/users/current/keys/" + fixtures.AccessKey.ID:
switch r.Method {
case http.MethodGet, http.MethodPost, http.MethodDelete:
case http.MethodGet, http.MethodDelete:
json.NewEncoder(w).Encode(fixtures.AccessKey)
}
case "/v1/access-key/search":
json.NewEncoder(w).Encode([]*models.AccessKey{fixtures.AccessKey})
case "/api/v1/users/current/keys":
switch r.Method {
case http.MethodPost:
json.NewEncoder(w).Encode(fixtures.AccessKey)
case http.MethodGet:
json.NewEncoder(w).Encode(response.PageModel[response.AccessKey]{
Content: []*response.AccessKey{fixtures.AccessKey},
})
}
default:
w.WriteHeader(http.StatusNotFound)
}
Expand All @@ -42,7 +49,9 @@ func TestAccessKeys(t *testing.T) {
t.Run("GetAccessKeys", func(t *testing.T) {
accessKeys, err := client.GetAccessKeys(context.Background(), nil, nil, nil)
require.NoError(t, err)
require.Equal(t, []*models.AccessKey{fixtures.AccessKey}, accessKeys)
require.Equal(t, response.PageModel[response.AccessKey]{
Content: []*response.AccessKey{fixtures.AccessKey},
}, accessKeys)
})

t.Run("CreateAccessKey", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion admin_contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAdminContactActions(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/v1/admin/contact/search" && r.Method == http.MethodPost:
c := fixtures.Contact
c := fixtures.OldContact
c.ID = "1"
content := models.PagedResponse[*models.Contact]{
Content: []*models.Contact{c},
Expand Down
2 changes: 1 addition & 1 deletion client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (w *httpConf) Configure(c *WalletClient) {
return
}

const basePath = "/v1"
const basePath = "/api/v1"
c.server = fmt.Sprintf("%s%s", baseURL, basePath)

c.httpClient = w.HTTPClient
Expand Down
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package walletclient

import "github.com/bitcoin-sv/spv-wallet/models"
import "github.com/bitcoin-sv/spv-wallet/models/response"

// TransportType the type of transport being used ('http' for usage or 'mock' for testing)
type TransportType string
Expand All @@ -18,9 +18,9 @@ const (

// Recipients is a struct for recipients
type Recipients struct {
OpReturn *models.OpReturn `json:"op_return"`
Satoshis uint64 `json:"satoshis"`
To string `json:"to"`
OpReturn *response.OpReturn `json:"opReturn"`
Satoshis uint64 `json:"satoshis"`
To string `json:"to"`
}

const (
Expand Down
43 changes: 28 additions & 15 deletions contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/bitcoin-sv/spv-wallet-go-client/fixtures"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/response"
responsemodels "github.com/bitcoin-sv/spv-wallet/models/response"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,25 +17,39 @@ import (
func TestContactActionsRouting(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch {
case strings.HasPrefix(r.URL.Path, "/v1/contact/rejected/"):
if r.Method == http.MethodPatch {
json.NewEncoder(w).Encode(map[string]string{"result": "rejected"})
switch r.URL.Path {
case "/api/v1/contacts/" + fixtures.PaymailAddress:
switch r.Method {
case http.MethodPut:
content := response.CreateContactResponse{
Contact: fixtures.Contact,
AdditionalInfo: map[string]string{},
}
json.NewEncoder(w).Encode(content)
case http.MethodDelete:
json.NewEncoder(w).Encode(map[string]any{})
case http.MethodGet:
json.NewEncoder(w).Encode(fixtures.Contact)
}
case r.URL.Path == "/v1/contact/accepted/":
if r.Method == http.MethodPost {
case "/api/v1/contacts/" + fixtures.PaymailAddress + "/confirmation":
switch r.Method {
case http.MethodPost, http.MethodDelete:
json.NewEncoder(w).Encode(map[string]string{"result": string(responsemodels.ContactNotConfirmed)})
}
case r.URL.Path == "/v1/contact/search":
if r.Method == http.MethodPost {
content := models.PagedResponse[*models.Contact]{
Content: []*models.Contact{fixtures.Contact},
case "/api/v1/contacts/":
if r.Method == http.MethodGet {
content := response.PageModel[response.Contact]{
Content: []*response.Contact{fixtures.Contact},
}
json.NewEncoder(w).Encode(content)
}
case strings.HasPrefix(r.URL.Path, "/v1/contact/"):
if r.Method == http.MethodPost || r.Method == http.MethodPut {
json.NewEncoder(w).Encode(map[string]string{"result": "upserted"})
case "/api/v1/invitations/" + fixtures.PaymailAddress + "/contacts":
if r.Method == http.MethodPost {
json.NewEncoder(w).Encode(map[string]any{})
}
case "/api/v1/invitations/" + fixtures.PaymailAddress:
if r.Method == http.MethodDelete {
json.NewEncoder(w).Encode(map[string]any{})
}
default:
w.WriteHeader(http.StatusNotFound)
Expand Down
95 changes: 0 additions & 95 deletions destinations_test.go

This file was deleted.

20 changes: 20 additions & 0 deletions examples/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,33 @@ tasks:
cmds:
- echo "running xpub_from_xpriv..."
- go run ./xpub_from_xpriv/xpub_from_xpriv.go

generate_totp:
desc: "running generate_totp..."
cmds:
- echo "running generate_totp..."
- go run ./generate_totp/generate_totp.go

webhooks:
desc: "running webhooks..."
cmds:
- echo "running webhooks..."
- go run ./webhooks/webhooks.go || true

access_key:
desc: "running access_key..."
cmds:
- echo "running access_key..."
- go run ./access_key/access_key.go

get_shared_config:
desc: "running get_shared_config..."
cmds:
- echo "running get_shared_config..."
- go run ./get_shared_config/get_shared_config.go

update_xpub_metadata:
desc: "running update_xpub_metadata..."
cmds:
- echo "running update_xpub_metadata..."
- go run ./update_xpub_metadata/update_xpub_metadata.go
49 changes: 49 additions & 0 deletions examples/access_key/access_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Package main - access_key example
*/
package main

import (
"context"
"fmt"
"os"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
defer examples.HandlePanic()

examples.CheckIfXPrivExists()

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

client := walletclient.NewWithXPriv(server, examples.ExampleXPriv)
ctx := context.Background()

metadata := map[string]any{"some_metadata": "example"}
createdAccessKey, err := client.CreateAccessKey(ctx, metadata)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Created access key ID: ", createdAccessKey.ID)
fmt.Println("Metadata: ", createdAccessKey.Metadata)
fmt.Println("Created at: ", createdAccessKey.CreatedAt)

fetchedAccessKey, err := client.GetAccessKey(ctx, createdAccessKey.ID)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Fetched access key ID: ", fetchedAccessKey.ID)

revokedAccessKey, err := client.RevokeAccessKey(ctx, createdAccessKey.ID)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Revoked access key ID: ", revokedAccessKey.ID)
fmt.Println("Revoked at: ", revokedAccessKey.RevokedAt)
}
32 changes: 32 additions & 0 deletions examples/get_shared_config/get_shared_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Package main - get_shared_config example
*/
package main

import (
"context"
"fmt"
"os"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/examples"
)

func main() {
defer examples.HandlePanic()

examples.CheckIfXPrivExists()

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

client := walletclient.NewWithXPriv(server, examples.ExampleXPriv)
ctx := context.Background()

sharedConfig, err := client.GetSharedConfig(ctx)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Shared config (PaymailDomains): ", sharedConfig.PaymailDomains)
fmt.Println("Shared config (ExperimentalFeatures): ", sharedConfig.ExperimentalFeatures)
}
Loading
Loading