diff --git a/package.json b/package.json index 93279753..fc7fb3d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "contract-proxy-kit", - "version": "2.2.0-alpha.1", + "version": "2.2.0-alpha.3", "description": "Enable batched transactions and contract account interactions using a unique deterministic Gnosis Safe.", "main": "lib/cjs/index.js", "module": "lib/esm/index.js", diff --git a/src/CPK.ts b/src/CPK.ts index f08ef7dc..267ed8ba 100644 --- a/src/CPK.ts +++ b/src/CPK.ts @@ -18,6 +18,7 @@ import { StandardTransaction, normalizeGasLimit } from './utils/transactions' +import { checkConnectedToSafe } from './utils/checkConnectedToSafe' export interface CPKConfig { ethLibAdapter: EthLibAdapter @@ -92,11 +93,7 @@ class CPK { const ownerAccount = await this.getOwnerAccount() - const provider = this.#ethLibAdapter.getProvider() - const wc = provider && (provider.wc || (provider.connection && provider.connection.wc)) - if (wc && wc.peerMeta && wc.peerMeta.name && wc.peerMeta.name.startsWith('Gnosis Safe')) { - this.#isConnectedToSafe = true - } + this.#isConnectedToSafe = await checkConnectedToSafe(this.#ethLibAdapter.getProvider()) this.#multiSend = this.#ethLibAdapter.getContract(multiSendAbi, network.multiSendAddress) diff --git a/src/utils/checkConnectedToSafe.ts b/src/utils/checkConnectedToSafe.ts new file mode 100644 index 00000000..1b7d0eb4 --- /dev/null +++ b/src/utils/checkConnectedToSafe.ts @@ -0,0 +1,21 @@ +export async function checkConnectedToSafe(provider: any): Promise { + if (provider == null) return false + + const wc = + (await provider.getWalletConnector?.()) || + (await provider.connection?.getWalletConnector?.()) || + provider.wc || + provider.connection?.wc + + const peerName = wc?.peerMeta?.name + + if (peerName === 'Safe Multisig WalletConnect' || peerName?.startsWith?.('Gnosis Safe')) { + return true + } + + if (provider._providers) { + return (await Promise.all(provider._providers.map(checkConnectedToSafe))).includes(true) + } + + return false +}