Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet-connect): switch to the safe-apps-provider under the hood #537

Merged
merged 2 commits into from
Oct 5, 2022
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
2 changes: 2 additions & 0 deletions apps/wallet-connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"dependencies": {
"@gnosis.pm/safe-react-components": "^0.9.7",
"@gnosis.pm/safe-react-gateway-sdk": "^2.10.1",
"@gnosis.pm/safe-apps-provider": "^0.13.2",
"@walletconnect/client": "^1.8.0",
"date-fns": "^2.29.3",
"ethers": "^5.7.1",
"jsqr": "^1.3.1"
},
"scripts": {
Expand Down
102 changes: 10 additions & 92 deletions apps/wallet-connect/src/hooks/useWalletConnect.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useState, useCallback, useEffect, useRef } from 'react'
import { useState, useCallback, useEffect, useRef, useMemo } from 'react'
import { ethers } from 'ethers'
import WalletConnect from '@walletconnect/client'
import { IClientMeta, IWalletConnectSession } from '@walletconnect/types'
import { useSafeAppsSDK } from '@gnosis.pm/safe-apps-react-sdk'
import { isMetaTxArray } from '../utils/transactions'
import { areStringsEqual } from '../utils/strings'
import { isObjectEIP712TypedData } from '@gnosis.pm/safe-apps-sdk'
import { SafeAppProvider } from '@gnosis.pm/safe-apps-provider'

const rejectWithMessage = (connector: WalletConnect, id: number | undefined, message: string) => {
connector.rejectRequest({ id, error: { message } })
Expand All @@ -14,6 +13,10 @@ const useWalletConnect = () => {
const { safe, sdk } = useSafeAppsSDK()
const [wcClientData, setWcClientData] = useState<IClientMeta | null>(null)
const [connector, setConnector] = useState<WalletConnect | undefined>()
const web3Provider = useMemo(
() => new ethers.providers.Web3Provider(new SafeAppProvider(safe, sdk)),
[sdk, safe],
)

const localStorageSessionKey = useRef(`session_${safe.safeAddress}`)

Expand Down Expand Up @@ -56,92 +59,7 @@ const useWalletConnect = () => {
}

try {
let result = '0x'

switch (payload.method) {
case 'eth_sendTransaction': {
const txInfo = payload.params[0]
const { safeTxHash } = await sdk.txs.send({
txs: [
{
to: txInfo.to,
value: txInfo.value || '0x0',
data: txInfo.data || '0x',
},
],
})

result = safeTxHash
break
}
case 'gs_multi_send': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I supose this was a test method that wasn't still implemented?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the method for batching transactions implemented by the OG app. I'm not sure if it's widely used. Quick GitHub search shows 1-2 apps: https://github.com/search?q=gs_multi_send&type=code

I think the mobile app continues to support it, so if we want to keep the support, we can implement it in the provider

const txs = payload.params

if (!isMetaTxArray(txs)) {
throw new Error('INVALID_TRANSACTIONS_PROVIDED')
}

const { safeTxHash } = await sdk.txs.send({
txs: txs.map(txInfo => ({
to: txInfo.to,
value: (txInfo.value || '0x0').toString(),
data: txInfo.data || '0x',
})),
})

result = safeTxHash
break
}

case 'personal_sign': {
const [message, address] = payload.params

if (!areStringsEqual(address, safe.safeAddress)) {
throw new Error('The address or message hash is invalid')
}

await sdk.txs.signMessage(message)

result = '0x'
break
}

case 'eth_sign': {
const [address, messageHash] = payload.params

if (!areStringsEqual(address, safe.safeAddress) || !messageHash.startsWith('0x')) {
throw new Error('The address or message hash is invalid')
}

await sdk.txs.signMessage(messageHash)

result = '0x'
break
}

case 'eth_signTypedData':
case 'eth_signTypedData_v4': {
const [address, typedDataString] = payload.params
const typedData = JSON.parse(typedDataString)

if (!areStringsEqual(address, safe.safeAddress)) {
throw new Error('The address is invalid')
}

if (isObjectEIP712TypedData(typedData)) {
dasanra marked this conversation as resolved.
Show resolved Hide resolved
await sdk.txs.signTypedMessage(typedData)

result = '0x'
break
} else {
throw new Error('Invalid typed data')
}
}
default: {
rejectWithMessage(wcConnector, payload.id, 'METHOD_NOT_SUPPORTED')
break
}
}
let result = await web3Provider.send(payload.method, payload.params)

wcConnector.approveRequest({
id: payload.id,
Expand All @@ -152,14 +70,14 @@ const useWalletConnect = () => {
}
})

wcConnector.on('disconnect', (error, payload) => {
wcConnector.on('disconnect', error => {
if (error) {
throw error
}
wcDisconnect()
})
},
[safe, sdk, wcDisconnect],
[safe, wcDisconnect, web3Provider],
)

useEffect(() => {
Expand Down
2 changes: 0 additions & 2 deletions apps/wallet-connect/src/utils/strings.ts

This file was deleted.

46 changes: 0 additions & 46 deletions apps/wallet-connect/src/utils/transactions.ts

This file was deleted.

Loading