@@ -556,10 +556,7 @@ export class DappClient {
556556 * }
557557 */
558558 async getFeeOptions ( chainId : number , transactions : Transaction [ ] ) : Promise < Relayer . FeeOption [ ] > {
559- if ( ! this . isInitialized ) throw new InitializationError ( 'Not initialized' )
560- const chainSessionManager = this . getChainSessionManager ( chainId )
561- if ( ! chainSessionManager . isInitialized )
562- throw new InitializationError ( `ChainSessionManager for chain ${ chainId } is not initialized.` )
559+ const chainSessionManager = await this . getOrInitializeChainManager ( chainId )
563560 return await chainSessionManager . getFeeOptions ( transactions )
564561 }
565562
@@ -584,11 +581,19 @@ export class DappClient {
584581 * @returns A promise that resolves to true if the session has permission, otherwise false.
585582 */
586583 async hasPermission ( chainId : number , transactions : Transaction [ ] ) : Promise < boolean > {
587- const chainSessionManager = this . chainSessionManagers . get ( chainId )
588- if ( ! chainSessionManager || ! chainSessionManager . isInitialized ) {
584+ if ( ! this . isInitialized ) {
585+ return false
586+ }
587+ try {
588+ const chainSessionManager = await this . getOrInitializeChainManager ( chainId )
589+ return await chainSessionManager . hasPermission ( transactions )
590+ } catch ( error ) {
591+ console . warn (
592+ `hasPermission check failed for chain ${ chainId } :` ,
593+ error instanceof Error ? error . message : String ( error ) ,
594+ )
589595 return false
590596 }
591- return await chainSessionManager . hasPermission ( transactions )
592597 }
593598
594599 /**
@@ -615,10 +620,7 @@ export class DappClient {
615620 * const txHash = await dappClient.sendTransaction(1, [transaction]);
616621 */
617622 async sendTransaction ( chainId : number , transactions : Transaction [ ] , feeOption ?: Relayer . FeeOption ) : Promise < Hex . Hex > {
618- if ( ! this . isInitialized ) throw new InitializationError ( 'Not initialized' )
619- const chainSessionManager = this . getChainSessionManager ( chainId )
620- if ( ! chainSessionManager . isInitialized )
621- throw new InitializationError ( `ChainSessionManager for chain ${ chainId } is not initialized.` )
623+ const chainSessionManager = await this . getOrInitializeChainManager ( chainId )
622624 return await chainSessionManager . buildSignAndSendTransactions ( transactions , feeOption )
623625 }
624626
@@ -795,6 +797,25 @@ export class DappClient {
795797 }
796798 }
797799
800+ /**
801+ * @private Retrieves or creates and initializes a ChainSessionManager for a given chain ID.
802+ * @param chainId The chain ID to get the ChainSessionManager for.
803+ * @returns The initialized ChainSessionManager for the given chain ID.
804+ */
805+ private async getOrInitializeChainManager ( chainId : number ) : Promise < ChainSessionManager > {
806+ if ( ! this . isInitialized || ! this . walletAddress ) {
807+ throw new InitializationError ( 'DappClient is not initialized.' )
808+ }
809+ const manager = this . getChainSessionManager ( chainId )
810+ if ( ! manager . isInitialized ) {
811+ await manager . initialize ( )
812+ }
813+ if ( ! manager . isInitialized ) {
814+ throw new InitializationError ( `ChainSessionManager for chain ${ chainId } could not be initialized.` )
815+ }
816+ return manager
817+ }
818+
798819 /**
799820 * @private Retrieves or creates a ChainSessionManager for a given chain ID.
800821 * @param chainId The chain ID to get the ChainSessionManager for.
0 commit comments