@@ -50,7 +50,7 @@ export default class SessionProvider extends BaseProvider {
5050 protected _disconnectRedirectUrl ?: string ;
5151 protected _policies : ParsedSessionPolicies ;
5252 protected _preset ?: string ;
53- private _policiesReady : Promise < void > ;
53+ private _ready : Promise < void > ;
5454 protected _keychainUrl : string ;
5555 protected _apiUrl : string ;
5656 protected _publicKey : string ;
@@ -103,15 +103,34 @@ export default class SessionProvider extends BaseProvider {
103103 this . _apiUrl = apiUrl ?? API_URL ;
104104 this . _signupOptions = signupOptions ;
105105
106- // If preset is provided, eagerly resolve policies from it.
107- // All async methods await this before accessing _policies.
108- this . _policiesReady = this . _preset
109- ? this . _resolvePresetPolicies ( )
110- : Promise . resolve ( ) ;
106+ // Eagerly start async init: resolve preset policies (if any),
107+ // then try to restore an existing session from storage.
108+ // All public async methods await this before proceeding.
109+ this . _ready = this . _init ( ) ;
111110
112- const account = ! this . _preset
113- ? this . tryRetrieveFromQueryOrStorage ( )
114- : undefined ;
111+ if ( typeof window !== "undefined" ) {
112+ ( window as any ) . starknet_controller_session = this ;
113+ }
114+ }
115+
116+ private async _init ( ) : Promise < void > {
117+ if ( this . _preset ) {
118+ const config = await loadConfig ( this . _preset ) ;
119+ if ( ! config ) {
120+ throw new Error ( `Failed to load preset: ${ this . _preset } ` ) ;
121+ }
122+
123+ const sessionPolicies = getPresetSessionPolicies ( config , this . _chainId ) ;
124+ if ( ! sessionPolicies ) {
125+ throw new Error (
126+ `No policies found for chain ${ this . _chainId } in preset ${ this . _preset } ` ,
127+ ) ;
128+ }
129+
130+ this . _policies = parsePolicies ( sessionPolicies ) ;
131+ }
132+
133+ const account = this . tryRetrieveFromQueryOrStorage ( ) ;
115134 if ( ! account ) {
116135 const pk = stark . randomAddress ( ) ;
117136 this . _publicKey = ec . starkCurve . getStarkKey ( pk ) ;
@@ -140,26 +159,6 @@ export default class SessionProvider extends BaseProvider {
140159 starknet : { privateKey : encode . addHexPrefix ( jsonPk . privKey ) } ,
141160 } ) ;
142161 }
143-
144- if ( typeof window !== "undefined" ) {
145- ( window as any ) . starknet_controller_session = this ;
146- }
147- }
148-
149- private async _resolvePresetPolicies ( ) : Promise < void > {
150- const config = await loadConfig ( this . _preset ! ) ;
151- if ( ! config ) {
152- throw new Error ( `Failed to load preset: ${ this . _preset } ` ) ;
153- }
154-
155- const sessionPolicies = getPresetSessionPolicies ( config , this . _chainId ) ;
156- if ( ! sessionPolicies ) {
157- throw new Error (
158- `No policies found for chain ${ this . _chainId } in preset ${ this . _preset } ` ,
159- ) ;
160- }
161-
162- this . _policies = parsePolicies ( sessionPolicies ) ;
163162 }
164163
165164 private validatePoliciesSubset (
@@ -249,8 +248,8 @@ export default class SessionProvider extends BaseProvider {
249248 }
250249
251250 async username ( ) {
252- await this . _policiesReady ;
253- await this . tryRetrieveFromQueryOrStorage ( ) ;
251+ await this . _ready ;
252+ this . tryRetrieveFromQueryOrStorage ( ) ;
254253 return this . _username ;
255254 }
256255
@@ -259,7 +258,7 @@ export default class SessionProvider extends BaseProvider {
259258 return this . account ;
260259 }
261260
262- await this . _policiesReady ;
261+ await this . _ready ;
263262 this . account = this . tryRetrieveFromQueryOrStorage ( ) ;
264263 return this . account ;
265264 }
@@ -269,7 +268,7 @@ export default class SessionProvider extends BaseProvider {
269268 return this . account ;
270269 }
271270
272- await this . _policiesReady ;
271+ await this . _ready ;
273272
274273 this . account = this . tryRetrieveFromQueryOrStorage ( ) ;
275274 if ( this . account ) {
0 commit comments