Skip to content

Commit

Permalink
feat(BUX-223): adjust methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pawellewandowski98 committed Feb 16, 2024
1 parent 3a1e7e6 commit 093a6af
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 129 deletions.
12 changes: 6 additions & 6 deletions examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"fmt"
buxmodels "github.com/BuxOrg/bux-models"
"github.com/BuxOrg/go-buxclient"
"github.com/BuxOrg/go-buxclient/xpriv"
)

func main() {
// Generate keys
keys, _ := xpriv.Generate()
// Replace with your admin keys
adminXpriv := "xprv9s21ZrQH143K3CbJXirfrtpLvhT3Vgusdo8coBritQ3rcS7Jy7sxWhatuxG5h2y1Cqj8FKmPp69536gmjYRpfga2MJdsGyBsnB12E19CESK"
adminXpub := "xpub661MyMwAqRbcFgfmdkPgE2m5UjHXu9dj124DbaGLSjaqVESTWfCD4VuNmEbVPkbYLCkykwVZvmA8Pbf8884TQr1FgdG2nPoHR8aB36YdDQh"

// Create a client
buxClient, _ := buxclient.New(
buxclient.WithXPriv(keys.XPriv()),
buxclient.WithXPriv(adminXpriv),
buxclient.WithHTTP("localhost:3003/v1"),
buxclient.WithSignRequest(true),
)

ctx := context.Background()

_ = buxClient.NewXpub(
ctx, keys.XPub().String(), &buxmodels.Metadata{"example_field": "example_data"},
_ = buxClient.AdminNewXpub(
ctx, adminXpub, &buxmodels.Metadata{"example_field": "example_data"},
)

xpubKey, err := buxClient.GetXPub(ctx)
Expand Down
13 changes: 0 additions & 13 deletions paymail_addresses.go

This file was deleted.

32 changes: 0 additions & 32 deletions paymail_addresses_test.go

This file was deleted.

38 changes: 0 additions & 38 deletions transports/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,6 @@ func (h *TransportHTTP) SetAdminKey(adminKey *bip32.ExtendedKey) {
h.adminXPriv = adminKey
}

// NewPaymail will register a new paymail
func (h *TransportHTTP) NewPaymail(ctx context.Context, rawXpub, paymailAddress, avatar, publicName string, metadata *buxmodels.Metadata) ResponseError {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldAddress: paymailAddress,
FieldAvatar: avatar,
FieldPublicName: publicName,
FieldMetadata: processMetadata(metadata),
FieldXpubKey: rawXpub,
})
if err != nil {
return WrapError(err)
}

var paymailData interface{}

return h.doHTTPRequest(
ctx, http.MethodPost, "/paymail", jsonStr, h.xPriv, true, &paymailData,
)
}

// DeletePaymail will delete a paymail address
func (h *TransportHTTP) DeletePaymail(ctx context.Context, paymailAddress string) ResponseError {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldAddress: paymailAddress,
})
if err != nil {
return WrapError(err)
}

if err := h.doHTTPRequest(
ctx, http.MethodDelete, "/paymail", jsonStr, h.xPriv, true, nil,
); err != nil {
return WrapError(err)
}

return nil
}

// GetXPub will get the xpub of the current xpub
func (h *TransportHTTP) GetXPub(ctx context.Context) (*buxmodels.Xpub, ResponseError) {
var xPub buxmodels.Xpub
Expand Down
22 changes: 8 additions & 14 deletions transports/http_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
buxmodels "github.com/BuxOrg/bux-models"
)

