Skip to content

Commit

Permalink
chore(BUX-585): update spv-wallet-go-client dependency (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski authored Feb 23, 2024
1 parent 33d4eea commit f7803a2
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 106 deletions.
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const (
EnvAdminXpriv = "spvwallet.admin.xpriv"
// EnvServerUrl define the url of the spv-wallet (non-custodial wallet) service.
EnvServerUrl = "spvwallet.server.url"
// EnvWithDebug define whether to turn debugging on.
EnvWithDebug = "spvwallet.withDebug"
// EnvSignRequest define whether to sign all requests.
EnvSignRequest = "spvwallet.sign.request"
// EnvPaymailDomain define the paymail domain.
Expand Down
1 change: 0 additions & 1 deletion config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func setHttpServerDefaults() {
func setSpvWalletDefaults() {
viper.SetDefault(EnvAdminXpriv, "xprv9s21ZrQH143K3CbJXirfrtpLvhT3Vgusdo8coBritQ3rcS7Jy7sxWhatuxG5h2y1Cqj8FKmPp69536gmjYRpfga2MJdsGyBsnB12E19CESK")
viper.SetDefault(EnvServerUrl, "http://localhost:3003/v1")
viper.SetDefault(EnvWithDebug, true)
viper.SetDefault(EnvSignRequest, true)
viper.SetDefault(EnvPaymailDomain, "example.com")
viper.SetDefault(EnvPaymailAvatar, "http://localhost:3003/static/paymail/avatar.jpg")
Expand Down
37 changes: 6 additions & 31 deletions domain/transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/rs/zerolog"

walletmodels "github.com/BuxOrg/bux-models"
"github.com/BuxOrg/go-buxclient/transports"
"github.com/avast/retry-go/v4"
"github.com/bitcoin-sv/spv-wallet-go-client/transports"
"github.com/bitcoin-sv/spv-wallet/models"
)

// TransactionService represents service whoch contains methods linked with transactions.
Expand Down Expand Up @@ -45,7 +45,7 @@ func (s *TransactionService) CreateTransaction(userPaymail, xpriv, recipient str
},
}

metadata := &walletmodels.Metadata{
metadata := &models.Metadata{
"receiver": recipient,
"sender": userPaymail,
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *TransactionService) GetTransactions(accessKey, userPaymail string, quer
return pTransactions, nil
}

func tryRecordTransaction(userWalletClient users.UserWalletClient, draftTx users.DraftTransaction, metadata *walletmodels.Metadata, log *zerolog.Logger) (*walletmodels.Transaction, error) {
func tryRecordTransaction(userWalletClient users.UserWalletClient, draftTx users.DraftTransaction, metadata *models.Metadata, log *zerolog.Logger) (*models.Transaction, error) {
retries := uint(3)
tx, recordErr := tryRecord(userWalletClient, draftTx, metadata, log, retries)

Expand All @@ -123,12 +123,6 @@ func tryRecordTransaction(userWalletClient users.UserWalletClient, draftTx users
Str("draftTxId", draftTx.GetDraftTransactionId()).
Msgf("record transaction failed: %s", recordErr.Error())

unreserveErr := tryUnreserve(userWalletClient, draftTx, log, retries)
if unreserveErr != nil {
log.Error().
Str("draftTxId", draftTx.GetDraftTransactionId()).
Msgf("unreserve transaction failed: %s", unreserveErr.Error())
}
return nil, recordErr
}

Expand All @@ -138,12 +132,12 @@ func tryRecordTransaction(userWalletClient users.UserWalletClient, draftTx users
return tx, nil
}

func tryRecord(userWalletClient users.UserWalletClient, draftTx users.DraftTransaction, metadata *walletmodels.Metadata, log *zerolog.Logger, retries uint) (*walletmodels.Transaction, error) {
func tryRecord(userWalletClient users.UserWalletClient, draftTx users.DraftTransaction, metadata *models.Metadata, log *zerolog.Logger, retries uint) (*models.Transaction, error) {
log.Debug().
Str("draftTxId", draftTx.GetDraftTransactionId()).
Msg("record transaction")

tx := &walletmodels.Transaction{}
tx := &models.Transaction{}
err := retry.Do(
func() error {
var err error
Expand All @@ -160,22 +154,3 @@ func tryRecord(userWalletClient users.UserWalletClient, draftTx users.DraftTrans
)
return tx, err
}

func tryUnreserve(userWalletClient users.UserWalletClient, draftTx users.DraftTransaction, log *zerolog.Logger, retries uint) error {
log.Debug().
Str("draftTxId", draftTx.GetDraftTransactionId()).
Msg("unreserve UTXOs from draft")

return retry.Do(
func() error {
return userWalletClient.UnreserveUtxos(draftTx.GetDraftTransactionId())
},
retry.Attempts(retries),
retry.Delay(1*time.Second),
retry.OnRetry(func(n uint, err error) {
log.Warn().
Str("draftTxId", draftTx.GetDraftTransactionId()).
Msgf("%d retry UnreserveUtxos after error: %v", n, err.Error())
}),
)
}
9 changes: 4 additions & 5 deletions domain/users/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package users
import (
"time"

walletmodels "github.com/BuxOrg/bux-models"
"github.com/BuxOrg/go-buxclient/transports"
"github.com/bitcoin-sv/spv-wallet-go-client/transports"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/libsv/go-bk/bip32"
)

Expand Down Expand Up @@ -67,9 +67,8 @@ type (
GetTransactions(queryParam transports.QueryParams, userPaymail string) ([]Transaction, error)
GetTransaction(transactionId, userPaymail string) (FullTransaction, error)
GetTransactionsCount() (int64, error)
CreateAndFinalizeTransaction(recipients []*transports.Recipients, metadata *walletmodels.Metadata) (DraftTransaction, error)
RecordTransaction(hex, draftTxId string, metadata *walletmodels.Metadata) (*walletmodels.Transaction, error)
UnreserveUtxos(draftTxId string) error
CreateAndFinalizeTransaction(recipients []*transports.Recipients, metadata *models.Metadata) (DraftTransaction, error)
RecordTransaction(hex, draftTxId string, metadata *models.Metadata) (*models.Transaction, error)
}

// AdminWalletClient defines methods which are available for an admin with admin key.
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/bitcoin-sv/spv-wallet-web-backend
go 1.21.5

require (
github.com/BuxOrg/bux-models v0.3.0
github.com/BuxOrg/go-buxclient v0.4.0
github.com/avast/retry-go/v4 v4.5.1
github.com/bitcoin-sv/spv-wallet-go-client v0.5.0
github.com/bitcoin-sv/spv-wallet/models v0.15.0
github.com/brianvoe/gofakeit/v6 v6.26.3
github.com/centrifugal/centrifuge v0.30.6
github.com/gin-contrib/sessions v0.0.5
Expand Down Expand Up @@ -57,7 +57,6 @@ require (
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/libsv/go-bt/v2 v2.2.5 // indirect
github.com/machinebox/graphql v0.2.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/BuxOrg/bux-models v0.3.0 h1:+pvpDdYaiIgeOhAO847RK07Vzc+vuUvy2ok/xJevQyg=
github.com/BuxOrg/bux-models v0.3.0/go.mod h1:JCpoXxVnKZmCy3rg6nOtLQL5AXh6iEo2tBvcD/qAxEY=
github.com/BuxOrg/go-buxclient v0.4.0 h1:gn5cXjf+9u7ObRIP5/ELyIBvXfhFiLNEUVVI3pK2wNM=
github.com/BuxOrg/go-buxclient v0.4.0/go.mod h1:OJerECkAm9XZX5srn/aCEs7E8o5eSd73zer7IR38WTQ=
github.com/FZambia/eagle v0.1.0 h1:9gyX6x+xjoIfglgyPTcYm7dvY7FJ93us1QY5De4CyXA=
github.com/FZambia/eagle v0.1.0/go.mod h1:YjGSPVkQTNcVLfzEUQJNgW9ScPR0K4u/Ky0yeFa4oDA=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
Expand All @@ -16,6 +12,10 @@ github.com/avast/retry-go/v4 v4.5.1 h1:AxIx0HGi4VZ3I02jr78j5lZ3M6x1E0Ivxa6b0pUUh
github.com/avast/retry-go/v4 v4.5.1/go.mod h1:/sipNsvNB3RRuT5iNcb6h73nw3IBmXJ/H3XrCQYSOpc=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bitcoin-sv/spv-wallet-go-client v0.5.0 h1:KHfOv1YjpR3cAOmYaYTzI6eJbVpldfdxKc742aJeJlY=
github.com/bitcoin-sv/spv-wallet-go-client v0.5.0/go.mod h1:AWfU/n+TTxdT013a7ENeqt6qzZnv2vQ/aeJi5Zr1BTw=
github.com/bitcoin-sv/spv-wallet/models v0.15.0 h1:qMp5mqmfaPpstNmltZ22uAtJrqjhHd+I0h/InaWHo0w=
github.com/bitcoin-sv/spv-wallet/models v0.15.0/go.mod h1:P8vXF1mPg1Zh3xSvB9yqwuPJfOR8Tt/SAG2FYztwENI=
github.com/bitcoinschema/go-bitcoin/v2 v2.0.5 h1:Sgh5Eb746Zck/46rFDrZZEXZWyO53fMuWYhNoZa1tck=
github.com/bitcoinschema/go-bitcoin/v2 v2.0.5/go.mod h1:JjO1ivfZv6vhK0uAXzyH08AAHlzNMAfnyK1Fiv9r4ZA=
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo=
Expand Down Expand Up @@ -138,14 +138,10 @@ github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c=
github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA=
github.com/libsv/go-bt/v2 v2.2.5 h1:VoggBLMRW9NYoFujqe5bSYKqnw5y+fYfufgERSoubog=
github.com/libsv/go-bt/v2 v2.2.5/go.mod h1:cV45+jDlPOLfhJLfpLmpQoWzrIvVth9Ao2ZO1f6CcqU=
github.com/machinebox/graphql v0.2.2 h1:dWKpJligYKhYKO5A2gvNhkJdQMNZeChZYyBbrZkBZfo=
github.com/machinebox/graphql v0.2.2/go.mod h1:F+kbVMHuwrQ5tYgU9JXlnskM8nOaFxCAEolaQybkjWA=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matryer/is v1.4.1 h1:55ehd8zaGABKLXQUe2awZ99BD/PTc2ls+KV/dXphgEQ=
github.com/matryer/is v1.4.1/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand Down
4 changes: 2 additions & 2 deletions notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/bitcoin-sv/spv-wallet-web-backend/transports/spvwallet"

walletmodels "github.com/BuxOrg/bux-models"
"github.com/bitcoin-sv/spv-wallet/models"
)

// BaseEvent represents base of notification.
Expand Down Expand Up @@ -34,7 +34,7 @@ type Transaction struct {
}

// PrepareTransactionEvent prepares event in NewTransactionEvent struct.
func PrepareTransactionEvent(tx *walletmodels.Transaction) TransactionEvent {
func PrepareTransactionEvent(tx *models.Transaction) TransactionEvent {
sender, receiver := spvwallet.GetPaymailsFromMetadata(tx, "unknown")
status := "unconfirmed"
if tx.BlockHeight > 0 {
Expand Down
24 changes: 5 additions & 19 deletions tests/mocks/users_interfaces_mq.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion transports/http/endpoints/api/transactions/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/bitcoin-sv/spv-wallet-web-backend/notification"
"github.com/bitcoin-sv/spv-wallet-web-backend/transports/websocket"

"github.com/BuxOrg/go-buxclient/transports"
"github.com/bitcoin-sv/spv-wallet-go-client/transports"
"github.com/rs/zerolog"

"github.com/bitcoin-sv/spv-wallet-web-backend/transports/http/auth"
Expand Down
10 changes: 5 additions & 5 deletions transports/spvwallet/admin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/rs/zerolog"

walletmodels "github.com/BuxOrg/bux-models"
walletclient "github.com/BuxOrg/go-buxclient"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/libsv/go-bk/bip32"
"github.com/spf13/viper"

Expand All @@ -16,7 +16,7 @@ import (

// AdminWalletClient is a wrapper for Admin SPV Wallet Client.
type AdminWalletClient struct {
client *walletclient.BuxClient
client *walletclient.WalletClient
log *zerolog.Logger
}

Expand All @@ -32,7 +32,7 @@ func (c *AdminWalletClient) RegisterXpub(xpriv *bip32.ExtendedKey) (string, erro

// Register new xpub in SPV Wallet.
err = c.client.NewXpub(
context.Background(), xpub.String(), &walletmodels.Metadata{},
context.Background(), xpub.String(), &models.Metadata{},
)

if err != nil {
Expand All @@ -57,7 +57,7 @@ func (c *AdminWalletClient) RegisterPaymail(alias, xpub string) (string, error)
avatar := viper.GetString(config.EnvPaymailAvatar)

// Register new xpub in SPV Wallet.
err := c.client.NewPaymail(context.Background(), xpub, address, avatar, alias, &walletmodels.Metadata{})
err := c.client.NewPaymail(context.Background(), xpub, address, avatar, alias, &models.Metadata{})

if err != nil {
c.log.Error().Msgf("Error while registering new paymail: %v", err.Error())
Expand Down
25 changes: 10 additions & 15 deletions transports/spvwallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (

"github.com/rs/zerolog"

walletmodels "github.com/BuxOrg/bux-models"
walletclient "github.com/BuxOrg/go-buxclient"
"github.com/BuxOrg/go-buxclient/transports"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/transports"
"github.com/bitcoin-sv/spv-wallet/models"

"github.com/bitcoin-sv/spv-wallet-web-backend/domain/users"
)

// Client implements UserWalletClient interface which wraps the spv-wallet-go-client and provides methods for user.
type Client struct {
client *walletclient.BuxClient
client *walletclient.WalletClient
log *zerolog.Logger
}

// CreateAccessKey creates new access key for user.
func (c *Client) CreateAccessKey() (users.AccKey, error) {
accessKey, err := c.client.CreateAccessKey(context.Background(), &walletmodels.Metadata{})
accessKey, err := c.client.CreateAccessKey(context.Background(), &models.Metadata{})
if err != nil {
c.log.Error().Msgf("Error while creating new accessKey: %v", err.Error())
return nil, err
Expand Down Expand Up @@ -91,7 +91,7 @@ func (c *Client) GetXPub() (users.PubKey, error) {
// SendToRecipients sends satoshis to recipients.
func (c *Client) SendToRecipients(recipients []*transports.Recipients, senderPaymail string) (users.Transaction, error) {
// Create matadata with sender and receiver paymails.
metadata := &walletmodels.Metadata{
metadata := &models.Metadata{
"receiver": recipients[0].To,
"sender": senderPaymail,
}
Expand All @@ -114,7 +114,7 @@ func (c *Client) SendToRecipients(recipients []*transports.Recipients, senderPay
}

// CreateAndFinalizeTransaction creates draft transaction and finalizes it.
func (c *Client) CreateAndFinalizeTransaction(recipients []*transports.Recipients, metadata *walletmodels.Metadata) (users.DraftTransaction, error) {
func (c *Client) CreateAndFinalizeTransaction(recipients []*transports.Recipients, metadata *models.Metadata) (users.DraftTransaction, error) {
// Create draft transaction.
draftTx, err := c.client.DraftToRecipients(context.Background(), recipients, metadata)
if err != nil {
Expand All @@ -138,7 +138,7 @@ func (c *Client) CreateAndFinalizeTransaction(recipients []*transports.Recipient
}

// RecordTransaction records transaction in SPV Wallet.
func (c *Client) RecordTransaction(hex, draftTxId string, metadata *walletmodels.Metadata) (*walletmodels.Transaction, error) {
func (c *Client) RecordTransaction(hex, draftTxId string, metadata *models.Metadata) (*models.Transaction, error) {
tx, err := c.client.RecordTransaction(context.Background(), hex, draftTxId, metadata)
if err != nil {
c.log.Error().Str("draftTxID", draftTxId).Msgf("Error while recording tx: %v", err.Error())
Expand All @@ -147,11 +147,6 @@ func (c *Client) RecordTransaction(hex, draftTxId string, metadata *walletmodels
return tx, nil
}

// UnreserveUtxos removes utxos from draft transaction in SPV Wallet.
func (c *Client) UnreserveUtxos(draftTxId string) error {
return c.client.UnreserveUtxos(context.Background(), draftTxId)
}

// GetTransactions returns all transactions.
func (c *Client) GetTransactions(queryParam transports.QueryParams, userPaymail string) ([]users.Transaction, error) {
conditions := make(map[string]interface{})
Expand All @@ -164,7 +159,7 @@ func (c *Client) GetTransactions(queryParam transports.QueryParams, userPaymail
queryParam.SortDirection = "desc"
}

transactions, err := c.client.GetTransactions(context.Background(), conditions, &walletmodels.Metadata{}, &queryParam)
transactions, err := c.client.GetTransactions(context.Background(), conditions, &models.Metadata{}, &queryParam)
if err != nil {
c.log.Error().
Str("userPaymail", userPaymail).
Expand Down Expand Up @@ -230,7 +225,7 @@ func (c *Client) GetTransaction(transactionId, userPaymail string) (users.FullTr
func (c *Client) GetTransactionsCount() (int64, error) {
conditions := make(map[string]interface{})

count, err := c.client.GetTransactionsCount(context.Background(), conditions, &walletmodels.Metadata{})
count, err := c.client.GetTransactionsCount(context.Background(), conditions, &models.Metadata{})
if err != nil {
c.log.Error().Msgf("Error while getting transactions count: %v", err.Error())
return 0, err
Expand Down
Loading

0 comments on commit f7803a2

Please sign in to comment.