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: Implement redesigned staking confirmation entry point #13361

Merged
merged 31 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4ffc18d
Initial commit
OGPoyraz Feb 5, 2025
eb30eca
Merge branch 'main' into ogp/4078
OGPoyraz Feb 5, 2025
da4eb23
Update
OGPoyraz Feb 5, 2025
bfdf0df
Update
OGPoyraz Feb 5, 2025
574eccb
Merge branch 'main' into ogp/4078
OGPoyraz Feb 5, 2025
d91c948
Merge branch 'main' into ogp/4078
OGPoyraz Feb 6, 2025
b57115b
Fix suggestions
OGPoyraz Feb 6, 2025
c4d7683
Merge branch 'main' into ogp/4078
OGPoyraz Feb 6, 2025
e4b52cf
Update
OGPoyraz Feb 6, 2025
ff9283d
Fix unit tests
OGPoyraz Feb 6, 2025
b94c23e
Fix unit tests
OGPoyraz Feb 6, 2025
83a4325
Fix tests
OGPoyraz Feb 6, 2025
b8a3894
Fix unit tests
OGPoyraz Feb 6, 2025
78a2f9e
Support environment variable for featureflags
OGPoyraz Feb 6, 2025
f49bd0f
Fix lint
OGPoyraz Feb 6, 2025
f8d4829
Fix tests
OGPoyraz Feb 6, 2025
ca575f4
Adjust flat confirmations
OGPoyraz Feb 6, 2025
0b19c44
import sorts
OGPoyraz Feb 6, 2025
8144bd6
Remove unused code
OGPoyraz Feb 6, 2025
e914848
Fix import order
OGPoyraz Feb 6, 2025
a229262
Rename useTransactionMetadataRequest
OGPoyraz Feb 6, 2025
93768cc
Adjust test name
OGPoyraz Feb 6, 2025
4e2844e
Fix suggestions
OGPoyraz Feb 7, 2025
5e89bbb
Add missing snapshot
OGPoyraz Feb 7, 2025
0da244d
Merge branch 'main' into ogp/4078
OGPoyraz Feb 7, 2025
4d4d6c9
Add missing test
OGPoyraz Feb 7, 2025
baf1019
Update test
OGPoyraz Feb 7, 2025
5252b8b
Update app/components/Approvals/TransactionApproval/TransactionApprov…
OGPoyraz Feb 7, 2025
c5c342e
Update app/components/Views/confirmations/hooks/useConfirmationRedesi…
OGPoyraz Feb 7, 2025
f6f8e71
Update TransactionApproval tests
OGPoyraz Feb 7, 2025
afec503
Merge branch 'main' into ogp/4078
OGPoyraz Feb 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Approve from '../../Views/confirmations/ApproveView/Approve';
import QRSigningModal from '../../UI/QRHardware/QRSigningModal';
import withQRHardwareAwareness from '../../UI/QRHardware/withQRHardwareAwareness';
import { IQRState } from '../../UI/QRHardware/types';
import { useConfirmationRedesignEnabled } from '../../Views/confirmations/hooks/useConfirmationRedesignEnabled';

