Skip to content

Commit

Permalink
Merge pull request #152 from gnosisguild/fix-nexus-connect
Browse files Browse the repository at this point in the history
Fix Safe app connection to nexus and stakewise
  • Loading branch information
jfschwarz authored Oct 4, 2024
2 parents 3340191 + 8e4d708 commit 563c15f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
52 changes: 47 additions & 5 deletions extension/src/bridge/SafeAppBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ChainId } from 'ser-kit'
import {
getBalances,
getSafeInfo,
SafeInfo,
TransactionDetails,
} from '@safe-global/safe-gateway-typescript-sdk'
import { CHAIN_CURRENCY, CHAIN_NAME, CHAIN_PREFIX } from '../chains'
Expand Down Expand Up @@ -50,16 +51,23 @@ export const SAFE_APP_WHITELIST = [
'https://app.spark.fi',
'https://community.safe.global',
'https://app.nexusmutual.io',
'https://v2.nexusmutual.io',
]

export default class SafeAppBridge {
private provider: Eip1193Provider
private connection: LegacyConnection
private connectedOrigin: string | undefined
private safeInfoPromise: Promise<SafeInfo>

constructor(provider: Eip1193Provider, connection: LegacyConnection) {
this.provider = provider
this.connection = connection

this.safeInfoPromise = getSafeInfo(
CHAIN_PREFIX[this.connection.chainId],
getAddress(this.connection.avatarAddress)
)
}

setProvider = (provider: Eip1193Provider) => {
Expand All @@ -71,6 +79,11 @@ export default class SafeAppBridge {
connection.avatarAddress !== this.connection.avatarAddress ||
connection.chainId !== this.connection.chainId

this.safeInfoPromise = getSafeInfo(
CHAIN_PREFIX[this.connection.chainId],
getAddress(this.connection.avatarAddress)
)

this.connection = connection
const href = await requestIframeHref()
const currentOrigin = href && new URL(href).origin
Expand Down Expand Up @@ -110,10 +123,12 @@ export default class SafeAppBridge {
| undefined
if (!handler) return

console.debug('SAFE_APP_MESSAGE', msg.data)
const logDetails = { data: msg.data, response: '⏳' } as any
console.debug('SAFE_APP_MESSAGE', logDetails)
try {
const response = await handler(msg.data.params, msg.data.id, msg.data.env)
if (typeof response !== 'undefined') {
logDetails.response = response
this.postResponse(msg.source, response, msg.data.id)
} else {
throw new Error('No response returned from handler')
Expand Down Expand Up @@ -174,10 +189,7 @@ export default class SafeAppBridge {
}),

[Methods.getSafeInfo]: async () => {
const info = await getSafeInfo(
CHAIN_PREFIX[this.connection.chainId],
getAddress(this.connection.avatarAddress)
)
const info = await this.safeInfoPromise
return {
safeAddress: getAddress(this.connection.avatarAddress),
chainId: this.connection.chainId,
Expand All @@ -187,6 +199,36 @@ export default class SafeAppBridge {
network:
LEGACY_CHAIN_NAME[this.connection.chainId] ||
CHAIN_NAME[this.connection.chainId].toUpperCase(),

// below is an example response as set within Safe{Wallet}:
// chainId: 1,
// fallbackHandler: '0xf48f2B2d2a534e402487b3ee7C18c33Aec0Fe5e4',
// guard: null,
// implementation: '0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552',
// isReadOnly: true,
// modules: [
// '0x27d8bb2e33Bc38A9CE93fdD90C80677b8436aFfb',
// '0x1cFB0CD7B1111bf2054615C7C491a15C4A3303cc',
// '0x0DA0C3e52C977Ed3cBc641fF02DD271c3ED55aFe',
// ],
// network: 'MAINNET',
// nonce: 2324,
// owners: [
// '0x1B0C638616Ed79dB430Edbf549ad9512FF4a8ed1',
// '0x507A7777E6DbF4680951E63fB3753a20F2c37706',
// '0xa3a3456BC0c8ce4e0a0415B619803ee96509Ce30',
// '0xe9eB7DA58f6B5CE5b0a6cFD778A2fa726203AAD5',
// '0xFcf00B0fEdBc8f2F35a3B8d4B858d5805f2Bb05D',
// '0xE4Df0cdC9eF7e388eA906226010bBD1B9A6fFeD9',
// '0xD68f1A882f3F9ffddaBd4D30c4F8Dfca1f9e51Ba',
// '0xA1cf7F847eCD82459ce05a218EaA38a9D92E7b6b',
// '0x8fd960F1B9D68BAD2B97bD232FB75CC1f186B064',
// '0x5eD64f02588C8B75582f2f8eFd7A5521e3F897CC',
// '0x0DA0C3e52C977Ed3cBc641fF02DD271c3ED55aFe',
// ],
// safeAddress: '0x849D52316331967b6fF1198e5E32A0eB168D039d',
// threshold: 3,
// version: '1.3.0',
}
},

Expand Down
1 change: 0 additions & 1 deletion extension/src/providers/ForkProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ class ForkProvider extends EventEmitter {
this.ownerAddress === ZeroAddress ? undefined : this.ownerAddress
const isSafe = await this.isSafePromise

console.log(this.moduleAddress, ownerAddress)
if (!isSafe && (this.moduleAddress || ownerAddress)) {
throw new Error(
'moduleAddress or ownerAddress is only supported for Safes as avatar'
Expand Down

0 comments on commit 563c15f

Please sign in to comment.