Skip to content

Commit

Permalink
Merge pull request #20 from BuxOrg/update-metadata
Browse files Browse the repository at this point in the history
Added new metadata update actions
  • Loading branch information
mrz1836 authored Mar 28, 2022
2 parents 033bfbe + 1b1782c commit bf7085c
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
173 changes: 173 additions & 0 deletions transports/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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 {

Expand Down
114 changes: 114 additions & 0 deletions transports/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions transports/transports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading

0 comments on commit bf7085c

Please sign in to comment.