From 1b1782c4ddc44d3de01fbdbd12257874f1c79d3a Mon Sep 17 00:00:00 2001 From: Siggi Date: Mon, 28 Mar 2022 22:07:16 +0200 Subject: [PATCH] Added new metadata update actions --- .golangci.yml | 2 +- destinations.go | 15 ++++ transactions.go | 5 ++ transports/graphql.go | 173 +++++++++++++++++++++++++++++++++++++++ transports/http.go | 114 ++++++++++++++++++++++++++ transports/transports.go | 5 ++ xpubs.go | 5 ++ 7 files changed, 318 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 4f89c5d..006dcd8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -84,7 +84,7 @@ linters-settings: max-blank-identifiers: 2 dupl: # tokens count to trigger issue, 150 by default - threshold: 100 + threshold: 150 errcheck: # report about not checking of errors in type assertions: `a := b.(MyStruct)`; # default is false: such cases aren't reported by default. diff --git a/destinations.go b/destinations.go index 331a0a5..a216fa6 100644 --- a/destinations.go +++ b/destinations.go @@ -30,3 +30,18 @@ func (b *BuxClient) GetDestinations(ctx context.Context, metadataConditions *bux func (b *BuxClient) NewDestination(ctx context.Context, metadata *bux.Metadata) (*bux.Destination, error) { return b.transport.NewDestination(ctx, metadata) } + +// UpdateDestinationMetadataByID updates the destination metadata by id +func (b *BuxClient) UpdateDestinationMetadataByID(ctx context.Context, id string, metadata *bux.Metadata) (*bux.Destination, error) { + return b.transport.UpdateDestinationMetadataByID(ctx, id, metadata) +} + +// UpdateDestinationMetadataByAddress updates the destination metadata by address +func (b *BuxClient) UpdateDestinationMetadataByAddress(ctx context.Context, address string, metadata *bux.Metadata) (*bux.Destination, error) { + return b.transport.UpdateDestinationMetadataByAddress(ctx, address, metadata) +} + +// UpdateDestinationMetadataByLockingScript updates the destination metadata by locking script +func (b *BuxClient) UpdateDestinationMetadataByLockingScript(ctx context.Context, lockingScript string, metadata *bux.Metadata) (*bux.Destination, error) { + return b.transport.UpdateDestinationMetadataByLockingScript(ctx, lockingScript, metadata) +} diff --git a/transactions.go b/transactions.go index 5981dda..1fa011a 100644 --- a/transactions.go +++ b/transactions.go @@ -46,6 +46,11 @@ func (b *BuxClient) RecordTransaction(ctx context.Context, hex, draftID string, return b.transport.RecordTransaction(ctx, hex, draftID, metadata) } +// UpdateTransactionMetadata update the metadata of a transaction +func (b *BuxClient) UpdateTransactionMetadata(ctx context.Context, txID string, metadata *bux.Metadata) (*bux.Transaction, error) { + return b.transport.UpdateTransactionMetadata(ctx, txID, metadata) +} + // FinalizeTransaction will finalize the transaction func (b *BuxClient) FinalizeTransaction(draft *bux.DraftTransaction) (string, error) { txDraft, err := bt.NewTxFromString(draft.Hex) diff --git a/transports/graphql.go b/transports/graphql.go index 86ac9d0..7333bb6 100644 --- a/transports/graphql.go +++ b/transports/graphql.go @@ -183,6 +183,36 @@ func (g *TransportGraphQL) GetXPub(ctx context.Context) (*bux.Xpub, error) { return respData.XPub, nil } +// UpdateXPubMetadata update the metadata of the logged in xpub +func (g *TransportGraphQL) UpdateXPubMetadata(ctx context.Context, metadata *bux.Metadata) (*bux.Xpub, error) { + + reqBody := ` + mutation ($metadata: Metadata!) { + xpub_metadata ( + metadata: $metadata + ) { + id + current_balance + next_internal_num + next_external_num + metadata + created_at + updated_at + deleted_at + } + }` + variables := map[string]interface{}{ + FieldMetadata: processMetadata(metadata), + } + + var respData XPubData + if err := g.doGraphQLQuery(ctx, reqBody, variables, &respData); err != nil { + return nil, err + } + + return respData.XPub, nil +} + // GetAccessKey will get an access key by id func (g *TransportGraphQL) GetAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) { @@ -401,6 +431,111 @@ func (g *TransportGraphQL) GetDestinationByAddress(ctx context.Context, address return respData.Destination, nil } +// UpdateDestinationMetadataByID updates the destination metadata by id +func (g *TransportGraphQL) UpdateDestinationMetadataByID(ctx context.Context, id string, metadata *bux.Metadata) (*bux.Destination, error) { + + reqBody := `{ + mutation ($id: String, $metadata: Metadata!) { + destination_metadata ( + id: $id + metadata: $metadata + ) { + id + xpub_id + locking_script + type + chain + num + address + metadata + created_at + updated_at + deleted_at + } + }` + variables := map[string]interface{}{ + "id": id, + FieldMetadata: processMetadata(metadata), + } + + var respData DestinationData + if err := g.doGraphQLQuery(ctx, reqBody, variables, &respData); err != nil { + return nil, err + } + + return respData.Destination, nil +} + +// UpdateDestinationMetadataByAddress updates the destination metadata by address +func (g *TransportGraphQL) UpdateDestinationMetadataByAddress(ctx context.Context, address string, metadata *bux.Metadata) (*bux.Destination, error) { + + reqBody := `{ + mutation ($address: String, $metadata: Metadata!) { + destination_metadata ( + address: $address + metadata: $metadata + ) { + id + xpub_id + locking_script + type + chain + num + address + metadata + created_at + updated_at + deleted_at + } + }` + variables := map[string]interface{}{ + "address": address, + FieldMetadata: processMetadata(metadata), + } + + var respData DestinationData + if err := g.doGraphQLQuery(ctx, reqBody, variables, &respData); err != nil { + return nil, err + } + + return respData.Destination, nil +} + +// UpdateDestinationMetadataByLockingScript updates the destination metadata by lockingScript +func (g *TransportGraphQL) UpdateDestinationMetadataByLockingScript(ctx context.Context, lockingScript string, metadata *bux.Metadata) (*bux.Destination, error) { + + reqBody := `{ + mutation ($locking_script: String, $metadata: Metadata!) { + destination_metadata ( + locking_script: $locking_script + metadata: $metadata + ) { + id + xpub_id + locking_script + type + chain + num + address + metadata + created_at + updated_at + deleted_at + } + }` + variables := map[string]interface{}{ + "locking_script": lockingScript, + FieldMetadata: processMetadata(metadata), + } + + var respData DestinationData + if err := g.doGraphQLQuery(ctx, reqBody, variables, &respData); err != nil { + return nil, err + } + + return respData.Destination, nil +} + // GetDestinations will get all destinations filtered by the medata conditions func (g *TransportGraphQL) GetDestinations(ctx context.Context, metadataConditions *bux.Metadata) ([]*bux.Destination, error) { @@ -670,6 +805,44 @@ func (g *TransportGraphQL) RecordTransaction(ctx context.Context, hex, reference return transaction, nil } +// UpdateTransactionMetadata update the metadata of a transaction +func (g *TransportGraphQL) UpdateTransactionMetadata(ctx context.Context, txID string, metadata *bux.Metadata) (*bux.Transaction, error) { + + reqBody := ` + mutation ($tx_id: String!, $metadata: Metadata!) { + destination_metadata ( + tx_id: $tx_id + metadata: $metadata + ) { + id + hex + block_hash + block_height + fee + number_of_inputs + number_of_outputs + output_value + total_value + direction + metadata + created_at + updated_at + deleted_at + } + }` + variables := map[string]interface{}{ + "tx_id": txID, + FieldMetadata: processMetadata(metadata), + } + + var respData TransactionData + if err := g.doGraphQLQuery(ctx, reqBody, variables, &respData); err != nil { + return nil, err + } + + return respData.Transaction, nil +} + func (g *TransportGraphQL) doGraphQLQuery(ctx context.Context, reqBody string, variables map[string]interface{}, respData interface{}) error { diff --git a/transports/http.go b/transports/http.go index b5557ba..f43e7db 100644 --- a/transports/http.go +++ b/transports/http.go @@ -119,6 +119,28 @@ func (h *TransportHTTP) GetXPub(ctx context.Context) (*bux.Xpub, error) { return &xPub, nil } +// UpdateXPubMetadata update the metadata of the logged in xpub +func (h *TransportHTTP) UpdateXPubMetadata(ctx context.Context, metadata *bux.Metadata) (*bux.Xpub, error) { + jsonStr, err := json.Marshal(map[string]interface{}{ + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, err + } + + var xPub bux.Xpub + if err := h.doHTTPRequest( + ctx, http.MethodPatch, "/xpub", jsonStr, h.xPriv, true, &xPub, + ); err != nil { + return nil, err + } + if h.debug { + log.Printf("xpub: %v\n", xPub) + } + + return &xPub, nil +} + // GetAccessKey will get an access key by id func (h *TransportHTTP) GetAccessKey(ctx context.Context, id string) (*bux.AccessKey, error) { var accessKey bux.AccessKey @@ -269,6 +291,75 @@ func (h *TransportHTTP) NewDestination(ctx context.Context, metadata *bux.Metada return &destination, nil } +// UpdateDestinationMetadataByID updates the destination metadata by id +func (h *TransportHTTP) UpdateDestinationMetadataByID(ctx context.Context, id string, metadata *bux.Metadata) (*bux.Destination, error) { + jsonStr, err := json.Marshal(map[string]interface{}{ + "id": id, + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, err + } + + var destination bux.Destination + if err := h.doHTTPRequest( + ctx, http.MethodPatch, "/destination", jsonStr, h.xPriv, true, &destination, + ); err != nil { + return nil, err + } + if h.debug { + log.Printf("destination: %v\n", destination) + } + + return &destination, nil +} + +// UpdateDestinationMetadataByAddress updates the destination metadata by address +func (h *TransportHTTP) UpdateDestinationMetadataByAddress(ctx context.Context, address string, metadata *bux.Metadata) (*bux.Destination, error) { + jsonStr, err := json.Marshal(map[string]interface{}{ + "address": address, + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, err + } + + var destination bux.Destination + if err := h.doHTTPRequest( + ctx, http.MethodPatch, "/destination", jsonStr, h.xPriv, true, &destination, + ); err != nil { + return nil, err + } + if h.debug { + log.Printf("destination: %v\n", destination) + } + + return &destination, nil +} + +// UpdateDestinationMetadataByLockingScript updates the destination metadata by locking script +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), + }) + if err != nil { + return nil, err + } + + var destination bux.Destination + if err := h.doHTTPRequest( + ctx, http.MethodPatch, "/destination", jsonStr, h.xPriv, true, &destination, + ); err != nil { + return nil, err + } + if h.debug { + log.Printf("destination: %v\n", destination) + } + + return &destination, nil +} + // GetTransaction will get a transaction by ID func (h *TransportHTTP) GetTransaction(ctx context.Context, txID string) (*bux.Transaction, error) { var transaction bux.Transaction @@ -388,6 +479,29 @@ func (h *TransportHTTP) RecordTransaction(ctx context.Context, hex, referenceID return &transaction, nil } +// UpdateTransactionMetadata update the metadata of a transaction +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, + FieldMetadata: processMetadata(metadata), + }) + if err != nil { + return nil, err + } + + var transaction bux.Transaction + if err := h.doHTTPRequest( + ctx, http.MethodPatch, "/transaction", jsonStr, h.xPriv, h.signRequest, &transaction, + ); err != nil { + return nil, err + } + if h.debug { + log.Printf("Transaction: %v\n", transaction) + } + + return &transaction, nil +} + // doHTTPRequest will create and submit the HTTP request func (h *TransportHTTP) doHTTPRequest(ctx context.Context, method string, path string, rawJSON []byte, xPriv *bip32.ExtendedKey, sign bool, responseJSON interface{}) error { diff --git a/transports/transports.go b/transports/transports.go index 0a6e2eb..6a033cb 100644 --- a/transports/transports.go +++ b/transports/transports.go @@ -61,6 +61,7 @@ type TransportService interface { 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) @@ -70,11 +71,15 @@ type TransportService interface { 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 diff --git a/xpubs.go b/xpubs.go index d64bd72..e18ac92 100644 --- a/xpubs.go +++ b/xpubs.go @@ -15,3 +15,8 @@ func (b *BuxClient) RegisterXpub(ctx context.Context, rawXPub string, metadata * func (b *BuxClient) GetXPub(ctx context.Context) (*bux.Xpub, error) { return b.transport.GetXPub(ctx) } + +// UpdateXPubMetadata update the metadata of the logged in xpub +func (b *BuxClient) UpdateXPubMetadata(ctx context.Context, metadata *bux.Metadata) (*bux.Xpub, error) { + return b.transport.UpdateXPubMetadata(ctx, metadata) +}