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 22, 2024
1 parent e801606 commit fa95446
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 135 deletions.
12 changes: 5 additions & 7 deletions examples/register_xpub/register_xpub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@ import (
"fmt"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
"github.com/bitcoin-sv/spv-wallet/models"
)

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

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

ctx := context.Background()

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

xpubKey, err := walletClient.GetXPub(ctx)
if err != nil {
Expand Down
15 changes: 0 additions & 15 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 *models.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) (*models.Xpub, ResponseError) {
var xPub models.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 (
"github.com/bitcoin-sv/spv-wallet/models"
)

// NewXpub will register an xPub
func (h *TransportHTTP) NewXpub(ctx context.Context, rawXPub string, metadata *models.Metadata) ResponseError {
// AdminNewXpub will register an xPub
func (h *TransportHTTP) AdminNewXpub(ctx context.Context, rawXPub string, metadata *models.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 *m
var xPubData models.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 *models.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) (*models.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 *models.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
15 changes: 2 additions & 13 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) (*models.Xpub, ResponseError)
NewXpub(ctx context.Context, rawXPub string, metadata *models.Metadata) ResponseError
RegisterXpub(ctx context.Context, rawXPub string, metadata *models.Metadata) ResponseError
UpdateXPubMetadata(ctx context.Context, metadata *models.Metadata) (*models.Xpub, ResponseError)
}

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

// PaymailService is the paymail related requests
type PaymailService interface {
NewPaymail(ctx context.Context, rawXpub, paymailAddress, avatar, publicName string, metadata *models.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 @@ -70,15 +62,13 @@ type AdminService interface {
AdminGetPaymail(ctx context.Context, address string) (*models.PaymailAddress, ResponseError)
AdminGetPaymails(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.PaymailAddress, ResponseError)
AdminGetPaymailsCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError)
// AdminCreatePaymail Create a paymail.
//
// Paymail address (ie. [email protected])
AdminCreatePaymail(ctx context.Context, xPubID string, address string, publicName string, avatar string) (*models.PaymailAddress, ResponseError)
AdminDeletePaymail(ctx context.Context, address string) (*models.PaymailAddress, ResponseError)
AdminDeletePaymail(ctx context.Context, address string) ResponseError
AdminGetTransactions(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Transaction, ResponseError)
AdminGetTransactionsCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError)
AdminGetUtxos(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Utxo, ResponseError)
AdminGetUtxosCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError)
AdminNewXpub(ctx context.Context, rawXPub string, metadata *models.Metadata) ResponseError
AdminGetXPubs(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata, queryParams *QueryParams) ([]*models.Xpub, ResponseError)
AdminGetXPubsCount(ctx context.Context, conditions map[string]interface{}, metadata *models.Metadata) (int64, ResponseError)
AdminRecordTransaction(ctx context.Context, hex string) (*models.Transaction, ResponseError)
Expand All @@ -89,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/bitcoin-sv/spv-wallet/models"
)

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

// GetXPub gets the current xpub
func (b *WalletClient) GetXPub(ctx context.Context) (*models.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 := getTestWalletClient(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 := getTestWalletClient(transportHandler, true)
Expand Down

0 comments on commit fa95446

Please sign in to comment.