Skip to content

Commit a47decf

Browse files
committed
Retain hasPermission
1 parent 49289e0 commit a47decf

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

packages/wallet/dapp-client/src/ChainSessionManager.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,41 @@ export class ChainSessionManager {
716716
throw new InitializationError(`Explicit session init failed after ${maxRetries} attempts: ${lastError.message}`)
717717
}
718718

719+
/**
720+
* Checks if the current session has permission to execute a set of transactions.
721+
* @param transactions The transactions to check permissions for.
722+
* @returns A promise that resolves to true if the session has permission, false otherwise.
723+
*/
724+
async hasPermission(transactions: Transaction[]): Promise<boolean> {
725+
if (!this.wallet || !this.sessionManager || !this.provider || !this.isInitialized) {
726+
return false
727+
}
728+
729+
try {
730+
const calls: Payload.Call[] = transactions.map((tx) => ({
731+
to: tx.to,
732+
value: tx.value ?? 0n,
733+
data: tx.data ?? '0x',
734+
gasLimit: tx.gasLimit ?? 0n,
735+
delegateCall: tx.delegateCall ?? false,
736+
onlyFallback: tx.onlyFallback ?? false,
737+
behaviorOnError: tx.behaviorOnError ?? ('revert' as const),
738+
}))
739+
740+
// Directly check if there are signers with the necessary permissions for all calls.
741+
// This will throw an error if any call is not supported.
742+
await this.sessionManager.findSignersForCalls(this.wallet.address, this.chainId, calls)
743+
return true
744+
} catch (error) {
745+
// An error from findSignersForCalls indicates a permission failure.
746+
console.warn(
747+
`Permission check failed for chain ${this.chainId}:`,
748+
error instanceof Error ? error.message : String(error),
749+
)
750+
return false
751+
}
752+
}
753+
719754
/**
720755
* Checks if the current session has a valid signer.
721756
* @returns A promise that resolves to true if the session has a valid signer, false otherwise.

packages/wallet/dapp-client/src/DappClient.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,20 @@ export class DappClient {
560560
return await chainSessionManager.getFeeOptions(this.walletAddress, transactions)
561561
}
562562

563+
/**
564+
* Checks if the current session has permission to execute a set of transactions on a specific chain.
565+
* @param chainId The chain ID on which to check the permissions.
566+
* @param transactions An array of transactions to check permissions for.
567+
* @returns A promise that resolves to true if the session has permission, otherwise false.
568+
*/
569+
async hasPermission(chainId: number, transactions: Transaction[]): Promise<boolean> {
570+
const chainSessionManager = this.chainSessionManagers.get(chainId)
571+
if (!chainSessionManager || !chainSessionManager.isInitialized) {
572+
return false
573+
}
574+
return await chainSessionManager.hasPermission(transactions)
575+
}
576+
563577
/**
564578
* Checks if the current session has a valid signer.
565579
* @param chainId The chain ID on which to check the signer.

0 commit comments

Comments
 (0)