Commit ba15f09
fix: SessionProvider preset support and address normalization (#2401)
## Summary
Fixes #2399.
Addresses `session/not-registered` errors caused by policy hash
divergence between the SDK and keychain.
### SDK changes (`@cartridge/controller`)
- **Add `preset` and `shouldOverridePresetPolicies` to
`SessionOptions`**: Developers can now pass `preset: "my-game"` instead
of manually duplicating policies.
Preset policies are resolved via `loadConfig()` from
`@cartridge/presets`, matching the keychain's behavior.
Policy precedence matches `ControllerProvider`: preset wins unless
`shouldOverridePresetPolicies: true`.
- **Consolidate async init into `_init()`**: Preset resolution, session
retrieval from storage, and signer key setup all happen in a single
eagerly-started async method.
Public methods (`username`, `probe`, `connect`) simply `await
this._ready`.
- **Normalize addresses in `toWasmPolicies`**: Applies
`getChecksumAddress()` to contract targets before hashing, preventing
divergence from inconsistent address casing.
- **Extract `getPresetSessionPolicies` utility**: Shared between
`SessionProvider` and the keychain's `getConfigChainPolicies`, replacing
~25 lines of defensive type narrowing with a clean helper.
- **Warn on silent policy drop**: When both `preset` and `policies` are
provided without the override flag, a `console.warn` is logged
(previously policies were silently ignored in `ControllerProvider`).
- **Send `preset` in keychain URL**: When preset is used, `connect()`
sends `&preset=` instead of `&policies=`, so the keychain resolves the
same policies from the same source.
### Keychain changes
- **Gate rendering on `isPoliciesResolved` in `Authentication`**: The
`Authentication` wrapper now waits for preset policies to fully resolve
before rendering child routes.
This replaces scattered `isPoliciesResolved` guards that were previously
in `ConnectRoute` and eliminates race conditions where components render
before async preset policy resolution completes.
### Root cause
When a developer configured presets and used `SessionProvider`, the
keychain would resolve policies from the preset while the SDK used the
developer's manually-passed policies.
If these differed (or if addresses had different casing), the session
hash would diverge, producing `session/not-registered` errors.
### Design note
Preset resolution is async (`loadConfig` fetches from the network), but
JS constructors can't be async.
The `_init()` method is eagerly started in the constructor and stores
its promise as `_ready`.
A cleaner alternative would be a static factory method
(`SessionProvider.create(options)`), but that's a breaking API change —
worth considering in a future major version.
### Follow-up
- Apply same preset support to `TelegramProvider` and Node.js
`SessionProvider`
## Test plan
- [x] All 45 controller unit tests pass (including updated
`toWasmPolicies` tests)
- [x] All 356 keychain tests pass
- [x] `tsc --noEmit` passes with zero errors
- [x] Full monorepo build succeeds
- [x] Lint and format checks pass
- [ ] Manual verification with a preset-configured app using
SessionProvider
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent bd9fd92 commit ba15f09
File tree
8 files changed
+285
-131
lines changed- packages
- controller/src
- __tests__
- iframe
- session
- keychain/src
- components
- hooks
- utils/api
8 files changed
+285
-131
lines changedLines changed: 89 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
4 | 15 | | |
5 | 16 | | |
6 | 17 | | |
7 | 18 | | |
8 | 19 | | |
9 | 20 | | |
10 | | - | |
| 21 | + | |
11 | 22 | | |
12 | 23 | | |
13 | | - | |
| 24 | + | |
14 | 25 | | |
15 | 26 | | |
16 | 27 | | |
| |||
19 | 30 | | |
20 | 31 | | |
21 | 32 | | |
22 | | - | |
| 33 | + | |
23 | 34 | | |
24 | 35 | | |
25 | | - | |
| 36 | + | |
26 | 37 | | |
27 | 38 | | |
28 | 39 | | |
| |||
32 | 43 | | |
33 | 44 | | |
34 | 45 | | |
35 | | - | |
36 | | - | |
37 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
38 | 49 | | |
39 | 50 | | |
40 | 51 | | |
41 | 52 | | |
42 | 53 | | |
43 | 54 | | |
44 | | - | |
| 55 | + | |
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
| |||
54 | 65 | | |
55 | 66 | | |
56 | 67 | | |
57 | | - | |
| 68 | + | |
58 | 69 | | |
59 | 70 | | |
60 | 71 | | |
| |||
74 | 85 | | |
75 | 86 | | |
76 | 87 | | |
77 | | - | |
| 88 | + | |
78 | 89 | | |
79 | 90 | | |
80 | 91 | | |
81 | 92 | | |
82 | 93 | | |
83 | | - | |
| 94 | + | |
84 | 95 | | |
85 | 96 | | |
86 | 97 | | |
87 | 98 | | |
88 | 99 | | |
89 | | - | |
| 100 | + | |
90 | 101 | | |
91 | 102 | | |
92 | 103 | | |
| |||
96 | 107 | | |
97 | 108 | | |
98 | 109 | | |
99 | | - | |
| 110 | + | |
100 | 111 | | |
101 | 112 | | |
102 | | - | |
| 113 | + | |
103 | 114 | | |
104 | 115 | | |
105 | 116 | | |
106 | 117 | | |
107 | 118 | | |
108 | | - | |
| 119 | + | |
109 | 120 | | |
110 | 121 | | |
111 | 122 | | |
| |||
119 | 130 | | |
120 | 131 | | |
121 | 132 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
127 | 138 | | |
128 | 139 | | |
129 | 140 | | |
130 | 141 | | |
131 | 142 | | |
132 | 143 | | |
133 | | - | |
| 144 | + | |
134 | 145 | | |
135 | 146 | | |
136 | | - | |
| 147 | + | |
137 | 148 | | |
138 | 149 | | |
139 | 150 | | |
| |||
142 | 153 | | |
143 | 154 | | |
144 | 155 | | |
145 | | - | |
| 156 | + | |
146 | 157 | | |
147 | 158 | | |
148 | | - | |
| 159 | + | |
149 | 160 | | |
150 | 161 | | |
151 | 162 | | |
| |||
157 | 168 | | |
158 | 169 | | |
159 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
160 | 199 | | |
161 | 200 | | |
162 | 201 | | |
| |||
179 | 218 | | |
180 | 219 | | |
181 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
182 | 227 | | |
183 | 228 | | |
184 | 229 | | |
185 | 230 | | |
186 | | - | |
| 231 | + | |
187 | 232 | | |
188 | 233 | | |
189 | 234 | | |
190 | | - | |
| 235 | + | |
191 | 236 | | |
192 | 237 | | |
193 | 238 | | |
| |||
200 | 245 | | |
201 | 246 | | |
202 | 247 | | |
203 | | - | |
204 | | - | |
| 248 | + | |
| 249 | + | |
205 | 250 | | |
206 | 251 | | |
207 | 252 | | |
| |||
213 | 258 | | |
214 | 259 | | |
215 | 260 | | |
216 | | - | |
| 261 | + | |
217 | 262 | | |
218 | 263 | | |
219 | 264 | | |
220 | | - | |
| 265 | + | |
221 | 266 | | |
222 | 267 | | |
223 | 268 | | |
| |||
237 | 282 | | |
238 | 283 | | |
239 | 284 | | |
240 | | - | |
| 285 | + | |
241 | 286 | | |
242 | 287 | | |
243 | 288 | | |
| |||
267 | 312 | | |
268 | 313 | | |
269 | 314 | | |
270 | | - | |
| 315 | + | |
271 | 316 | | |
272 | 317 | | |
273 | 318 | | |
274 | | - | |
| 319 | + | |
275 | 320 | | |
276 | 321 | | |
277 | 322 | | |
| |||
290 | 335 | | |
291 | 336 | | |
292 | 337 | | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
293 | 342 | | |
294 | 343 | | |
295 | 344 | | |
296 | | - | |
| 345 | + | |
297 | 346 | | |
298 | 347 | | |
299 | 348 | | |
| |||
307 | 356 | | |
308 | 357 | | |
309 | 358 | | |
310 | | - | |
| 359 | + | |
311 | 360 | | |
312 | 361 | | |
313 | 362 | | |
| |||
316 | 365 | | |
317 | 366 | | |
318 | 367 | | |
319 | | - | |
| 368 | + | |
320 | 369 | | |
321 | 370 | | |
322 | 371 | | |
323 | | - | |
| 372 | + | |
324 | 373 | | |
325 | 374 | | |
326 | 375 | | |
| |||
339 | 388 | | |
340 | 389 | | |
341 | 390 | | |
342 | | - | |
| 391 | + | |
343 | 392 | | |
344 | 393 | | |
345 | 394 | | |
| |||
352 | 401 | | |
353 | 402 | | |
354 | 403 | | |
355 | | - | |
| 404 | + | |
356 | 405 | | |
357 | 406 | | |
358 | 407 | | |
359 | 408 | | |
360 | | - | |
| 409 | + | |
361 | 410 | | |
362 | 411 | | |
363 | 412 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
107 | 114 | | |
108 | 115 | | |
109 | 116 | | |
| |||
0 commit comments