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-1303): update go client version client in regression tests. #840

Merged
3 changes: 2 additions & 1 deletion regression_tests/go.mod

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

4 changes: 4 additions & 0 deletions regression_tests/go.sum

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

36 changes: 27 additions & 9 deletions regression_tests/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"time"

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

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
)

Expand Down Expand Up @@ -177,13 +180,19 @@ func recreateUser(paymailAlias string, config *regressionTestConfig) (*regressio
// useUserFromXPriv fills missing user data using provided xpriv.
func useUserFromXPriv(paymailAlias string) (*regressionTestUser, error) {
validatedXPriv := getValidXPriv()
keys, err := xpriv.FromString(validatedXPriv)
xPriv, err := walletkeys.XPrivFromString(validatedXPriv)
if err != nil {
return nil, fmt.Errorf("failed to generate an extended private key (xPriv) from the given string: %w", err)
}

xPub, err := walletkeys.XPubFromXPriv(validatedXPriv)
if err != nil {
return nil, fmt.Errorf("error parsing xpriv: %w", err)
return nil, fmt.Errorf("failed to derive an extended public key (xPub) from the given string: %w", err)
}

return &regressionTestUser{
XPriv: keys.XPriv(),
XPub: keys.XPub().String(),
XPriv: xPriv.String(),
XPub: xPub,
Paymail: preparePaymail(leaderPaymailAlias, paymailAlias),
}, nil
}
Expand Down Expand Up @@ -250,7 +259,10 @@ func takeMasterUrlAndXPriv(leaderPaymail *regressionTestUser) error {

// sendFundsWithGoClient sends funds using the Go client.
func sendFundsWithGoClient(instanceUrl string, istanceXPriv string, receiverPaymail string) error {
client, err := walletclient.NewWithXPriv(addPrefixIfNeeded(instanceUrl), istanceXPriv)
client, err := walletclient.NewUserAPIWithXPriv(config.New(
config.WithAddr(instanceUrl),
), istanceXPriv)

if err != nil {
return fmt.Errorf("error initalizing client: %w", err)
}
Expand All @@ -263,10 +275,16 @@ func sendFundsWithGoClient(instanceUrl string, istanceXPriv string, receiverPaym
if balance <= minimalBalance {
return fmt.Errorf("balance too low: %d", balance)
}
recipient := walletclient.Recipients{To: receiverPaymail, Satoshis: uint64(minimalBalance)}
recipients := []*walletclient.Recipients{&recipient}

_, err = client.SendToRecipients(ctx, recipients, map[string]any{"message": "regression test funds"})
_, err = client.SendToRecipients(ctx, &commands.SendToRecipients{
Recipients: []*commands.Recipients{
{
Satoshis: uint64(minimalBalance),
To: receiverPaymail,
},
},
Metadata: map[string]any{"message": "regression test funds"},
})
if err != nil {
return fmt.Errorf("error sending to recipients: %w", err)
}
Expand Down
53 changes: 35 additions & 18 deletions regression_tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import (
"time"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
"github.com/bitcoin-sv/spv-wallet-go-client/xpriv"
"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-go-client/walletkeys"

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/response"
"github.com/joho/godotenv"
)

Expand Down Expand Up @@ -120,13 +123,13 @@ func addPrefixIfNeeded(url string) string {
}

// getSharedConfig retrieves the shared configuration from the SPV Wallet.
func getSharedConfig(ctx context.Context) (*models.SharedConfig, error) {
wc, err := walletclient.NewWithXPriv(addPrefixIfNeeded(domainLocalHost), adminXPriv)
func getSharedConfig(ctx context.Context) (*response.SharedConfig, error) {
wc, err := walletclient.NewAdminAPIWithXPriv(walletclientcfg.New(walletclientcfg.WithAddr(domainLocalHost)), adminXPriv)
if err != nil {
return nil, err
}

sharedConfig, err := wc.GetSharedConfig(ctx)
sharedConfig, err := wc.SharedConfig(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,29 +188,38 @@ func preparePaymail(paymailAlias string, domain string) string {
}

func createUser(paymail string, config *regressionTestConfig) (*regressionTestUser, error) {
keys, err := xpriv.Generate()
keys, err := walletkeys.RandomKeys()
if err != nil {
return nil, err
}

user := &regressionTestUser{
XPriv: keys.XPriv(),
XPub: keys.XPub().String(),
XPub: keys.XPub(),
Paymail: preparePaymail(leaderPaymailAlias, paymail),
}

adminClient, err := walletclient.NewWithAdminKey(config.ClientOneURL, adminXPriv)
adminClient, err := walletclient.NewAdminAPIWithXPriv(walletclientcfg.New(walletclientcfg.WithAddr(config.ClientOneURL)), adminXPriv)
if err != nil {
return nil, err
}
ctx := context.Background()

if err := adminClient.AdminNewXpub(ctx, user.XPub, map[string]any{"purpose": "regression-tests"}); err != nil {
_, err = adminClient.CreateXPub(ctx, &commands.CreateUserXpub{
Metadata: map[string]any{"purpose": "regression-tests"},
XPub: user.XPub,
})
if err != nil {
fmt.Println("adminNewXpub failed with status code:", err)
return nil, err
}

createPaymailRes, err := adminClient.AdminCreatePaymail(ctx, user.XPub, user.Paymail, "Regression tests", "")
createPaymailRes, err := adminClient.CreatePaymail(ctx, &commands.CreatePaymail{
Key: user.XPub,
Address: user.Paymail,
PublicName: "Regression tests",
})

if err != nil {
if err.Error() == spverrors.ErrPaymailAlreadyExists.Error() {
return user, err
Expand All @@ -222,27 +234,32 @@ func createUser(paymail string, config *regressionTestConfig) (*regressionTestUs

// useUserFromEnv fills missing user data using xpriv from the environment variables.
func useUserFromEnv(paymailDomain string, config *regressionTestConfig) (*regressionTestUser, error) {
keys, err := xpriv.FromString(config.ClientOneLeaderXPriv)
xPriv, err := walletkeys.XPrivFromString(config.ClientOneLeaderXPriv)
if err != nil {
return nil, fmt.Errorf("error parsing xpriv: %w", err)
return nil, fmt.Errorf("failed to generate an extended private key (xPriv) from a string: %w", err)
}
xPub, err := walletkeys.XPubFromXPriv(xPriv.String())
if err != nil {
return nil, fmt.Errorf("failed to generate an extended public key (xPub) from a xPriv string: %w", err)
}

return &regressionTestUser{
XPriv: keys.XPriv(),
XPub: keys.XPub().String(),
XPriv: xPriv.String(),
XPub: xPub,
Paymail: preparePaymail(leaderPaymailAlias, paymailDomain),
}, nil
}

// deleteUser soft deletes paymail address from the SPV Wallet.
func deleteUser(paymail string, config *regressionTestConfig) error {
paymail = preparePaymail(leaderPaymailAlias, paymail)
adminClient, err := walletclient.NewWithAdminKey(addPrefixIfNeeded(config.ClientOneURL), adminXPriv)
adminClient, err := walletclient.NewAdminAPIWithXPriv(walletclientcfg.New(walletclientcfg.WithAddr(config.ClientOneURL)), adminXPriv)
if err != nil {
return err
}

ctx := context.Background()
err = adminClient.AdminDeletePaymail(ctx, paymail)
err = adminClient.DeletePaymail(ctx, paymail)
if err != nil {
return err
}
Expand Down Expand Up @@ -287,13 +304,13 @@ func isValidURL(rawURL string) bool {

// checkBalance checks the balance of the specified xpriv at the given domain with given xpriv.
func checkBalance(domain, xpriv string) (int, error) {
client, err := walletclient.NewWithXPriv(addPrefixIfNeeded(domain), xpriv)
client, err := walletclient.NewUserAPIWithXPriv(walletclientcfg.New(walletclientcfg.WithAddr(domain)), xpriv)
if err != nil {
return wrongInput, err
}
ctx := context.Background()

xpubInfo, err := client.GetXPub(ctx)
xpubInfo, err := client.XPub(ctx)
if err != nil {
return wrongInput, fmt.Errorf("error getting xpub info: %w", err)
}
Expand Down
Loading