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

feat: Redesign Signature Decoding Simulation #12994

Open
wants to merge 85 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
87b746e
feat: Permit Simulation
digiwand Dec 6, 2024
60086e1
refactor: PermitSimulation const name
digiwand Dec 6, 2024
ae2a2cb
fix: label_change_type i18n
digiwand Dec 9, 2024
5b5d094
feat: add PermitSimulation metrics
digiwand Dec 9, 2024
dfc086f
feat: create ButtonPill component
digiwand Dec 10, 2024
d96f9aa
feat: PermitSimulation token value BottomModal
digiwand Dec 12, 2024
a665679
Merge branch 'main' into feat-permit-simulation
digiwand Dec 13, 2024
518e51d
feat: Add Permit Simulation to TypedSignV3V4 redesign
digiwand Dec 16, 2024
f069e37
refactor: cleanup
digiwand Dec 17, 2024
fe15f12
chore: update todo ValueDisplay shorten comment
digiwand Dec 17, 2024
6777319
refactor: update useGetTokenStandardAndDetails import
digiwand Dec 18, 2024
4bc90e0
fix: PermitSimulation changeType
digiwand Dec 18, 2024
b65adf3
test: PermitSimulation
digiwand Dec 18, 2024
a79f2cd
fix: useTrackERC20WithoutDecimalInformation should run once
digiwand Dec 18, 2024
a5d96ec
fix: include typedSignV4ConfirmationState for PermitSimulation test
digiwand Dec 18, 2024
05669d3
test: create PermitSimulationValueDisplay test
digiwand Dec 18, 2024
bcccb06
fix: TS lint signature util
digiwand Dec 19, 2024
22f754f
Merge branch 'main' into feat-permit-simulation
digiwand Jan 7, 2025
567fc66
fix: readd dep to useTrackERC20WithoutDecimalInformation
digiwand Jan 7, 2025
0c215c4
chore: update useTrackERC20WithoutDecimalInformation TS metricLocation
digiwand Jan 7, 2025
1683123
test: ValueDisplay test
digiwand Jan 7, 2025
79a4003
fix: test allow style param InfoAddress
digiwand Jan 7, 2025
aa43eb9
test:fix: OrderDetails allow undefined style param
digiwand Jan 7, 2025
8577591
test:fix: include InfoAddress undefined style param
digiwand Jan 7, 2025
d53035f
chore: ButtonPill pressed -> isPressed
digiwand Jan 7, 2025
2bdb950
build: mv ButtonPill files -> components-temp
digiwand Jan 7, 2025
1ffe22e
test: create confirmations/utils/signature test
digiwand Jan 9, 2025
779f36f
test: add parseTypedDataMessage tests
digiwand Jan 9, 2025
5852d87
test: add shortenString tests for skipCharacterInEnd + update TS
digiwand Jan 9, 2025
fce5367
Merge branch 'main' into feat-permit-simulation
digiwand Jan 11, 2025
f0cc923
build: mv permit simulation files
digiwand Jan 11, 2025
98d89c7
build: rn path Simulation/Permit -> Simulation/TypedSignPermit
digiwand Jan 11, 2025
666f11d
fix: Simulation/TypedSignPermit path
digiwand Jan 13, 2025
f2e16ef
build: mv Simulation Value Display
digiwand Jan 13, 2025
fb9e3ae
refactor: rn PermitSimulationValueDisplay ->
digiwand Jan 13, 2025
4e32c99
Merge branch 'main' into feat-permit-simulation
digiwand Jan 14, 2025
f99a239
refactor: rn onPressTokenValue -> handlePressTokenValue
digiwand Jan 14, 2025
2e75fb1
refactor: PermitSimulation use signatureRequest chain id instead of g…
digiwand Jan 14, 2025
3bfc4ae
Merge branch 'main' into feat-permit-simulation
digiwand Jan 14, 2025
b5a8372
test:fix: ValueDisplay update mock paths
digiwand Jan 14, 2025
72ba710
refactor: revert Address style param + wrap ValueDisplay address
digiwand Jan 14, 2025
9053d15
refactor: useGetTokenStandardAndDetails accept options param
digiwand Jan 14, 2025
ddfbfb0
feat: permit simulation include NetworkClientId to get token details
digiwand Jan 14, 2025
e89622c
fix: TypedSignPermit use networkClientId by chainID
digiwand Jan 14, 2025
ab0b8c2
fix: Simulation permit following use of useSignatureRequest hook
digiwand Jan 14, 2025
a415894
fix: rm unused useSelector import Permit simulation
digiwand Jan 14, 2025
97ad785
build: mv simulation code back into info
digiwand Jan 14, 2025
da11a11
Merge branch 'main' into feat-permit-simulation
digiwand Jan 14, 2025
c88eec4
Merge branch 'main' into feat-permit-simulation
digiwand Jan 15, 2025
cab0624
test:fix: mock paths after relocation Simulation files
digiwand Jan 15, 2025
2a6f66a
Merge branch 'main' into feat-permit-simulation
digiwand Jan 15, 2025
0505891
style: add alignItems: 'center' to flexRowTokenValueAndAddress
digiwand Jan 15, 2025
41c0cbd
refactor: add isRecognizedPermit SignatureRequest support
digiwand Jan 15, 2025
64d7415
refactor: configure decoding for SignatureController
digiwand Jan 15, 2025
770e80b
feat: add NativeValueDisplay component and update ValueDisplay component
digiwand Jan 15, 2025
95387ba
refactor: update type ApprovalRequestType to include decoding props
digiwand Jan 15, 2025
1dc8c13
feat: add Decoding Simulation components
digiwand Jan 15, 2025
724bcbe
fix: selectConversionRate optional param
digiwand Jan 15, 2025
b71c8ef
refactor: selectCurrencyRateControllerState optionalize showFiatOnTes…
digiwand Jan 15, 2025
2f8a2b4
fix: tslint
digiwand Jan 15, 2025
ad6945a
fix: replace Ramp/Box with View components
digiwand Jan 15, 2025
2cb0929
Merge branch 'main' into feat-decoding-simulation
digiwand Jan 15, 2025
4273dab
fix: Revoke en i18n casing
digiwand Jan 15, 2025
786ed46
fix: i18n en alphabetical ordering
digiwand Jan 15, 2025
7770519
fix: optional chainId selectConversionRate selector
digiwand Jan 15, 2025
b19b59c
refactor: format code Simulation
digiwand Jan 15, 2025
2c78752
refactor: revert selectConversionRate param updates + fix lint
digiwand Jan 15, 2025
f3c81b9
refactor: useTypesSignSimulationEnabled -> useTypedSignSimulationEnabled
digiwand Jan 15, 2025
9f96a79
refactor: update useTypedSignSimulationEnabled to use useSignatureReq…
digiwand Jan 15, 2025
43d7a0f
fix: mv DECODING_API_URL to AppConstants
digiwand Jan 16, 2025
af9e69b
fix: update isRecognizedPermit to check only signature request types
digiwand Jan 16, 2025
18999f8
fix: rm unused ApprovalRequest import
digiwand Jan 16, 2025
29fbf83
fix: revert ApprovalRequestType type update
digiwand Jan 16, 2025
f42522d
refactor: rm useMemo from ValueDisplay + add isNumberValue util
digiwand Jan 16, 2025
a083490
fix: update ValueDisplay disable ButtonPill when tokenId
digiwand Jan 16, 2025
00d6f09
revert: selectConversionRate optional chainId param
digiwand Jan 16, 2025
fb047c5
refactor: utilize new selectConversionRateByChainId in NativeValueDis…
digiwand Jan 16, 2025
28fcc62
revert: selectConversionRate 5th param
digiwand Jan 16, 2025
76ba380
test: CurrencyRateController#selectConversionRateByChainId
digiwand Jan 16, 2025
21ad733
refactor: non-default export useTypedSignSimulationEnabled
digiwand Jan 16, 2025
08030f0
fix: lint unused imports currencyRateController test
digiwand Jan 16, 2025
ccc001a
test: create TypedSignDecoded tests
digiwand Jan 16, 2025
add8c45
fix: lint paths
digiwand Jan 16, 2025
ee1d98d
refactor: remove duplicate NativeValueDisplay styles
digiwand Jan 16, 2025
95ffdf8
fix: NativeValueDisplay style names
digiwand Jan 16, 2025
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
3 changes: 3 additions & 0 deletions .js.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export SEGMENT_FLUSH_INTERVAL="1"
# example for flush when 1 event is queued
export SEGMENT_FLUSH_EVENT_LIMIT="1"

