From 770530cf0b95d637cb53b5cbb5b75c409212912d Mon Sep 17 00:00:00 2001 From: Icaro Motta Date: Fri, 23 Aug 2024 16:18:24 -0300 Subject: [PATCH] chore(multiaccount)_: Provide geth_backend API to accept terms --- api/backend_test.go | 27 +++++++++++++++++++ api/geth_backend.go | 26 ++++++++++++++++++ ...obile_user_upgrading_from_v1_to_v2_test.go | 5 ++++ mobile/status.go | 8 ++++++ 4 files changed, 66 insertions(+) diff --git a/api/backend_test.go b/api/backend_test.go index 4d39cc69a..f34854eeb 100644 --- a/api/backend_test.go +++ b/api/backend_test.go @@ -869,6 +869,7 @@ func TestLoginAccount(t *testing.T) { require.NotEmpty(t, accounts[0].KeyUID) require.Equal(t, acc.KeyUID, accounts[0].KeyUID) + require.True(t, acc.HasAcceptedTerms) loginAccountRequest := &requests.Login{ KeyUID: accounts[0].KeyUID, @@ -1760,6 +1761,32 @@ func TestRestoreAccountAndLogin(t *testing.T) { require.Empty(t, mnemonic) } +func TestAcceptTerms(t *testing.T) { + tmpdir := t.TempDir() + b := NewGethStatusBackend() + conf, err := params.NewNodeConfig(tmpdir, 1777) + require.NoError(t, b.AccountManager().InitKeystore(conf.KeyStoreDir)) + b.UpdateRootDataDir(conf.DataDir) + require.NoError(t, b.OpenAccounts()) + + nameserver := "8.8.8.8" + createAccountRequest := &requests.CreateAccount{ + DisplayName: "some-display-name", + CustomizationColor: "#ffffff", + Password: "some-password", + RootDataDir: tmpdir, + LogFilePath: tmpdir + "/log", + WakuV2Nameserver: &nameserver, + WakuV2Fleet: "status.staging", + } + + _, err = b.CreateAccountAndLogin(createAccountRequest) + require.NoError(t, err) + + err = b.AcceptTerms() + require.NoError(t, err) +} + func TestCreateAccountPathsValidation(t *testing.T) { tmpdir := t.TempDir() diff --git a/api/geth_backend.go b/api/geth_backend.go index 91c85fcdf..4765bedff 100644 --- a/api/geth_backend.go +++ b/api/geth_backend.go @@ -238,6 +238,24 @@ func (b *GethStatusBackend) GetAccounts() ([]multiaccounts.Account, error) { return b.multiaccountsDB.GetAccounts() } +func (b *GethStatusBackend) AcceptTerms() error { + b.mu.Lock() + defer b.mu.Unlock() + if b.multiaccountsDB == nil { + return errors.New("accounts db wasn't initialized") + } + + accounts, err := b.multiaccountsDB.GetAccounts() + if err != nil { + return err + } + if len(accounts) == 0 { + return errors.New("accounts is empty") + } + + return b.multiaccountsDB.UpdateHasAcceptedTerms(accounts[0].KeyUID, true) +} + func (b *GethStatusBackend) getAccountByKeyUID(keyUID string) (*multiaccounts.Account, error) { b.mu.Lock() defer b.mu.Unlock() @@ -1576,6 +1594,14 @@ func (b *GethStatusBackend) buildAccount(request *requests.CreateAccount, input acc.KDFIterations = dbsetup.ReducedKDFIterationsNumber } + count, err := b.multiaccountsDB.GetAccountsCount() + if err != nil { + return nil, err + } + if count == 0 { + acc.HasAcceptedTerms = true + } + if request.ImagePath != "" { imageCropRectangle := request.ImageCropRectangle if imageCropRectangle == nil { diff --git a/api/old_mobile_user_upgrading_from_v1_to_v2_test.go b/api/old_mobile_user_upgrading_from_v1_to_v2_test.go index 58eb52acf..8f651df7d 100644 --- a/api/old_mobile_user_upgrading_from_v1_to_v2_test.go +++ b/api/old_mobile_user_upgrading_from_v1_to_v2_test.go @@ -141,6 +141,11 @@ func (s *OldMobileUserUpgradingFromV1ToV2Test) TestLoginAndMigrationsStillWorkWi s.Require().True(len(keyKps[0].Accounts) == 1) info, err = generator.LoadAccount(keyKps[0].Accounts[0].Address.Hex(), oldMobileUserPasswd) s.Require().NoError(err) + + // The user should manually accept terms, so we make sure we don't set it + // automatically by mistake. + s.Require().False(info.ToMultiAccount().HasAcceptedTerms) + s.Require().Equal(keyKps[0].KeyUID, info.KeyUID) s.Require().Equal(keyKps[0].Accounts[0].KeyUID, info.KeyUID) info, err = generator.ImportPrivateKey("c3ad0b50652318f845565c13761e5369ce75dcbc2a94616e15b829d4b07410fe") diff --git a/mobile/status.go b/mobile/status.go index a4c2d4b5e..b04059f8e 100644 --- a/mobile/status.go +++ b/mobile/status.go @@ -349,6 +349,14 @@ func CreateAccountAndLogin(requestJSON string) string { return makeJSONResponse(nil) } +func AcceptTerms() string { + err := statusBackend.AcceptTerms() + if err != nil { + return makeJSONResponse(err) + } + return makeJSONResponse(nil) +} + func LoginAccount(requestJSON string) string { var request requests.Login err := json.Unmarshal([]byte(requestJSON), &request)