Skip to content

Commit

Permalink
fix: use ethereum app for message signing on ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelylovas committed Sep 25, 2024
1 parent fbac3fd commit 6ef1131
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 71 deletions.
17 changes: 2 additions & 15 deletions src/background/services/wallet/WalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,7 @@ import { SeedlessTokenStorage } from '../seedless/SeedlessTokenStorage';
import { SeedlessSessionManager } from '../seedless/SeedlessSessionManager';
import { getProviderForNetwork } from '@src/utils/network/getProviderForNetwork';
import { Network } from '../network/models';

const hexToBytes = (hexString: string) => {
const unprefixed = hexString.startsWith('0x')
? hexString.substring(2)
: hexString;
const matches = unprefixed.match(/.{1,2}/g);

if (matches) {
return Uint8Array.from(matches.map((byte) => parseInt(byte, 16)));
}

throw new Error('Invalid hex string');
};
import { utils } from '@avalabs/avalanchejs';

@singleton()
export class WalletService implements OnLock, OnUnlock {
Expand Down Expand Up @@ -746,8 +734,7 @@ export class WalletService implements OnLock, OnUnlock {
} else if (
[MessageType.ETH_SIGN, MessageType.PERSONAL_SIGN].includes(messageType)
) {
const isHex = isHexString(data); // we should likely sign it as bytes if that's the case
const dataToSign = isHex ? hexToBytes(data) : data;
const dataToSign = isHexString(data) ? utils.hexToBuffer(data) : data;

return wallet.signMessage(dataToSign);
} else {
Expand Down
63 changes: 7 additions & 56 deletions src/pages/SignMessage/SignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
Typography,
} from '@avalabs/core-k2-components';

import Dialog from '@src/components/common/Dialog';
import { ActionStatus } from '@src/background/services/actions/models';
import { MessageType } from '@src/background/services/messages/models';
import { SiteAvatar } from '@src/components/common/SiteAvatar';
Expand All @@ -33,11 +32,7 @@ import { SignTxErrorBoundary } from '../SignTransaction/components/SignTxErrorBo
import { useIsIntersecting } from './hooks/useIsIntersecting';
import { DAppProviderRequest } from '@src/background/connections/dAppConnection/models';
import { useLedgerDisconnectedDialog } from '@src/pages/SignTransaction/hooks/useLedgerDisconnectedDialog';
import {
LEDGER_VERSION_WITH_EIP_712,
LedgerAppType,
useLedgerContext,
} from '@src/contexts/LedgerProvider';
import { LedgerAppType } from '@src/contexts/LedgerProvider';
import { LedgerApprovalOverlay } from '@src/pages/SignTransaction/components/LedgerApprovalOverlay';
import { WalletConnectApprovalOverlay } from '../SignTransaction/components/WalletConnectApproval/WalletConnectApprovalOverlay';
import useIsUsingWalletConnectAccount from '@src/hooks/useIsUsingWalletConnectAccount';
Expand All @@ -52,7 +47,6 @@ import { FunctionIsOffline } from '@src/components/common/FunctionIsOffline';
import { useAccountsContext } from '@src/contexts/AccountsProvider';
import { AccountType } from '@src/background/services/accounts/models';
import { truncateAddress } from '@src/utils/truncateAddress';
import { isLedgerVersionCompatible } from '@src/utils/isLedgerVersionCompatible';
import { MaliciousTxAlert } from '@src/components/common/MaliciousTxAlert';
import { TxWarningBox } from '@src/components/common/TxWarningBox';

Expand All @@ -66,7 +60,6 @@ export function SignMessage() {
} = useApproveAction(requestId);

const isUsingLedgerWallet = useIsUsingLedgerWallet();
const { avaxAppVersion, hasLedgerTransport } = useLedgerContext();
const isUsingWalletConnectAccount = useIsUsingWalletConnectAccount();
const isFireblocksAccount = useIsUsingFireblocksAccount();
const { isFunctionAvailable: isSigningAvailable } = useIsFunctionAvailable(
Expand All @@ -76,21 +69,10 @@ export function SignMessage() {
accounts: { active: activeAccount, primary: primaryAccounts },
} = useAccountsContext();

const [showNotSupportedDialog, setShowNotSupportedDialog] = useState(false);
const [disableSubmitButton, setDisableSubmitButton] = useState(true);
const [messageAlertClosed, setMessageAlertClosed] = useState(false);
const endContentRef = useRef(null);
const isIntersecting = useIsIntersecting({ ref: endContentRef });
const disabledForLedger = useMemo(() => {
return (
isUsingLedgerWallet &&
hasLedgerTransport &&
avaxAppVersion &&
!isLedgerVersionCompatible(avaxAppVersion, LEDGER_VERSION_WITH_EIP_712) &&
action &&
action.method !== DAppProviderRequest.AVALANCHE_SIGN_MESSAGE
);
}, [isUsingLedgerWallet, hasLedgerTransport, avaxAppVersion, action]);

const signingAccountAddress = useMemo(() => {
if (!action || !activeAccount) {
Expand Down Expand Up @@ -150,12 +132,6 @@ export function SignMessage() {
}
}

useEffect(() => {
if (disabledForLedger) {
setShowNotSupportedDialog(true);
}
}, [disabledForLedger]);

const { handleApproval, handleRejection, isApprovalOverlayVisible } =
useApprovalHelpers({
onApprove: signMessage,
Expand Down Expand Up @@ -188,30 +164,11 @@ export function SignMessage() {
return null;
};

useLedgerDisconnectedDialog(() => handleRejection(), LedgerAppType.AVALANCHE);

const notSupportedDialog = (
<Stack sx={{ justifyContent: 'center', width: '100%' }}>
<Typography variant="h5" sx={{ textAlign: 'center' }}>
{t('Not Supported')}
</Typography>
<Typography variant="body2" sx={{ textAlign: 'center', mt: 1 }}>
{t(
'Message signing not supported by this version of the Avalanche Ledger app'
)}
<br />
{t('Please update to version 0.8.0 or newer')}
</Typography>
<Stack
sx={{
mt: 3,
}}
>
<Button sx={{ mb: 1 }} onClick={handleRejection}>
{t('Close')}
</Button>
</Stack>
</Stack>
useLedgerDisconnectedDialog(
() => handleRejection(),
action?.method === DAppProviderRequest.AVALANCHE_SIGN_MESSAGE
? LedgerAppType.AVALANCHE
: LedgerAppType.ETHEREUM
);

if (!action) {
Expand Down Expand Up @@ -483,20 +440,14 @@ export function SignMessage() {
<Button
color="primary"
size="large"
disabled={disabledForLedger || disableSubmitButton}
disabled={disableSubmitButton}
onClick={handleApproval}
fullWidth
>
{t('Approve')}
</Button>
</Stack>
</SignTxErrorBoundary>
<Dialog
onClose={() => window.close()}
open={showNotSupportedDialog}
content={notSupportedDialog}
bgColorDefault
/>
</Stack>
</>
);
Expand Down

0 comments on commit 6ef1131

Please sign in to comment.