# URL of the decoding API used to provide additional data from signature requests
export DECODING_API_URL: 'https://signature-insights.api.cx.metamask.io/v1'

# URL of security alerts API used to validate dApp requests.
export SECURITY_ALERTS_API_URL="https://security-alerts.api.cx.metamask.io"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';

import { useTypedSignSimulationEnabled } from '../../../../../hooks/useTypedSignSimulationEnabled';
import { isRecognizedPermit } from '../../../../../utils/signature';
import { useSignatureRequest } from '../../../../../hooks/useSignatureRequest';
import DecodedSimulation from './TypedSignDecoded';
import PermitSimulation from './TypedSignPermit';

const TypedSignV3V4Simulation: React.FC<object> = () => {
const signatureRequest = useSignatureRequest();
const isPermit = signatureRequest && isRecognizedPermit(signatureRequest);
const isSimulationSupported = useTypedSignSimulationEnabled();

if (!isSimulationSupported || !signatureRequest) {
return null;
}

const { decodingData, decodingLoading } = signatureRequest;
const hasDecodingData = !(
(!decodingLoading && decodingData === undefined) ||
decodingData?.error
);

if (!hasDecodingData && isPermit) {
return <PermitSimulation />;
}

return <DecodedSimulation />;
};

export default TypedSignV3V4Simulation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { StyleSheet, View } from 'react-native';

