From 6150f46d4b6234cc7acab69c558257114d9c726e Mon Sep 17 00:00:00 2001 From: Jakub Kowalski <155538368+jakubmkowalski@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:24:02 +0200 Subject: [PATCH] feat(SPV-896): switches to GetSharedConfig method to get paymail domain --- regression_tests/regression_test.go | 4 +-- regression_tests/utils.go | 49 ++++++----------------------- 2 files changed, 11 insertions(+), 42 deletions(-) diff --git a/regression_tests/regression_test.go b/regression_tests/regression_test.go index 53ab42f..c3817bd 100644 --- a/regression_tests/regression_test.go +++ b/regression_tests/regression_test.go @@ -36,12 +36,12 @@ func TestRegression(t *testing.T) { t.Run("Initialize Shared Configurations", func(t *testing.T) { t.Run("Should get sharedConfig for instance one", func(t *testing.T) { - paymailDomainInstanceOne, err = getPaymailDomain(adminXPub, rtConfig.ClientOneURL) + paymailDomainInstanceOne, err = getPaymailDomain(ctx, adminXPub, rtConfig.ClientOneURL) require.NoError(t, err, fmt.Sprintf(errGettingSharedConfig, err)) }) t.Run("Should get shared config for instance two", func(t *testing.T) { - paymailDomainInstanceTwo, err = getPaymailDomain(adminXPub, rtConfig.ClientTwoURL) + paymailDomainInstanceTwo, err = getPaymailDomain(ctx, adminXPub, rtConfig.ClientTwoURL) require.NoError(t, err, fmt.Sprintf(errGettingSharedConfig, err)) }) }) diff --git a/regression_tests/utils.go b/regression_tests/utils.go index 3094a81..b410c32 100644 --- a/regression_tests/utils.go +++ b/regression_tests/utils.go @@ -2,15 +2,11 @@ package regressiontests import ( "context" - "encoding/json" "errors" "fmt" - "io" - "net/http" "os" "regexp" "strings" - "time" walletclient "github.com/bitcoin-sv/spv-wallet-go-client" "github.com/bitcoin-sv/spv-wallet-go-client/xpriv" @@ -27,13 +23,11 @@ const ( ClientTwoURLEnvVar = "CLIENT_TWO_URL" ClientOneLeaderXPrivEnvVar = "CLIENT_ONE_LEADER_XPRIV" ClientTwoLeaderXPrivEnvVar = "CLIENT_TWO_LEADER_XPRIV" - - timeoutDuration = 30 * time.Second ) var ( - explicitHTTPURLRegex = regexp.MustCompile(`^https?://`) - envVariableError = errors.New("missing xpriv variables") + explicitHTTPURLRegex = regexp.MustCompile(`^https?://`) + errEmptyXPrivEnvVariables = errors.New("missing xpriv variables") ) type regressionTestUser struct { @@ -59,7 +53,7 @@ func getEnvVariables() (*regressionTestConfig, error) { } if rtConfig.ClientOneLeaderXPriv == "" || rtConfig.ClientTwoLeaderXPriv == "" { - return nil, envVariableError + return nil, errEmptyXPrivEnvVariables } if rtConfig.ClientOneURL == "" || rtConfig.ClientTwoURL == "" { rtConfig.ClientOneURL = "http://localhost:3003" @@ -73,41 +67,16 @@ func getEnvVariables() (*regressionTestConfig, error) { } // getPaymailDomain retrieves the shared configuration from the SPV Wallet. -func getPaymailDomain(xpub string, clientUrl string) (string, error) { - req, err := http.NewRequest(http.MethodGet, clientUrl+domainSuffixSharedConfig, nil) +func getPaymailDomain(ctx context.Context, xpub string, clientUrl string) (string, error) { + wc := walletclient.NewWithXPub(clientUrl, xpub) + sharedConfig, err := wc.GetSharedConfig(ctx) if err != nil { return "", err } - - req.Header.Set(models.AuthHeader, xpub) - client := http.Client{ - Timeout: timeoutDuration, - } - resp, err := client.Do(req) - if err != nil { - return "", err + if len(sharedConfig.PaymailDomains) != 1 { + return "", fmt.Errorf("expected 1 paymail domain, got %d", len(sharedConfig.PaymailDomains)) } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - return "", fmt.Errorf("failed to get shared config: %s", resp.Status) - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - - var configResponse models.SharedConfig - if err := json.Unmarshal(body, &configResponse); err != nil { - return "", err - } - - if len(configResponse.PaymailDomains) != 1 { - return "", fmt.Errorf("expected 1 paymail domain, got %d", len(configResponse.PaymailDomains)) - } - - return configResponse.PaymailDomains[0], nil + return sharedConfig.PaymailDomains[0], nil } // createUser creates a set of keys and new paymail in the SPV Wallet.