mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Merge pull request #3767 from MetaMask/tx-state-undefined-value
tx controller - stricter validation and other improvements
This commit is contained in:
commit
5a9aa153ec
@ -250,7 +250,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
// wait for a nonce
|
// wait for a nonce
|
||||||
nonceLock = await this.nonceTracker.getNonceLock(fromAddress)
|
nonceLock = await this.nonceTracker.getNonceLock(fromAddress)
|
||||||
// add nonce to txParams
|
// add nonce to txParams
|
||||||
// if txMeta has lastGasPrice then it is a retry at same nonce with higher
|
// if txMeta has lastGasPrice then it is a retry at same nonce with higher
|
||||||
// gas price transaction and their for the nonce should not be calculated
|
// gas price transaction and their for the nonce should not be calculated
|
||||||
const nonce = txMeta.lastGasPrice ? txMeta.txParams.nonce : nonceLock.nextNonce
|
const nonce = txMeta.lastGasPrice ? txMeta.txParams.nonce : nonceLock.nextNonce
|
||||||
txMeta.txParams.nonce = ethUtil.addHexPrefix(nonce.toString(16))
|
txMeta.txParams.nonce = ethUtil.addHexPrefix(nonce.toString(16))
|
||||||
@ -273,12 +273,14 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
|
|
||||||
async signTransaction (txId) {
|
async signTransaction (txId) {
|
||||||
const txMeta = this.txStateManager.getTx(txId)
|
const txMeta = this.txStateManager.getTx(txId)
|
||||||
const txParams = txMeta.txParams
|
|
||||||
const fromAddress = txParams.from
|
|
||||||
// add network/chain id
|
// add network/chain id
|
||||||
txParams.chainId = ethUtil.addHexPrefix(this.getChainId().toString(16))
|
const chainId = this.getChainId()
|
||||||
|
const txParams = Object.assign({}, txMeta.txParams, { chainId })
|
||||||
|
// sign tx
|
||||||
|
const fromAddress = txParams.from
|
||||||
const ethTx = new Transaction(txParams)
|
const ethTx = new Transaction(txParams)
|
||||||
await this.signEthTx(ethTx, fromAddress)
|
await this.signEthTx(ethTx, fromAddress)
|
||||||
|
// set state to signed
|
||||||
this.txStateManager.setTxStatusSigned(txMeta.id)
|
this.txStateManager.setTxStatusSigned(txMeta.id)
|
||||||
const rawTx = ethUtil.bufferToHex(ethTx.serialize())
|
const rawTx = ethUtil.bufferToHex(ethTx.serialize())
|
||||||
return rawTx
|
return rawTx
|
||||||
|
@ -106,12 +106,9 @@ module.exports = class TransactionStateManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateTx (txMeta, note) {
|
updateTx (txMeta, note) {
|
||||||
|
// validate txParams
|
||||||
if (txMeta.txParams) {
|
if (txMeta.txParams) {
|
||||||
Object.keys(txMeta.txParams).forEach((key) => {
|
this.validateTxParams(txMeta.txParams)
|
||||||
const value = txMeta.txParams[key]
|
|
||||||
if (typeof value !== 'string') console.error(`${key}: ${value} in txParams is not a string`)
|
|
||||||
if (!ethUtil.isHexPrefixed(value)) console.error('is not hex prefixed, anything on txParams must be hex prefixed')
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create txMeta snapshot for history
|
// create txMeta snapshot for history
|
||||||
@ -139,6 +136,15 @@ module.exports = class TransactionStateManager extends EventEmitter {
|
|||||||
this.updateTx(txMeta, `txStateManager#updateTxParams`)
|
this.updateTx(txMeta, `txStateManager#updateTxParams`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validates txParams members by type
|
||||||
|
validateTxParams(txParams) {
|
||||||
|
Object.keys(txParams).forEach((key) => {
|
||||||
|
const value = txParams[key]
|
||||||
|
if (typeof value !== 'string') throw new Error(`${key}: ${value} in txParams is not a string`)
|
||||||
|
if (!ethUtil.isHexPrefixed(value)) throw new Error('is not hex prefixed, everything on txParams must be hex prefixed')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Takes an object of fields to search for eg:
|
Takes an object of fields to search for eg:
|
||||||
let thingsToLookFor = {
|
let thingsToLookFor = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user