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

Prevent user from editing a contract interaction created by a dapp #16498

Merged
merged 6 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
107 changes: 107 additions & 0 deletions test/e2e/tests/dapp-tx-edit.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const { strict: assert } = require('assert');
const { convertToHexValue, withFixtures } = require('../helpers');
const { SMART_CONTRACTS } = require('../seeder/smart-contracts');
const FixtureBuilder = require('../fixture-builder');

describe('Editing confirmations of dapp initiated contract interactions', function () {
const ganacheOptions = {
accounts: [
{
secretKey:
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC',
balance: convertToHexValue(25000000000000000000),
},
],
};
const smartContract = SMART_CONTRACTS.PIGGYBANK;
it('should NOT show an edit button on a contract interaction confirmation iniated by a dapp', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
smartContract,
title: this.test.title,
},
async ({ driver, contractRegistry }) => {
const contractAddress = await contractRegistry.getContractAddress(
smartContract,
);
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);

// deploy contract
await driver.openNewPage(
`http://127.0.0.1:8080/?contract=${contractAddress}`,
);

// wait for deployed contract, calls and confirms a contract method where ETH is sent
await driver.findClickableElement('#deployButton');
await driver.clickElement('#depositButton');
await driver.waitUntilXWindowHandles(3);
const windowHandles = await driver.getAllWindowHandles();

await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
await driver.waitForSelector({
css: '.confirm-page-container-summary__action__name',
text: 'Deposit',
});
const editTransactionButton = await driver.isElementPresentAndVisible(
'[data-testid="confirm-page-back-edit-button"]',
);
assert.equal(
editTransactionButton,
false,
`Edit transaction button should not be visible on a contract interaction created by a dapp`,
);
},
);
});

it('should show an edit button on a simple ETH send iniated by a dapp', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.build(),
ganacheOptions,
smartContract,
title: this.test.title,
},
async ({ driver }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);

await driver.openNewPage(`http://127.0.0.1:8080/`);
await driver.clickElement('#sendButton');
await driver.waitUntilXWindowHandles(3);
const windowHandles = await driver.getAllWindowHandles();

await driver.switchToWindowWithTitle(
'MetaMask Notification',
windowHandles,
);
await driver.waitForSelector({
css: '.confirm-page-container-summary__action__name',
text: 'Sending ETH',
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
});
const editTransactionButton = await driver.isElementPresentAndVisible(
'[data-testid="confirm-page-back-edit-button"]',
);
assert.equal(
editTransactionButton,
true,
`Edit transaction button should be visible on a contract interaction created by a dapp`,
);
},
);
});
});
9 changes: 9 additions & 0 deletions test/e2e/webdriver/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ class Driver {
}
}

async isElementPresentAndVisible(rawLocator) {
try {
await this.findVisibleElement(rawLocator);
return true;
} catch (err) {
return false;
}
}

/**
* Paste a string into a field.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export default function ConfirmPageContainerHeader({
>
<IconCaretLeft />
<span
data-testid="confirm-page-back-edit-button"
className="confirm-page-container-header__back-button"
onClick={() => onEdit()}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,11 +1157,11 @@ export default class ConfirmTransactionBase extends Component {
requestsWaitingText,
} = this.getNavigateTxData();

let functionType;
if (
const isContractInteractionFromDapp =
txData.type === TRANSACTION_TYPES.CONTRACT_INTERACTION &&
txData.origin !== 'metamask'
) {
txData.origin !== 'metamask';
let functionType;
if (isContractInteractionFromDapp) {
functionType = getMethodName(name);
}

Expand All @@ -1183,7 +1183,7 @@ export default class ConfirmTransactionBase extends Component {
toAddress={toAddress}
toEns={toEns}
toNickname={toNickname}
showEdit={Boolean(onEdit)}
showEdit={!isContractInteractionFromDapp && Boolean(onEdit)}
action={functionType}
title={title}
image={image}
Expand Down