import { useStyles } from '../../../../../../../../../component-library/hooks';
import InfoRow from '../../../../../UI/InfoRow';
import InfoSection from '../../../../../UI/InfoRow/InfoSection';
import Loader from '../../../../../../../../../component-library/components-temp/Loader';

const styleSheet = () => StyleSheet.create({
base: {
display: 'flex',
justifyContent: 'space-between',
},
loaderContainer: {
display: 'flex',
justifyContent: 'center',
},
});

const StaticSimulation: React.FC<{
title: string;
titleTooltip: string;
description?: string;
simulationElements: React.ReactNode;
isLoading?: boolean;
isCollapsed?: boolean;
}> = ({
title,
titleTooltip,
description,
simulationElements,
isLoading,
isCollapsed = false,
}) => {
const { styles } = useStyles(styleSheet, {});

return(
<View style={isCollapsed ? styles.base : {}}>
<InfoSection>
<InfoRow label={title} tooltip={titleTooltip}>
{description}
</InfoRow>
{isLoading ? (
<View style={styles.loaderContainer}>
<Loader size={'small'} />
</View>
) : (
simulationElements
)}
</InfoSection>
</View>
);
};

export default StaticSimulation;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Static';
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import React from 'react';
import cloneDeep from 'lodash/cloneDeep';
import {
DecodingDataChangeType,
DecodingDataStateChanges,
SignatureRequest,
} from '@metamask/signature-controller';

import { strings } from '../../../../../../../../../../locales/i18n';
import { typedSignV4ConfirmationState } from '../../../../../../../../../util/test/confirm-data-helpers';
import renderWithProvider from '../../../../../../../../../util/test/renderWithProvider';
import TypedSignDecoded, { getStateChangeToolip, getStateChangeType, StateChangeType } from './TypedSignDecoded';

const stateChangesApprove = [
{
assetType: 'ERC20',
changeType: DecodingDataChangeType.Approve,
address: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad',
amount: '12345',
contractAddress: '0x6b175474e89094c44da98b954eedeac495271d0f',
},
];

const stateChangesListingERC1155: DecodingDataStateChanges = [
{
assetType: 'NATIVE',
changeType: DecodingDataChangeType.Receive,
address: '',
amount: '900000000000000000',
contractAddress: '',
},
{
assetType: 'ERC1155',
changeType: DecodingDataChangeType.Listing,
address: '',
amount: '',
contractAddress: '0xafd4896984CA60d2feF66136e57f958dCe9482d5',
tokenID: '77789',
},
];

