From 71236f86162e86c4325ed18e765e6a7c6a886b94 Mon Sep 17 00:00:00 2001 From: mgosek-4chain Date: Wed, 8 Jan 2025 11:26:56 +0100 Subject: [PATCH 1/5] refactor(SPV-1303): update go client version client, update method calls. --- regression_tests/go.mod | 3 ++- regression_tests/go.sum | 4 ++++ regression_tests/operator.go | 36 +++++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/regression_tests/go.mod b/regression_tests/go.mod index 2c658589a..912351f53 100644 --- a/regression_tests/go.mod +++ b/regression_tests/go.mod @@ -6,7 +6,7 @@ toolchain go1.23.3 require ( github.com/bitcoin-sv/spv-wallet v1.0.0-beta.39 - github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.18 + github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.19 github.com/bitcoin-sv/spv-wallet/models v1.0.0-beta.39 github.com/joho/godotenv v1.5.1 ) @@ -24,6 +24,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.1 // indirect + github.com/go-resty/resty/v2 v2.16.2 // indirect github.com/goccy/go-json v0.10.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.9 // indirect diff --git a/regression_tests/go.sum b/regression_tests/go.sum index 118372edd..11b8b9bf6 100644 --- a/regression_tests/go.sum +++ b/regression_tests/go.sum @@ -4,6 +4,8 @@ github.com/bitcoin-sv/spv-wallet v1.0.0-beta.39 h1:W2I9P8QyEoTlxrhUKwpOW+RFu3O24 github.com/bitcoin-sv/spv-wallet v1.0.0-beta.39/go.mod h1:8ZZ4Gm0owfpgTj8v7dhGvzspIW1b/aR/ySfXR6XkJcs= github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.18 h1:T+uMOybKMCmOgme8Rvm1grdVX+WiZornNL98C2hsGhs= github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.18/go.mod h1:5enqQHpwLp1yOvwbDr3kU4kF3fFW5hf5llZNkTvNhXU= +github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.19 h1:tU3SBiKVjiZ01O8pN9wWeeYxWE3u12wNjNsWw+zaYEU= +github.com/bitcoin-sv/spv-wallet-go-client v1.0.0-beta.19/go.mod h1:Z2uUaJCXQW1I3kPJP41xjhCOfBHhDBIu+g2IQ285WlY= 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= @@ -38,6 +40,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.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg= +github.com/go-resty/resty/v2 v2.16.2/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= diff --git a/regression_tests/operator.go b/regression_tests/operator.go index 29b8de4a9..2bd7afe7b 100644 --- a/regression_tests/operator.go +++ b/regression_tests/operator.go @@ -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" ) @@ -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) + keys, 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 ®ressionTestUser{ - XPriv: keys.XPriv(), - XPub: keys.XPub().String(), + XPriv: keys.String(), + XPub: xPub, Paymail: preparePaymail(leaderPaymailAlias, paymailAlias), }, nil } @@ -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) } @@ -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) } From 70abc835e38e9a06d68dd0bcb039da158ea710c5 Mon Sep 17 00:00:00 2001 From: mgosek-4chain Date: Wed, 8 Jan 2025 11:46:43 +0100 Subject: [PATCH 2/5] refactor(SPV-1303): fix failed delay simulation for test. --- engine/notifications/webhook_notifier_test.go | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/engine/notifications/webhook_notifier_test.go b/engine/notifications/webhook_notifier_test.go index d3c553ee8..db9f7efb0 100644 --- a/engine/notifications/webhook_notifier_test.go +++ b/engine/notifications/webhook_notifier_test.go @@ -201,20 +201,31 @@ func TestWebhookNotifier(t *testing.T) { client := newMockClient("http://localhost:8080") ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + n := NewNotifications(ctx, &nopLogger) notifier := NewWebhookNotifier(ctx, &nopLogger, newMockWebhookModel(client.url, "", ""), make(chan string)) n.AddNotifier(client.url, notifier.Channel) expected := []string{} - for i := 0; i < 10; i++ { - msg := fmt.Sprintf("msg-%d", i) - n.Notify(newMockEvent(msg)) - time.Sleep(100 * time.Microsecond) - expected = append(expected, msg) - } + done := make(chan struct{}) - time.Sleep(100 * time.Millisecond) - cancel() + go func() { + for i := 0; i < 10; i++ { + msg := fmt.Sprintf("msg-%d", i) + n.Notify(newMockEvent(msg)) + time.Sleep(100 * time.Microsecond) + expected = append(expected, msg) + + } + close(done) + }() + + select { + case <-done: + case <-time.After(1 * time.Second): + t.Fatal("Test timed out waiting for notifications") + } client.assertEvents(t, expected) client.assertEventsWereSentInBatches(t, false) From 04218c60ce5376be10c9bedb9fcff69a99b1987c Mon Sep 17 00:00:00 2001 From: mgosek-4chain Date: Wed, 8 Jan 2025 12:07:43 +0100 Subject: [PATCH 3/5] Revert "refactor(SPV-1303): fix failed delay simulation for test." This reverts commit 70abc835e38e9a06d68dd0bcb039da158ea710c5. --- engine/notifications/webhook_notifier_test.go | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/engine/notifications/webhook_notifier_test.go b/engine/notifications/webhook_notifier_test.go index db9f7efb0..d3c553ee8 100644 --- a/engine/notifications/webhook_notifier_test.go +++ b/engine/notifications/webhook_notifier_test.go @@ -201,32 +201,21 @@ func TestWebhookNotifier(t *testing.T) { client := newMockClient("http://localhost:8080") ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - n := NewNotifications(ctx, &nopLogger) notifier := NewWebhookNotifier(ctx, &nopLogger, newMockWebhookModel(client.url, "", ""), make(chan string)) n.AddNotifier(client.url, notifier.Channel) expected := []string{} - done := make(chan struct{}) - - go func() { - for i := 0; i < 10; i++ { - msg := fmt.Sprintf("msg-%d", i) - n.Notify(newMockEvent(msg)) - time.Sleep(100 * time.Microsecond) - expected = append(expected, msg) - - } - close(done) - }() - - select { - case <-done: - case <-time.After(1 * time.Second): - t.Fatal("Test timed out waiting for notifications") + for i := 0; i < 10; i++ { + msg := fmt.Sprintf("msg-%d", i) + n.Notify(newMockEvent(msg)) + time.Sleep(100 * time.Microsecond) + expected = append(expected, msg) } + time.Sleep(100 * time.Millisecond) + cancel() + client.assertEvents(t, expected) client.assertEventsWereSentInBatches(t, false) }) From d9690d2e51463bce63002f7c0919f03138889b7a Mon Sep 17 00:00:00 2001 From: mgosek-4chain Date: Thu, 9 Jan 2025 07:48:15 +0100 Subject: [PATCH 4/5] refactor(SPV-1303): update old method calls from previous version of spv wallet go client. --- regression_tests/utils.go | 53 ++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/regression_tests/utils.go b/regression_tests/utils.go index 9404f35a8..608727879 100644 --- a/regression_tests/utils.go +++ b/regression_tests/utils.go @@ -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" ) @@ -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 } @@ -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 := ®ressionTestUser{ 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 @@ -222,13 +234,18 @@ 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 ®ressionTestUser{ - XPriv: keys.XPriv(), - XPub: keys.XPub().String(), + XPriv: xPriv.String(), + XPub: xPub, Paymail: preparePaymail(leaderPaymailAlias, paymailDomain), }, nil } @@ -236,13 +253,13 @@ func useUserFromEnv(paymailDomain string, config *regressionTestConfig) (*regres // 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 } @@ -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) } From f1f1f7d09bbc0eed314af43dad7dd459c9866519 Mon Sep 17 00:00:00 2001 From: mgosek-4chain Date: Thu, 9 Jan 2025 07:49:02 +0100 Subject: [PATCH 5/5] refactor(SPV-1303): update variable name to be more readable. --- regression_tests/operator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/regression_tests/operator.go b/regression_tests/operator.go index 2bd7afe7b..4d88d7868 100644 --- a/regression_tests/operator.go +++ b/regression_tests/operator.go @@ -180,7 +180,7 @@ 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 := walletkeys.XPrivFromString(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) } @@ -191,7 +191,7 @@ func useUserFromXPriv(paymailAlias string) (*regressionTestUser, error) { } return ®ressionTestUser{ - XPriv: keys.String(), + XPriv: xPriv.String(), XPub: xPub, Paymail: preparePaymail(leaderPaymailAlias, paymailAlias), }, nil