Skip to content

Commit

Permalink
Fixed names, comments, broke files apart, upgraded deps, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Mar 28, 2022
1 parent bf7085c commit 4115ec9
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 246 deletions.
16 changes: 8 additions & 8 deletions buxclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ func TestDraftTransaction(t *testing.T) {
}
}

// TestRegisterXpub will test the RegisterXpub method
func TestRegisterXpub(t *testing.T) {
// TestNewXpub will test the NewXpub method
func TestNewXpub(t *testing.T) {
transportHandlers := []testTransportHandler{{
Type: requestTypeHTTP,
Path: "/xpub",
Expand All @@ -275,12 +275,12 @@ func TestRegisterXpub(t *testing.T) {
}}

for _, transportHandler := range transportHandlers {
t.Run("draft transaction "+transportHandler.Type, func(t *testing.T) {
t.Run("NewXpub "+transportHandler.Type, func(t *testing.T) {
client := getTestBuxClient(transportHandler, true)
metadata := &bux.Metadata{
"test-key": "test-value",
}
err := client.RegisterXpub(context.Background(), xPubString, metadata)
err := client.NewXpub(context.Background(), xPubString, metadata)
assert.NoError(t, err)
})
}
Expand All @@ -303,7 +303,7 @@ func TestDraftToRecipients(t *testing.T) {
}}

for _, transportHandler := range transportHandlers {
t.Run("draft transaction "+transportHandler.Type, func(t *testing.T) {
t.Run("DraftToRecipients "+transportHandler.Type, func(t *testing.T) {
client := getTestBuxClient(transportHandler, false)

recipients := []*transports.Recipients{{
Expand Down Expand Up @@ -461,7 +461,7 @@ func TestRecordTransaction(t *testing.T) {
}}

for _, transportHandler := range transportHandlers {
t.Run("get transactions "+transportHandler.Type, func(t *testing.T) {
t.Run("record transaction "+transportHandler.Type, func(t *testing.T) {
client := getTestBuxClient(transportHandler, false)

hex := ""
Expand Down Expand Up @@ -537,7 +537,7 @@ func TestSendToRecipients(t *testing.T) {
// TestFinalizeTransaction will test the FinalizeTransaction method
func TestFinalizeTransaction(t *testing.T) {

t.Run("mock", func(t *testing.T) {
t.Run("finalize transaction", func(t *testing.T) {
httpclient := &http.Client{Transport: localRoundTripper{handler: http.NewServeMux()}}
client, err := New(
WithXPriv(xPrivString),
Expand Down Expand Up @@ -574,7 +574,7 @@ func TestGetTransport(t *testing.T) {
assert.IsType(t, &transports.TransportHTTP{}, *transport)
})

t.Run("client", func(t *testing.T) {
t.Run("client GetTransport", func(t *testing.T) {
client, _ := New(
WithXPriv(xPrivString),
WithGraphQL(serverURL),
Expand Down
2 changes: 1 addition & 1 deletion examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
log.Fatalln(err.Error())
}

if err = buxClient.RegisterXpub(
if err = buxClient.NewXpub(
context.Background(), rawXPub, &bux.Metadata{"example_field": "example_data"},
); err != nil {
log.Fatalln(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions paymail_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/BuxOrg/bux"
)

// RegisterPaymail registers a new paymail
func (b *BuxClient) RegisterPaymail(ctx context.Context, rawXPub, paymailAddress string, metadata *bux.Metadata) error {
return b.transport.RegisterPaymail(ctx, rawXPub, paymailAddress, metadata)
// NewPaymail registers a new paymail
func (b *BuxClient) NewPaymail(ctx context.Context, rawXPub, paymailAddress string, metadata *bux.Metadata) error {
return b.transport.NewPaymail(ctx, rawXPub, paymailAddress, metadata)
}
140 changes: 140 additions & 0 deletions transports/client_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package transports

import (
"net/http"

"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bk/bip32"
)

// WithXPriv will set the xPriv
func WithXPriv(xPriv *bip32.ExtendedKey) ClientOps {
return func(c *Client) {
if c != nil {
c.xPriv = xPriv
}
}
}

// WithXPub will set the xPub
func WithXPub(xPub *bip32.ExtendedKey) ClientOps {
return func(c *Client) {
if c != nil {
c.xPub = xPub
}
}
}

// WithAccessKey will set the access key
func WithAccessKey(accessKey *bec.PrivateKey) ClientOps {
return func(c *Client) {
if c != nil {
c.accessKey = accessKey
}
}
}

// WithGraphQL will overwrite the default client with a custom client
func WithGraphQL(serverURL string) ClientOps {
return func(c *Client) {
if c != nil {
c.transport = NewTransportService(&TransportGraphQL{
debug: c.debug,
server: serverURL,
signRequest: c.signRequest,
adminXPriv: c.adminXPriv,
httpClient: &http.Client{},
xPriv: c.xPriv,
xPub: c.xPub,
accessKey: c.accessKey,
})
}
}
}

// WithHTTP will overwrite the default client with a custom client
func WithHTTP(serverURL string) ClientOps {
return func(c *Client) {
if c != nil {
c.transport = NewTransportService(&TransportHTTP{
debug: c.debug,
server: serverURL,
signRequest: c.signRequest,
adminXPriv: c.adminXPriv,
httpClient: &http.Client{},
xPriv: c.xPriv,
xPub: c.xPub,
accessKey: c.accessKey,
})
}
}
}

// WithGraphQLClient will overwrite the default client with a custom client
func WithGraphQLClient(serverURL string, httpClient *http.Client) ClientOps {
return func(c *Client) {
if c != nil {
c.transport = NewTransportService(&TransportGraphQL{
debug: c.debug,
server: serverURL,
signRequest: c.signRequest,
adminXPriv: c.adminXPriv,
httpClient: httpClient,
xPriv: c.xPriv,
xPub: c.xPub,
accessKey: c.accessKey,
})
}
}
}

// WithHTTPClient will overwrite the default client with a custom client
func WithHTTPClient(serverURL string, httpClient *http.Client) ClientOps {
return func(c *Client) {
if c != nil {
c.transport = NewTransportService(&TransportHTTP{
debug: c.debug,
server: serverURL,
signRequest: c.signRequest,
adminXPriv: c.adminXPriv,
httpClient: httpClient,
xPriv: c.xPriv,
xPub: c.xPub,
accessKey: c.accessKey,
})
}
}
}

// WithAdminKey will set the admin key for admin requests
func WithAdminKey(adminKey string) ClientOps {
return func(c *Client) {
if c != nil {
c.adminKey = adminKey
}
}
}

// WithSignRequest will set whether to sign all requests
func WithSignRequest(signRequest bool) ClientOps {
return func(c *Client) {
if c != nil {
c.signRequest = signRequest
if c.transport != nil {
c.transport.SetSignRequest(signRequest)
}
}
}
}

// WithDebugging will set whether to turn debugging on
func WithDebugging(debug bool) ClientOps {
return func(c *Client) {
if c != nil {
c.debug = debug
if c.transport != nil {
c.transport.SetDebug(debug)
}
}
}
}
29 changes: 29 additions & 0 deletions transports/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ package transports

import "github.com/BuxOrg/bux"

// TransportType the type of transport being used (http or graphql)
type TransportType string

// BuxUserAgent the bux user agent sent to the bux server
const BuxUserAgent = "BUX: go-client " + BuxClientVersion

// BuxClientVersion is the version of the client
const BuxClientVersion = "v0.1.7"

const (
// BuxTransportHTTP uses the http transport for all bux server actions
BuxTransportHTTP TransportType = "http"

// BuxTransportGraphQL uses the graphql transport for all bux server actions
BuxTransportGraphQL TransportType = "graphql"

// BuxTransportMock uses the mock transport for all bux server actions
BuxTransportMock TransportType = "mock"
)

// Recipients is a struct for recipients
type Recipients struct {
OpReturn *bux.OpReturn `json:"op_return"`
Expand Down Expand Up @@ -42,4 +62,13 @@ const (

// FieldReferenceID is the field name for "reference_id"
FieldReferenceID = "reference_id"

// FieldID is the id field for most models
FieldID = "id"

// FieldLockingScript is the field for locking script
FieldLockingScript = "locking_script"

// FieldUserAgent is the field for storing the user agent
FieldUserAgent = "user_agent"
)
3 changes: 3 additions & 0 deletions transports/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ import "errors"

// ErrAdminKey admin key not set
var ErrAdminKey = errors.New("an admin key must be set to be able to create an xpub")

// ErrNoClientSet is when no client is set
var ErrNoClientSet = errors.New("no transport client set")
8 changes: 4 additions & 4 deletions transports/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (g *TransportGraphQL) IsSignRequest() bool {
return g.signRequest
}

// RegisterPaymail will register a new paymail
func (g *TransportGraphQL) RegisterPaymail(ctx context.Context, rawXpub, paymailAddress string, metadata *bux.Metadata) error {
// NewPaymail will register a new paymail
func (g *TransportGraphQL) NewPaymail(ctx context.Context, rawXpub, paymailAddress string, metadata *bux.Metadata) error {
// TODO: Implement this
return nil
}
Expand All @@ -118,8 +118,8 @@ func (g *TransportGraphQL) GetXpub(ctx context.Context, rawXpub string) (*bux.Xp
return nil, nil
}

// RegisterXpub will register an xPub
func (g *TransportGraphQL) RegisterXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error {
// NewXpub will register an xPub
func (g *TransportGraphQL) NewXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error {

// adding a xpub needs to be signed by an admin key
if g.adminXPriv == nil {
Expand Down
14 changes: 5 additions & 9 deletions transports/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,39 @@ const (
xPubString = "xpub661MyMwAqRbcFrBJbKwBGCB7d3fr2SaAuXGM95BA62X41m6eW2ehRQGW4xLi9wkEXUGnQZYxVVj4PxXnyrLk7jdqvBAs1Qq9gf6ykMvjR7J"
)

// TransportGraphQLMock ...
type TransportGraphQLMock struct {
TransportGraphQL
client *GraphQLMockClient
}

// Init() ...
func (t *TransportGraphQLMock) Init() error {
t.client = &GraphQLMockClient{}
return nil
}

// GraphQLMockClient ...
type GraphQLMockClient struct {
Response interface{}
Request *graphql.Request
Error error
}

// Run ...
func (g *GraphQLMockClient) Run(_ context.Context, req *graphql.Request, resp interface{}) error {
j, _ := json.Marshal(g.Response) // nolint: errchkjson // used for testing only
_ = json.Unmarshal(j, &resp)
g.Request = req
return g.Error
}

// TestRegisterXpub will test the RegisterXpub method
func TestRegisterXpub(t *testing.T) {
// TestNewXpub will test the NewXpub method
func TestNewXpub(t *testing.T) {
xPriv, _ := bip32.NewKeyFromString(xPrivString)
// xPub, _ := xPriv.Neuter()

t.Run("no admin key", func(t *testing.T) {
client := TransportGraphQLMock{
TransportGraphQL: TransportGraphQL{},
}
err := client.RegisterXpub(context.Background(), xPubString, nil)
err := client.NewXpub(context.Background(), xPubString, nil)
assert.ErrorIs(t, err, ErrAdminKey)
})

Expand All @@ -68,7 +64,7 @@ func TestRegisterXpub(t *testing.T) {
},
},
}
err := client.RegisterXpub(context.Background(), xPubString, nil)
err := client.NewXpub(context.Background(), xPubString, nil)
assert.ErrorIs(t, err, errTestTerror)
})

Expand All @@ -87,7 +83,7 @@ func TestRegisterXpub(t *testing.T) {
client: &graphqlClient,
},
}
err := client.RegisterXpub(context.Background(), xPubString, nil)
err := client.NewXpub(context.Background(), xPubString, nil)
assert.NoError(t, err)
})
}
Expand Down
Loading

0 comments on commit 4115ec9

Please sign in to comment.