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

refactor: accept SignController approval request from frontend #6600

Merged
merged 32 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f755671
feat: use approval accept/reject in ui for signatures
OGPoyraz Jun 14, 2023
4524285
fix tests
OGPoyraz Jun 15, 2023
f48f65a
fix snapshots
OGPoyraz Jun 15, 2023
4076db3
fix e2e tests
OGPoyraz Jun 15, 2023
61658fd
fix unit test
OGPoyraz Jun 16, 2023
3731818
Rename sign callbacks
OGPoyraz Jun 21, 2023
6da631b
Add userRejectedUser as default to rejectPendingApproval
OGPoyraz Jun 21, 2023
36e133d
Temp new sign fns
OGPoyraz Jun 21, 2023
c1fd6a6
Add signError listener to Sign componnets
OGPoyraz Jun 23, 2023
cabea38
rename acceptPendingApproval
OGPoyraz Jun 23, 2023
6347138
fix test
OGPoyraz Jun 23, 2023
a15ad2b
Fix unit tests
OGPoyraz Jun 23, 2023
57b0fef
add parseJsonData option for mobile
OGPoyraz Jun 23, 2023
4ee5cfc
push latest patches
OGPoyraz Jun 29, 2023
76da355
update yarn.lock
OGPoyraz Jun 29, 2023
3eb55b1
fix lint
OGPoyraz Jun 29, 2023
77ecb23
update patch
OGPoyraz Jun 30, 2023
9cf64e0
update lock file
OGPoyraz Jun 30, 2023
adc68a8
update lock file
OGPoyraz Jun 30, 2023
b6c2bd0
update package.json
OGPoyraz Jun 30, 2023
41ff016
passing waitForResult on accept approval
vinistevam Jul 7, 2023
57f46d1
update patch
vinistevam Jul 7, 2023
6c447fe
set wait for result to false
vinistevam Jul 14, 2023
ecded04
reduce code duplication
vinistevam Jul 18, 2023
2dfc784
reduce sonar duplication
vinistevam Jul 18, 2023
97f8dd4
create generateRawSignature function
vinistevam Jul 19, 2023
e0c015f
convert messageSign into typescript
vinistevam Jul 20, 2023
aafb7ea
fix unit test
vinistevam Jul 20, 2023
83fb72f
fix conflicts
vinistevam Jul 24, 2023
e85a121
adapt changes based on the new approvals
vinistevam Jul 24, 2023
92a3f12
fix lint
vinistevam Jul 25, 2023
591b3e2
update yarn.lock
vinistevam Jul 25, 2023
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 @@ -4,9 +4,11 @@ import { ApprovalTypes } from '../../../core/RPCMethods/RPCMethodMiddleware';
import SignatureRequestRoot from '../../UI/SignatureRequest/Root';

