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

Feature: Warn when attempting to send tx with data to non-contract #5283

Merged
merged 4 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Update transaction statuses when switching networks.
- [#5470](https://github.com/MetaMask/metamask-extension/pull/5470) 100% coverage in French locale, fixed the procedure to verify proposed locale.
- [#5283](https://github.com/MetaMask/metamask-extension/pull/5283): Fix bug when eth.getCode() called with no contract

## 4.12.0 Thursday September 27 2018

Expand Down
3 changes: 3 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,9 @@
"transactionError": {
"message": "Transaction Error. Exception thrown in contract code."
},
"transactionErrorNoContract": {
"message": "Trying to call a contract function at an address that is not a contract address."
},
"transactionMemo": {
"message": "Transaction memo (optional)"
},
Expand Down
34 changes: 25 additions & 9 deletions app/scripts/controllers/transactions/tx-gas-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
const { addHexPrefix } = require('ethereumjs-util')
const SIMPLE_GAS_COST = '0x5208' // Hex for 21000, cost of a simple send.

import { TRANSACTION_NO_CONTRACT_ERROR_KEY } from '../../../../ui/app/constants/error-keys'

/**
tx-gas-utils are gas utility methods for Transaction manager
its passed ethquery
Expand All @@ -32,6 +34,7 @@ class TxGasUtil {
} catch (err) {
txMeta.simulationFails = {
reason: err.message,
errorKey: err.errorKey,
}
return txMeta
}
Expand All @@ -56,24 +59,37 @@ class TxGasUtil {
return txParams.gas
}

// if recipient has no code, gas is 21k max:
const recipient = txParams.to
const hasRecipient = Boolean(recipient)
let code
if (recipient) code = await this.query.getCode(recipient)

if (hasRecipient && (!code || code === '0x')) {
txParams.gas = SIMPLE_GAS_COST
txMeta.simpleSend = true // Prevents buffer addition
return SIMPLE_GAS_COST
// see if we can set the gas based on the recipient
if (hasRecipient) {
const code = await this.query.getCode(recipient)
// For an address with no code, geth will return '0x', and ganache-core v2.2.1 will return '0x0'
const codeIsEmpty = !code || code === '0x' || code === '0x0'

if (codeIsEmpty) {
// if there's data in the params, but there's no contract code, it's not a valid transaction
if (txParams.data) {
const err = new Error()
err.errorKey = TRANSACTION_NO_CONTRACT_ERROR_KEY
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with this style of instantiating errors - is this correct? How does the error message get set?

is it appropriate to throw errors in this estimateTxGas function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The errorKey is converted to 'transactionErrorNoContract' in ui\app\constants\error-keys.js, and then "Trying to call a contract function at an address that is not a contract address." in app\_locales\en\messages.json.

throw err
}

// This is a standard ether simple send, gas requirement is exactly 21k
txParams.gas = SIMPLE_GAS_COST
// prevents buffer addition
txMeta.simpleSend = true
return SIMPLE_GAS_COST
}
}

// if not, fall back to block gasLimit
// fallback to block gasLimit
const blockGasLimitBN = hexToBn(blockGasLimitHex)
const saferGasLimitBN = BnMultiplyByFraction(blockGasLimitBN, 19, 20)
txParams.gas = bnToHex(saferGasLimitBN)

// run tx
// estimate tx gas requirements
return await this.query.estimateGas(txParams)
}

Expand Down
2 changes: 1 addition & 1 deletion old-ui/app/components/pending-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ PendingTx.prototype.verifyGasParams = function () {
}

PendingTx.prototype._notZeroOrEmptyString = function (obj) {
return obj !== '' && obj !== '0x0'
return obj !== '' && obj !== '0x0' && obj !== '0x' // '0x' means null
}

PendingTx.prototype.bnMultiplyByFraction = function (targetBN, numerator, denominator) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class ConfirmTransactionBase extends Component {
if (simulationFails) {
return {
valid: true,
errorKey: TRANSACTION_ERROR_KEY,
errorKey: simulationFails.errorKey ? simulationFails.errorKey : TRANSACTION_ERROR_KEY,
}
}

Expand Down
2 changes: 1 addition & 1 deletion ui/app/components/send/send.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async function estimateGas ({
// if recipient has no code, gas is 21k max:
if (!selectedToken && !data) {
const code = Boolean(to) && await global.eth.getCode(to)
if (!code || code === '0x') {
if (!code || code === '0x' || code === '0x0') { // Infura will return '0x', and ganache-core v2.2.1 will return '0x0'
return SIMPLE_GAS_COST
}
} else if (selectedToken && !to) {
Expand Down
1 change: 1 addition & 0 deletions ui/app/constants/error-keys.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const INSUFFICIENT_FUNDS_ERROR_KEY = 'insufficientFunds'
export const GAS_LIMIT_TOO_LOW_ERROR_KEY = 'gasLimitTooLow'
export const TRANSACTION_ERROR_KEY = 'transactionError'
export const TRANSACTION_NO_CONTRACT_ERROR_KEY = 'transactionErrorNoContract'
2 changes: 1 addition & 1 deletion ui/app/helpers/transactions.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function getLatestSubmittedTxWithNonce (transactions = [], nonce = '0x0')

export async function isSmartContractAddress (address) {
const code = await global.eth.getCode(address)
return code && code !== '0x'
return code && code !== '0x' && code !== '0x0' // Infura will return '0x', and ganache-core v2.2.1 will return '0x0'
}

export function sumHexes (...args) {
Expand Down