wallet-connect-web-examples/wallets/react-wallet-v2/src/utils/EIP155RequestHandlerUtil.ts
Gancho Radkov d8ebe4509b
chore: updated @walletconnect deps (#164)
* chore: updates auth dapp

* chore: updates react sign dapp

* chore: updates cosmos dapp

* chore: updates ethers dapp

* chore: updates web3js dapp

* chore: updates vue auth dapp

* chore: updates auth wallet

* chore: updates eip155 wallet

* updates react-wallet-v2

* chore: updates web3wallet example

* feat: adds optional namespaces to main example wallet

* chore: eip155 example lock

* chore: removes console log

---------

Co-authored-by: Gancho Radkov <ganchoradkov@gmail.com>
2023-05-23 14:42:34 +03:00

80 lines
3.1 KiB
TypeScript

import { EIP155_CHAINS, EIP155_SIGNING_METHODS, TEIP155Chain } from '@/data/EIP155Data'
import { eip155Addresses, eip155Wallets } from '@/utils/EIP155WalletUtil'
import {
getSignParamsMessage,
getSignTypedDataParamsData,
getWalletAddressFromParams
} from '@/utils/HelperUtil'
import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils'
import { SignClientTypes } from '@walletconnect/types'
import { getSdkError } from '@walletconnect/utils'
import { providers } from 'ethers'
type RequestEventArgs = Omit<SignClientTypes.EventArguments['session_request'], 'verifyContext'>
export async function approveEIP155Request(requestEvent: RequestEventArgs) {
const { params, id } = requestEvent
const { chainId, request } = params
const wallet = eip155Wallets[getWalletAddressFromParams(eip155Addresses, params)]
switch (request.method) {
case EIP155_SIGNING_METHODS.PERSONAL_SIGN:
case EIP155_SIGNING_METHODS.ETH_SIGN:
try {
const message = getSignParamsMessage(request.params)
const signedMessage = await wallet.signMessage(message)
return formatJsonRpcResult(id, signedMessage)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA:
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V3:
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V4:
try {
const { domain, types, message: data } = getSignTypedDataParamsData(request.params)
// https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471
delete types.EIP712Domain
const signedData = await wallet._signTypedData(domain, types, data)
return formatJsonRpcResult(id, signedData)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION:
try {
const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc)
const sendTransaction = request.params[0]
const connectedWallet = wallet.connect(provider)
const { hash } = await connectedWallet.sendTransaction(sendTransaction)
return formatJsonRpcResult(id, hash)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
case EIP155_SIGNING_METHODS.ETH_SIGN_TRANSACTION:
try {
const signTransaction = request.params[0]
const signature = await wallet.signTransaction(signTransaction)
return formatJsonRpcResult(id, signature)
} catch (error: any) {
console.error(error)
alert(error.message)
return formatJsonRpcError(id, error.message)
}
default:
throw new Error(getSdkError('INVALID_METHOD').message)
}
}
export function rejectEIP155Request(request: RequestEventArgs) {
const { id } = request
return formatJsonRpcError(id, getSdkError('USER_REJECTED').message)
}