const SignatureApproval = () => {
const { approvalRequest } = useApprovalRequest();
const { approvalRequest, onReject, onConfirm } = useApprovalRequest();

const onSign = useCallback(() => undefined, []);
const onSignConfirm = useCallback(() => {
onConfirm({ waitForResult: true });
}, [onConfirm]);

const messageParams =
approvalRequest &&
Expand All @@ -22,7 +24,8 @@ const SignatureApproval = () => {
<SignatureRequestRoot
messageParams={messageParams}
approvalType={approvalRequest?.type}
onSign={onSign}
onSignConfirm={onSignConfirm}
onSignReject={onReject}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`SignatureApproval populates message params if approval type is eth_sign
"test": "value",
}
}
onSign={[Function]}
onSignConfirm={[Function]}
/>
`;

Expand All @@ -20,7 +20,7 @@ exports[`SignatureApproval populates message params if approval type is eth_sign
"test": "value",
}
}
onSign={[Function]}
onSignConfirm={[Function]}
/>
`;

Expand All @@ -32,19 +32,19 @@ exports[`SignatureApproval populates message params if approval type is personal
"test": "value",
}
}
onSign={[Function]}
onSignConfirm={[Function]}
/>
`;

exports[`SignatureApproval provides no message params if incorrect approval request type 1`] = `
<Root
approvalType="ADD_ETHEREUM_CHAIN"
onSign={[Function]}
onSignConfirm={[Function]}
/>
`;

exports[`SignatureApproval provides no message params if no approval request 1`] = `
<Root
onSign={[Function]}
onSignConfirm={[Function]}
/>
`;
198 changes: 198 additions & 0 deletions app/components/UI/MessageSign/MessageSign.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import React, { PureComponent } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { fontStyles } from '../../../styles/common';
import SignatureRequest from '../SignatureRequest';
import ExpandedMessage from '../SignatureRequest/ExpandedMessage';
import { KEYSTONE_TX_CANCELED } from '../../../constants/error';
import { MetaMetricsEvents } from '../../../core/Analytics';
import AnalyticsV2 from '../../../util/analyticsV2';
import { ThemeContext, mockTheme } from '../../../util/theme';
import {
addSignatureErrorListener,
getAnalyticsParams,
handleSignatureAction,
removeSignatureErrorListener,
} from '../../../util/confirmation/signatureUtils';
import { MessageParams, PageMeta } from '../SignatureRequest/types';
import { Colors } from '../../../util/theme/models';

interface MessageSignProps {
/**
* react-navigation object used for switching between screens
*/
navigation: any;
/**
* Callback triggered when this message signature is rejected
*/
onReject: () => void;
/**
* Callback triggered when this message signature is approved
*/
onConfirm: () => void;
/**
* Message to be displayed to the user
*/
messageParams: MessageParams;
/**
* Object containing current page title and url
*/
currentPageInformation: PageMeta;
/**
* Hides or shows the expanded signing message
*/
toggleExpandedMessage: () => void;
/**
* Indicated whether or not the expanded message is shown
*/
showExpandedMessage: boolean;
}

interface MessageSignState {
truncateMessage: boolean;
}

const createStyles = (colors: Colors) =>
StyleSheet.create({
expandedMessage: {
textAlign: 'center',
...fontStyles.normal,
fontSize: 14,
color: colors.text.default,
},
messageText: {
color: colors.text.default,
},
messageWrapper: {
marginBottom: 4,
},
});

/**
* Component that supports eth_sign
*/
class MessageSign extends PureComponent<MessageSignProps, MessageSignState> {
static contextType = ThemeContext;

state: MessageSignState = {
truncateMessage: false,
};

componentDidMount = () => {
const {
messageParams: { metamaskId },
} = this.props;
AnalyticsV2.trackEvent(
MetaMetricsEvents.SIGN_REQUEST_STARTED,
getAnalyticsParams(),
);
addSignatureErrorListener(metamaskId, this.onSignatureError);
};

componentWillUnmount = () => {
const {
messageParams: { metamaskId },
} = this.props;
removeSignatureErrorListener(metamaskId, this.onSignatureError);
};

onSignatureError = ({ error }: any) => {
if (error?.message.startsWith(KEYSTONE_TX_CANCELED)) {
AnalyticsV2.trackEvent(
MetaMetricsEvents.QR_HARDWARE_TRANSACTION_CANCELED,
getAnalyticsParams(),
);
}
};

rejectSignature = async () => {
const { messageParams, onReject } = this.props;
await handleSignatureAction(onReject, messageParams, 'eth', false);
};

confirmSignature = async () => {
const { messageParams, onConfirm } = this.props;
await handleSignatureAction(onConfirm, messageParams, 'eth', true);
};

getStyles = () => {
const colors = this.context.colors || mockTheme.colors;
return createStyles(colors);
};

renderMessageText = () => {
const { messageParams, showExpandedMessage } = this.props;
const { truncateMessage } = this.state;
const styles = this.getStyles();

let messageText;
if (showExpandedMessage) {
messageText = (
<Text style={styles.expandedMessage}>{messageParams.data}</Text>
);
} else {
messageText = truncateMessage ? (
<Text
style={styles.messageText}
numberOfLines={5}
ellipsizeMode={'tail'}
>
{messageParams.data}
</Text>
) : (
<Text
style={styles.messageText}
onTextLayout={this.shouldTruncateMessage}
>
{messageParams.data}
</Text>
);
}
return messageText;
};

shouldTruncateMessage = (e: any) => {
if (e.nativeEvent.lines.length > 5) {
this.setState({ truncateMessage: true });
return;
}
this.setState({ truncateMessage: false });
};

render() {
const {
currentPageInformation,
navigation,
showExpandedMessage,
toggleExpandedMessage,
messageParams: { from },
} = this.props;
const styles = this.getStyles();

const rootView = showExpandedMessage ? (
<ExpandedMessage
currentPageInformation={currentPageInformation}
renderMessage={this.renderMessageText}
toggleExpandedMessage={toggleExpandedMessage}
/>
) : (
<SignatureRequest
navigation={navigation}
onReject={this.rejectSignature}
onConfirm={this.confirmSignature}
currentPageInformation={currentPageInformation}
truncateMessage={this.state.truncateMessage}
showExpandedMessage={showExpandedMessage}
toggleExpandedMessage={toggleExpandedMessage}
type="ethSign"
showWarning
fromAddress={from}
testID={'eth-signature-request'}
>
<View style={styles.messageWrapper}>{this.renderMessageText()}</View>
</SignatureRequest>
);
return rootView;
}
}

export default MessageSign;
12 changes: 10 additions & 2 deletions app/components/UI/MessageSign/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ exports[`MessageSign should render correctly 1`] = `
messageParams={
Object {
"data": "message",
"from": "0x0",
"metamaskId": "TestMessageId",
"origin": "example.com",
}
}
onCancel={[Function]}
onConfirm={[Function]}
navigation={
Object {
"navigate": [MockFunction],
}
}
onConfirm={[MockFunction]}
onReject={[MockFunction]}
showExpandedMessage={false}
toggleExpandedMessage={[MockFunction]}
/>
`;
Loading
Loading