Skip to content

Commit

Permalink
refactor(SPV-1306): old go version client replacement.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgosek-4chain committed Jan 9, 2025
1 parent 14decdb commit 0785517
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 356 deletions.
4 changes: 2 additions & 2 deletions domain/transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/avast/retry-go/v4"
walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/commands"
"github.com/bitcoin-sv/spv-wallet-web-backend/domain/users"
"github.com/bitcoin-sv/spv-wallet-web-backend/notification"
"github.com/bitcoin-sv/spv-wallet-web-backend/spverrors"
Expand Down Expand Up @@ -38,7 +38,7 @@ func (s *TransactionService) CreateTransaction(userPaymail, xpriv, recipient str
return spverrors.ErrCreateTransaction.Wrap(err)
}

var recipients = []*walletclient.Recipients{
var recipients = []*commands.Recipients{
{
Satoshis: satoshis,
To: recipient,
Expand Down
6 changes: 3 additions & 3 deletions domain/users/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/commands"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
"github.com/libsv/go-bk/bip32"
Expand Down Expand Up @@ -65,11 +65,11 @@ type (
// XPub Key methods
GetXPub() (PubKey, error)
// Transaction methods
SendToRecipients(recipients []*walletclient.Recipients, senderPaymail string) (Transaction, error)
SendToRecipients(recipients []*commands.Recipients, senderPaymail string) (Transaction, error)
GetTransactions(queryParam *filter.QueryParams, userPaymail string) ([]Transaction, error)
GetTransaction(transactionID, userPaymail string) (FullTransaction, error)
GetTransactionsCount() (int64, error)
CreateAndFinalizeTransaction(recipients []*walletclient.Recipients, metadata map[string]any) (DraftTransaction, error)
CreateAndFinalizeTransaction(recipients []*commands.Recipients, metadata map[string]any) (DraftTransaction, error)
RecordTransaction(hex, draftTxID string, metadata map[string]any) (*models.Transaction, error)
// Contacts methods
UpsertContact(ctx context.Context, paymail, fullName, requesterPaymail string, metadata map[string]any) (*models.Contact, error)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ toolchain go1.22.6

require (
github.com/avast/retry-go/v4 v4.6.0
github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.16
github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.34
github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.21
github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.39
github.com/brianvoe/gofakeit/v6 v6.28.0
github.com/centrifugal/centrifuge v0.33.4
github.com/gin-contrib/sessions v1.0.1
Expand All @@ -28,7 +28,7 @@ require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/antonlindstrom/pgstore v0.0.0-20220421113606-e3a6e3fed12a // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitcoin-sv/go-sdk v1.1.14 // indirect
github.com/bitcoin-sv/go-sdk v1.1.16 // indirect
github.com/boombuler/barcode v1.0.2 // indirect
github.com/bytedance/sonic v1.11.9 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
Expand All @@ -48,6 +48,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-resty/resty/v2 v2.15.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/context v1.1.2 // indirect
Expand Down
19 changes: 13 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ github.com/avast/retry-go/v4 v4.6.0 h1:K9xNA+KeB8HHc2aWFuLb25Offp+0iVRXEvFx8IinR
github.com/avast/retry-go/v4 v4.6.0/go.mod h1:gvWlPhBVsvBbLkVGDg/KwvBv0bEkCOLRRSHKIr2PyOE=
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/go-sdk v1.1.14 h1:65PrWc8H5in138fplswpxpXuV3HwQQgyXDBegioyVbY=
github.com/bitcoin-sv/go-sdk v1.1.14/go.mod h1:khREs6RkJuYkxFD3ZB9I+zcGjfxoHeACg9Zr3f7Fn7s=
github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.16 h1:PGlo27UEdJKq4vup9pyE6BtZ2YWS2En3SJhjBu0EiEs=
github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.16/go.mod h1:E0G3YfQj6KDKs6CoMLbSxW2fMIqn5bMeOSi5G4BL4a0=
github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.34 h1:9MMqlZSyCP+RFt0FNnwcbJEgUsRKy5w9Ga7FXHs/XXo=
github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.34/go.mod h1:PEJdH9ZWKOiKHyOZkzYsRbKuZjzlRaEJy3GsM75Icdo=
github.com/bitcoin-sv/go-sdk v1.1.16 h1:n2X0RiENFGD/1fQ/1y6osbostRB7I/xq9I7tcIKcCPY=
github.com/bitcoin-sv/go-sdk v1.1.16/go.mod h1:3CsNdEDBwB+SIv6UBcJPC9bTvPqxQvg3GULt7wsuL58=
github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.21 h1:N6JbmVjoifPJwKu5DedS8xEKwHYIRv9AewD7eefxwyg=
github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.21/go.mod h1:WaO6ESKa4RX9QEpy1/6v3jcTcznwPx+MYlADg15c1dY=
github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.39 h1:qo74o72mcdj7AYJoCq7RG3enHJiqtbkFEY9uXvEEG2M=
github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.39/go.mod h1:UdY5AGsO9IomUEYSPilcSY+3BTQRJswdfZNveLt6LZQ=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.2 h1:79yrbttoZrLGkL/oOI8hBrUKucwOL0oOjUgEguGMcJ4=
github.com/boombuler/barcode v1.0.2/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
Expand Down Expand Up @@ -92,6 +92,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-resty/resty/v2 v2.15.3 h1:bqff+hcqAflpiF591hhJzNdkRsFhlB96CYfBwSFvql8=
github.com/go-resty/resty/v2 v2.15.3/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down Expand Up @@ -121,6 +123,8 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down Expand Up @@ -226,6 +230,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -319,6 +324,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
Expand Down
20 changes: 19 additions & 1 deletion notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

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

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

// PrepareTransactionEvent prepares event in NewTransactionEvent struct.
func PrepareTransactionEvent(tx *models.Transaction) TransactionEvent {
sender, receiver := spvwallet.GetPaymailsFromMetadata(tx, "unknown")
sender, receiver := spvwallet.GetPaymailsFromMetadata(&response.Transaction{
Model: response.Model(tx.Model),
ID: tx.ID,
Hex: tx.Hex,
XpubInIDs: tx.XpubInIDs,
XpubOutIDs: tx.XpubOutIDs,
BlockHash: tx.BlockHash,
BlockHeight: tx.BlockHeight,
Fee: tx.Fee,
NumberOfInputs: tx.NumberOfInputs,
NumberOfOutputs: tx.NumberOfOutputs,
DraftID: tx.DraftID,
TotalValue: tx.TotalValue,
OutputValue: tx.OutputValue,
Outputs: tx.Outputs,
Status: tx.Status,
TransactionDirection: tx.TransactionDirection,
}, "unknown")
status := "unconfirmed"
if tx.BlockHeight > 0 {
status = "confirmed"
Expand Down
7 changes: 3 additions & 4 deletions tests/mocks/users_interfaces_mq.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
mock "github.com/bitcoin-sv/spv-wallet-web-backend/tests/mocks"
"github.com/bitcoin-sv/spv-wallet-web-backend/tests/utils"
"github.com/bitcoin-sv/spv-wallet-web-backend/transports/spvwallet"

"github.com/brianvoe/gofakeit/v6"
"github.com/golang/mock/gomock"
"github.com/rs/zerolog"
Expand Down
1 change: 1 addition & 0 deletions transports/http/endpoints/api/transactions/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/bitcoin-sv/spv-wallet-web-backend/transports/http/auth"
router "github.com/bitcoin-sv/spv-wallet-web-backend/transports/http/endpoints/routes"
"github.com/bitcoin-sv/spv-wallet-web-backend/transports/spvwallet"

"github.com/bitcoin-sv/spv-wallet-web-backend/transports/websocket"
"github.com/bitcoin-sv/spv-wallet/models/filter"
"github.com/gin-gonic/gin"
Expand Down
65 changes: 38 additions & 27 deletions transports/spvwallet/admin_client.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
// Package spvwallet contains spv-wallet client adapters
//
//nolint:wrapcheck // error wrapped by service
package spvwallet

import (
"context"
"fmt"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/commands"
walletclientCfg "github.com/bitcoin-sv/spv-wallet-go-client/config"

"github.com/bitcoin-sv/spv-wallet-web-backend/config"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/libsv/go-bk/bip32"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)

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

// RegisterXpub registers xpub in SPV Wallet.
func (c *AdminWalletClient) RegisterXpub(xpriv *bip32.ExtendedKey) (string, error) {
func (a *AdminClientAdapter) RegisterXpub(xpriv *bip32.ExtendedKey) (string, error) {
// Get xpub from xpriv.
xpub, err := xpriv.Neuter()

if err != nil {
c.log.Error().Msgf("Error while creating new xPub: %v", err.Error())
a.log.Error().Msgf("Error while creating new xPub: %v", err.Error())
return "", err
}

// Register new xpub in SPV Wallet.
err = c.client.AdminNewXpub(context.Background(), xpub.String(), nil)

_, err = a.api.CreateXPub(context.TODO(), &commands.CreateUserXpub{XPub: xpriv.String()})
if err != nil {
c.log.Error().
Str("xpub", xpub.String()).
Msgf("Error while registering new xPub: %v", err.Error())
a.log.Error().Str("xpub", xpub.String()).Msgf("Error while registering new xPub: %v", err.Error())
return "", err
}

return xpub.String(), nil
}

// RegisterPaymail registers new paymail in SPV Wallet.
func (c *AdminWalletClient) RegisterPaymail(alias, xpub string) (string, error) {
func (a *AdminClientAdapter) RegisterPaymail(alias, xpub string) (string, error) {
// Get paymail domain from env.
domain := viper.GetString(config.EnvPaymailDomain)

Expand All @@ -55,21 +47,40 @@ func (c *AdminWalletClient) RegisterPaymail(alias, xpub string) (string, error)
// Get avatar url from env.
avatar := viper.GetString(config.EnvPaymailAvatar)

_, err := c.client.AdminCreatePaymail(context.Background(), xpub, address, alias, avatar)

_, err := a.api.CreatePaymail(context.TODO(), &commands.CreatePaymail{
Key: xpub,
Address: address,
PublicName: alias,
Avatar: avatar,
})
if err != nil {
c.log.Error().Msgf("Error while registering new paymail: %v", err.Error())
a.log.Error().Msgf("Error while registering new paymail: %v", err.Error())
return "", err
}

return address, nil
}

// GetSharedConfig returns shared config from SPV Wallet.
func (c *AdminWalletClient) GetSharedConfig() (*models.SharedConfig, error) {
sharedConfig, err := c.client.GetSharedConfig(context.Background())
func (a *AdminClientAdapter) GetSharedConfig() (*models.SharedConfig, error) {
sharedConfig, err := a.api.SharedConfig(context.TODO())
if err != nil {
c.log.Error().Msgf("Error while getting shared config: %v", err.Error())
a.log.Error().Msgf("Error while getting shared config: %v", err.Error())
return nil, err
}
return sharedConfig, nil

return &models.SharedConfig{
PaymailDomains: sharedConfig.PaymailDomains,
ExperimentalFeatures: sharedConfig.ExperimentalFeatures,
}, nil
}

func NewAdminClientAdapter(log *zerolog.Logger) (*AdminClientAdapter, error) {
adminKey := viper.GetString(config.EnvAdminXpriv)
serverURL := viper.GetString(config.EnvServerURL)
api, err := walletclient.NewAdminAPIWithXPriv(walletclientCfg.New(walletclientCfg.WithAddr(serverURL)), adminKey)
if err != nil {
return nil, fmt.Errorf("failed to initialize admin API: %w", err)
}

return &AdminClientAdapter{api: api, log: log}, nil
}
Loading

0 comments on commit 0785517

Please # to comment.