From 6996c3e7b75a16c17c1a6f02e5ccbae47e5acec2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:59:08 +0100 Subject: [PATCH] chore: cherry-pick #8157 (#8497) This PR cherry-picks #8157 Co-authored-by: Matthew Walsh --- .../UI/PersonalSign/PersonalSign.tsx | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/app/components/UI/PersonalSign/PersonalSign.tsx b/app/components/UI/PersonalSign/PersonalSign.tsx index c4e58a45ada..0ba3d6e9083 100644 --- a/app/components/UI/PersonalSign/PersonalSign.tsx +++ b/app/components/UI/PersonalSign/PersonalSign.tsx @@ -4,7 +4,6 @@ import { View, Text, InteractionManager } from 'react-native'; import Engine from '../../../core/Engine'; import SignatureRequest from '../SignatureRequest'; import ExpandedMessage from '../SignatureRequest/ExpandedMessage'; -import { hexToText } from '@metamask/controller-utils'; import NotificationManager from '../../../core/NotificationManager'; import { strings } from '../../../../locales/i18n'; import { WALLET_CONNECT_ORIGIN } from '../../../util/walletconnect'; @@ -13,6 +12,7 @@ import AnalyticsV2 from '../../../util/analyticsV2'; import { getAddressAccountType, isExternalHardwareAccount, + stripHexPrefix, } from '../../../util/address'; import sanitizeString from '../../../util/string'; import { KEYSTONE_TX_CANCELED } from '../../../constants/error'; @@ -28,6 +28,25 @@ import { store } from '../../../store'; import { getBlockaidMetricsParams } from '../../../util/blockaid'; import { SecurityAlertResponse } from '../BlockaidBanner/BlockaidBanner.types'; import { SigningModalSelectorsIDs } from '../../../../e2e/selectors/Modals/SigningModal.selectors'; +import Logger from '../../../util/Logger'; + +/** + * Converts a hexadecimal string to a utf8 string. + * If the hexadecimal string is 32 bytes long, it is assumed to be a hash and returned as is. + * + * @param {string} hex - Hexadecimal string to convert + * @returns {string} - The utf8 equivalent or the original hexadecimal string. + */ +function msgHexToText(hex: string): string { + try { + const stripped = stripHexPrefix(hex); + const buff = Buffer.from(stripped, 'hex'); + return buff.length === 32 ? hex : buff.toString('utf8'); + } catch (e) { + Logger.log(e); + return hex; + } +} /** * Component that supports personal_sign @@ -161,7 +180,7 @@ const PersonalSign = ({ }; const renderMessageText = () => { - const textChild = sanitizeString(hexToText(messageParams.data)) + const textChild = sanitizeString(msgHexToText(messageParams.data)) .split('\n') .map((line: string, i: number) => (