Commit 28d715f
authored
chore: refactor delete wallet into Authentication service (MetaMask#23882)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->
## **Description**
This PR refactors wallet deletion logic by consolidating the
`useDeleteWallet` hook into the `Authentication` service class. The
refactoring improves code organization, ensures consistent wallet
deletion behavior, and simplifies the codebase by removing redundant
hook logic.
**Reason for the change:**
1. The `useDeleteWallet` hook duplicated logic that should be
centralized in the `Authentication` service
2. `resetWalletState` and `deleteUser` were always used together, but
there was no enforcement of this pattern
3. Having wallet deletion logic in a hook created unnecessary coupling
between React components and core authentication logic
4. The hook pattern was not appropriate for core service operations that
don't depend on React lifecycle
**Improvement/Solution:**
1. **Moved Logic to Authentication Service**: Consolidated
`resetWalletState` and `deleteUser` methods into
`app/core/Authentication/Authentication.ts` as protected methods
2. **Created Public API**: Added `deleteWallet()` as the main public
method that ensures `resetWalletState()` is always called before
`deleteUser()`
3. **Protected Methods**: Made `resetWalletState` and `deleteUser`
protected to prevent direct external access and enforce the correct
usage pattern through `deleteWallet()`
4. **Updated All Call Sites**: Refactored `usePromptSeedlessRelogin` and
`DeleteWalletModal` to use the new `Authentication.deleteWallet()`
method
5. **Comprehensive Test Coverage**: Added tests for the new
`deleteWallet()` method and maintained test coverage for the protected
methods
**Key Technical Improvements:**
- **Removed Hook**: Deleted
`app/components/hooks/DeleteWallet/useDeleteWallet.ts` and related test
files
- **Centralized Logic**: All wallet deletion logic is now in the
`Authentication` service, following the single responsibility principle
- **Enforced Usage Pattern**: The protected methods ensure that wallet
reset and user deletion always happen in the correct sequence
- **Better Encapsulation**: Protected methods prevent accidental misuse
and make the API surface clearer
- **Consistent Behavior**: All wallet deletion flows now use the same
underlying implementation
## **Changelog**
CHANGELOG entry: Refactored wallet deletion logic by consolidating
useDeleteWallet hook into Authentication service
## **Related issues**
Fixes:
## **Manual testing steps**
```gherkin
Feature: Wallet Deletion
Scenario: Delete wallet from seedless relogin prompt
Given the user encounters a seedless controller error
And the error prompt is displayed
When the user taps the primary button to delete wallet
Then the wallet state should be reset
And user data should be deleted
And the user should be navigated to onboarding
And no errors should occur during the deletion process
Scenario: Delete wallet from settings
Given the user is viewing the Security Settings screen
And the user navigates to the Delete Wallet modal
When the user confirms wallet deletion
Then the wallet state should be reset
And user data should be deleted
And cookies should be cleared
And the user should be navigated to onboarding
And analytics should track the deletion event
Scenario: Wallet deletion error handling
Given the user attempts to delete the wallet
When an error occurs during wallet reset
Then the error should be logged
And the loading state should be reset
And the user should see appropriate error feedback
And the app should remain in a stable state
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!--
Before: Wallet deletion logic was split between a hook and the
Authentication service
- useDeleteWallet hook managed resetWalletState and deleteUser
separately
- Components had to call both methods in sequence manually
- No enforcement that both methods were always called together
-->
### **After**
https://github.com/user-attachments/assets/7dcb9cc3-cb52-4c50-8f1c-009fa402d1de
<!--
After: Wallet deletion is centralized in Authentication service
- Single deleteWallet() method ensures correct sequence
- Protected methods prevent misuse
- Cleaner component code with single method call
-->
## **Pre-merge author checklist**
- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Centralizes wallet deletion into `Authentication.deleteWallet()` and
refactors consumers/tests to use it, removing the old `useDeleteWallet`
hook.
>
> - **Core (Authentication)**:
> - Add `Authentication.deleteWallet()` as the public API; implements
sequential `resetWalletState` → `deleteUser`.
> - `resetWalletState` (protected): clears vault backups,
disables/re-enables `EngineClass.disableAutomaticVaultBackup`, creates
temp wallet, clears seedless state, resets rewards via
`Engine.controllerMessenger`, clears provider token, locks app (no
navigation).
> - `deleteUser` (protected): dispatches `setExistingUser(false)` and
triggers `MetaMetrics.createDataDeletionTask`.
> - Also removes `OPTIN_META_METRICS_UI_SEEN` and dispatches
`setCompletedOnboarding(false)`.
> - **UI**:
> - `DeleteWalletModal`: replace hook flow with
`Authentication.deleteWallet()`, keep sign-out, clear cookies, dispatch
`clearHistory`, track event, then navigate to onboarding.
> - `usePromptSeedlessRelogin`: invoke `Authentication.deleteWallet()`
and simplify flow; maintain loading/error state and `clearHistory` +
sign-out + navigation.
> - **Hooks**:
> - Remove `app/components/hooks/DeleteWallet/*` (hook and tests).
> - **Tests**:
> - Add comprehensive tests for `deleteWallet`, `resetWalletState`, and
`deleteUser` in `Authentication.test.ts`.
> - Update component/hook tests to assert calls to
`Authentication.deleteWallet()` and revised action sequences; remove
assertions tied to deleted hook/storage calls.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
b71e98d. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent e630595 commit 28d715f
9 files changed
Lines changed: 468 additions & 358 deletions
File tree
- app
- components
- UI/DeleteWalletModal
- hooks
- DeleteWallet
- SeedlessHooks
- core/Authentication
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | 12 | | |
14 | | - | |
15 | | - | |
16 | 13 | | |
| 14 | + | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
| |||
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | 23 | | |
32 | 24 | | |
33 | 25 | | |
| |||
74 | 66 | | |
75 | 67 | | |
76 | 68 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
82 | 73 | | |
83 | 74 | | |
84 | 75 | | |
| |||
153 | 144 | | |
154 | 145 | | |
155 | 146 | | |
156 | | - | |
157 | 147 | | |
158 | 148 | | |
159 | 149 | | |
| |||
164 | 154 | | |
165 | 155 | | |
166 | 156 | | |
167 | | - | |
168 | 157 | | |
169 | 158 | | |
170 | | - | |
171 | | - | |
| 159 | + | |
172 | 160 | | |
173 | 161 | | |
174 | 162 | | |
| |||
178 | 166 | | |
179 | 167 | | |
180 | 168 | | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
188 | 173 | | |
189 | 174 | | |
190 | 175 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
| |||
38 | 37 | | |
39 | 38 | | |
40 | 39 | | |
41 | | - | |
42 | | - | |
43 | 40 | | |
44 | 41 | | |
45 | 42 | | |
| |||
62 | 59 | | |
63 | 60 | | |
64 | 61 | | |
65 | | - | |
66 | 62 | | |
67 | 63 | | |
68 | 64 | | |
| |||
115 | 111 | | |
116 | 112 | | |
117 | 113 | | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
| 114 | + | |
122 | 115 | | |
123 | 116 | | |
124 | 117 | | |
| |||
This file was deleted.
Lines changed: 0 additions & 199 deletions
This file was deleted.
This file was deleted.
0 commit comments