Skip to content

Commit

Permalink
chore(multiaccount)_: Provide geth_backend API to accept terms
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmotta committed Aug 24, 2024
1 parent cb5f243 commit 5daec47
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions api/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,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,
Expand Down Expand Up @@ -1767,6 +1768,33 @@ 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, err)
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()

Expand Down
26 changes: 26 additions & 0 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,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()
Expand Down Expand Up @@ -1579,6 +1597,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 {
Expand Down
5 changes: 5 additions & 0 deletions api/old_mobile_user_upgrading_from_v1_to_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions mobile/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5daec47

Please sign in to comment.