// NewXpub will register an xPub
func (h *TransportHTTP) NewXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) ResponseError {
// AdminNewXpub will register an xPub
func (h *TransportHTTP) AdminNewXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) ResponseError {
// Adding a xpub needs to be signed by an admin key
if h.adminXPriv == nil {
return WrapError(ErrAdminKey)
Expand All @@ -26,15 +26,10 @@ func (h *TransportHTTP) NewXpub(ctx context.Context, rawXPub string, metadata *b
var xPubData buxmodels.Xpub

return h.doHTTPRequest(
ctx, http.MethodPost, "/xpub", jsonStr, h.adminXPriv, true, &xPubData,
ctx, http.MethodPost, "/admin/xpub", jsonStr, h.adminXPriv, true, &xPubData,
)
}

// RegisterXpub alias for NewXpub
func (h *TransportHTTP) RegisterXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) ResponseError {
return h.NewXpub(ctx, rawXPub, metadata)
}

// AdminGetStatus get whether admin key is valid
func (h *TransportHTTP) AdminGetStatus(ctx context.Context) (bool, ResponseError) {
var status bool
Expand Down Expand Up @@ -177,22 +172,21 @@ func (h *TransportHTTP) AdminCreatePaymail(ctx context.Context, xPubID string, a
}

// AdminDeletePaymail delete a paymail address from the database
func (h *TransportHTTP) AdminDeletePaymail(ctx context.Context, address string) (*buxmodels.PaymailAddress, ResponseError) {
func (h *TransportHTTP) AdminDeletePaymail(ctx context.Context, address string) ResponseError {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldAddress: address,
})
if err != nil {
return nil, WrapError(err)
return WrapError(err)
}

var model *buxmodels.PaymailAddress
if err := h.doHTTPRequest(
ctx, http.MethodDelete, "/admin/paymail/delete", jsonStr, h.xPriv, true, &model,
ctx, http.MethodDelete, "/admin/paymail/delete", jsonStr, h.xPriv, true, nil,
); err != nil {
return nil, err
return err
}

return model, nil
return nil
}

// AdminGetTransactions get all block transactions filtered by conditions
Expand Down
12 changes: 2 additions & 10 deletions transports/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
// XpubService is the xPub related requests
type XpubService interface {
GetXPub(ctx context.Context) (*buxmodels.Xpub, ResponseError)
NewXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) ResponseError
RegisterXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) ResponseError
UpdateXPubMetadata(ctx context.Context, metadata *buxmodels.Metadata) (*buxmodels.Xpub, ResponseError)
}

Expand Down Expand Up @@ -51,12 +49,6 @@ type TransactionService interface {
GetUtxosCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError)
}

// PaymailService is the paymail related requests
type PaymailService interface {
NewPaymail(ctx context.Context, rawXpub, paymailAddress, avatar, publicName string, metadata *buxmodels.Metadata) ResponseError
DeletePaymail(ctx context.Context, paymailAddress string) ResponseError
}

// AdminService is the admin related requests
type AdminService interface {
AdminGetStatus(ctx context.Context) (bool, ResponseError)
Expand All @@ -71,11 +63,12 @@ type AdminService interface {
AdminGetPaymails(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata, queryParams *QueryParams) ([]*buxmodels.PaymailAddress, ResponseError)
AdminGetPaymailsCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError)
AdminCreatePaymail(ctx context.Context, xPubID string, address string, publicName string, avatar string) (*buxmodels.PaymailAddress, ResponseError)
AdminDeletePaymail(ctx context.Context, address string) (*buxmodels.PaymailAddress, ResponseError)
AdminDeletePaymail(ctx context.Context, address string) ResponseError
AdminGetTransactions(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata, queryParams *QueryParams) ([]*buxmodels.Transaction, ResponseError)
AdminGetTransactionsCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError)
AdminGetUtxos(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata, queryParams *QueryParams) ([]*buxmodels.Utxo, ResponseError)
AdminGetUtxosCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError)
AdminNewXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) ResponseError
AdminGetXPubs(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata, queryParams *QueryParams) ([]*buxmodels.Xpub, ResponseError)
AdminGetXPubsCount(ctx context.Context, conditions map[string]interface{}, metadata *buxmodels.Metadata) (int64, ResponseError)
AdminRecordTransaction(ctx context.Context, hex string) (*buxmodels.Transaction, ResponseError)
Expand All @@ -86,7 +79,6 @@ type TransportService interface {
AccessKeyService
AdminService
DestinationService
PaymailService
TransactionService
XpubService
Init() error
Expand Down
5 changes: 0 additions & 5 deletions xpubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ import (
"github.com/BuxOrg/go-buxclient/transports"
)

// NewXpub registers a new xpub - admin key needed
func (b *BuxClient) NewXpub(ctx context.Context, rawXPub string, metadata *buxmodels.Metadata) transports.ResponseError {
return b.transport.NewXpub(ctx, rawXPub, metadata)
}

// GetXPub gets the current xpub
func (b *BuxClient) GetXPub(ctx context.Context) (*buxmodels.Xpub, transports.ResponseError) {
return b.transport.GetXPub(ctx)
Expand Down
11 changes: 0 additions & 11 deletions xpubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ func TestXpub(t *testing.T) {
Client: WithHTTPClient,
}

t.Run("NewXpub", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, true)

// when
err := client.NewXpub(context.Background(), fixtures.XPubString, fixtures.TestMetadata)

// then
assert.NoError(t, err)
})

t.Run("GetXPub", func(t *testing.T) {
// given
client := getTestBuxClient(transportHandler, true)
Expand Down

0 comments on commit 093a6af

Please sign in to comment.