From 4115ec97aff8c889b790996e118b7401ac972967 Mon Sep 17 00:00:00 2001 From: mrz1836 Date: Mon, 28 Mar 2022 16:27:54 -0400 Subject: [PATCH] Fixed names, comments, broke files apart, upgraded deps, etc --- buxclient_test.go | 16 +- examples/register_xpub/register_xpub.go | 2 +- go.mod | 4 +- go.sum | 8 +- paymail_addresses.go | 6 +- transports/client_options.go | 140 +++++++++++++++++ transports/config.go | 29 ++++ transports/errors.go | 3 + transports/graphql.go | 8 +- transports/graphql_test.go | 14 +- transports/http.go | 52 ++++--- transports/interface.go | 65 ++++++++ transports/transports.go | 191 +----------------------- xpubs.go | 6 +- 14 files changed, 298 insertions(+), 246 deletions(-) create mode 100644 transports/client_options.go create mode 100644 transports/interface.go diff --git a/buxclient_test.go b/buxclient_test.go index ef69a0b..22ce206 100644 --- a/buxclient_test.go +++ b/buxclient_test.go @@ -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", @@ -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) }) } @@ -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{{ @@ -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 := "" @@ -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), @@ -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), diff --git a/examples/register_xpub/register_xpub.go b/examples/register_xpub/register_xpub.go index 9a46165..44cba82 100644 --- a/examples/register_xpub/register_xpub.go +++ b/examples/register_xpub/register_xpub.go @@ -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()) diff --git a/go.mod b/go.mod index 7d70eba..8e76eee 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/BuxOrg/go-buxclient go 1.17 require ( - github.com/BuxOrg/bux v0.1.11 + github.com/BuxOrg/bux v0.1.12 github.com/bitcoinschema/go-bitcoin/v2 v2.0.0-alpha.3 github.com/libsv/go-bk v0.1.6 github.com/libsv/go-bt/v2 v2.1.0-beta.2.0.20211221142324-0d686850c5e0 @@ -107,7 +107,7 @@ require ( golang.org/x/text v0.3.7 // indirect golang.org/x/tools v0.1.10 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect - google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb // indirect + google.golang.org/genproto v0.0.0-20220328180837-c47567c462d1 // indirect google.golang.org/grpc v1.45.0 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect diff --git a/go.sum b/go.sum index c31a0d9..1803f1e 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/99designs/gqlgen v0.17.2 h1:yczvlwMsfcVu/JtejqfrLwXuSP0yZFhmcss3caEvH github.com/99designs/gqlgen v0.17.2/go.mod h1:K5fzLKwtph+FFgh9j7nFbRUdBKvTcGnsta51fsMTn3o= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/BuxOrg/bux v0.1.11 h1:S9dZVWleBT/TUafZJkMqGgTRmPJsLHtsRYwHSqx0K1E= -github.com/BuxOrg/bux v0.1.11/go.mod h1:yvUy9+Zl/9dgG2CnBE1m31Nk84JecS+p7uVp1w1fbGg= +github.com/BuxOrg/bux v0.1.12 h1:8i5i6/FSnZZ3nQr89O1dU/392h/g/2lsuXXSSAq7+4o= +github.com/BuxOrg/bux v0.1.12/go.mod h1:9TGEgMoqWDCi0mb0PyGvnl2HFrMW2hDWV54WFPFzx8M= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/DataDog/datadog-go v3.7.1+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= @@ -626,8 +626,8 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98 google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20220216160803-4663080d8bc8/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI= -google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb h1:0m9wktIpOxGw+SSKmydXWB3Z3GTfcPP6+q75HCQa6HI= -google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E= +google.golang.org/genproto v0.0.0-20220328180837-c47567c462d1 h1:t2YeOD7KxzUL6g5yg4+aCD/Fc0gk+O31HMo09I/QVIM= +google.golang.org/genproto v0.0.0-20220328180837-c47567c462d1/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= diff --git a/paymail_addresses.go b/paymail_addresses.go index a701932..5262a5f 100644 --- a/paymail_addresses.go +++ b/paymail_addresses.go @@ -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) } diff --git a/transports/client_options.go b/transports/client_options.go new file mode 100644 index 0000000..3551247 --- /dev/null +++ b/transports/client_options.go @@ -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) + } + } + } +} diff --git a/transports/config.go b/transports/config.go index 52eb7bf..ea56400 100644 --- a/transports/config.go +++ b/transports/config.go @@ -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"` @@ -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" ) diff --git a/transports/errors.go b/transports/errors.go index 3a20a3f..ab2ee6e 100644 --- a/transports/errors.go +++ b/transports/errors.go @@ -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") diff --git a/transports/graphql.go b/transports/graphql.go index 7333bb6..b07d7a2 100644 --- a/transports/graphql.go +++ b/transports/graphql.go @@ -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 } @@ -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 { diff --git a/transports/graphql_test.go b/transports/graphql_test.go index 681e7e3..7aaecc9 100644 --- a/transports/graphql_test.go +++ b/transports/graphql_test.go @@ -18,26 +18,22 @@ 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) @@ -45,8 +41,8 @@ func (g *GraphQLMockClient) Run(_ context.Context, req *graphql.Request, resp in 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() @@ -54,7 +50,7 @@ func TestRegisterXpub(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) }) @@ -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) }) @@ -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) }) } diff --git a/transports/http.go b/transports/http.go index f43e7db..3a7b213 100644 --- a/transports/http.go +++ b/transports/http.go @@ -57,8 +57,8 @@ func (h *TransportHTTP) SetAdminKey(adminKey *bip32.ExtendedKey) { h.adminXPriv = adminKey } -// RegisterPaymail will register a new paymail -func (h *TransportHTTP) RegisterPaymail(ctx context.Context, rawXpub, paymailAddress string, metadata *bux.Metadata) error { +// NewPaymail will register a new paymail +func (h *TransportHTTP) NewPaymail(ctx context.Context, rawXpub, paymailAddress string, metadata *bux.Metadata) error { jsonStr, err := json.Marshal(map[string]interface{}{ FieldAddress: paymailAddress, FieldMetadata: processMetadata(metadata), @@ -78,8 +78,8 @@ func (h *TransportHTTP) RegisterPaymail(ctx context.Context, rawXpub, paymailAdd return nil } -// RegisterXpub will register an xPub -func (h *TransportHTTP) RegisterXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error { +// NewXpub will register an xPub +func (h *TransportHTTP) NewXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error { // Adding a xpub needs to be signed by an admin key if h.adminXPriv == nil { @@ -129,7 +129,7 @@ func (h *TransportHTTP) UpdateXPubMetadata(ctx context.Context, metadata *bux.Me } var xPub bux.Xpub - if err := h.doHTTPRequest( + if err = h.doHTTPRequest( ctx, http.MethodPatch, "/xpub", jsonStr, h.xPriv, true, &xPub, ); err != nil { return nil, err @@ -145,7 +145,7 @@ func (h *TransportHTTP) UpdateXPubMetadata(ctx context.Context, metadata *bux.Me func (h *TransportHTTP) GetAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) { var accessKey bux.AccessKey if err := h.doHTTPRequest( - ctx, http.MethodGet, "/access-key?id="+id, nil, h.xPriv, true, &accessKey, + ctx, http.MethodGet, "/access-key?"+FieldID+"="+id, nil, h.xPriv, true, &accessKey, ); err != nil { return nil, err } @@ -178,7 +178,7 @@ func (h *TransportHTTP) GetAccessKeys(ctx context.Context, metadataConditions *b func (h *TransportHTTP) RevokeAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) { var accessKey bux.AccessKey if err := h.doHTTPRequest( - ctx, http.MethodDelete, "/access-key?id="+id, nil, h.xPriv, true, &accessKey, + ctx, http.MethodDelete, "/access-key?"+FieldID+"="+id, nil, h.xPriv, true, &accessKey, ); err != nil { return nil, err } @@ -211,7 +211,7 @@ func (h *TransportHTTP) CreateAccessKey(ctx context.Context, metadata *bux.Metad func (h *TransportHTTP) GetDestinationByID(ctx context.Context, id string) (*bux.Destination, error) { var destination bux.Destination if err := h.doHTTPRequest( - ctx, http.MethodGet, "/destination?id="+id, nil, h.xPriv, true, &destination, + ctx, http.MethodGet, "/destination?"+FieldID+"="+id, nil, h.xPriv, true, &destination, ); err != nil { return nil, err } @@ -226,7 +226,7 @@ func (h *TransportHTTP) GetDestinationByID(ctx context.Context, id string) (*bux func (h *TransportHTTP) GetDestinationByAddress(ctx context.Context, address string) (*bux.Destination, error) { var destination bux.Destination if err := h.doHTTPRequest( - ctx, http.MethodGet, "/destination?address="+address, nil, h.xPriv, true, &destination, + ctx, http.MethodGet, "/destination?"+FieldAddress+"="+address, nil, h.xPriv, true, &destination, ); err != nil { return nil, err } @@ -241,7 +241,7 @@ func (h *TransportHTTP) GetDestinationByAddress(ctx context.Context, address str func (h *TransportHTTP) GetDestinationByLockingScript(ctx context.Context, lockingScript string) (*bux.Destination, error) { var destination bux.Destination if err := h.doHTTPRequest( - ctx, http.MethodGet, "/destination?locking_script="+lockingScript, nil, h.xPriv, true, &destination, + ctx, http.MethodGet, "/destination?"+FieldLockingScript+"="+lockingScript, nil, h.xPriv, true, &destination, ); err != nil { return nil, err } @@ -292,9 +292,10 @@ func (h *TransportHTTP) NewDestination(ctx context.Context, metadata *bux.Metada } // UpdateDestinationMetadataByID updates the destination metadata by id -func (h *TransportHTTP) UpdateDestinationMetadataByID(ctx context.Context, id string, metadata *bux.Metadata) (*bux.Destination, error) { +func (h *TransportHTTP) UpdateDestinationMetadataByID(ctx context.Context, id string, + metadata *bux.Metadata) (*bux.Destination, error) { jsonStr, err := json.Marshal(map[string]interface{}{ - "id": id, + FieldID: id, FieldMetadata: processMetadata(metadata), }) if err != nil { @@ -302,7 +303,7 @@ func (h *TransportHTTP) UpdateDestinationMetadataByID(ctx context.Context, id st } var destination bux.Destination - if err := h.doHTTPRequest( + if err = h.doHTTPRequest( ctx, http.MethodPatch, "/destination", jsonStr, h.xPriv, true, &destination, ); err != nil { return nil, err @@ -315,9 +316,10 @@ func (h *TransportHTTP) UpdateDestinationMetadataByID(ctx context.Context, id st } // UpdateDestinationMetadataByAddress updates the destination metadata by address -func (h *TransportHTTP) UpdateDestinationMetadataByAddress(ctx context.Context, address string, metadata *bux.Metadata) (*bux.Destination, error) { +func (h *TransportHTTP) UpdateDestinationMetadataByAddress(ctx context.Context, address string, + metadata *bux.Metadata) (*bux.Destination, error) { jsonStr, err := json.Marshal(map[string]interface{}{ - "address": address, + FieldAddress: address, FieldMetadata: processMetadata(metadata), }) if err != nil { @@ -325,7 +327,7 @@ func (h *TransportHTTP) UpdateDestinationMetadataByAddress(ctx context.Context, } var destination bux.Destination - if err := h.doHTTPRequest( + if err = h.doHTTPRequest( ctx, http.MethodPatch, "/destination", jsonStr, h.xPriv, true, &destination, ); err != nil { return nil, err @@ -338,17 +340,18 @@ func (h *TransportHTTP) UpdateDestinationMetadataByAddress(ctx context.Context, } // UpdateDestinationMetadataByLockingScript updates the destination metadata by locking script -func (h *TransportHTTP) UpdateDestinationMetadataByLockingScript(ctx context.Context, lockingScript string, metadata *bux.Metadata) (*bux.Destination, error) { +func (h *TransportHTTP) UpdateDestinationMetadataByLockingScript(ctx context.Context, lockingScript string, + metadata *bux.Metadata) (*bux.Destination, error) { jsonStr, err := json.Marshal(map[string]interface{}{ - "lockingScript": lockingScript, - FieldMetadata: processMetadata(metadata), + FieldLockingScript: lockingScript, + FieldMetadata: processMetadata(metadata), }) if err != nil { return nil, err } var destination bux.Destination - if err := h.doHTTPRequest( + if err = h.doHTTPRequest( ctx, http.MethodPatch, "/destination", jsonStr, h.xPriv, true, &destination, ); err != nil { return nil, err @@ -364,7 +367,7 @@ func (h *TransportHTTP) UpdateDestinationMetadataByLockingScript(ctx context.Con func (h *TransportHTTP) GetTransaction(ctx context.Context, txID string) (*bux.Transaction, error) { var transaction bux.Transaction if err := h.doHTTPRequest( - ctx, http.MethodGet, "/transaction?id="+txID, nil, h.xPriv, h.signRequest, &transaction, + ctx, http.MethodGet, "/transaction?"+FieldID+"="+txID, nil, h.xPriv, h.signRequest, &transaction, ); err != nil { return nil, err } @@ -480,9 +483,10 @@ func (h *TransportHTTP) RecordTransaction(ctx context.Context, hex, referenceID } // UpdateTransactionMetadata update the metadata of a transaction -func (h *TransportHTTP) UpdateTransactionMetadata(ctx context.Context, txID string, metadata *bux.Metadata) (*bux.Transaction, error) { +func (h *TransportHTTP) UpdateTransactionMetadata(ctx context.Context, txID string, + metadata *bux.Metadata) (*bux.Transaction, error) { jsonStr, err := json.Marshal(map[string]interface{}{ - "tx_id": txID, + FieldID: txID, FieldMetadata: processMetadata(metadata), }) if err != nil { @@ -490,7 +494,7 @@ func (h *TransportHTTP) UpdateTransactionMetadata(ctx context.Context, txID stri } var transaction bux.Transaction - if err := h.doHTTPRequest( + if err = h.doHTTPRequest( ctx, http.MethodPatch, "/transaction", jsonStr, h.xPriv, h.signRequest, &transaction, ); err != nil { return nil, err diff --git a/transports/interface.go b/transports/interface.go new file mode 100644 index 0000000..18cea34 --- /dev/null +++ b/transports/interface.go @@ -0,0 +1,65 @@ +package transports + +import ( + "context" + + "github.com/BuxOrg/bux" + "github.com/libsv/go-bk/bip32" +) + +// XpubService is the xPub related requests +type XpubService interface { + GetXPub(ctx context.Context) (*bux.Xpub, error) + NewXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error + UpdateXPubMetadata(ctx context.Context, metadata *bux.Metadata) (*bux.Xpub, error) +} + +// AccessKeyService is the access key related requests +type AccessKeyService interface { + CreateAccessKey(ctx context.Context, metadata *bux.Metadata) (*bux.AccessKey, error) + GetAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) + GetAccessKeys(ctx context.Context, metadataConditions *bux.Metadata) ([]*bux.AccessKey, error) + RevokeAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) +} + +// DestinationService is the destination related requests +type DestinationService interface { + GetDestinationByAddress(ctx context.Context, address string) (*bux.Destination, error) + GetDestinationByID(ctx context.Context, id string) (*bux.Destination, error) + GetDestinationByLockingScript(ctx context.Context, lockingScript string) (*bux.Destination, error) + GetDestinations(ctx context.Context, metadataConditions *bux.Metadata) ([]*bux.Destination, error) + NewDestination(ctx context.Context, metadata *bux.Metadata) (*bux.Destination, error) + UpdateDestinationMetadataByAddress(ctx context.Context, lockingScript string, metadata *bux.Metadata) (*bux.Destination, error) + UpdateDestinationMetadataByID(ctx context.Context, id string, metadata *bux.Metadata) (*bux.Destination, error) + UpdateDestinationMetadataByLockingScript(ctx context.Context, address string, metadata *bux.Metadata) (*bux.Destination, error) +} + +// TransactionService is the transaction related requests +type TransactionService interface { + DraftToRecipients(ctx context.Context, recipients []*Recipients, metadata *bux.Metadata) (*bux.DraftTransaction, error) + DraftTransaction(ctx context.Context, transactionConfig *bux.TransactionConfig, metadata *bux.Metadata) (*bux.DraftTransaction, error) + GetTransaction(ctx context.Context, txID string) (*bux.Transaction, error) + GetTransactions(ctx context.Context, conditions map[string]interface{}, metadataConditions *bux.Metadata) ([]*bux.Transaction, error) + RecordTransaction(ctx context.Context, hex, referenceID string, metadata *bux.Metadata) (*bux.Transaction, error) + UpdateTransactionMetadata(ctx context.Context, txID string, metadata *bux.Metadata) (*bux.Transaction, error) +} + +// PaymailService is the paymail related requests +type PaymailService interface { + NewPaymail(ctx context.Context, rawXpub, paymailAddress string, metadata *bux.Metadata) error +} + +// TransportService the transport service interface +type TransportService interface { + AccessKeyService + DestinationService + PaymailService + TransactionService + XpubService + Init() error + IsDebug() bool + IsSignRequest() bool + SetAdminKey(adminKey *bip32.ExtendedKey) + SetDebug(debug bool) + SetSignRequest(debug bool) +} diff --git a/transports/transports.go b/transports/transports.go index 6a033cb..6893376 100644 --- a/transports/transports.go +++ b/transports/transports.go @@ -1,8 +1,6 @@ package transports import ( - "context" - "errors" "net/http" "github.com/BuxOrg/bux" @@ -10,26 +8,6 @@ import ( "github.com/libsv/go-bk/bip32" ) -// 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.6" - -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" -) - // Client is the transport client type Client struct { accessKey *bec.PrivateKey @@ -50,38 +28,6 @@ func addSignature(header *http.Header, xPriv *bip32.ExtendedKey, bodyString stri return bux.SetSignature(header, xPriv, bodyString) } -// TransportService the transport service interface -type TransportService interface { - Init() error - SetAdminKey(adminKey *bip32.ExtendedKey) - SetDebug(debug bool) - IsDebug() bool - SetSignRequest(debug bool) - IsSignRequest() bool - RegisterXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error - RegisterPaymail(ctx context.Context, rawXpub, paymailAddress string, metadata *bux.Metadata) error - GetXPub(ctx context.Context) (*bux.Xpub, error) - UpdateXPubMetadata(ctx context.Context, metadata *bux.Metadata) (*bux.Xpub, error) - GetAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) - GetAccessKeys(ctx context.Context, metadataConditions *bux.Metadata) ([]*bux.AccessKey, error) - CreateAccessKey(ctx context.Context, metadata *bux.Metadata) (*bux.AccessKey, error) - RevokeAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) - GetDestinationByID(ctx context.Context, id string) (*bux.Destination, error) - GetDestinationByAddress(ctx context.Context, address string) (*bux.Destination, error) - GetDestinationByLockingScript(ctx context.Context, lockingScript string) (*bux.Destination, error) - GetDestinations(ctx context.Context, metadataConditions *bux.Metadata) ([]*bux.Destination, error) - NewDestination(ctx context.Context, metadata *bux.Metadata) (*bux.Destination, error) - UpdateDestinationMetadataByID(ctx context.Context, id string, metadata *bux.Metadata) (*bux.Destination, error) - UpdateDestinationMetadataByLockingScript(ctx context.Context, address string, metadata *bux.Metadata) (*bux.Destination, error) - UpdateDestinationMetadataByAddress(ctx context.Context, lockingScript string, metadata *bux.Metadata) (*bux.Destination, error) - GetTransaction(ctx context.Context, txID string) (*bux.Transaction, error) - GetTransactions(ctx context.Context, conditions map[string]interface{}, metadataConditions *bux.Metadata) ([]*bux.Transaction, error) - DraftToRecipients(ctx context.Context, recipients []*Recipients, metadata *bux.Metadata) (*bux.DraftTransaction, error) - DraftTransaction(ctx context.Context, transactionConfig *bux.TransactionConfig, metadata *bux.Metadata) (*bux.DraftTransaction, error) - RecordTransaction(ctx context.Context, hex, referenceID string, metadata *bux.Metadata) (*bux.Transaction, error) - UpdateTransactionMetadata(ctx context.Context, txID string, metadata *bux.Metadata) (*bux.Transaction, error) -} - // NewTransport create a new transport service object func NewTransport(opts ...ClientOps) (TransportService, error) { client := Client{} @@ -91,7 +37,7 @@ func NewTransport(opts ...ClientOps) (TransportService, error) { } if client.transport == nil { - return nil, errors.New("no transport client set") + return nil, ErrNoClientSet } if err := client.transport.Init(); err != nil { @@ -115,145 +61,14 @@ func NewTransportService(transportService TransportService) TransportService { return transportService } +// processMetadata will process the metadata func processMetadata(metadata *bux.Metadata) *bux.Metadata { if metadata == nil { m := make(bux.Metadata) metadata = &m } - (*metadata)["user_agent"] = BuxUserAgent + (*metadata)[FieldUserAgent] = BuxUserAgent return metadata } - -// 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) - } - } - } -} diff --git a/xpubs.go b/xpubs.go index e18ac92..1e01888 100644 --- a/xpubs.go +++ b/xpubs.go @@ -6,9 +6,9 @@ import ( "github.com/BuxOrg/bux" ) -// RegisterXpub registers a new xpub - admin key needed -func (b *BuxClient) RegisterXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error { - return b.transport.RegisterXpub(ctx, rawXPub, metadata) +// NewXpub registers a new xpub - admin key needed +func (b *BuxClient) NewXpub(ctx context.Context, rawXPub string, metadata *bux.Metadata) error { + return b.transport.NewXpub(ctx, rawXPub, metadata) } // GetXPub gets the current xpub