Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(SPV-1306): old go version client replacement. #114

Merged
merged 6 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func setHTTPServerDefaults() {
// setSpvWalletDefaults sets default values for spv-wallet connection.
func setSpvWalletDefaults() {
viper.SetDefault(EnvAdminXpriv, "xprv9s21ZrQH143K3CbJXirfrtpLvhT3Vgusdo8coBritQ3rcS7Jy7sxWhatuxG5h2y1Cqj8FKmPp69536gmjYRpfga2MJdsGyBsnB12E19CESK")
viper.SetDefault(EnvServerURL, "http://localhost:3003/v1")
viper.SetDefault(EnvServerURL, "http://localhost:3003")
viper.SetDefault(EnvPaymailDomain, "example.com")
viper.SetDefault(EnvPaymailAvatar, "http://localhost:3003/static/paymail/avatar.jpg")
}
Expand Down
19 changes: 7 additions & 12 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,17 +38,8 @@ func (s *TransactionService) CreateTransaction(userPaymail, xpriv, recipient str
return spverrors.ErrCreateTransaction.Wrap(err)
}

var recipients = []*walletclient.Recipients{
{
Satoshis: satoshis,
To: recipient,
},
}

metadata := map[string]any{
"receiver": recipient,
"sender": userPaymail,
}
recipients := []*commands.Recipients{{Satoshis: satoshis, To: recipient}}
metadata := map[string]any{"receiver": recipient, "sender": userPaymail}

draftTransaction, err := userWalletClient.CreateAndFinalizeTransaction(recipients, metadata)
if err != nil {
Expand All @@ -57,6 +48,10 @@ func (s *TransactionService) CreateTransaction(userPaymail, xpriv, recipient str
}

go func() {
if err == nil {
return
}

tx, err := tryRecordTransaction(userWalletClient, draftTransaction, metadata, s.log)
if err != nil {
events <- notification.PrepareTransactionErrorEvent(err)
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.6
github.com/gin-contrib/sessions v1.0.2
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.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
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.

75 changes: 0 additions & 75 deletions transports/spvwallet/admin_client.go

This file was deleted.

86 changes: 86 additions & 0 deletions transports/spvwallet/admin_client_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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/pkg/errors"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)

type adminClientAdapter struct {
log *zerolog.Logger
api *walletclient.AdminAPI
}

func (a *adminClientAdapter) RegisterXpub(xpriv *bip32.ExtendedKey) (string, error) {
// Get xpub from xpriv.
xpub, err := xpriv.Neuter()
if err != nil {
a.log.Error().Msgf("Error while returning a new extended public key from the xPriv: %v", err.Error())
return "", errors.Wrap(err, "error while returning a new extended public key from the xPriv")
}

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

return xpub.String(), nil
}

func (a *adminClientAdapter) RegisterPaymail(alias, xpub string) (string, error) {
// Get paymail domain from env.
domain := viper.GetString(config.EnvPaymailDomain)

// Create paymail address.
address := fmt.Sprintf("%s@%s", alias, domain)

// Get avatar url from env.
avatar := viper.GetString(config.EnvPaymailAvatar)

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

return address, nil
}

func (a *adminClientAdapter) GetSharedConfig() (*models.SharedConfig, error) {
sharedConfig, err := a.api.SharedConfig(context.Background())
if err != nil {
a.log.Error().Msgf("Error while fetching shared config: %v", err.Error())
return nil, errors.Wrap(err, "error while fetching shared config")
}

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, errors.Wrap(err, "failed to initialize admin API")
}

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