Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/wallet/dapp-client/src/ChainSessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import {
ModifyExplicitSessionPayload,
SessionResponse,
AddExplicitSessionPayload,
GetFeeTokensResponse,
} from './types/index.js'
import { CACHE_DB_NAME, VALUE_FORWARDER_ADDRESS } from './utils/constants.js'
import { ExplicitSession, ImplicitSession, ExplicitSessionConfig } from './index.js'
Expand Down Expand Up @@ -232,7 +231,7 @@ export class ChainSessionManager {

const implicitSession = await this.sequenceStorage.getImplicitSession()

if (implicitSession && implicitSession.chainId === this.chainId) {
if (implicitSession) {
await this._initializeImplicitSessionInternal(
implicitSession.pk,
walletAddress,
Expand Down
43 changes: 32 additions & 11 deletions packages/wallet/dapp-client/src/DappClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,7 @@ export class DappClient {
* }
*/
async getFeeOptions(chainId: number, transactions: Transaction[]): Promise<Relayer.FeeOption[]> {
if (!this.isInitialized) throw new InitializationError('Not initialized')
const chainSessionManager = this.getChainSessionManager(chainId)
if (!chainSessionManager.isInitialized)
throw new InitializationError(`ChainSessionManager for chain ${chainId} is not initialized.`)
const chainSessionManager = await this.getOrInitializeChainManager(chainId)
return await chainSessionManager.getFeeOptions(transactions)
}

Expand All @@ -584,11 +581,19 @@ export class DappClient {
* @returns A promise that resolves to true if the session has permission, otherwise false.
*/
async hasPermission(chainId: number, transactions: Transaction[]): Promise<boolean> {
const chainSessionManager = this.chainSessionManagers.get(chainId)
if (!chainSessionManager || !chainSessionManager.isInitialized) {
if (!this.isInitialized) {
return false
}
try {
const chainSessionManager = await this.getOrInitializeChainManager(chainId)
return await chainSessionManager.hasPermission(transactions)
} catch (error) {
console.warn(
`hasPermission check failed for chain ${chainId}:`,
error instanceof Error ? error.message : String(error),
)
return false
}
return await chainSessionManager.hasPermission(transactions)
}

/**
Expand All @@ -615,10 +620,7 @@ export class DappClient {
* const txHash = await dappClient.sendTransaction(1, [transaction]);
*/
async sendTransaction(chainId: number, transactions: Transaction[], feeOption?: Relayer.FeeOption): Promise<Hex.Hex> {
if (!this.isInitialized) throw new InitializationError('Not initialized')
const chainSessionManager = this.getChainSessionManager(chainId)
if (!chainSessionManager.isInitialized)
throw new InitializationError(`ChainSessionManager for chain ${chainId} is not initialized.`)
const chainSessionManager = await this.getOrInitializeChainManager(chainId)
return await chainSessionManager.buildSignAndSendTransactions(transactions, feeOption)
}

Expand Down Expand Up @@ -795,6 +797,25 @@ export class DappClient {
}
}

/**
* @private Retrieves or creates and initializes a ChainSessionManager for a given chain ID.
* @param chainId The chain ID to get the ChainSessionManager for.
* @returns The initialized ChainSessionManager for the given chain ID.
*/
private async getOrInitializeChainManager(chainId: number): Promise<ChainSessionManager> {
if (!this.isInitialized || !this.walletAddress) {
throw new InitializationError('DappClient is not initialized.')
}
const manager = this.getChainSessionManager(chainId)
if (!manager.isInitialized) {
await manager.initialize()
}
if (!manager.isInitialized) {
throw new InitializationError(`ChainSessionManager for chain ${chainId} could not be initialized.`)
}
return manager
}

/**
* @private Retrieves or creates a ChainSessionManager for a given chain ID.
* @param chainId The chain ID to get the ChainSessionManager for.
Expand Down