Commit c85107f
authored
feat: Add optional JWT token authentication to multi-chain accounts API (#7165)
## Explanation
### What is the current state and why does it need to change?
Currently, the multi-chain accounts API calls in
`TokenDetectionController` and `TokenBalancesController` are made
without authentication. This limits the ability to provide user-specific
data and secure API endpoints that require authenticated requests.
### What is the solution and how does it work?
This PR adds optional JWT token authentication and timeout protection to
the accounts API calls:
1. **API Layer Changes** (`multi-chain-accounts.ts`):
- Added optional `jwtToken` parameter to `fetchMultiChainBalances` and
`fetchMultiChainBalancesV4`
- When a JWT token is provided, it's included in the `Authorization:
Bearer <token>` header
- The token is optional to maintain backward compatibility
2. **Controller Integration**:
- **TokenDetectionController**:
- Fetches JWT token from `AuthenticationController:getBearerToken` and
passes it to `fetchMultiChainBalances` when detecting tokens via
Accounts API
- **TokenBalancesController**: Fetches JWT token and passes it through
the balance fetcher chain to `fetchMultiChainBalancesV4`
3. **Balance Fetcher Updates** (`api-balance-fetcher.ts`):
- Updated `AccountsApiBalanceFetcher` to accept and pass JWT token
through the fetch chain
- Token flows from `updateBalances` → `fetch` → `#fetchBalances` → API
calls
### Key Design Decisions
- **Optional Parameter**: The JWT token is optional throughout the call
chain, ensuring backward compatibility for environments where
authentication is not available or required
- **Graceful Degradation**: If no token is provided, API calls proceed
without authentication, allowing the system to work in both
authenticated and unauthenticated scenarios
- **No Breaking Changes**: Existing callers continue to work without
modification
## References
<!-- Add any related issue numbers here, for example:
- Related to #XXXXX (if there's a tracking issue for JWT authentication)
-->
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- Added tests in `TokenDetectionController.test.ts` to verify JWT token
is passed correctly
- Added test in `TokenDetectionController.test.ts` to verify 30-second
timeout triggers RPC fallback
- Added tests in `TokenBalancesController.test.ts` to verify JWT token
flows through balance fetcher
- Added tests in `multi-chain-accounts.test.ts` to verify Authorization
header is set correctly
- Added tests for both scenarios: with and without JWT token
- Verified timeout behavior using fake timers (sinon) and the
`advanceTime` helper
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- Updated JSDoc comments for `fetchMultiChainBalances` and
`fetchMultiChainBalancesV4`
- Updated JSDoc for controller methods that now handle JWT tokens
- Added inline comments explaining timeout logic and fallback mechanism
- [ ] I've communicated my changes to consumers by updating changelogs
for packages I've changed, highlighting breaking changes as necessary
- **Note**: No breaking changes - all JWT token parameters are optional
and timeout is an internal improvement
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
- **Note**: Not required - no breaking changes introduced
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Adds optional JWT bearer authentication to multi‑chain Accounts API
calls and wires token retrieval through controllers, while lowering
timeouts to 10s and reducing API batch size to 20.
>
> - **API/Services**:
> - Add optional `jwtToken` to
`fetchMultiChainBalances`/`fetchMultiChainBalancesV4`; include
`Authorization: Bearer <token>` header when provided.
> - Reduce Accounts API request timeout to `10s`; reduce V4 batch size
from `50` to `20`.
> - **Controllers**:
> - `TokenDetectionController` and `TokenBalancesController` fetch JWT
via `AuthenticationController:getBearerToken` and pass it through
balance/token detection flows.
> - `TokenDetectionController` Accounts API timeout lowered to `10s`.
> - **Balance Fetcher**:
> - `AccountsApiBalanceFetcher` accepts/forwards `jwtToken`; applies 10s
timeout and 20-size batching.
> - **Tests**:
> - Add coverage for JWT header behavior (with/without token) and
updated batching/timeout.
> - Test scaffolding updated to mock
`AuthenticationController:getBearerToken` and token list state
injection.
> - **Dependencies/Meta**:
> - Add `@metamask/profile-sync-controller` as dev/peer dep.
> - Update `CHANGELOG.md` to document the new optional auth.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
d645dcf. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 4238ec7 commit c85107f
File tree
11 files changed
+176
-92
lines changed- packages/assets-controllers
- src
- multi-chain-accounts-service
11 files changed
+176
-92
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
10 | 19 | | |
11 | 20 | | |
12 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
| |||
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
| 128 | + | |
127 | 129 | | |
128 | 130 | | |
129 | 131 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| |||
Lines changed: 13 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
130 | 132 | | |
131 | 133 | | |
132 | 134 | | |
133 | | - | |
| 135 | + | |
| 136 | + | |
134 | 137 | | |
135 | 138 | | |
136 | 139 | | |
| |||
640 | 643 | | |
641 | 644 | | |
642 | 645 | | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
643 | 654 | | |
644 | 655 | | |
645 | 656 | | |
| |||
658 | 669 | | |
659 | 670 | | |
660 | 671 | | |
| 672 | + | |
661 | 673 | | |
662 | 674 | | |
663 | 675 | | |
| |||
Lines changed: 38 additions & 76 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
| 207 | + | |
207 | 208 | | |
208 | 209 | | |
209 | 210 | | |
| |||
3748 | 3749 | | |
3749 | 3750 | | |
3750 | 3751 | | |
3751 | | - | |
3752 | | - | |
3753 | | - | |
3754 | | - | |
3755 | | - | |
3756 | | - | |
3757 | | - | |
3758 | | - | |
3759 | | - | |
| 3752 | + | |
3760 | 3753 | | |
3761 | 3754 | | |
3762 | 3755 | | |
| |||
3773 | 3766 | | |
3774 | 3767 | | |
3775 | 3768 | | |
3776 | | - | |
3777 | | - | |
3778 | | - | |
3779 | | - | |
3780 | | - | |
| 3769 | + | |
| 3770 | + | |
| 3771 | + | |
3781 | 3772 | | |
3782 | 3773 | | |
3783 | 3774 | | |
| |||
3813 | 3804 | | |
3814 | 3805 | | |
3815 | 3806 | | |
3816 | | - | |
3817 | | - | |
3818 | | - | |
3819 | | - | |
3820 | | - | |
3821 | | - | |
3822 | | - | |
3823 | | - | |
3824 | | - | |
3825 | | - | |
| 3807 | + | |
3826 | 3808 | | |
3827 | 3809 | | |
3828 | 3810 | | |
3829 | 3811 | | |
3830 | 3812 | | |
3831 | 3813 | | |
3832 | | - | |
3833 | | - | |
3834 | | - | |
3835 | | - | |
3836 | | - | |
| 3814 | + | |
| 3815 | + | |
| 3816 | + | |
3837 | 3817 | | |
3838 | 3818 | | |
3839 | 3819 | | |
| |||
3877 | 3857 | | |
3878 | 3858 | | |
3879 | 3859 | | |
3880 | | - | |
3881 | | - | |
3882 | | - | |
3883 | | - | |
3884 | | - | |
3885 | | - | |
3886 | | - | |
3887 | | - | |
3888 | | - | |
3889 | | - | |
| 3860 | + | |
3890 | 3861 | | |
3891 | 3862 | | |
3892 | 3863 | | |
| |||
3912 | 3883 | | |
3913 | 3884 | | |
3914 | 3885 | | |
3915 | | - | |
3916 | | - | |
3917 | | - | |
3918 | | - | |
3919 | | - | |
| 3886 | + | |
| 3887 | + | |
| 3888 | + | |
3920 | 3889 | | |
3921 | 3890 | | |
3922 | 3891 | | |
| |||
3965 | 3934 | | |
3966 | 3935 | | |
3967 | 3936 | | |
3968 | | - | |
3969 | | - | |
3970 | | - | |
3971 | | - | |
3972 | | - | |
3973 | | - | |
3974 | | - | |
3975 | | - | |
3976 | | - | |
| 3937 | + | |
3977 | 3938 | | |
3978 | 3939 | | |
3979 | 3940 | | |
| |||
3990 | 3951 | | |
3991 | 3952 | | |
3992 | 3953 | | |
3993 | | - | |
3994 | | - | |
3995 | | - | |
3996 | | - | |
3997 | | - | |
| 3954 | + | |
| 3955 | + | |
| 3956 | + | |
3998 | 3957 | | |
3999 | 3958 | | |
4000 | 3959 | | |
| |||
4031 | 3990 | | |
4032 | 3991 | | |
4033 | 3992 | | |
4034 | | - | |
4035 | | - | |
4036 | | - | |
4037 | | - | |
4038 | | - | |
4039 | | - | |
4040 | | - | |
4041 | | - | |
4042 | | - | |
| 3993 | + | |
4043 | 3994 | | |
4044 | 3995 | | |
4045 | 3996 | | |
| |||
4056 | 4007 | | |
4057 | 4008 | | |
4058 | 4009 | | |
4059 | | - | |
4060 | | - | |
4061 | | - | |
4062 | | - | |
4063 | | - | |
| 4010 | + | |
| 4011 | + | |
| 4012 | + | |
4064 | 4013 | | |
4065 | 4014 | | |
4066 | 4015 | | |
| |||
4157 | 4106 | | |
4158 | 4107 | | |
4159 | 4108 | | |
| 4109 | + | |
4160 | 4110 | | |
| 4111 | + | |
4161 | 4112 | | |
4162 | 4113 | | |
4163 | 4114 | | |
| |||
4177 | 4128 | | |
4178 | 4129 | | |
4179 | 4130 | | |
4180 | | - | |
| 4131 | + | |
4181 | 4132 | | |
4182 | 4133 | | |
4183 | 4134 | | |
| |||
4240 | 4191 | | |
4241 | 4192 | | |
4242 | 4193 | | |
4243 | | - | |
| 4194 | + | |
4244 | 4195 | | |
4245 | 4196 | | |
4246 | | - | |
| 4197 | + | |
| 4198 | + | |
| 4199 | + | |
| 4200 | + | |
4247 | 4201 | | |
4248 | 4202 | | |
4249 | 4203 | | |
| |||
4253 | 4207 | | |
4254 | 4208 | | |
4255 | 4209 | | |
| 4210 | + | |
| 4211 | + | |
| 4212 | + | |
| 4213 | + | |
| 4214 | + | |
| 4215 | + | |
| 4216 | + | |
| 4217 | + | |
4256 | 4218 | | |
4257 | 4219 | | |
4258 | 4220 | | |
| |||
4312 | 4274 | | |
4313 | 4275 | | |
4314 | 4276 | | |
4315 | | - | |
| 4277 | + | |
4316 | 4278 | | |
4317 | 4279 | | |
4318 | 4280 | | |
| |||
0 commit comments