mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
make publishTransaction and signTransaction async methods
This commit is contained in:
parent
d6001daab8
commit
7eccf5905a
@ -3,7 +3,6 @@ const async = require('async')
|
|||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const ethUtil = require('ethereumjs-util')
|
const ethUtil = require('ethereumjs-util')
|
||||||
const denodeify = require('denodeify')
|
|
||||||
const TxProviderUtil = require('../lib/tx-utils')
|
const TxProviderUtil = require('../lib/tx-utils')
|
||||||
const createId = require('../lib/random-id')
|
const createId = require('../lib/random-id')
|
||||||
const NonceTracker = require('../lib/nonce-tracker')
|
const NonceTracker = require('../lib/nonce-tracker')
|
||||||
@ -195,7 +194,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
txMeta.txParams.nonce = nonceLock.nextNonce
|
txMeta.txParams.nonce = nonceLock.nextNonce
|
||||||
this.updateTx(txMeta)
|
this.updateTx(txMeta)
|
||||||
// sign transaction
|
// sign transaction
|
||||||
const rawTx = await denodeify(this.signTransaction.bind(this))(txId)
|
const rawTx = await this.signTransaction(txId)
|
||||||
await this.publishTransaction(txId, rawTx)
|
await this.publishTransaction(txId, rawTx)
|
||||||
// must set transaction to submitted/failed before releasing lock
|
// must set transaction to submitted/failed before releasing lock
|
||||||
nonceLock.releaseLock()
|
nonceLock.releaseLock()
|
||||||
@ -231,32 +230,27 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
signTransaction (txId, cb) {
|
async signTransaction (txId) {
|
||||||
const txMeta = this.getTx(txId)
|
const txMeta = this.getTx(txId)
|
||||||
const txParams = txMeta.txParams
|
const txParams = txMeta.txParams
|
||||||
const fromAddress = txParams.from
|
const fromAddress = txParams.from
|
||||||
// add network/chain id
|
// add network/chain id
|
||||||
txParams.chainId = this.getChainId()
|
txParams.chainId = this.getChainId()
|
||||||
const ethTx = this.txProviderUtils.buildEthTxFromParams(txParams)
|
const ethTx = this.txProviderUtils.buildEthTxFromParams(txParams)
|
||||||
this.signEthTx(ethTx, fromAddress).then(() => {
|
const rawTx = await this.signEthTx(ethTx, fromAddress).then(() => {
|
||||||
this.setTxStatusSigned(txMeta.id)
|
this.setTxStatusSigned(txMeta.id)
|
||||||
cb(null, ethUtil.bufferToHex(ethTx.serialize()))
|
return ethUtil.bufferToHex(ethTx.serialize())
|
||||||
}).catch((err) => {
|
|
||||||
cb(err)
|
|
||||||
})
|
})
|
||||||
|
return rawTx
|
||||||
}
|
}
|
||||||
|
|
||||||
publishTransaction (txId, rawTx) {
|
async publishTransaction (txId, rawTx) {
|
||||||
const txMeta = this.getTx(txId)
|
const txMeta = this.getTx(txId)
|
||||||
txMeta.rawTx = rawTx
|
txMeta.rawTx = rawTx
|
||||||
this.updateTx(txMeta)
|
this.updateTx(txMeta)
|
||||||
return new Promise((resolve, reject) => {
|
await this.txProviderUtils.publishTransaction(rawTx).then((txHash) => {
|
||||||
this.txProviderUtils.publishTransaction(rawTx, (err, txHash) => {
|
this.setTxHash(txId, txHash)
|
||||||
if (err) reject(err)
|
this.setTxStatusSubmitted(txId)
|
||||||
this.setTxHash(txId, txHash)
|
|
||||||
this.setTxStatusSubmitted(txId)
|
|
||||||
resolve()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -435,8 +429,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
const pending = this.getTxsByMetaData('status', 'submitted')
|
const pending = this.getTxsByMetaData('status', 'submitted')
|
||||||
// only try resubmitting if their are transactions to resubmit
|
// only try resubmitting if their are transactions to resubmit
|
||||||
if (!pending.length) return
|
if (!pending.length) return
|
||||||
const resubmit = denodeify(this._resubmitTx.bind(this))
|
pending.forEach((txMeta) => this._resubmitTx(txMeta).catch((err) => {
|
||||||
pending.forEach((txMeta) => resubmit(txMeta).catch((err) => {
|
|
||||||
/*
|
/*
|
||||||
Dont marked as failed if the error is a "known" transaction warning
|
Dont marked as failed if the error is a "known" transaction warning
|
||||||
"there is already a transaction with the same sender-nonce
|
"there is already a transaction with the same sender-nonce
|
||||||
@ -463,7 +456,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
_resubmitTx (txMeta, cb) {
|
async _resubmitTx (txMeta, cb) {
|
||||||
const address = txMeta.txParams.from
|
const address = txMeta.txParams.from
|
||||||
const balance = this.ethStore.getState().accounts[address].balance
|
const balance = this.ethStore.getState().accounts[address].balance
|
||||||
if (!('retryCount' in txMeta)) txMeta.retryCount = 0
|
if (!('retryCount' in txMeta)) txMeta.retryCount = 0
|
||||||
@ -482,7 +475,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
// Increment a try counter.
|
// Increment a try counter.
|
||||||
txMeta.retryCount++
|
txMeta.retryCount++
|
||||||
const rawTx = txMeta.rawTx
|
const rawTx = txMeta.rawTx
|
||||||
this.txProviderUtils.publishTransaction(rawTx, cb)
|
return await this.txProviderUtils.publishTransaction(rawTx, cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks the network for signed txs and
|
// checks the network for signed txs and
|
||||||
|
@ -106,8 +106,13 @@ module.exports = class txProviderUtils {
|
|||||||
return ethTx
|
return ethTx
|
||||||
}
|
}
|
||||||
|
|
||||||
publishTransaction (rawTx, cb) {
|
publishTransaction (rawTx) {
|
||||||
this.query.sendRawTransaction(rawTx, cb)
|
return new Promise((resolve, reject) => {
|
||||||
|
this.query.sendRawTransaction(rawTx, (err, ress) => {
|
||||||
|
if (err) reject(err)
|
||||||
|
else resolve(ress)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
validateTxParams (txParams, cb) {
|
validateTxParams (txParams, cb) {
|
||||||
|
@ -270,7 +270,7 @@ describe('Transaction Controller', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
it('does not overwrite set values', function () {
|
it('does not overwrite set values', function (done) {
|
||||||
this.timeout(15000)
|
this.timeout(15000)
|
||||||
const wrongValue = '0x05'
|
const wrongValue = '0x05'
|
||||||
|
|
||||||
@ -283,37 +283,35 @@ describe('Transaction Controller', function () {
|
|||||||
.callsArgWithAsync(0, null, wrongValue)
|
.callsArgWithAsync(0, null, wrongValue)
|
||||||
|
|
||||||
|
|
||||||
const signStub = sinon.stub(txController, 'signTransaction')
|
const signStub = sinon.stub(txController, 'signTransaction', () => Promise.resolve())
|
||||||
.callsArgWithAsync(1, null, noop)
|
|
||||||
|
|
||||||
const pubStub = sinon.stub(txController.txProviderUtils, 'publishTransaction')
|
const pubStub = sinon.stub(txController.txProviderUtils, 'publishTransaction', () => Promise.resolve(originalValue))
|
||||||
.callsArgWithAsync(1, null, originalValue)
|
|
||||||
|
|
||||||
return txController.approveTransaction(txMeta.id).then(() => {
|
txController.approveTransaction(txMeta.id).then(() => {
|
||||||
const result = txController.getTx(txMeta.id)
|
const result = txController.getTx(txMeta.id)
|
||||||
const params = result.txParams
|
const params = result.txParams
|
||||||
|
|
||||||
assert.equal(params.gas, originalValue, 'gas unmodified')
|
assert.equal(params.gas, originalValue, 'gas unmodified')
|
||||||
assert.equal(params.gasPrice, originalValue, 'gas price unmodified')
|
assert.equal(params.gasPrice, originalValue, 'gas price unmodified')
|
||||||
assert.equal(result.hash, originalValue, 'hash was set')
|
assert.equal(result.hash, originalValue, `hash was set \n got: ${result.hash} \n expected: ${originalValue}`)
|
||||||
|
|
||||||
estimateStub.restore()
|
estimateStub.restore()
|
||||||
priceStub.restore()
|
priceStub.restore()
|
||||||
signStub.restore()
|
signStub.restore()
|
||||||
pubStub.restore()
|
pubStub.restore()
|
||||||
})
|
done()
|
||||||
|
}).catch(done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#sign replay-protected tx', function () {
|
describe('#sign replay-protected tx', function () {
|
||||||
it('prepares a tx with the chainId set', function (done) {
|
it('prepares a tx with the chainId set', function (done) {
|
||||||
txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
|
txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
|
||||||
txController.signTransaction('1', (err, rawTx) => {
|
txController.signTransaction('1').then((rawTx) => {
|
||||||
if (err) return done('it should not fail')
|
|
||||||
const ethTx = new EthTx(ethUtil.toBuffer(rawTx))
|
const ethTx = new EthTx(ethUtil.toBuffer(rawTx))
|
||||||
assert.equal(ethTx.getChainId(), currentNetworkId)
|
assert.equal(ethTx.getChainId(), currentNetworkId)
|
||||||
done()
|
done()
|
||||||
})
|
}).catch(done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user