Skip to content

Commit cb77f94

Browse files
Ensure that addresses are properly hex-prefixed in the generate ERC token 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 <frederik.bolding@gmail.com>
1 parent 5290402 commit cb77f94

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

ui/ducks/send/send.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ setBackgroundConnection({
8888

8989
describe('Send Slice', () => {
9090
let getTokenStandardAndDetailsStub;
91+
let addUnapprovedTransactionAndRouteToConfirmationPageStub;
9192
beforeEach(() => {
9293
jest.useFakeTimers();
9394
getTokenStandardAndDetailsStub = jest
9495
.spyOn(Actions, 'getTokenStandardAndDetails')
9596
.mockImplementation(() => Promise.resolve({ standard: 'ERC20' }));
97+
addUnapprovedTransactionAndRouteToConfirmationPageStub = jest.spyOn(
98+
Actions,
99+
'addUnapprovedTransactionAndRouteToConfirmationPage',
100+
);
96101
jest
97102
.spyOn(Actions, 'estimateGas')
98103
.mockImplementation(() => Promise.resolve('0x0'));
@@ -1925,6 +1930,60 @@ describe('Send Slice', () => {
19251930
expect(actionResult[1].type).toStrictEqual('SHOW_CONF_TX_PAGE');
19261931
});
19271932

1933+
describe('with token transfers', () => {
1934+
it('should pass the correct transaction parameters to addUnapprovedTransactionAndRouteToConfirmationPage', async () => {
1935+
const tokenTransferTxState = {
1936+
metamask: {
1937+
unapprovedTxs: {
1938+
1: {
1939+
id: 1,
1940+
txParams: {
1941+
value: 'oldTxValue',
1942+
},
1943+
},
1944+
},
1945+
},
1946+
send: {
1947+
...signTransactionState.send,
1948+
stage: SEND_STAGES.DRAFT,
1949+
id: 1,
1950+
account: {
1951+
address: '0x6784e8507A1A46443f7bDc8f8cA39bdA92A675A6',
1952+
},
1953+
asset: {
1954+
details: {
1955+
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
1956+
},
1957+
type: 'TOKEN',
1958+
},
1959+
recipient: {
1960+
address: '4F90e18605Fd46F9F9Fab0e225D88e1ACf5F5324',
1961+
},
1962+
amount: {
1963+
value: '0x1',
1964+
},
1965+
},
1966+
};
1967+
1968+
jest.mock('../../store/actions.js');
1969+
1970+
const store = mockStore(tokenTransferTxState);
1971+
1972+
await store.dispatch(signTransaction());
1973+
1974+
expect(
1975+
addUnapprovedTransactionAndRouteToConfirmationPageStub.mock
1976+
.calls[0][0].data,
1977+
).toStrictEqual(
1978+
'0xa9059cbb0000000000000000000000004f90e18605fd46f9f9fab0e225d88e1acf5f53240000000000000000000000000000000000000000000000000000000000000001',
1979+
);
1980+
expect(
1981+
addUnapprovedTransactionAndRouteToConfirmationPageStub.mock
1982+
.calls[0][0].to,
1983+
).toStrictEqual('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2');
1984+
});
1985+
});
1986+
19281987
it('should create actions for updateTransaction rejecting', async () => {
19291988
const editStageSignTxState = {
19301989
metamask: {

ui/pages/send/send-content/add-recipient/ens-input.component.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import classnames from 'classnames';
44

5+
import { addHexPrefix } from '../../../../../app/scripts/lib/util';
56
import { isValidDomainName } from '../../../../helpers/utils/util';
67
import {
78
isBurnAddress,
@@ -36,14 +37,15 @@ export default class EnsInput extends Component {
3637

3738
onPaste = (event) => {
3839
if (event.clipboardData.items?.length) {
40+
event.preventDefault();
3941
const clipboardItem = event.clipboardData.items[0];
4042
clipboardItem?.getAsString((text) => {
4143
const input = text.trim();
4244
if (
4345
!isBurnAddress(input) &&
4446
isValidHexAddress(input, { mixedCaseUseChecksum: true })
4547
) {
46-
this.props.onPaste(input);
48+
this.props.onPaste(addHexPrefix(input));
4749
}
4850
});
4951
}
@@ -74,7 +76,7 @@ export default class EnsInput extends Component {
7476
!isBurnAddress(input) &&
7577
isValidHexAddress(input, { mixedCaseUseChecksum: true })
7678
) {
77-
onValidAddressTyped(input);
79+
onValidAddressTyped(addHexPrefix(input));
7880
}
7981
}
8082

ui/pages/send/send.utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function generateERC20TransferData({
142142
.call(
143143
abi.rawEncode(
144144
['address', 'uint256'],
145-
[toAddress, addHexPrefix(amount)],
145+
[addHexPrefix(toAddress), addHexPrefix(amount)],
146146
),
147147
(x) => `00${x.toString(16)}`.slice(-2),
148148
)
@@ -164,7 +164,7 @@ function generateERC721TransferData({
164164
.call(
165165
abi.rawEncode(
166166
['address', 'address', 'uint256'],
167-
[fromAddress, toAddress, tokenId],
167+
[addHexPrefix(fromAddress), addHexPrefix(toAddress), tokenId],
168168
),
169169
(x) => `00${x.toString(16)}`.slice(-2),
170170
)

ui/pages/send/send.utils.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('send utils', () => {
7373
expect(rawEncode.mock.calls[0].toString()).toStrictEqual(
7474
[
7575
['address', 'uint256'],
76-
['mockAddress', '0xab'],
76+
['0xmockAddress', '0xab'],
7777
].toString(),
7878
);
7979
});

0 commit comments

Comments
 (0)