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 775) refactor the codebase and sync its functionality with the current state of the js client #225

Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
16a8da3
init refactoring
ac4ch May 10, 2024
28be53f
Merge branch 'main' into feat/SPV-775/Refactor_the_codebase_and_sync_…
ac4ch May 10, 2024
fab2f8e
debugging client
ac4ch May 12, 2024
1a7d420
latest working
ac4ch May 12, 2024
2f586bf
latest working
ac4ch May 12, 2024
85f978c
latest working
ac4ch May 12, 2024
c2b70a9
latest working
ac4ch May 12, 2024
51a1637
latest working
ac4ch May 12, 2024
b2044d5
fixed my issue with merfing local debug branch
ac4ch May 12, 2024
e0188ca
fixed my issue with merfing local debug branch
ac4ch May 13, 2024
cc5db48
adding unit to admin
ac4ch May 14, 2024
c13ec12
correcting per comments
ac4ch May 14, 2024
c9591a5
correcting examples code
ac4ch May 14, 2024
cb92eb8
correcting examples code
ac4ch May 14, 2024
afe07aa
correcting examples code
ac4ch May 14, 2024
5d58909
addressing comments in review per naming
ac4ch May 15, 2024
6406f0c
changing adminkey constructor to use the admin key
ac4ch May 15, 2024
8f37026
removing debug line
ac4ch May 15, 2024
b54efea
addressing some not needed fields
ac4ch May 15, 2024
5a1e03c
removed init keys as is not needed any more
ac4ch May 15, 2024
513a5b6
correcting units per changes
ac4ch May 15, 2024
bf02213
renaming options
ac4ch May 15, 2024
83f9b02
reomved pointers to sign and server
ac4ch May 15, 2024
6596a55
addressing review comments
ac4ch May 16, 2024
df99079
addressed reviwer comments
ac4ch May 16, 2024
06b0eea
reomved unused var
ac4ch May 16, 2024
a795937
corrected comments and other code challanges
ac4ch May 17, 2024
d2471de
addressing review comments per unit tests
ac4ch May 17, 2024
7f1c206
addressing lint errors
ac4ch May 17, 2024
5f231e5
Merge branch 'main' into feat/SPV-775/Refactor_the_codebase_and_sync_…
ac4ch May 17, 2024
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
28 changes: 0 additions & 28 deletions access_keys.go

This file was deleted.

83 changes: 34 additions & 49 deletions access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,61 @@ package walletclient

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/bitcoin-sv/spv-wallet/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

// TestAccessKeys will test the AccessKey methods
func TestAccessKeys(t *testing.T) {
transportHandler := testTransportHandler{
Type: fixtures.RequestType,
Path: "/access-key",
Result: fixtures.MarshallForTestHandler(fixtures.AccessKey),
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/access-key":
if r.Method == http.MethodGet {
json.NewEncoder(w).Encode(fixtures.AccessKey)
} else if r.Method == http.MethodPost {
json.NewEncoder(w).Encode(fixtures.AccessKey)
} else if r.Method == http.MethodDelete {
json.NewEncoder(w).Encode(fixtures.AccessKey)
}
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
case "/access-key/search":
json.NewEncoder(w).Encode([]*models.AccessKey{fixtures.AccessKey})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()

t.Run("GetAccessKey", func(t *testing.T) {
// given
client := getTestWalletClient(transportHandler, true)
client, err := NewWalletClientWithAccessKey(fixtures.AccessKeyString, server.URL, true)
require.NoError(t, err)

// when
t.Run("GetAccessKey", func(t *testing.T) {
accessKey, err := client.GetAccessKey(context.Background(), fixtures.AccessKey.ID)

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
require.NoError(t, err)
require.Equal(t, fixtures.AccessKey, accessKey)
})

t.Run("GetAccessKeys", func(t *testing.T) {
// given
transportHandler := testTransportHandler{
Type: fixtures.RequestType,
Path: "/access-key/search",
Result: fixtures.MarshallForTestHandler([]*models.AccessKey{fixtures.AccessKey}),
ClientURL: fixtures.ServerURL,
Client: WithHTTPClient,
}
client := getTestWalletClient(transportHandler, true)

// when
accessKeys, err := client.GetAccessKeys(context.Background(), fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, accessKeys, []*models.AccessKey{fixtures.AccessKey})
accessKeys, err := client.GetAccessKeys(context.Background(), nil)
require.NoError(t, err)
require.Equal(t, []*models.AccessKey{fixtures.AccessKey}, accessKeys)
})

t.Run("CreateAccessKey", func(t *testing.T) {
// given
client := getTestWalletClient(transportHandler, true)

// when
accessKey, err := client.CreateAccessKey(context.Background(), fixtures.TestMetadata)

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
accessKey, err := client.CreateAccessKey(context.Background(), nil)
require.NoError(t, err)
require.Equal(t, fixtures.AccessKey, accessKey)
})

t.Run("RevokeAccessKey", func(t *testing.T) {
// given
client := getTestWalletClient(transportHandler, true)

// when
accessKey, err := client.RevokeAccessKey(context.Background(), fixtures.AccessKey.ID)

// then
assert.NoError(t, err)
assert.Equal(t, accessKey, fixtures.AccessKey)
require.NoError(t, err)
require.Equal(t, fixtures.AccessKey, accessKey)
})
}
34 changes: 0 additions & 34 deletions admin_contacts.go

This file was deleted.

75 changes: 75 additions & 0 deletions admin_contacts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package walletclient

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/bitcoin-sv/spv-wallet/models"
"github.com/stretchr/testify/require"

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

// TestAdminContactActions testing Admin contacts methods
func TestAdminContactActions(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/admin/contact/search" && r.Method == http.MethodPost:
c := fixtures.Contact
c.ID = "1"
contacts := []*models.Contact{c}
json.NewEncoder(w).Encode(contacts)
case r.URL.Path == "/admin/contact/1" && r.Method == http.MethodPatch:
contact := fixtures.Contact
json.NewEncoder(w).Encode(contact)
case r.URL.Path == "/admin/contact/1" && r.Method == http.MethodDelete:
w.WriteHeader(http.StatusOK)
case r.URL.Path == "/admin/contact/accepted/1" && r.Method == http.MethodPatch:
contact := fixtures.Contact
contact.Status = "accepted"
json.NewEncoder(w).Encode(contact)
case r.URL.Path == "/admin/contact/rejected/1" && r.Method == http.MethodPatch:
contact := fixtures.Contact
contact.Status = "rejected"
json.NewEncoder(w).Encode(contact)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()

client, err := NewWalletClientWithAdminKey(fixtures.XPrivString, server.URL, true)
require.NoError(t, err)

t.Run("AdminGetContacts", func(t *testing.T) {
contacts, err := client.AdminGetContacts(context.Background(), nil, nil, nil)
require.NoError(t, err)
require.Equal(t, "1", contacts[0].ID)
})

t.Run("AdminUpdateContact", func(t *testing.T) {
contact, err := client.AdminUpdateContact(context.Background(), "1", "Jane Doe", nil)
require.NoError(t, err)
require.Equal(t, "Test User", contact.FullName)
})

t.Run("AdminDeleteContact", func(t *testing.T) {
err := client.AdminDeleteContact(context.Background(), "1")
require.NoError(t, err)
})

t.Run("AdminAcceptContact", func(t *testing.T) {
contact, err := client.AdminAcceptContact(context.Background(), "1")
require.NoError(t, err)
require.Equal(t, models.ContactStatus("accepted"), contact.Status)
})

t.Run("AdminRejectContact", func(t *testing.T) {
contact, err := client.AdminRejectContact(context.Background(), "1")
require.NoError(t, err)
require.Equal(t, models.ContactStatus("rejected"), contact.Status)
})
}
2 changes: 1 addition & 1 deletion transports/authentication.go β†’ authentication.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package transports
package walletclient

import (
"encoding/hex"
Expand Down
103 changes: 55 additions & 48 deletions client_options.go
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,75 @@ package walletclient
import (
"net/http"

"github.com/bitcoin-sv/spv-wallet-go-client/transports"
"github.com/bitcoinschema/go-bitcoin/v2"
)

// WithXPriv will set xPrivString on the client
func WithXPriv(xPrivString string) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.xPrivString = xPrivString
}
}
// WalletClientConfigurator is the interface for configuring WalletClient
type WalletClientConfigurator interface {
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
Configure(c *WalletClient)
}

// WithXPub will set xPubString on the client
func WithXPub(xPubString string) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.xPubString = xPubString
}
}
// WithXPriv sets the xPrivString field of a WalletClient
type WithXPriv struct {
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
XPrivString *string
ac4ch marked this conversation as resolved.
Show resolved Hide resolved
}

