fix: ensure wallet alerts visibly and sends JSON-RPC error response

This commit is contained in:
Ben Kremer 2023-04-05 13:50:32 +02:00
parent 649cf28159
commit c128875314

View File

@ -20,30 +20,54 @@ export async function approveEIP155Request(
switch (request.method) { switch (request.method) {
case EIP155_SIGNING_METHODS.PERSONAL_SIGN: case EIP155_SIGNING_METHODS.PERSONAL_SIGN:
case EIP155_SIGNING_METHODS.ETH_SIGN: case EIP155_SIGNING_METHODS.ETH_SIGN:
const message = getSignParamsMessage(request.params) try {
const signedMessage = await wallet.signMessage(message) const message = getSignParamsMessage(request.params)
return formatJsonRpcResult(id, signedMessage) 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:
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V3: case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V3:
case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V4: case EIP155_SIGNING_METHODS.ETH_SIGN_TYPED_DATA_V4:
const { domain, types, message: data } = getSignTypedDataParamsData(request.params) try {
// https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471 const { domain, types, message: data } = getSignTypedDataParamsData(request.params)
delete types.EIP712Domain // https://github.com/ethers-io/ethers.js/issues/687#issuecomment-714069471
const signedData = await wallet._signTypedData(domain, types, data) delete types.EIP712Domain
return formatJsonRpcResult(id, signedData) 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: case EIP155_SIGNING_METHODS.ETH_SEND_TRANSACTION:
const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc) try {
const sendTransaction = request.params[0] const provider = new providers.JsonRpcProvider(EIP155_CHAINS[chainId as TEIP155Chain].rpc)
const connectedWallet = wallet.connect(provider) const sendTransaction = request.params[0]
const { hash } = await connectedWallet.sendTransaction(sendTransaction) const connectedWallet = wallet.connect(provider)
return formatJsonRpcResult(id, hash) 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: case EIP155_SIGNING_METHODS.ETH_SIGN_TRANSACTION:
const signTransaction = request.params[0] try {
const signature = await wallet.signTransaction(signTransaction) const signTransaction = request.params[0]
return formatJsonRpcResult(id, signature) 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: default:
throw new Error(getSdkError('INVALID_METHOD').message) throw new Error(getSdkError('INVALID_METHOD').message)
@ -53,5 +77,5 @@ export async function approveEIP155Request(
export function rejectEIP155Request(request: SignClientTypes.EventArguments['session_request']) { export function rejectEIP155Request(request: SignClientTypes.EventArguments['session_request']) {
const { id } = request const { id } = request
return formatJsonRpcError(id, getSdkError('USER_REJECTED_METHODS').message) return formatJsonRpcError(id, getSdkError('USER_REJECTED').message)
} }