2016-12-14 21:55:41 +01:00
|
|
|
const EventEmitter = require('events')
|
2017-02-03 05:20:13 +01:00
|
|
|
const ObservableStore = require('obs-store')
|
2016-12-16 19:33:36 +01:00
|
|
|
const ethUtil = require('ethereumjs-util')
|
2017-09-06 06:50:36 +02:00
|
|
|
const Transaction = require('ethereumjs-tx')
|
2017-08-02 17:47:13 +02:00
|
|
|
const EthQuery = require('ethjs-query')
|
2018-03-14 15:15:04 +01:00
|
|
|
const TransactionStateManager = require('../lib/tx-state-manager')
|
2017-09-06 06:50:36 +02:00
|
|
|
const TxGasUtil = require('../lib/tx-gas-utils')
|
2017-08-09 00:30:49 +02:00
|
|
|
const PendingTransactionTracker = require('../lib/pending-tx-tracker')
|
2017-06-15 08:44:02 +02:00
|
|
|
const NonceTracker = require('../lib/nonce-tracker')
|
2018-04-12 23:06:59 +02:00
|
|
|
const log = require('loglevel')
|
2016-12-14 21:55:41 +01:00
|
|
|
|
2017-09-06 06:50:36 +02:00
|
|
|
/*
|
|
|
|
Transaction Controller is an aggregate of sub-controllers and trackers
|
|
|
|
composing them in a way to be exposed to the metamask controller
|
|
|
|
- txStateManager
|
|
|
|
responsible for the state of a transaction and
|
|
|
|
storing the transaction
|
|
|
|
- pendingTxTracker
|
|
|
|
watching blocks for transactions to be include
|
|
|
|
and emitting confirmed events
|
|
|
|
- txGasUtil
|
|
|
|
gas calculations and safety buffering
|
|
|
|
- nonceTracker
|
|
|
|
calculating nonces
|
|
|
|
*/
|
2016-12-14 21:55:41 +01:00
|
|
|
|
2017-05-23 23:49:10 +02:00
|
|
|
module.exports = class TransactionController extends EventEmitter {
|
2016-12-14 21:55:41 +01:00
|
|
|
constructor (opts) {
|
|
|
|
super()
|
2017-02-03 05:59:47 +01:00
|
|
|
this.networkStore = opts.networkStore || new ObservableStore({})
|
2017-02-03 06:09:17 +01:00
|
|
|
this.preferencesStore = opts.preferencesStore || new ObservableStore({})
|
2017-05-23 07:56:10 +02:00
|
|
|
this.provider = opts.provider
|
|
|
|
this.blockTracker = opts.blockTracker
|
2017-08-04 20:41:35 +02:00
|
|
|
this.signEthTx = opts.signTransaction
|
2018-01-06 06:24:20 +01:00
|
|
|
this.getGasPrice = opts.getGasPrice
|
2017-08-04 20:41:35 +02:00
|
|
|
|
2017-08-11 23:19:35 +02:00
|
|
|
this.memStore = new ObservableStore({})
|
|
|
|
this.query = new EthQuery(this.provider)
|
2017-09-06 06:50:36 +02:00
|
|
|
this.txGasUtil = new TxGasUtil(this.provider)
|
2017-08-11 23:19:35 +02:00
|
|
|
|
2018-03-14 15:15:04 +01:00
|
|
|
this.txStateManager = new TransactionStateManager({
|
2017-09-12 18:59:59 +02:00
|
|
|
initState: opts.initState,
|
2017-08-11 23:19:35 +02:00
|
|
|
txHistoryLimit: opts.txHistoryLimit,
|
|
|
|
getNetwork: this.getNetwork.bind(this),
|
2017-08-18 21:23:35 +02:00
|
|
|
})
|
2018-01-18 00:42:01 +01:00
|
|
|
|
|
|
|
this.txStateManager.getFilteredTxList({
|
|
|
|
status: 'unapproved',
|
|
|
|
loadingDefaults: true,
|
|
|
|
}).forEach((tx) => {
|
|
|
|
this.addTxDefaults(tx)
|
|
|
|
.then((txMeta) => {
|
|
|
|
txMeta.loadingDefaults = false
|
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions: gas estimation for tx on boot')
|
2018-01-18 00:48:37 +01:00
|
|
|
}).catch((error) => {
|
|
|
|
this.txStateManager.setTxStatusFailed(tx.id, error)
|
2018-01-18 00:42:01 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-01-18 01:49:38 +01:00
|
|
|
this.txStateManager.getFilteredTxList({
|
|
|
|
status: 'approved',
|
|
|
|
}).forEach((txMeta) => {
|
2018-01-18 02:56:31 +01:00
|
|
|
const txSignError = new Error('Transaction found as "approved" during boot - possibly stuck during signing')
|
2018-01-18 01:49:38 +01:00
|
|
|
this.txStateManager.setTxStatusFailed(txMeta.id, txSignError)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-09-27 01:52:08 +02:00
|
|
|
this.store = this.txStateManager.store
|
|
|
|
this.txStateManager.on('tx:status-update', this.emit.bind(this, 'tx:status-update'))
|
2017-06-15 08:44:02 +02:00
|
|
|
this.nonceTracker = new NonceTracker({
|
|
|
|
provider: this.provider,
|
2017-09-08 23:24:40 +02:00
|
|
|
getPendingTransactions: this.txStateManager.getPendingTransactions.bind(this.txStateManager),
|
2017-08-18 22:54:16 +02:00
|
|
|
getConfirmedTransactions: (address) => {
|
2017-09-06 23:01:07 +02:00
|
|
|
return this.txStateManager.getFilteredTxList({
|
2017-08-18 22:54:16 +02:00
|
|
|
from: address,
|
|
|
|
status: 'confirmed',
|
|
|
|
err: undefined,
|
|
|
|
})
|
|
|
|
},
|
2017-06-15 08:44:02 +02:00
|
|
|
})
|
2017-08-04 20:41:35 +02:00
|
|
|
|
2017-08-09 00:30:49 +02:00
|
|
|
this.pendingTxTracker = new PendingTransactionTracker({
|
2017-08-04 20:41:35 +02:00
|
|
|
provider: this.provider,
|
|
|
|
nonceTracker: this.nonceTracker,
|
2017-09-12 21:19:26 +02:00
|
|
|
publishTransaction: (rawTx) => this.query.sendRawTransaction(rawTx),
|
2017-09-08 23:24:40 +02:00
|
|
|
getPendingTransactions: this.txStateManager.getPendingTransactions.bind(this.txStateManager),
|
2017-10-06 21:50:33 +02:00
|
|
|
getCompletedTransactions: this.txStateManager.getConfirmedTransactions.bind(this.txStateManager),
|
2017-08-04 20:41:35 +02:00
|
|
|
})
|
|
|
|
|
2017-09-27 01:52:08 +02:00
|
|
|
this.txStateManager.store.subscribe(() => this.emit('update:badge'))
|
2017-09-06 06:50:36 +02:00
|
|
|
|
2017-10-02 22:41:29 +02:00
|
|
|
this.pendingTxTracker.on('tx:warning', (txMeta) => {
|
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:warning')
|
|
|
|
})
|
2018-03-08 07:01:14 +01:00
|
|
|
this.pendingTxTracker.on('tx:confirmed', (txId) => this._markNonceDuplicatesDropped(txId))
|
2017-09-27 01:55:11 +02:00
|
|
|
this.pendingTxTracker.on('tx:failed', this.txStateManager.setTxStatusFailed.bind(this.txStateManager))
|
2017-12-05 21:51:14 +01:00
|
|
|
this.pendingTxTracker.on('tx:block-update', (txMeta, latestBlockNumber) => {
|
|
|
|
if (!txMeta.firstRetryBlockNumber) {
|
|
|
|
txMeta.firstRetryBlockNumber = latestBlockNumber
|
2017-12-06 17:37:31 +01:00
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:block-update')
|
2017-12-05 21:51:14 +01:00
|
|
|
}
|
|
|
|
})
|
2017-09-27 07:42:59 +02:00
|
|
|
this.pendingTxTracker.on('tx:retry', (txMeta) => {
|
|
|
|
if (!('retryCount' in txMeta)) txMeta.retryCount = 0
|
|
|
|
txMeta.retryCount++
|
2017-10-02 22:41:29 +02:00
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:retry')
|
2017-09-27 07:42:59 +02:00
|
|
|
})
|
2017-08-04 20:41:35 +02:00
|
|
|
|
2017-09-26 22:37:13 +02:00
|
|
|
this.blockTracker.on('block', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker))
|
2017-07-07 12:05:39 +02:00
|
|
|
// this is a little messy but until ethstore has been either
|
|
|
|
// removed or redone this is to guard against the race condition
|
2017-09-29 21:54:05 +02:00
|
|
|
this.blockTracker.on('latest', this.pendingTxTracker.resubmitPendingTxs.bind(this.pendingTxTracker))
|
2017-08-09 00:30:49 +02:00
|
|
|
this.blockTracker.on('sync', this.pendingTxTracker.queryPendingTxs.bind(this.pendingTxTracker))
|
2017-02-03 06:09:17 +01:00
|
|
|
// memstore is computed from a few different stores
|
2017-02-03 05:20:13 +01:00
|
|
|
this._updateMemstore()
|
2017-09-12 18:59:59 +02:00
|
|
|
this.txStateManager.store.subscribe(() => this._updateMemstore())
|
2017-04-27 06:05:45 +02:00
|
|
|
this.networkStore.subscribe(() => this._updateMemstore())
|
|
|
|
this.preferencesStore.subscribe(() => this._updateMemstore())
|
2016-12-16 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getState () {
|
2017-02-03 05:20:13 +01:00
|
|
|
return this.memStore.getState()
|
2016-12-14 21:55:41 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 05:59:47 +01:00
|
|
|
getNetwork () {
|
2017-05-23 08:12:28 +02:00
|
|
|
return this.networkStore.getState()
|
2017-02-03 05:59:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-03 06:09:17 +01:00
|
|
|
getSelectedAddress () {
|
|
|
|
return this.preferencesStore.getState().selectedAddress
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:58:05 +02:00
|
|
|
getUnapprovedTxCount () {
|
2017-08-18 21:23:35 +02:00
|
|
|
return Object.keys(this.txStateManager.getUnapprovedTxList()).length
|
2017-07-26 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-08 23:24:40 +02:00
|
|
|
getPendingTxCount (account) {
|
|
|
|
return this.txStateManager.getPendingTransactions(account).length
|
2017-07-26 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 03:11:51 +02:00
|
|
|
getFilteredTxList (opts) {
|
|
|
|
return this.txStateManager.getFilteredTxList(opts)
|
2017-07-26 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 06:50:36 +02:00
|
|
|
getChainId () {
|
|
|
|
const networkState = this.networkStore.getState()
|
|
|
|
const getChainId = parseInt(networkState)
|
|
|
|
if (Number.isNaN(getChainId)) {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return getChainId
|
|
|
|
}
|
2017-07-26 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 10:25:32 +01:00
|
|
|
wipeTransactions (address) {
|
|
|
|
this.txStateManager.wipeTransactions(address)
|
2018-01-31 09:33:15 +01:00
|
|
|
}
|
|
|
|
|
2016-12-14 21:55:41 +01:00
|
|
|
// Adds a tx to the txlist
|
2017-01-13 11:00:11 +01:00
|
|
|
addTx (txMeta) {
|
2017-08-11 23:19:35 +02:00
|
|
|
this.txStateManager.addTx(txMeta)
|
2016-12-16 19:33:36 +01:00
|
|
|
this.emit(`${txMeta.id}:unapproved`, txMeta)
|
2016-12-14 21:55:41 +01:00
|
|
|
}
|
|
|
|
|
2018-03-31 01:00:11 +02:00
|
|
|
async newUnapprovedTransaction (txParams, opts = {}) {
|
2017-08-03 00:58:05 +02:00
|
|
|
log.debug(`MetaMaskController newUnapprovedTransaction ${JSON.stringify(txParams)}`)
|
2017-12-08 00:04:14 +01:00
|
|
|
const initialTxMeta = await this.addUnapprovedTransaction(txParams)
|
2018-03-31 00:25:13 +02:00
|
|
|
initialTxMeta.origin = opts.origin
|
|
|
|
this.txStateManager.updateTx(initialTxMeta, '#newUnapprovedTransaction - adding the origin')
|
2017-08-03 00:58:05 +02:00
|
|
|
// listen for tx completion (success, fail)
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-08 00:04:14 +01:00
|
|
|
this.txStateManager.once(`${initialTxMeta.id}:finished`, (finishedTxMeta) => {
|
|
|
|
switch (finishedTxMeta.status) {
|
2017-08-03 00:58:05 +02:00
|
|
|
case 'submitted':
|
2017-12-08 00:04:14 +01:00
|
|
|
return resolve(finishedTxMeta.hash)
|
2017-08-03 00:58:05 +02:00
|
|
|
case 'rejected':
|
|
|
|
return reject(new Error('MetaMask Tx Signature: User denied transaction signature.'))
|
2017-12-08 00:04:14 +01:00
|
|
|
case 'failed':
|
|
|
|
return reject(new Error(finishedTxMeta.err.message))
|
2017-08-03 00:58:05 +02:00
|
|
|
default:
|
2017-12-08 00:04:14 +01:00
|
|
|
return reject(new Error(`MetaMask Tx Signature: Unknown problem: ${JSON.stringify(finishedTxMeta.txParams)}`))
|
2017-08-03 00:58:05 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:56:52 +02:00
|
|
|
async addUnapprovedTransaction (txParams) {
|
|
|
|
// validate
|
2018-04-05 21:12:02 +02:00
|
|
|
const normalizedTxParams = this._normalizeTxParams(txParams)
|
|
|
|
this._validateTxParams(normalizedTxParams)
|
2017-07-26 20:56:52 +02:00
|
|
|
// construct txMeta
|
2018-04-05 21:12:02 +02:00
|
|
|
let txMeta = this.txStateManager.generateTxMeta({ txParams: normalizedTxParams })
|
2018-01-14 23:00:17 +01:00
|
|
|
this.addTx(txMeta)
|
|
|
|
this.emit('newUnapprovedTx', txMeta)
|
2017-07-26 20:56:52 +02:00
|
|
|
// add default tx params
|
2018-01-18 00:42:01 +01:00
|
|
|
try {
|
2018-04-03 00:43:32 +02:00
|
|
|
txMeta = await this.addTxDefaults(txMeta)
|
2018-01-18 00:42:01 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
this.txStateManager.setTxStatusFailed(txMeta.id, error)
|
|
|
|
throw error
|
|
|
|
}
|
2018-01-14 23:00:17 +01:00
|
|
|
txMeta.loadingDefaults = false
|
2017-07-26 20:56:52 +02:00
|
|
|
// save txMeta
|
2018-01-14 23:00:17 +01:00
|
|
|
this.txStateManager.updateTx(txMeta)
|
2018-01-18 00:42:01 +01:00
|
|
|
|
2017-07-26 20:56:52 +02:00
|
|
|
return txMeta
|
2016-12-16 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2017-07-26 20:56:52 +02:00
|
|
|
async addTxDefaults (txMeta) {
|
2017-03-28 22:35:27 +02:00
|
|
|
const txParams = txMeta.txParams
|
|
|
|
// ensure value
|
2017-10-05 18:58:04 +02:00
|
|
|
txMeta.gasPriceSpecified = Boolean(txParams.gasPrice)
|
2018-01-08 22:19:18 +01:00
|
|
|
let gasPrice = txParams.gasPrice
|
|
|
|
if (!gasPrice) {
|
|
|
|
gasPrice = this.getGasPrice ? this.getGasPrice() : await this.query.gasPrice()
|
|
|
|
}
|
2017-09-06 06:50:36 +02:00
|
|
|
txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16))
|
2017-10-02 22:41:29 +02:00
|
|
|
txParams.value = txParams.value || '0x0'
|
2017-06-20 02:50:06 +02:00
|
|
|
// set gasLimit
|
2017-09-06 06:50:36 +02:00
|
|
|
return await this.txGasUtil.analyzeGasUsage(txMeta)
|
2017-01-10 20:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 00:14:18 +01:00
|
|
|
async retryTransaction (originalTxId) {
|
|
|
|
const originalTxMeta = this.txStateManager.getTx(originalTxId)
|
|
|
|
const lastGasPrice = originalTxMeta.txParams.gasPrice
|
|
|
|
const txMeta = this.txStateManager.generateTxMeta({
|
|
|
|
txParams: originalTxMeta.txParams,
|
|
|
|
lastGasPrice,
|
|
|
|
loadingDefaults: false,
|
|
|
|
})
|
|
|
|
this.addTx(txMeta)
|
|
|
|
this.emit('newUnapprovedTx', txMeta)
|
2018-03-13 22:42:26 +01:00
|
|
|
return txMeta
|
2017-12-07 04:20:11 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 18:17:16 +01:00
|
|
|
async updateTransaction (txMeta) {
|
|
|
|
this.txStateManager.updateTx(txMeta, 'confTx: user updated transaction')
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:56:52 +02:00
|
|
|
async updateAndApproveTransaction (txMeta) {
|
2017-10-02 22:41:29 +02:00
|
|
|
this.txStateManager.updateTx(txMeta, 'confTx: user approved transaction')
|
2017-07-26 20:56:52 +02:00
|
|
|
await this.approveTransaction(txMeta.id)
|
2016-12-14 21:55:41 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 00:07:56 +02:00
|
|
|
async approveTransaction (txId) {
|
2017-06-15 08:44:02 +02:00
|
|
|
let nonceLock
|
|
|
|
try {
|
|
|
|
// approve
|
2017-08-18 21:23:35 +02:00
|
|
|
this.txStateManager.setTxStatusApproved(txId)
|
2017-06-15 08:44:02 +02:00
|
|
|
// get next nonce
|
2017-08-18 21:23:35 +02:00
|
|
|
const txMeta = this.txStateManager.getTx(txId)
|
2017-06-15 08:44:02 +02:00
|
|
|
const fromAddress = txMeta.txParams.from
|
2017-07-26 19:40:08 +02:00
|
|
|
// wait for a nonce
|
2017-06-15 08:44:02 +02:00
|
|
|
nonceLock = await this.nonceTracker.getNonceLock(fromAddress)
|
2017-07-26 19:40:08 +02:00
|
|
|
// add nonce to txParams
|
2018-03-28 08:55:18 +02:00
|
|
|
// if txMeta has lastGasPrice then it is a retry at same nonce with higher
|
2018-03-08 07:01:14 +01:00
|
|
|
// gas price transaction and their for the nonce should not be calculated
|
|
|
|
const nonce = txMeta.lastGasPrice ? txMeta.txParams.nonce : nonceLock.nextNonce
|
2017-12-07 15:30:05 +01:00
|
|
|
txMeta.txParams.nonce = ethUtil.addHexPrefix(nonce.toString(16))
|
2017-07-26 19:40:08 +02:00
|
|
|
// add nonce debugging information to txMeta
|
|
|
|
txMeta.nonceDetails = nonceLock.nonceDetails
|
2017-10-02 22:41:29 +02:00
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions#approveTransaction')
|
2017-06-15 08:44:02 +02:00
|
|
|
// sign transaction
|
2017-07-13 21:25:43 +02:00
|
|
|
const rawTx = await this.signTransaction(txId)
|
2017-06-22 04:51:00 +02:00
|
|
|
await this.publishTransaction(txId, rawTx)
|
2017-06-15 08:44:02 +02:00
|
|
|
// must set transaction to submitted/failed before releasing lock
|
|
|
|
nonceLock.releaseLock()
|
|
|
|
} catch (err) {
|
2017-08-18 21:23:35 +02:00
|
|
|
this.txStateManager.setTxStatusFailed(txId, err)
|
2017-06-15 08:44:02 +02:00
|
|
|
// must set transaction to submitted/failed before releasing lock
|
|
|
|
if (nonceLock) nonceLock.releaseLock()
|
|
|
|
// continue with error chain
|
2017-07-13 00:07:56 +02:00
|
|
|
throw err
|
2017-06-15 08:44:02 +02:00
|
|
|
}
|
2016-12-16 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:25:43 +02:00
|
|
|
async signTransaction (txId) {
|
2017-08-18 21:23:35 +02:00
|
|
|
const txMeta = this.txStateManager.getTx(txId)
|
2017-03-30 23:23:23 +02:00
|
|
|
// add network/chain id
|
2018-03-28 08:55:18 +02:00
|
|
|
const chainId = this.getChainId()
|
|
|
|
const txParams = Object.assign({}, txMeta.txParams, { chainId })
|
|
|
|
// sign tx
|
|
|
|
const fromAddress = txParams.from
|
2017-09-06 06:50:36 +02:00
|
|
|
const ethTx = new Transaction(txParams)
|
2017-08-03 00:58:05 +02:00
|
|
|
await this.signEthTx(ethTx, fromAddress)
|
2018-03-28 08:55:18 +02:00
|
|
|
// set state to signed
|
2017-08-18 21:23:35 +02:00
|
|
|
this.txStateManager.setTxStatusSigned(txMeta.id)
|
2017-08-03 00:58:05 +02:00
|
|
|
const rawTx = ethUtil.bufferToHex(ethTx.serialize())
|
2017-07-13 21:25:43 +02:00
|
|
|
return rawTx
|
2017-01-13 11:00:11 +01:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:25:43 +02:00
|
|
|
async publishTransaction (txId, rawTx) {
|
2017-08-18 21:23:35 +02:00
|
|
|
const txMeta = this.txStateManager.getTx(txId)
|
2017-05-23 20:49:25 +02:00
|
|
|
txMeta.rawTx = rawTx
|
2017-10-02 22:41:29 +02:00
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions#publishTransaction')
|
2017-09-06 06:50:36 +02:00
|
|
|
const txHash = await this.query.sendRawTransaction(rawTx)
|
2017-08-03 00:58:05 +02:00
|
|
|
this.setTxHash(txId, txHash)
|
2017-08-18 21:23:35 +02:00
|
|
|
this.txStateManager.setTxStatusSubmitted(txId)
|
2017-01-13 11:00:11 +01:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:58:05 +02:00
|
|
|
async cancelTransaction (txId) {
|
2017-08-18 21:23:35 +02:00
|
|
|
this.txStateManager.setTxStatusRejected(txId)
|
2017-07-26 20:56:52 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 20:33:37 +01:00
|
|
|
// receives a txHash records the tx as signed
|
|
|
|
setTxHash (txId, txHash) {
|
2016-12-20 22:12:14 +01:00
|
|
|
// Add the tx hash to the persisted meta-tx object
|
2017-08-18 21:23:35 +02:00
|
|
|
const txMeta = this.txStateManager.getTx(txId)
|
2017-01-13 11:00:11 +01:00
|
|
|
txMeta.hash = txHash
|
2017-10-02 22:41:29 +02:00
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions#setTxHash')
|
2016-12-14 21:55:41 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:59:59 +02:00
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
2017-02-03 05:20:13 +01:00
|
|
|
|
2018-04-04 21:25:51 +02:00
|
|
|
_normalizeTxParams (txParams) {
|
2018-04-05 21:12:02 +02:00
|
|
|
// functions that handle normalizing of that key in txParams
|
|
|
|
const whiteList = {
|
|
|
|
from: from => ethUtil.addHexPrefix(from).toLowerCase(),
|
|
|
|
to: to => ethUtil.addHexPrefix(txParams.to).toLowerCase(),
|
|
|
|
nonce: nonce => ethUtil.addHexPrefix(nonce),
|
|
|
|
value: value => ethUtil.addHexPrefix(value),
|
|
|
|
data: data => ethUtil.addHexPrefix(data),
|
|
|
|
gas: gas => ethUtil.addHexPrefix(gas),
|
|
|
|
gasPrice: gasPrice => ethUtil.addHexPrefix(gasPrice),
|
2018-04-04 23:56:30 +02:00
|
|
|
}
|
2018-04-04 21:25:51 +02:00
|
|
|
|
2018-04-05 21:12:02 +02:00
|
|
|
// apply only keys in the whiteList
|
|
|
|
const normalizedTxParams = {}
|
|
|
|
Object.keys(whiteList).forEach((key) => {
|
|
|
|
if (txParams[key]) normalizedTxParams[key] = whiteList[key](txParams[key])
|
|
|
|
})
|
2018-04-04 21:25:51 +02:00
|
|
|
|
2018-04-05 21:12:02 +02:00
|
|
|
return normalizedTxParams
|
2018-04-04 21:25:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 23:56:30 +02:00
|
|
|
_validateTxParams (txParams) {
|
2018-04-04 21:25:51 +02:00
|
|
|
this._validateFrom(txParams)
|
|
|
|
this._validateRecipient(txParams)
|
|
|
|
if ('value' in txParams) {
|
|
|
|
const value = txParams.value.toString()
|
|
|
|
if (value.includes('-')) {
|
|
|
|
throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.includes('.')) {
|
|
|
|
throw new Error(`Invalid transaction value of ${txParams.value} number must be in wei`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateFrom (txParams) {
|
|
|
|
if ( !(typeof txParams.from === 'string') ) throw new Error(`Invalid from address ${txParams.from} not a string`)
|
|
|
|
if (!ethUtil.isValidAddress(txParams.from)) throw new Error('Invalid from address')
|
|
|
|
}
|
|
|
|
|
|
|
|
_validateRecipient (txParams) {
|
|
|
|
if (txParams.to === '0x' || txParams.to === null ) {
|
|
|
|
if (txParams.data) {
|
|
|
|
delete txParams.to
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid recipient address')
|
|
|
|
}
|
|
|
|
} else if ( txParams.to !== undefined && !ethUtil.isValidAddress(txParams.to) ) {
|
|
|
|
throw new Error('Invalid recipient address')
|
|
|
|
}
|
|
|
|
return txParams
|
|
|
|
}
|
|
|
|
|
2018-03-08 07:01:14 +01:00
|
|
|
_markNonceDuplicatesDropped (txId) {
|
|
|
|
this.txStateManager.setTxStatusConfirmed(txId)
|
|
|
|
// get the confirmed transactions nonce and from address
|
|
|
|
const txMeta = this.txStateManager.getTx(txId)
|
|
|
|
const { nonce, from } = txMeta.txParams
|
|
|
|
const sameNonceTxs = this.txStateManager.getFilteredTxList({nonce, from})
|
|
|
|
if (!sameNonceTxs.length) return
|
|
|
|
// mark all same nonce transactions as dropped and give i a replacedBy hash
|
|
|
|
sameNonceTxs.forEach((otherTxMeta) => {
|
2018-03-08 19:36:31 +01:00
|
|
|
if (otherTxMeta.id === txId) return
|
2018-03-08 07:01:14 +01:00
|
|
|
otherTxMeta.replacedBy = txMeta.hash
|
|
|
|
this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:confirmed reference to confirmed txHash with same nonce')
|
|
|
|
this.txStateManager.setTxStatusDropped(otherTxMeta.id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-02-03 05:20:13 +01:00
|
|
|
_updateMemstore () {
|
2017-08-18 21:23:35 +02:00
|
|
|
const unapprovedTxs = this.txStateManager.getUnapprovedTxList()
|
|
|
|
const selectedAddressTxList = this.txStateManager.getFilteredTxList({
|
2017-02-03 05:20:13 +01:00
|
|
|
from: this.getSelectedAddress(),
|
|
|
|
metamaskNetworkId: this.getNetwork(),
|
|
|
|
})
|
|
|
|
this.memStore.updateState({ unapprovedTxs, selectedAddressTxList })
|
2016-12-20 22:12:14 +01:00
|
|
|
}
|
2017-08-24 07:29:08 +02:00
|
|
|
}
|