export enum TransactionModalType {
Transaction = 'transaction',
Expand All @@ -24,6 +25,7 @@ export interface TransactionApprovalProps {

const TransactionApprovalInternal = (props: TransactionApprovalProps) => {
const { approvalRequest } = useApprovalRequest();
const { isRedesignedEnabled } = useConfirmationRedesignEnabled();
const [modalVisible, setModalVisible] = useState(false);
const { onComplete: propsOnComplete } = props;

Expand All @@ -32,7 +34,10 @@ const TransactionApprovalInternal = (props: TransactionApprovalProps) => {
propsOnComplete();
}, [propsOnComplete]);

if (approvalRequest?.type !== ApprovalTypes.TRANSACTION && !modalVisible) {
if (
(approvalRequest?.type !== ApprovalTypes.TRANSACTION && !modalVisible) ||
isRedesignedEnabled
) {
return null;
}

Expand Down
13 changes: 9 additions & 4 deletions app/components/Views/confirmations/Confirm/Confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { View, ScrollView } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { TransactionType } from '@metamask/transaction-controller';
import { ApprovalType } from '@metamask/controller-utils';

import { useStyles } from '../../../../component-library/hooks';
import AccountNetworkInfo from '../components/Confirm/AccountNetworkInfo';
Expand All @@ -12,13 +13,13 @@ import SignatureBlockaidBanner from '../components/Confirm/SignatureBlockaidBann
import Title from '../components/Confirm/Title';
import useApprovalRequest from '../hooks/useApprovalRequest';
import { useConfirmationRedesignEnabled } from '../hooks/useConfirmationRedesignEnabled';

import { useTransactionMetadata } from '../hooks/useTransactionMetadata';
import styleSheet from './Confirm.styles';

// todo: if possible derive way to dynamically check if confirmation should be rendered flat
// todo: unit test coverage to be added once we have flat confirmations in place
const FLAT_CONFIRMATIONS: TransactionType[] = [
// To be filled with flat confirmations
const FLAT_CONFIRMATIONS: (TransactionType | ApprovalType)[] = [
TransactionType.stakingDeposit,
];

const ConfirmWrapped = () => (
Expand All @@ -35,6 +36,7 @@ const ConfirmWrapped = () => (

const Confirm = () => {
const { approvalRequest } = useApprovalRequest();
const transactionMetadata = useTransactionMetadata();
const { isRedesignedEnabled } = useConfirmationRedesignEnabled();
const { styles } = useStyles(styleSheet, {});

Expand All @@ -43,7 +45,10 @@ const Confirm = () => {
}

jpuri marked this conversation as resolved.
Show resolved Hide resolved
const isFlatConfirmation = FLAT_CONFIRMATIONS.includes(
approvalRequest?.type as TransactionType,
// order is important here, as transactionMetadata.type is more specific
(transactionMetadata?.type || approvalRequest?.type) as
OGPoyraz marked this conversation as resolved.
Show resolved Hide resolved
| TransactionType
| ApprovalType,
);

if (isFlatConfirmation) {
Expand Down
NicolasMassart marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { TransactionType } from '@metamask/transaction-controller';
import { ApprovalType } from '@metamask/controller-utils';
import React from 'react';

import useApprovalRequest from '../../../hooks/useApprovalRequest';
import { useTransactionMetadata } from '../../../hooks/useTransactionMetadata';
import PersonalSign from './PersonalSign';
import TypedSignV1 from './TypedSignV1';
import TypedSignV3V4 from './TypedSignV3V4';
import StakingDeposit from './StakingDeposit';

type ConfirmationInfoComponentRequest = {

Check failure on line 12 in app/components/Views/confirmations/components/Confirm/Info/Info.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

Use an `interface` instead of a `type`
signatureRequestVersion?: string;
transactionType?: TransactionType;
};

const ConfirmationInfoComponentMap = {
[TransactionType.personalSign]: () => PersonalSign,
[TransactionType.signTypedData]: (approvalRequestVersion: string) => {
if (approvalRequestVersion === 'V1') return TypedSignV1;
[TransactionType.signTypedData]: ({
signatureRequestVersion,
}: ConfirmationInfoComponentRequest) => {
if (signatureRequestVersion === 'V1') return TypedSignV1;
return TypedSignV3V4;
},
[ApprovalType.Transaction]: ({
transactionType,
}: ConfirmationInfoComponentRequest) => {
if (transactionType === TransactionType.stakingDeposit)
return StakingDeposit;
return null;
},
};

const Info = () => {
const { approvalRequest } = useApprovalRequest();
const transactionMetadata = useTransactionMetadata();

if (!approvalRequest?.type) {
return null;
Expand All @@ -24,11 +42,16 @@
const { requestData } = approvalRequest ?? {
requestData: {},
};
const approvalRequestVersion = requestData?.version;
const signatureRequestVersion = requestData?.version;
const transactionType = transactionMetadata?.type;

const InfoComponent: React.FC = ConfirmationInfoComponentMap[
const InfoComponent: React.FC | null = ConfirmationInfoComponentMap[
approvalRequest?.type as keyof typeof ConfirmationInfoComponentMap
](approvalRequestVersion);
]({ signatureRequestVersion, transactionType });

if (!InfoComponent) {
return null;
}
jpuri marked this conversation as resolved.
Show resolved Hide resolved

return <InfoComponent />;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
Matt561 marked this conversation as resolved.
Show resolved Hide resolved
import { Text } from 'react-native';

const StakingDeposit = () => <Text>Staking Deposit</Text>;

export default StakingDeposit;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './StakingDeposit';
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ApprovalType } from '@metamask/controller-utils';
import {
TransactionMeta,
TransactionControllerState,
TransactionType,
} from '@metamask/transaction-controller';

// eslint-disable-next-line import/no-namespace
import * as AddressUtils from '../../../../util/address';
import { renderHookWithProvider } from '../../../../util/test/renderWithProvider';
import { personalSignatureConfirmationState } from '../../../../util/test/confirm-data-helpers';

import { useConfirmationRedesignEnabled } from './useConfirmationRedesignEnabled';

jest.mock('../../../../core/Engine', () => ({
Expand All @@ -20,25 +25,133 @@ jest.mock('../../../../core/Engine', () => ({
},
}));

describe('useConfirmationRedesignEnabled', () => {
it('return true for personal sign request', async () => {
const { result } = renderHookWithProvider(
() => useConfirmationRedesignEnabled(),
{
state: personalSignatureConfirmationState,
const ID_MOCK = '123-456-789';

function renderHook({
approvalType,
transactionMetadata,
overrideRemoteFlags,
}: {
approvalType?: ApprovalType;
transactionMetadata?: Partial<TransactionMeta>;
overrideRemoteFlags?: Record<string, boolean>;
}) {
const { result } = renderHookWithProvider(useConfirmationRedesignEnabled, {
state: {
engine: {
backgroundState: {
ApprovalController: {
pendingApprovals: {
[ID_MOCK]: {
id: ID_MOCK,
type: approvalType ?? ApprovalType.Transaction,
},
},
},
TransactionController: {
transactions: transactionMetadata ? [transactionMetadata] : [],
} as unknown as TransactionControllerState,
RemoteFeatureFlagController: {
remoteFeatureFlags: {
confirmation_redesign: {
signatures: true,
stakingDeposit: true,
...overrideRemoteFlags,
},
},
},
},
jpuri marked this conversation as resolved.
Show resolved Hide resolved
},
);
expect(result?.current.isRedesignedEnabled).toBeTruthy();
},
});

it('return false for external accounts', async () => {
jest.spyOn(AddressUtils, 'isHardwareAccount').mockReturnValue(true);
const { result } = renderHookWithProvider(
() => useConfirmationRedesignEnabled(),
{
state: personalSignatureConfirmationState,
},
);
expect(result?.current.isRedesignedEnabled).toBeFalsy();
return result.current;
}

describe('useConfirmationRedesignEnabled', () => {
describe('signature confirmations', () => {
it('return true for personal sign request', async () => {
const result = renderHook({
approvalType: ApprovalType.PersonalSign,
});

expect(result.isRedesignedEnabled).toBe(true);
});

it('return false for external accounts', async () => {
jest.spyOn(AddressUtils, 'isHardwareAccount').mockReturnValue(true);
const result = renderHook({
approvalType: ApprovalType.PersonalSign,
});

expect(result.isRedesignedEnabled).toBe(false);
});

it('return false when remote flag is disabled', async () => {
const result = renderHook({
approvalType: ApprovalType.PersonalSign,
overrideRemoteFlags: {
signatures: false,
},
});

expect(result.isRedesignedEnabled).toBe(false);
});
});

describe('transaction redesigned confirmations', () => {
describe('staking confirmations', () => {
describe('staking deposit', () => {
it('return true when enabled', async () => {
const result = renderHook({
approvalType: ApprovalType.Transaction,
transactionMetadata: {
id: ID_MOCK,
type: TransactionType.stakingDeposit,
},
});

expect(result.isRedesignedEnabled).toBe(true);
});

it('return false when remote flag is disabled', async () => {
const result = renderHook({
approvalType: ApprovalType.Transaction,
transactionMetadata: {
id: ID_MOCK,
type: TransactionType.stakingDeposit,
},
overrideRemoteFlags: {
stakingDeposit: false,
},
});

expect(result.isRedesignedEnabled).toBe(false);
});

it('return false when transactionMeta is not present', async () => {
const result = renderHook({
approvalType: ApprovalType.Transaction,
overrideRemoteFlags: {
stakingDeposit: false,
},
});

expect(result.isRedesignedEnabled).toBe(false);
});

it('return false when approval type is not transaction', async () => {
const result = renderHook({
approvalType: 'Not transaction' as ApprovalType,
transactionMetadata: {
id: ID_MOCK,
type: TransactionType.stakingDeposit,
},
});

expect(result.isRedesignedEnabled).toBe(false);
});
});
});
});
});
Loading
Loading