Skip to content

Commit

Permalink
addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ac4ch committed May 16, 2024
1 parent 83f9b02 commit 6596a55
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 85 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 := NewWithAccessKey(fixtures.AccessKeyString, server.URL)
client := NewWithAccessKey(server.URL, fixtures.AccessKeyString)
require.NotNil(t, client.accessKey)

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 := NewWithAdminKey(fixtures.XPrivString, server.URL)
client := NewWithAdminKey(server.URL, fixtures.XPrivString)
require.NotNil(t, client.adminXPriv)

t.Run("AdminGetContacts", func(t *testing.T) {
Expand Down
70 changes: 35 additions & 35 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,69 @@ import (
"github.com/pkg/errors"
)

// WalletClientConfigurator is the interface for configuring WalletClient
type WalletClientConfigurator interface {
// Configurator is the interface for configuring WalletClient
type Configurator interface {
Configure(c *WalletClient)
}

// XPriv sets the xPrivString field of a WalletClient
type XPriv struct {
XPrivString *string
// xPrivConf sets the xPrivString field of a WalletClient
type xPrivConf struct {
XPrivString string
}

func (w *XPriv) Configure(c *WalletClient) {
func (w *xPrivConf) Configure(c *WalletClient) {
var err error
if c.xPriv, err = bitcoin.GenerateHDKeyFromString(*w.XPrivString); err != nil {
if c.xPriv, err = bitcoin.GenerateHDKeyFromString(w.XPrivString); err != nil {
c.xPriv = nil
}
}

// XPub sets the xPubString on the client
type XPub struct {
XPubString *string
// xPubConf sets the xPubString on the client
type xPubConf struct {
XPubString string
}

func (w *XPub) Configure(c *WalletClient) {
func (w *xPubConf) Configure(c *WalletClient) {
var err error
if c.xPub, err = bitcoin.GetHDKeyFromExtendedPublicKey(*w.XPubString); err != nil {
w.XPubString = nil
if c.xPub, err = bitcoin.GetHDKeyFromExtendedPublicKey(w.XPubString); err != nil {
c.xPub = nil
}

}

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

func (w *AccessKey) Configure(c *WalletClient) {
func (w *accessKeyConf) Configure(c *WalletClient) {
var err error
if c.accessKey, err = w.initializeAccessKey(); err != nil {
c.accessKey = nil
}
}

// AdminKey sets the admin key for creating new xpubs
type AdminKey struct {
AdminKeyString *string
// adminKeyConf sets the admin key for creating new xpubs
type adminKeyConf struct {
AdminKeyString string
}

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

// HTTP sets the URL and HTTP client of a WalletClient
type HTTP struct {
ServerURL *string
// httpConf sets the URL and httpConf client of a WalletClient
type httpConf struct {
ServerURL string
HTTPClient *http.Client
}

func (w *HTTP) Configure(c *WalletClient) {
c.server = *w.ServerURL
func (w *httpConf) Configure(c *WalletClient) {
c.server = w.ServerURL
c.httpClient = w.HTTPClient
if w.HTTPClient != nil {
c.httpClient = w.HTTPClient
Expand All @@ -80,23 +80,23 @@ func (w *HTTP) Configure(c *WalletClient) {
}
}

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

func (w *SignRequest) Configure(c *WalletClient) {
c.signRequest = *w.Sign
func (w *signRequest) Configure(c *WalletClient) {
c.signRequest = w.Sign
}

// initializeAccessKey handles the specific initialization of the access key.
func (c *AccessKey) initializeAccessKey() (*bec.PrivateKey, error) {
func (c *accessKeyConf) initializeAccessKey() (*bec.PrivateKey, error) {
var err error
var privateKey *bec.PrivateKey
var decodedWIF *wif.WIF

if decodedWIF, err = wif.DecodeWIF(*c.AccessKeyString); err != nil {
if privateKey, err = bitcoin.PrivateKeyFromString(*c.AccessKeyString); err != nil {
if decodedWIF, err = wif.DecodeWIF(c.AccessKeyString); err != nil {
if privateKey, err = bitcoin.PrivateKeyFromString(c.AccessKeyString); err != nil {
return nil, errors.Wrap(err, "failed to decode access key")
}
} else {
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 := NewWithAccessKey(fixtures.AccessKeyString, server.URL)
client := NewWithAccessKey(server.URL, fixtures.AccessKeyString)
require.NotNil(t, client.accessKey)

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 := NewWithAccessKey(fixtures.AccessKeyString, server.URL)
client := NewWithAccessKey(server.URL, fixtures.AccessKeyString)
require.NotNil(t, client.accessKey)

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.NewWithXPriv(keys.XPriv(), "localhost:3001")
wc := walletclient.NewWithXPriv("localhost:3001", keys.XPriv())
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("http://localhost:3003/v1", exampleAccessKey)

}
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("localhost:3001", keys.XPriv())
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("localhost:3003/v1", keys.XPriv())
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 := NewWithXPriv(fixtures.XPrivString, "localhost:3001")
sut := NewWithXPriv("localhost:3001", fixtures.XPrivString)
require.NotNil(t, sut.xPriv)

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

t.Run("WalletClient without xPriv - returns error", func(t *testing.T) {
// given
sut := NewWithXPub(fixtures.XPubString, "localhost:3001")
sut := NewWithXPub("localhost:3001", fixtures.XPubString)
require.NotNil(t, sut.xPub)
// when
_, err := sut.GenerateTotpForContact(nil, 30, 2)
Expand All @@ -42,7 +42,7 @@ func TestGenerateTotpForContact(t *testing.T) {

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

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

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

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

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

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

invalidContact := &models.Contact{
PubKey: "invalid_pub_key_format",
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 := NewWithXPriv(fixtures.XPrivString, server.URL)
client := NewWithXPriv(server.URL, fixtures.XPrivString)
require.NotNil(t, client.xPriv)

t.Run("GetTransaction", func(t *testing.T) {
Expand Down
44 changes: 22 additions & 22 deletions walletclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,52 @@ 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 {
return newWalletClient(
&XPriv{XPrivString: &xPriv},
&HTTP{ServerURL: &serverURL},
&SignRequest{Sign: Ptr(true)},
func NewWithXPriv(serverURL, xPriv string) *WalletClient {
return makeClient(
&xPrivConf{XPrivString: xPriv},
&httpConf{ServerURL: serverURL},
&signRequest{Sign: true},
)
}

// NewWalletClientWithXPublic creates a new WalletClient instance using a public key (xPub).
// 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 {
return newWalletClient(
&XPub{XPubString: &xPub},
&HTTP{ServerURL: &serverURL},
&SignRequest{Sign: Ptr(false)},
func NewWithXPub(serverURL, xPub string) *WalletClient {
return makeClient(
&xPubConf{XPubString: xPub},
&httpConf{ServerURL: serverURL},
&signRequest{Sign: false},
)
}

// NewWalletClientWithAdminKey creates a new WalletClient using an administrative key for advanced operations.
// 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 {
return newWalletClient(
&AdminKey{AdminKeyString: &adminKey},
&HTTP{ServerURL: &serverURL},
&SignRequest{Sign: Ptr(true)},
func NewWithAdminKey(serverURL, adminKey string) *WalletClient {
return makeClient(
&adminKeyConf{AdminKeyString: adminKey},
&httpConf{ServerURL: serverURL},
&signRequest{Sign: true},
)
}

// NewWalletClientWithAccessKey creates a new WalletClient configured with an access key for API authentication.
// 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 {
return newWalletClient(
&AccessKey{AccessKeyString: &accessKey},
&HTTP{ServerURL: &serverURL},
&SignRequest{Sign: Ptr(true)},
func NewWithAccessKey(serverURL, accessKey string) *WalletClient {
return makeClient(
&accessKeyConf{AccessKeyString: accessKey},
&httpConf{ServerURL: serverURL},
&signRequest{Sign: true},
)
}

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

for _, configurator := range configurators {
Expand Down
18 changes: 7 additions & 11 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 := NewWithXPriv(keys.XPriv(), server.URL)
client := NewWithXPriv(server.URL, keys.XPriv())
require.NotNil(t, client.xPriv)
require.Equal(t, keys.XPriv(), client.xPriv.String())
require.NotNil(t, client.httpClient)
Expand All @@ -44,7 +44,7 @@ func TestNewWalletClient(t *testing.T) {
t.Run("NewWalletClientWithXPublic success", func(t *testing.T) {
keys, err := xpriv.Generate()
require.NoError(t, err)
client := NewWithXPub(keys.XPub().String(), server.URL)
client := NewWithXPub(server.URL, keys.XPub().String())
require.NotNil(t, client.xPub)
require.Equal(t, keys.XPub().String(), client.xPub.String())
require.NotNil(t, client.httpClient)
Expand All @@ -57,13 +57,12 @@ func TestNewWalletClient(t *testing.T) {
})

t.Run("NewWalletClientWithXPublic fail", func(t *testing.T) {
xpub := "invalid_key"
client := NewWithXPub(xpub, server.URL)
client := NewWithXPub(server.URL, "invalid_key")
require.Nil(t, client.xPub)
})

t.Run("NewWalletClientWithAdminKey success", func(t *testing.T) {
client := NewWithAdminKey(fixtures.XPrivString, server.URL)
client := NewWithAdminKey(server.URL, fixtures.XPrivString)
require.NotNil(t, client.adminXPriv)
require.Nil(t, client.xPriv)
require.Equal(t, fixtures.XPrivString, client.adminXPriv.String())
Expand All @@ -78,15 +77,13 @@ func TestNewWalletClient(t *testing.T) {
})

t.Run("NewWalletClientWithAdminKey fail", func(t *testing.T) {
adminKey := "invalid_key"
client := NewWithAdminKey(adminKey, server.URL)
client := NewWithAdminKey(server.URL, "invalid_key")
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 := NewWithAccessKey(accessKey, server.URL)
client := NewWithAccessKey(server.URL, fixtures.AccessKeyString)
require.NotNil(t, client.accessKey)

require.Equal(t, server.URL, client.server)
Expand All @@ -100,8 +97,7 @@ func TestNewWalletClient(t *testing.T) {
})

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

0 comments on commit 6596a55

Please sign in to comment.