Fix logic for personal_sign signing

This commit is contained in:
Ilja 2022-02-14 15:46:11 +02:00
parent e2ac17957e
commit 354cf0bf34
3 changed files with 14 additions and 13 deletions

View File

@ -1,3 +1,5 @@
import { utils } from 'ethers'
/** /**
* Truncates string (in the middle) via given lenght value * Truncates string (in the middle) via given lenght value
*/ */
@ -15,12 +17,12 @@ export function truncate(value: string, length: number) {
} }
/** /**
* Helps to get message from various sign methods present in eth * Converts hex to utf8 string if it is valid bytes
* @details https://docs.metamask.io/guide/signing-data.html#a-brief-history
*/ */
export function getSignMessage(params: string[], walletAddress: string) { export function convertHexToUtf8(value: string) {
// Remove our own address from params, so we are left with message if (utils.isHexString(value)) {
params.filter(p => p !== walletAddress) return utils.toUtf8String(value)
}
return params[0] return value
} }

View File

@ -1,4 +1,5 @@
import { EIP155_SIGNING_METHODS } from '@/data/EIP155Data' import { EIP155_SIGNING_METHODS } from '@/data/EIP155Data'
import { convertHexToUtf8 } from '@/utils/HelperUtil'
import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils' import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils'
import { RequestEvent } from '@walletconnect/types' import { RequestEvent } from '@walletconnect/types'
import { ERROR } from '@walletconnect/utils' import { ERROR } from '@walletconnect/utils'
@ -9,11 +10,11 @@ export async function approveEIP155Request(request: RequestEvent['request'], wal
switch (method) { switch (method) {
case EIP155_SIGNING_METHODS.PERSONAL_SIGN: case EIP155_SIGNING_METHODS.PERSONAL_SIGN:
const personalSignResult = await wallet.signMessage(params[0]) const personalSignResult = await wallet.signMessage(convertHexToUtf8(params[0]))
return formatJsonRpcResult(id, personalSignResult) return formatJsonRpcResult(id, personalSignResult)
case EIP155_SIGNING_METHODS.ETH_SIGN: case EIP155_SIGNING_METHODS.ETH_SIGN:
const ethSignResult = await wallet.signMessage(params[1]) const ethSignResult = await wallet.signMessage(convertHexToUtf8(params[1]))
return formatJsonRpcResult(id, ethSignResult) return formatJsonRpcResult(id, ethSignResult)
default: default:

View File

@ -1,10 +1,10 @@
import { EIP155_CHAINS, EIP155_SIGNING_METHODS, TEIP155Chain } from '@/data/EIP155Data' import { EIP155_CHAINS, EIP155_SIGNING_METHODS, TEIP155Chain } from '@/data/EIP155Data'
import ModalStore from '@/store/ModalStore' import ModalStore from '@/store/ModalStore'
import { convertHexToUtf8 } from '@/utils/HelperUtil'
import { approveEIP155Request, rejectEIP155Request } from '@/utils/RequestHandlerUtil' import { approveEIP155Request, rejectEIP155Request } from '@/utils/RequestHandlerUtil'
import { walletConnectClient } from '@/utils/WalletConnectUtil' import { walletConnectClient } from '@/utils/WalletConnectUtil'
import { wallet } from '@/utils/WalletUtil' import { wallet } from '@/utils/WalletUtil'
import { Avatar, Button, Col, Container, Divider, Link, Modal, Row, Text } from '@nextui-org/react' import { Avatar, Button, Col, Container, Divider, Link, Modal, Row, Text } from '@nextui-org/react'
import { utils } from 'ethers'
import { Fragment } from 'react' import { Fragment } from 'react'
export default function SessionSignModal() { export default function SessionSignModal() {
@ -23,11 +23,9 @@ export default function SessionSignModal() {
const { protocol } = requestSession.relay const { protocol } = requestSession.relay
const { name, icons, url } = requestSession.peer.metadata const { name, icons, url } = requestSession.peer.metadata
// Get message as utf string // Get message, convert it to UTF8 string if it is valid hex
let message = method === EIP155_SIGNING_METHODS.PERSONAL_SIGN ? params[0] : params[1] let message = method === EIP155_SIGNING_METHODS.PERSONAL_SIGN ? params[0] : params[1]
if (utils.isHexString(message)) { message = convertHexToUtf8(message)
message = utils.toUtf8String(message)
}
// Handle approve action (logic varies based on request method) // Handle approve action (logic varies based on request method)
async function onApprove() { async function onApprove() {