Skip to content

Commit

Permalink
Ensure that addresses are properly hex-prefixed in the generate ERC t…
Browse files Browse the repository at this point in the history
…oken transfer functions (#15064)

* Ensure that addresses are properly hex-prefixed in the generate ERC token transfer functions

* Ensure hex-prefixed addresses in send input

* Update unit tests

Co-authored-by: Frederik Bolding <[email protected]>
  • Loading branch information
danjm and FrederikBolding authored Jun 28, 2022
1 parent 5290402 commit cb77f94
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
59 changes: 59 additions & 0 deletions ui/ducks/send/send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ setBackgroundConnection({

describe('Send Slice', () => {
let getTokenStandardAndDetailsStub;
let addUnapprovedTransactionAndRouteToConfirmationPageStub;
beforeEach(() => {
jest.useFakeTimers();
getTokenStandardAndDetailsStub = jest
.spyOn(Actions, 'getTokenStandardAndDetails')
.mockImplementation(() => Promise.resolve({ standard: 'ERC20' }));
addUnapprovedTransactionAndRouteToConfirmationPageStub = jest.spyOn(
Actions,
'addUnapprovedTransactionAndRouteToConfirmationPage',
);
jest
.spyOn(Actions, 'estimateGas')
.mockImplementation(() => Promise.resolve('0x0'));
Expand Down Expand Up @@ -1925,6 +1930,60 @@ describe('Send Slice', () => {
expect(actionResult[1].type).toStrictEqual('SHOW_CONF_TX_PAGE');
});

describe('with token transfers', () => {
it('should pass the correct transaction parameters to addUnapprovedTransactionAndRouteToConfirmationPage', async () => {
const tokenTransferTxState = {
metamask: {
unapprovedTxs: {
1: {
id: 1,
txParams: {
value: 'oldTxValue',
},
},
},
},
send: {
...signTransactionState.send,
stage: SEND_STAGES.DRAFT,
id: 1,
account: {
address: '0x6784e8507A1A46443f7bDc8f8cA39bdA92A675A6',
},
asset: {
details: {
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
},
type: 'TOKEN',
},
recipient: {
address: '4F90e18605Fd46F9F9Fab0e225D88e1ACf5F5324',
},
amount: {
value: '0x1',
},
},
};

jest.mock('../../store/actions.js');

const store = mockStore(tokenTransferTxState);

await store.dispatch(signTransaction());

expect(
addUnapprovedTransactionAndRouteToConfirmationPageStub.mock
.calls[0][0].data,
).toStrictEqual(
'0xa9059cbb0000000000000000000000004f90e18605fd46f9f9fab0e225d88e1acf5f53240000000000000000000000000000000000000000000000000000000000000001',
);
expect(
addUnapprovedTransactionAndRouteToConfirmationPageStub.mock
.calls[0][0].to,
).toStrictEqual('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2');
});
});

it('should create actions for updateTransaction rejecting', async () => {
const editStageSignTxState = {
metamask: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';

import { addHexPrefix } from '../../../../../app/scripts/lib/util';
import { isValidDomainName } from '../../../../helpers/utils/util';
import {
isBurnAddress,
Expand Down Expand Up @@ -36,14 +37,15 @@ export default class EnsInput extends Component {

onPaste = (event) => {
if (event.clipboardData.items?.length) {
event.preventDefault();
const clipboardItem = event.clipboardData.items[0];
clipboardItem?.getAsString((text) => {
const input = text.trim();
if (
!isBurnAddress(input) &&
isValidHexAddress(input, { mixedCaseUseChecksum: true })
) {
this.props.onPaste(input);
this.props.onPaste(addHexPrefix(input));
}
});
}
Expand Down Expand Up @@ -74,7 +76,7 @@ export default class EnsInput extends Component {
!isBurnAddress(input) &&
isValidHexAddress(input, { mixedCaseUseChecksum: true })
) {
onValidAddressTyped(input);
onValidAddressTyped(addHexPrefix(input));
}
}

Expand Down
4 changes: 2 additions & 2 deletions ui/pages/send/send.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function generateERC20TransferData({
.call(
abi.rawEncode(
['address', 'uint256'],
[toAddress, addHexPrefix(amount)],
[addHexPrefix(toAddress), addHexPrefix(amount)],
),
(x) => `00${x.toString(16)}`.slice(-2),
)
Expand All @@ -164,7 +164,7 @@ function generateERC721TransferData({
.call(
abi.rawEncode(
['address', 'address', 'uint256'],
[fromAddress, toAddress, tokenId],
[addHexPrefix(fromAddress), addHexPrefix(toAddress), tokenId],
),
(x) => `00${x.toString(16)}`.slice(-2),
)
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/send/send.utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('send utils', () => {
expect(rawEncode.mock.calls[0].toString()).toStrictEqual(
[
['address', 'uint256'],
['mockAddress', '0xab'],
['0xmockAddress', '0xab'],
].toString(),
);
});
Expand Down

0 comments on commit cb77f94

Please sign in to comment.