const stateChangesNftListing: DecodingDataStateChanges = [
{
assetType: 'ERC721',
changeType: DecodingDataChangeType.Listing,
address: '',
amount: '',
contractAddress: '0x922dC160f2ab743312A6bB19DD5152C1D3Ecca33',
tokenID: '22222',
},
{
assetType: 'ERC20',
changeType: DecodingDataChangeType.Receive,
address: '',
amount: '950000000000000000',
contractAddress: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
},
];

const stateChangesNftBidding: DecodingDataStateChanges = [
{
assetType: 'ERC20',
changeType: DecodingDataChangeType.Bidding,
address: '',
amount: '',
contractAddress: '0x922dC160f2ab743312A6bB19DD5152C1D3Ecca33',
tokenID: '189',
},
{
assetType: 'ERC721',
changeType: DecodingDataChangeType.Receive,
address: '',
amount: '950000000000000000',
contractAddress: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
},
];

const mockState = (mockStateChanges: DecodingDataStateChanges, stubDecodingLoading: boolean = false) => {
const clonedMockState = cloneDeep(typedSignV4ConfirmationState);
const request = clonedMockState.engine.backgroundState.SignatureController.signatureRequests['fb2029e1-b0ab-11ef-9227-05a11087c334'] as SignatureRequest;
request.decodingLoading = stubDecodingLoading;
request.decodingData = {
stateChanges: mockStateChanges,
};

return clonedMockState;
};

describe('DecodedSimulation', () => {
it('renders for ERC20 approval', async () => {
const { getByText } = renderWithProvider(<TypedSignDecoded />, {
state: mockState(stateChangesApprove),
});

expect(await getByText('Estimated changes')).toBeDefined();
expect(await getByText('Spending cap')).toBeDefined();
expect(await getByText('12,345')).toBeDefined();
});

it('renders for ERC712 token', async () => {
const { getByText } = renderWithProvider(<TypedSignDecoded />, {
state: mockState(stateChangesNftListing),
});

expect(await getByText('Estimated changes')).toBeDefined();
expect(await getByText('Listing price')).toBeDefined();
expect(await getByText('You list')).toBeDefined();
expect(await getByText('#22222')).toBeDefined();
});

it('renders for ERC1155 token', async () => {
const { getByText } = renderWithProvider(<TypedSignDecoded />, {
state: mockState(stateChangesListingERC1155),
});

expect(await getByText('Estimated changes')).toBeDefined();
expect(await getByText('You receive')).toBeDefined();
expect(await getByText('You list')).toBeDefined();
expect(await getByText('#77789')).toBeDefined();
});

it('renders label only once if there are multiple state changes of same changeType', async () => {
const { getAllByText } = renderWithProvider(<TypedSignDecoded />, {
state: mockState([stateChangesApprove[0], stateChangesApprove[0], stateChangesApprove[0]]),
});

expect(await getAllByText('12,345')).toHaveLength(3);
expect(await getAllByText('Spending cap')).toHaveLength(1);
});

it('renders unavailable message if no state change is returned', async () => {
const { getByText } = renderWithProvider(<TypedSignDecoded />, {
state: mockState([]),
});

expect(await getByText('Estimated changes')).toBeDefined();
expect(await getByText('Unavailable')).toBeDefined();
});

describe('getStateChangeToolip', () => {
it('return correct tooltip when permit is for listing NFT', () => {
const tooltip = getStateChangeToolip(
StateChangeType.NFTListingReceive,
);
expect(tooltip).toBe(strings('confirm.simulation.decoded_tooltip_list_nft'));
});

it('return correct tooltip when permit is for bidding NFT', () => {
const tooltip = getStateChangeToolip(
StateChangeType.NFTBiddingReceive,
);
expect(tooltip).toBe(strings('confirm.simulation.decoded_tooltip_bid_nft'));
});
});

describe('getStateChangeType', () => {
it('return correct state change type for NFT listing receive', () => {
const stateChange = getStateChangeType(stateChangesNftListing, stateChangesNftListing[1]);
expect(stateChange).toBe(StateChangeType.NFTListingReceive);
});

it('return correct state change type for NFT bidding receive', () => {
const stateChange = getStateChangeType(stateChangesNftBidding, stateChangesNftBidding[1]);
expect(stateChange).toBe(StateChangeType.NFTBiddingReceive);
});
});
});
Loading
Loading