Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: personal sign with ledger #52

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/background/services/wallet/WalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
SignTypedDataVersion,
} from '@metamask/eth-sig-util';
import { LedgerService } from '../ledger/LedgerService';
import { BaseWallet, Wallet } from 'ethers';
import { BaseWallet, Wallet, isHexString } from 'ethers';
import { networks, Transaction } from 'bitcoinjs-lib';
import { prepareBtcTxForLedger } from './utils/prepareBtcTxForLedger';
import { ImportData, ImportType } from '../accounts/models';
Expand All @@ -65,6 +65,7 @@ import { SeedlessTokenStorage } from '../seedless/SeedlessTokenStorage';
import { SeedlessSessionManager } from '../seedless/SeedlessSessionManager';
import { getProviderForNetwork } from '@src/utils/network/getProviderForNetwork';
import { Network } from '../network/models';
import { utils } from '@avalabs/avalanchejs';

@singleton()
export class WalletService implements OnLock, OnUnlock {
Expand Down Expand Up @@ -733,7 +734,9 @@ export class WalletService implements OnLock, OnUnlock {
} else if (
[MessageType.ETH_SIGN, MessageType.PERSONAL_SIGN].includes(messageType)
) {
return wallet.signMessage(data);
const dataToSign = isHexString(data) ? utils.hexToBuffer(data) : data;

return wallet.signMessage(dataToSign);
} else {
throw new Error(`this function is not supported on your wallet`);
}
Expand Down
3 changes: 0 additions & 3 deletions src/localization/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@
"Maximum Limit": "Maximum Limit",
"Maximum priority fee cannot be greater than maximum fee": "Maximum priority fee cannot be greater than maximum fee",
"Memo": "Memo",
"Message signing not supported by this version of the Avalanche Ledger app": "Message signing not supported by this version of the Avalanche Ledger app",
"Message:": "Message:",
"Method": "Method",
"More instructions can be found <instructionLink>here</instructionLink>.": "More instructions can be found <instructionLink>here</instructionLink>.",
Expand Down Expand Up @@ -542,7 +541,6 @@
"Node ID": "Node ID",
"Normal": "Normal",
"Not Connected": "Not Connected",
"Not Supported": "Not Supported",
"Not a valid Avalanche (C-Chain) address. C-Chain addresses being with 0x": "Not a valid Avalanche (C-Chain) address. C-Chain addresses being with 0x",
"Not a valid Bitcoin address": "Not a valid Bitcoin address",
"Not a valid ERC-20 token address.": "Not a valid ERC-20 token address.",
Expand Down Expand Up @@ -607,7 +605,6 @@
"Please try again later or contact support.": "Please try again later or contact support.",
"Please try again.": "Please try again.",
"Please update the <typography>Avalanche Application</typography> on your Ledger device to continue.": "Please update the <typography>Avalanche Application</typography> on your Ledger device to continue.",
"Please update to version 0.8.0 or newer": "Please update to version 0.8.0 or newer",
"Please use your FIDO device (<b>{{ deviceName }}</b>) to continue.": "Please use your FIDO device (<b>{{ deviceName }}</b>) to continue.",
"Please use your FIDO device to continue.": "Please use your FIDO device to continue.",
"Please verify your network connection or try again later.": "Please verify your network connection or try again later.",
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
Loading