Skip to content

Commit

Permalink
correcting units per changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ac4ch committed May 15, 2024
1 parent 5a1e03c commit 513a5b6
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 73 deletions.
4 changes: 2 additions & 2 deletions access_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestAccessKeys(t *testing.T) {
}))
defer server.Close()

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

t.Run("GetAccessKey", func(t *testing.T) {
accessKey, err := client.GetAccessKey(context.Background(), fixtures.AccessKey.ID)
Expand Down
4 changes: 2 additions & 2 deletions admin_contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestAdminContactActions(t *testing.T) {
}))
defer server.Close()

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

t.Run("AdminGetContacts", func(t *testing.T) {
contacts, err := client.AdminGetContacts(context.Background(), nil, nil, nil)
Expand Down
4 changes: 2 additions & 2 deletions contacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func TestContactActionsRouting(t *testing.T) {
}))
defer server.Close()

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

t.Run("RejectContact", func(t *testing.T) {
err := client.RejectContact(context.Background(), fixtures.PaymailAddress)
Expand Down
4 changes: 2 additions & 2 deletions destinations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestDestinations(t *testing.T) {
}))
defer server.Close()

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

t.Run("GetDestinationByID", func(t *testing.T) {
destination, err := client.GetDestinationByID(context.Background(), fixtures.Destination.ID)
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.NewWithXPriv(keys.XPriv(), "localhost:3001")
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.NewWithAccessKey(exampleAccessKey, "http://localhost:3003/v1")
_ = 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.NewWithXPriv(keys.XPriv(), "localhost:3001")
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.NewWithXPriv(keys.XPriv(), "localhost:3003/v1")
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
37 changes: 16 additions & 21 deletions totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import (
func TestGenerateTotpForContact(t *testing.T) {
t.Run("success", func(t *testing.T) {
// given
sut, err := NewWithXPriv(fixtures.XPrivString, "localhost:3001")
require.NoError(t, err)
sut := NewWithXPriv(fixtures.XPrivString, "localhost:3001")
require.NotNil(t, sut.xPriv)

contact := models.Contact{PubKey: fixtures.PubKey}

// when
pass, err := sut.GenerateTotpForContact(&contact, 30, 2)

Expand All @@ -32,25 +31,23 @@ func TestGenerateTotpForContact(t *testing.T) {

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

sut := NewWithXPub(fixtures.XPubString, "localhost:3001")
require.NotNil(t, sut.xPub)
// when
_, err = sut.GenerateTotpForContact(nil, 30, 2)
_, err := sut.GenerateTotpForContact(nil, 30, 2)

// then
require.ErrorIs(t, err, ErrClientInitNoXpriv)
})

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

contact := models.Contact{PubKey: "invalid-pk-format"}

// when
_, err = sut.GenerateTotpForContact(&contact, 30, 2)
_, err := sut.GenerateTotpForContact(&contact, 30, 2)

// then
require.ErrorContains(t, err, "contact's PubKey is invalid:")
Expand All @@ -73,10 +70,10 @@ func TestValidateTotpForContact(t *testing.T) {
require.NoError(t, err)

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

aliceContact := &models.Contact{
PubKey: makeMockPKI(aliceKeys.XPub().String()),
Expand All @@ -97,21 +94,19 @@ func TestValidateTotpForContact(t *testing.T) {
})

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

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

invalidContact := &models.Contact{
PubKey: "invalid_pub_key_format",
Paymail: "[email protected]",
}

_, err = sut.ValidateTotpForContact(invalidContact, "123456", "[email protected]", 3600, 6)
_, err := sut.ValidateTotpForContact(invalidContact, "123456", "[email protected]", 3600, 6)
require.Error(t, err)
require.Contains(t, err.Error(), "contact's PubKey is invalid")
})
Expand Down
4 changes: 2 additions & 2 deletions transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func TestTransactions(t *testing.T) {
}))
defer server.Close()

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

t.Run("GetTransaction", func(t *testing.T) {
tx, err := client.GetTransaction(context.Background(), fixtures.Transaction.ID)
Expand Down
12 changes: 6 additions & 6 deletions walletclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type WalletClient struct {
// It configures the client with a specific server URL and a flag indicating whether requests should be signed.
// - `xPriv`: The extended private key used for cryptographic operations.
// - `serverURL`: The URL of the server the client will interact with.
func NewWithXPriv(xPriv, serverURL string) (*WalletClient, error) {
func NewWithXPriv(xPriv, serverURL string) *WalletClient {
return newWalletClient(
&WithXPriv{XPrivString: &xPriv},
&WithHTTP{ServerURL: &serverURL},
Expand All @@ -35,7 +35,7 @@ func NewWithXPriv(xPriv, serverURL string) (*WalletClient, error) {
// This client is configured for operations that require a public key, such as verifying signatures or receiving transactions.
// - `xPub`: The extended public key used for cryptographic verification and other public operations.
// - `serverURL`: The URL of the server the client will interact with.
func NewWithXPub(xPub, serverURL string) (*WalletClient, error) {
func NewWithXPub(xPub, serverURL string) *WalletClient {
return newWalletClient(
&WithXPub{XPubString: &xPub},
&WithHTTP{ServerURL: &serverURL},
Expand All @@ -47,7 +47,7 @@ func NewWithXPub(xPub, serverURL string) (*WalletClient, error) {
// This configuration is typically used for administrative tasks such as managing sub-wallets or configuring system-wide settings.
// - `adminKey`: The extended private key used for administrative operations.
// - `serverURL`: The URL of the server the client will interact with.
func NewWithAdminKey(adminKey, serverURL string) (*WalletClient, error) {
func NewWithAdminKey(adminKey, serverURL string) *WalletClient {
return newWalletClient(
&WithAdminKey{AdminKeyString: &adminKey},
&WithHTTP{ServerURL: &serverURL},
Expand All @@ -59,7 +59,7 @@ func NewWithAdminKey(adminKey, serverURL string) (*WalletClient, error) {
// This method is useful for scenarios where the client needs to authenticate using a less sensitive key than an xPriv.
// - `accessKey`: The access key used for API authentication.
// - `serverURL`: The URL of the server the client will interact with.
func NewWithAccessKey(accessKey, serverURL string) (*WalletClient, error) {
func NewWithAccessKey(accessKey, serverURL string) *WalletClient {
return newWalletClient(
&WithAccessKey{AccessKeyString: &accessKey},
&WithHTTP{ServerURL: &serverURL},
Expand All @@ -68,14 +68,14 @@ func NewWithAccessKey(accessKey, serverURL string) (*WalletClient, error) {
}

// newWalletClient creates a new WalletClient using the provided configuration options.
func newWalletClient(configurators ...WalletClientConfigurator) (*WalletClient, error) {
func newWalletClient(configurators ...WalletClientConfigurator) *WalletClient {
client := &WalletClient{}

for _, configurator := range configurators {
configurator.Configure(client)
}

return client, nil
return client
}

// processMetadata will process the metadata
Expand Down
49 changes: 19 additions & 30 deletions walletclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ func TestNewWalletClient(t *testing.T) {
t.Run("NewWalletClientWithXPrivate success", func(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)
client, err := NewWithXPriv(keys.XPriv(), server.URL)
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, keys.XPriv(), *client.xPrivString)
client := NewWithXPriv(keys.XPriv(), server.URL)
require.NotNil(t, client.xPriv)
require.Equal(t, keys.XPriv(), client.xPriv.String())
require.NotNil(t, client.httpClient)
require.True(t, *client.signRequest)

Expand All @@ -38,18 +37,16 @@ func TestNewWalletClient(t *testing.T) {

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

t.Run("NewWalletClientWithXPublic success", func(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)
client, err := NewWithXPub(keys.XPub().String(), server.URL)
require.NoError(t, err)
require.NotNil(t, client)
require.Equal(t, keys.XPub().String(), *client.xPubString)
client := NewWithXPub(keys.XPub().String(), server.URL)
require.NotNil(t, client.xPub)
require.Equal(t, keys.XPub().String(), client.xPub.String())
require.NotNil(t, client.httpClient)
require.False(t, *client.signRequest)

Expand All @@ -61,16 +58,14 @@ func TestNewWalletClient(t *testing.T) {

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

t.Run("NewWalletClientWithAdminKey success", func(t *testing.T) {
client, err := NewWithAdminKey(fixtures.XPrivString, server.URL)
require.NoError(t, err)
require.NotNil(t, client)
require.Nil(t, client.xPrivString)
client := NewWithAdminKey(fixtures.XPrivString, server.URL)
require.NotNil(t, client.adminXPriv)
require.Nil(t, client.xPriv)
require.Equal(t, fixtures.XPrivString, client.adminXPriv.String())
require.Equal(t, server.URL, *client.server)
require.NotNil(t, client.httpClient)
Expand All @@ -84,20 +79,16 @@ func TestNewWalletClient(t *testing.T) {

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

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

require.NoError(t, err)
require.NotNil(t, client)

require.Equal(t, &accessKey, client.accessKeyString)
require.Equal(t, &server.URL, client.server)
require.True(t, *client.signRequest)
require.NotNil(t, client.httpClient)
Expand All @@ -110,9 +101,7 @@ func TestNewWalletClient(t *testing.T) {

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

require.Error(t, err)
require.Nil(t, client)
client := NewWithAccessKey(accessKey, server.URL)
require.Nil(t, client.accessKey)
})
}
4 changes: 2 additions & 2 deletions xpubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func TestXpub(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)

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

t.Run("GetXPub", func(t *testing.T) {
xpub, err := client.GetXPub(context.Background())
Expand Down

0 comments on commit 513a5b6

Please sign in to comment.