Skip to content

Commit

Permalink
addressing comments in review per naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ac4ch committed May 15, 2024
1 parent afe07aa commit 5d58909
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAccessKeys(t *testing.T) {
}))
defer server.Close()

client, err := NewWalletClientWithAccessKey(fixtures.AccessKeyString, server.URL, true)
client, err := NewWithAccessKey(fixtures.AccessKeyString, server.URL)
require.NoError(t, err)

t.Run("GetAccessKey", 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 @@ -41,7 +41,7 @@ func TestAdminContactActions(t *testing.T) {
}))
defer server.Close()

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

t.Run("AdminGetContacts", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestContactActionsRouting(t *testing.T) {
}))
defer server.Close()

client, err := NewWalletClientWithAccessKey(fixtures.AccessKeyString, server.URL, true)
client, err := NewWithAccessKey(fixtures.AccessKeyString, server.URL)
require.NoError(t, err)

t.Run("RejectContact", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion destinations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestDestinations(t *testing.T) {
}))
defer server.Close()

client, err := NewWalletClientWithAccessKey(fixtures.AccessKeyString, server.URL, true)
client, err := NewWithAccessKey(fixtures.AccessKeyString, server.URL)
require.NoError(t, err)

t.Run("GetDestinationByID", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion examples/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func main() {
keys, _ := xpriv.Generate()

// Create a client
wc, _ := walletclient.NewWalletClientWithXPrivate(keys.XPriv(), "localhost:3001", true)
wc, _ := walletclient.NewWithXPriv(keys.XPriv(), "localhost:3001")
fmt.Println(wc.IsSignRequest())
}
2 changes: 1 addition & 1 deletion examples/http_with_access_key/http_with_access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ func main() {
exampleAccessKey := "some_generated_access_key"

// Create a client
_, _ = walletclient.NewWalletClientWithAccessKey(exampleAccessKey, "http://localhost:3003/v1", true)
_, _ = walletclient.NewWithAccessKey(exampleAccessKey, "http://localhost:3003/v1")

}
2 changes: 1 addition & 1 deletion examples/new_paymail/new_paymail.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func main() {
keys, _ := xpriv.Generate()

// Create a client
wc, _ := walletclient.NewWalletClientWithXPrivate(keys.XPriv(), "localhost:3001", true)
wc, _ := walletclient.NewWithXPriv(keys.XPriv(), "localhost:3001")
wc.AdminCreatePaymail(context.Background(), keys.XPub().String(), "[email protected]", "", "Foo")
}
2 changes: 1 addition & 1 deletion examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
keys, _ := xpriv.Generate()

// Create a client
wc, _ := walletclient.NewWalletClientWithXPrivate(keys.XPriv(), "localhost:3003/v1", true)
wc, _ := walletclient.NewWithXPriv(keys.XPriv(), "localhost:3003/v1")
ctx := context.Background()
_ = wc.AdminNewXpub(ctx, keys.XPub().String(), &models.Metadata{"example_field": "example_data"})

Expand Down
14 changes: 7 additions & 7 deletions totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestGenerateTotpForContact(t *testing.T) {
t.Run("success", func(t *testing.T) {
// given
sut, err := NewWalletClientWithXPrivate(fixtures.XPrivString, "localhost:3001", false)
sut, err := NewWithXPriv(fixtures.XPrivString, "localhost:3001")
require.NoError(t, err)

contact := models.Contact{PubKey: fixtures.PubKey}
Expand All @@ -32,7 +32,7 @@ func TestGenerateTotpForContact(t *testing.T) {

t.Run("WalletClient without xPriv - returns error", func(t *testing.T) {
// given
sut, err := NewWalletClientWithXPublic(fixtures.XPubString, "localhost:3001", false)
sut, err := NewWithXPub(fixtures.XPubString, "localhost:3001")
require.NoError(t, err)

// when
Expand All @@ -44,7 +44,7 @@ func TestGenerateTotpForContact(t *testing.T) {

t.Run("contact has invalid PubKey - returns error", func(t *testing.T) {
// given
sut, err := NewWalletClientWithXPrivate(fixtures.XPrivString, "localhost:3001", false)
sut, err := NewWithXPriv(fixtures.XPrivString, "localhost:3001")
require.NoError(t, err)

contact := models.Contact{PubKey: "invalid-pk-format"}
Expand Down Expand Up @@ -73,9 +73,9 @@ func TestValidateTotpForContact(t *testing.T) {
require.NoError(t, err)

// Set up the WalletClient for Alice and Bob
clientAlice, err := NewWalletClientWithXPrivate(aliceKeys.XPriv(), server.URL, true)
clientAlice, err := NewWithXPriv(aliceKeys.XPriv(), server.URL)
require.NoError(t, err)
clientBob, err := NewWalletClientWithXPrivate(bobKeys.XPriv(), server.URL, true)
clientBob, err := NewWithXPriv(bobKeys.XPriv(), server.URL)
require.NoError(t, err)

aliceContact := &models.Contact{
Expand All @@ -97,13 +97,13 @@ func TestValidateTotpForContact(t *testing.T) {
})

t.Run("WalletClient without xPriv - returns error", func(t *testing.T) {
client, err := NewWalletClientWithXPublic("invalid_xpub", server.URL, true)
client, err := NewWithXPub("invalid_xpub", server.URL)
require.Error(t, err)
require.Nil(t, client)
})

t.Run("contact has invalid PubKey - returns error", func(t *testing.T) {
sut, err := NewWalletClientWithXPrivate(fixtures.XPrivString, server.URL, true)
sut, err := NewWithXPriv(fixtures.XPrivString, server.URL)
require.NoError(t, err)

invalidContact := &models.Contact{
Expand Down
2 changes: 1 addition & 1 deletion transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestTransactions(t *testing.T) {
}))
defer server.Close()

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

t.Run("GetTransaction", func(t *testing.T) {
Expand Down
22 changes: 13 additions & 9 deletions walletclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ type WalletClient struct {
// - `xPriv`: The extended private key used for cryptographic operations.
// - `serverURL`: The URL of the server the client will interact with.
// - `sign`: A boolean flag to determine if the outgoing requests should be signed.
func NewWalletClientWithXPrivate(xPriv, serverURL string, sign bool) (*WalletClient, error) {
func NewWithXPriv(xPriv, serverURL string) (*WalletClient, error) {
return newWalletClient(
&WithXPriv{XPrivString: &xPriv},
&WithHTTP{ServerURL: &serverURL},
&WithSignRequest{Sign: &sign},
&WithSignRequest{Sign: Ptr(true)},
)
}

Expand All @@ -43,11 +43,11 @@ func NewWalletClientWithXPrivate(xPriv, serverURL string, sign bool) (*WalletCli
// - `xPub`: The extended public key used for cryptographic verification and other public operations.
// - `serverURL`: The URL of the server the client will interact with.
// - `sign`: A boolean flag to determine if the outgoing requests should be signed.
func NewWalletClientWithXPublic(xPub, serverURL string, sign bool) (*WalletClient, error) {
func NewWithXPub(xPub, serverURL string) (*WalletClient, error) {
return newWalletClient(
&WithXPub{XPubString: &xPub},
&WithHTTP{ServerURL: &serverURL},
&WithSignRequest{Sign: &sign},
&WithSignRequest{Sign: Ptr(false)},
)
}

Expand All @@ -56,12 +56,12 @@ func NewWalletClientWithXPublic(xPub, serverURL string, sign bool) (*WalletClien
// - `adminKey`: The extended private key used for administrative operations.
// - `serverURL`: The URL of the server the client will interact with.
// - `sign`: A boolean flag to determine if the outgoing requests should be signed.
func NewWalletClientWithAdminKey(adminKey, serverURL string, sign bool) (*WalletClient, error) {
func NewWithAdminKey(adminKey, serverURL string) (*WalletClient, error) {
return newWalletClient(
&WithXPriv{XPrivString: &adminKey},
&WithXPriv{XPrivString: &adminKey}, // this need to be removed .. pls ignore for now
&WithAdminKey{AdminKeyString: &adminKey},
&WithHTTP{ServerURL: &serverURL},
&WithSignRequest{Sign: &sign},
&WithSignRequest{Sign: Ptr(true)},
)
}

Expand All @@ -70,11 +70,11 @@ func NewWalletClientWithAdminKey(adminKey, serverURL string, sign bool) (*Wallet
// - `accessKey`: The access key used for API authentication.
// - `serverURL`: The URL of the server the client will interact with.
// - `sign`: A boolean flag to determine if the outgoing requests should be signed.
func NewWalletClientWithAccessKey(accessKey, serverURL string, sign bool) (*WalletClient, error) {
func NewWithAccessKey(accessKey, serverURL string) (*WalletClient, error) {
return newWalletClient(
&WithAccessKey{AccessKeyString: &accessKey},
&WithHTTP{ServerURL: &serverURL},
&WithSignRequest{Sign: &sign},
&WithSignRequest{Sign: Ptr(true)},
)
}

Expand Down Expand Up @@ -149,3 +149,7 @@ func processMetadata(metadata *models.Metadata) *models.Metadata {
func addSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString string) ResponseError {
return setSignature(header, xPriv, bodyString)
}

func Ptr[T any](obj T) *T {
return &obj
}
16 changes: 8 additions & 8 deletions walletclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewWalletClient(t *testing.T) {
t.Run("NewWalletClientWithXPrivate success", func(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)
client, err := NewWalletClientWithXPrivate(keys.XPriv(), server.URL, true)
client, err := NewWithXPriv(keys.XPriv(), server.URL)
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, keys.XPriv(), *client.xPrivString)
Expand All @@ -38,15 +38,15 @@ func TestNewWalletClient(t *testing.T) {

t.Run("NewWalletClientWithXPrivate fail", func(t *testing.T) {
xPriv := "invalid_key"
client, err := NewWalletClientWithXPrivate(xPriv, "http://example.com", true)
client, err := NewWithXPriv(xPriv, "http://example.com")
require.Error(t, err) // Expect error due to invalid key
require.Nil(t, client)
})

t.Run("NewWalletClientWithXPublic success", func(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)
client, err := NewWalletClientWithXPublic(keys.XPub().String(), server.URL, false)
client, err := NewWithXPub(keys.XPub().String(), server.URL)
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, keys.XPub().String(), *client.xPubString)
Expand All @@ -61,13 +61,13 @@ func TestNewWalletClient(t *testing.T) {

t.Run("NewWalletClientWithXPublic fail", func(t *testing.T) {
xpub := "invalid_key"
client, err := NewWalletClientWithXPublic(xpub, server.URL, false)
client, err := NewWithXPub(xpub, server.URL)
require.Error(t, err) // Expect error due to invalid key
require.Nil(t, client)
})

t.Run("NewWalletClientWithAdminKey success", func(t *testing.T) {
client, err := NewWalletClientWithAdminKey(fixtures.XPrivString, server.URL, true)
client, err := NewWithAdminKey(fixtures.XPrivString, server.URL)
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, fixtures.XPrivString, *client.xPrivString)
Expand All @@ -84,15 +84,15 @@ func TestNewWalletClient(t *testing.T) {

t.Run("NewWalletClientWithAdminKey fail", func(t *testing.T) {
adminKey := "invalid_key"
client, err := NewWalletClientWithAdminKey(adminKey, server.URL, true)
client, err := NewWithAdminKey(adminKey, server.URL)
require.Error(t, err)
require.Nil(t, client)
})

t.Run("NewWalletClientWithAccessKey success", func(t *testing.T) {
// Attempt to create a new WalletClient with an access key
accessKey := fixtures.AccessKeyString
client, err := NewWalletClientWithAccessKey(accessKey, server.URL, true)
client, err := NewWithAccessKey(accessKey, server.URL)

require.NoError(t, err)
require.NotNil(t, client)
Expand All @@ -110,7 +110,7 @@ func TestNewWalletClient(t *testing.T) {

t.Run("NewWalletClientWithAccessKey fail", func(t *testing.T) {
accessKey := "invalid_key"
client, err := NewWalletClientWithAccessKey(accessKey, server.URL, true)
client, err := NewWithAccessKey(accessKey, server.URL)

require.Error(t, err)
require.Nil(t, client)
Expand Down
2 changes: 1 addition & 1 deletion xpubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestXpub(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)

client, err := NewWalletClientWithXPrivate(keys.XPriv(), server.URL, true)
client, err := NewWithXPriv(keys.XPriv(), server.URL)
require.NoError(t, err)

t.Run("GetXPub", func(t *testing.T) {
Expand Down

0 comments on commit 5d58909

Please sign in to comment.