Skip to content

Commit

Permalink
feat(suite): handle unknown protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
enjojoy authored and tomasklim committed Oct 18, 2024
1 parent 1d536b1 commit 36ce065
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export const NotificationRenderer = ({
return error(render, notification, 'TOAST_QR_INCORRECT_COIN_SCHEME_PROTOCOL', {
coin: notification.coin,
});
case 'qr-unknown-scheme-protocol':
return error(render, notification, 'TOAST_QR_UNKNOWN_SCHEME_PROTOCOL', {
scheme: notification.scheme,
error: notification.error,
});
case 'tor-toggle-error':
return error(render, notification, notification.error);
case 'tor-is-slow':
Expand Down
5 changes: 5 additions & 0 deletions packages/suite/src/support/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3677,6 +3677,11 @@ export default defineMessages({
id: 'TOAST_QR_INCORRECT_COIN_SCHEME_PROTOCOL',
defaultMessage: 'QR code is defined for {coin} account',
},
TOAST_QR_UNKNOWN_SCHEME_PROTOCOL: {
id: 'TOAST_QR_UNKNOWN_SCHEME_PROTOCOL',
defaultMessage:
'Unknown protocol scheme: "{scheme}". Please try again or enter the address manually.',
},
TOAST_COIN_SCHEME_PROTOCOL: {
id: 'TOAST_COIN_SCHEME_PROTOCOL',
describe: 'Required for current notifications. Do not change.',
Expand Down
31 changes: 17 additions & 14 deletions packages/suite/src/utils/suite/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export type CoinProtocolInfo = {

const removeLeadingTrailingSlashes = (text: string) => text.replace(/^\/{0,2}|\/$/g, '');

export const getProtocolInfo = (uri: string): CoinProtocolInfo | null => {
export const getProtocolInfo = (
uri: string,
): CoinProtocolInfo | null | { error: string; scheme: string } => {
const url = parseUri(uri);

if (url) {
Expand All @@ -27,23 +29,24 @@ export const getProtocolInfo = (uri: string): CoinProtocolInfo | null => {
},
});

if (getNetworkSymbolForProtocol(scheme)) {
if (!pathname && !host) return null; // address may be in pathname (regular bitcoin:addr) or host (bitcoin://addr)
if (!getNetworkSymbolForProtocol(scheme)) {
return { error: 'Unknown protocol', scheme };
}

const floatAmount = Number.parseFloat(params.amount ?? '');
const amount = !Number.isNaN(floatAmount) && floatAmount > 0 ? floatAmount : undefined;
if (!pathname && !host) return null; // address may be in pathname (regular bitcoin:addr) or host (bitcoin://addr)

const address =
removeLeadingTrailingSlashes(pathname) || removeLeadingTrailingSlashes(host);
const floatAmount = Number.parseFloat(params.amount ?? '');
const amount = !Number.isNaN(floatAmount) && floatAmount > 0 ? floatAmount : undefined;

return {
scheme,
address,
amount,
};
}
const address =
removeLeadingTrailingSlashes(pathname) || removeLeadingTrailingSlashes(host);

return {
scheme,
address,
amount,
};
}

return null;
};
export { getNetworkSymbolForProtocol };
18 changes: 16 additions & 2 deletions packages/suite/src/views/wallet/send/Outputs/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Row } from '@trezor/components';
import { HELP_CENTER_EVM_ADDRESS_CHECKSUM } from '@trezor/urls';
import { spacings } from '@trezor/theme';
import { CoinLogo } from '@trezor/product-components';
import { captureSentryMessage } from 'src/utils/suite/sentry';

const Container = styled.div`
position: relative;
Expand Down Expand Up @@ -113,9 +114,22 @@ export const Address = ({ output, outputId, outputsCount }: AddressProps) => {

const protocol = getProtocolInfo(uri);

if (protocol) {
const isSymbolValidProtocol = getNetworkSymbolForProtocol(protocol.scheme) === symbol;
if (protocol && 'error' in protocol) {
dispatch(
notificationsActions.addToast({
type: 'qr-unknown-scheme-protocol',
scheme: protocol.scheme,
error: protocol.error,
}),
);

captureSentryMessage(`QR code with unknown scheme: ${protocol.scheme}`);

return;
}

if (protocol && 'scheme' in protocol) {
const isSymbolValidProtocol = getNetworkSymbolForProtocol(protocol.scheme) === symbol; //is protocol valid for this account network
if (!isSymbolValidProtocol) {
dispatch(
notificationsActions.addToast({
Expand Down
5 changes: 5 additions & 0 deletions suite-common/toast-notifications/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export type ToastPayload = (
type: 'qr-incorrect-coin-scheme-protocol';
coin: string;
}
| {
type: 'qr-unknown-scheme-protocol';
scheme: string;
error: string;
}
| {
type: 'coin-scheme-protocol';
scheme: Protocol;
Expand Down

0 comments on commit 36ce065

Please sign in to comment.