Skip to content

Commit

Permalink
Updates to personal_sign and fix QR code paste emulator button (#5071)
Browse files Browse the repository at this point in the history
* Replace Prompt wrapper with RN Alert prompt

* Add support for WC v2 flow in QR code paste emulator button

* Decode personal_sign hex messages to UTF8 if possible, otherwise use message as is

* Replace wc legacy-util with consistent hexToUtf8 method in parsing display details for requests

* Remove unnecessary message display modification in TransactionMessage
component

The message passed to the TransactionMessage component has already
been set to be the display message
  • Loading branch information
jinchung authored and Ibrahim Taveras committed Sep 25, 2023
1 parent c371c16 commit a15ccb2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
27 changes: 18 additions & 9 deletions src/components/qrcode-scanner/EmulatorPasteUriButton.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import lang from 'i18n-js';
import React, { useCallback } from 'react';
import { Alert } from 'react-native';
import { useIsEmulator } from 'react-native-device-info';
import { Prompt } from '../alerts';
import { Button } from '../buttons';
import { useWalletConnectConnections } from '@/hooks';
import { pair as pairWalletConnect } from '@/walletConnect';
import { parseUri } from '@walletconnect/utils';

export default function EmulatorPasteUriButton() {
const { result: isEmulator } = useIsEmulator();
const { walletConnectOnSessionRequest } = useWalletConnectConnections();
const { colors } = useTheme();

const handlePastedUri = useCallback(
async uri => walletConnectOnSessionRequest(uri),
[walletConnectOnSessionRequest]
uri => {
const { version } = parseUri(uri);
if (version === 1) {
walletConnectOnSessionRequest(uri);
} else if (version === 2) {
pairWalletConnect({ uri });
}
},
[pairWalletConnect, walletConnectOnSessionRequest]
);

const handlePressPasteSessionUri = useCallback(() => {
Prompt({
buttons: [{ onPress: handlePastedUri, text: lang.t('button.confirm') }],
message: lang.t('walletconnect.paste_uri.message'),
title: lang.t('walletconnect.paste_uri.title'),
type: 'plain-text',
});
Alert.prompt(
lang.t('walletconnect.paste_uri.title'),
lang.t('walletconnect.paste_uri.message'),
[{ onPress: handlePastedUri, text: lang.t('button.confirm') }],
'plain-text'
);
}, [handlePastedUri]);

return isEmulator ? (
Expand Down
10 changes: 1 addition & 9 deletions src/components/transaction/TransactionMessage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { addHexPrefix } from '@walletconnect/legacy-utils';
import React from 'react';
import { ScrollView } from 'react-native-gesture-handler';
import { isSignTypedData, PERSONAL_SIGN } from '../../utils/signingMethods';
import { isSignTypedData } from '../../utils/signingMethods';
import { Row } from '../layout';
import { Text } from '../text';
import { isHexString } from '@/handlers/web3';
import styled from '@/styled-thing';
import { padding } from '@/styles';
import { deviceUtils } from '@/utils';
Expand Down Expand Up @@ -39,13 +38,6 @@ const TransactionMessage = ({ maxHeight = 150, message, method }) => {
// eslint-disable-next-line no-empty
} catch (e) {}
msg = JSON.stringify(msg, null, 4);
} else if (method === PERSONAL_SIGN) {
if (isHexString(addHexPrefix(msg))) {
const normalizedMsg = addHexPrefix(msg);
const stripped = normalizedMsg.substring(2);
const buff = Buffer.from(stripped, 'hex');
msg = buff.toString('utf8');
}
}

return (
Expand Down
11 changes: 8 additions & 3 deletions src/parsers/requests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { convertHexToUtf8 } from '@walletconnect/legacy-utils';
import BigNumber from 'bignumber.js';
import { isNil } from 'lodash';
import { isHexString } from '@/handlers/web3';
Expand All @@ -9,6 +8,7 @@ import {
convertRawAmountToDecimalFormat,
fromWei,
} from '@/helpers/utilities';
import { logger } from '@/logger';
import { ethereumUtils } from '@/utils';
import {
isSignTypedData,
Expand All @@ -18,6 +18,7 @@ import {
SIGN_TRANSACTION,
} from '@/utils/signingMethods';
import { isAddress } from '@ethersproject/address';
import { toUtf8String } from '@ethersproject/strings';

export const getRequestDisplayDetails = (
payload,
Expand Down Expand Up @@ -62,10 +63,14 @@ export const getRequestDisplayDetails = (
let message = payload?.params?.find(p => !isAddress(p));
try {
if (isHexString(message)) {
message = convertHexToUtf8(message);
message = toUtf8String(message);
}
} catch (error) {
// TODO error handling
logger.debug(
'WC v2: getting display details, unable to decode hex message to UTF8 string',
{},
logger.DebugContext.walletconnect
);
}
return getMessageDisplayDetails(message, timestampInMs);
}
Expand Down
13 changes: 12 additions & 1 deletion src/walletConnect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,18 @@ export function parseRPCParams({
const [address, message] = params.sort(a => (isAddress(a) ? -1 : 1));
const isHex = isHexString(message);

const decodedMessage = isHex ? toUtf8String(message) : message;
let decodedMessage = message;
try {
if (isHex) {
decodedMessage = toUtf8String(message);
}
} catch (err) {
logger.debug(
'WC v2: parsing RPC params unable to decode hex message to UTF8 string',
{},
logger.DebugContext.walletconnect
);
}

return {
address: getAddress(address),
Expand Down

0 comments on commit a15ccb2

Please sign in to comment.