// WithAccessKey will set the access key on the client
func WithAccessKey(accessKeyString string) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.accessKeyString = accessKeyString
}
}
func (w *WithXPriv) Configure(c *WalletClient) {
c.xPrivString = w.XPrivString
}

// WithHTTP will overwrite the default client with a custom client
func WithHTTP(serverURL string) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.transportOptions = append(c.transportOptions, transports.WithHTTP(serverURL))
}
}
// WithXPub sets the xPubString on the client
type WithXPub struct {
XPubString *string
}

// WithHTTPClient will overwrite the default client with a custom client
func WithHTTPClient(serverURL string, httpClient *http.Client) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.transportOptions = append(c.transportOptions, transports.WithHTTPClient(serverURL, httpClient))
}
}
func (w *WithXPub) Configure(c *WalletClient) {
c.xPubString = w.XPubString
}

// WithAccessKey sets the accessKeyString on the client
type WithAccessKey struct {
AccessKeyString *string
}

func (w *WithAccessKey) Configure(c *WalletClient) {
c.accessKeyString = w.AccessKeyString
}

// WithAdminKey will set the admin key for admin requests
func WithAdminKey(adminKey string) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.transportOptions = append(c.transportOptions, transports.WithAdminKey(adminKey))
}
// WithAdminKey sets the admin key for creating new xpubs
type WithAdminKey struct {
AdminKeyString *string
}

func (w *WithAdminKey) Configure(c *WalletClient) {
var err error
c.adminXPriv, err = bitcoin.GenerateHDKeyFromString(*w.AdminKeyString)
if err != nil {
c.adminXPriv = nil
}
}

// WithSignRequest will set whether to sign all requests
func WithSignRequest(signRequest bool) ClientOps {
return func(c *WalletClient) {
if c != nil {
c.transportOptions = append(c.transportOptions, transports.WithSignRequest(signRequest))
}
// WithHTTP sets the URL and HTTP client of a WalletClient
type WithHTTP struct {
ServerURL *string
HTTPClient *http.Client
}

func (w *WithHTTP) Configure(c *WalletClient) {
c.server = w.ServerURL
c.httpClient = w.HTTPClient
if w.HTTPClient != nil {
c.httpClient = w.HTTPClient
} else {
c.httpClient = http.DefaultClient
}
}

// WithSignRequest configures whether to sign HTTP requests
type WithSignRequest struct {
Sign *bool
}

func (w *WithSignRequest) Configure(c *WalletClient) {
c.signRequest = w.Sign
}
2 changes: 1 addition & 1 deletion transports/config.go β†’ config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package transports
package walletclient

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

Expand Down
Loading
Loading