Skip to content

Commit

Permalink
Merge pull request #134 from gnosis/detect-safe
Browse files Browse the repository at this point in the history
Detect Adjust WalletConnected Safes
  • Loading branch information
germartinez authored Nov 20, 2020
2 parents 4d738f7 + a98a369 commit e52b8b4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 2 additions & 5 deletions src/CPK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
StandardTransaction,
normalizeGasLimit
} from './utils/transactions'
import { checkConnectedToSafe } from './utils/checkConnectedToSafe'

export interface CPKConfig {
ethLibAdapter: EthLibAdapter
Expand Down Expand Up @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions src/utils/checkConnectedToSafe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export async function checkConnectedToSafe(provider: any): Promise<boolean> {
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
}

0 comments on commit e52b8b4

Please sign in to comment.