1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 16:18:07 +01:00
metamask-extension/test/unit/tx-controller-test.js

404 lines
15 KiB
JavaScript
Raw Normal View History

const assert = require('assert')
2017-03-30 23:23:23 +02:00
const ethUtil = require('ethereumjs-util')
const EthTx = require('ethereumjs-tx')
2017-02-03 05:59:47 +01:00
const ObservableStore = require('obs-store')
const sinon = require('sinon')
2017-05-16 19:27:41 +02:00
const TransactionController = require('../../app/scripts/controllers/transactions')
2017-09-06 06:50:36 +02:00
const TxGasUtils = require('../../app/scripts/lib/tx-gas-utils')
2017-09-23 01:15:18 +02:00
const { createStubedProvider } = require('../stub/provider')
const noop = () => true
2017-03-30 23:23:23 +02:00
const currentNetworkId = 42
const otherNetworkId = 36
const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex')
2017-08-02 17:34:45 +02:00
describe('Transaction Controller', function () {
2017-09-26 04:47:03 +02:00
let txController, provider, providerResultStub
2017-05-04 23:35:10 +02:00
beforeEach(function () {
2017-08-09 00:30:22 +02:00
providerResultStub = {}
provider = createStubedProvider(providerResultStub)
2017-05-16 19:27:41 +02:00
txController = new TransactionController({
2017-08-09 00:30:22 +02:00
provider,
2017-05-23 08:12:28 +02:00
networkStore: new ObservableStore(currentNetworkId),
2016-12-16 19:33:36 +01:00
txHistoryLimit: 10,
blockTracker: { getCurrentBlock: noop, on: noop, once: noop },
2017-03-30 23:23:23 +02:00
signTransaction: (ethTx) => new Promise((resolve) => {
ethTx.sign(privKey)
resolve()
2017-05-04 23:35:10 +02:00
}),
})
txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop })
2017-09-06 06:50:36 +02:00
txController.txProviderUtils = new TxGasUtils(txController.provider)
})
describe('#getState', function () {
2017-09-23 01:15:18 +02:00
it('should return a state object with the right keys and datat types', function () {
const exposedState = txController.getState()
assert('unapprovedTxs' in exposedState, 'state should have the key unapprovedTxs')
assert('selectedAddressTxList' in exposedState, 'state should have the key selectedAddressTxList')
assert(typeof exposedState.unapprovedTxs === 'object', 'should be an object')
assert(Array.isArray(exposedState.selectedAddressTxList), 'should be an array')
})
})
describe('#getUnapprovedTxCount', function () {
it('should return the number of unapproved txs', function () {
txController.txStateManager._saveTxList([
{ id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 2, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 3, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
])
const unapprovedTxCount = txController.getUnapprovedTxCount()
assert.equal(unapprovedTxCount, 3, 'should be 3')
})
})
2017-09-13 23:07:22 +02:00
describe('#getPendingTxCount', function () {
it('should return the number of pending txs', function () {
txController.txStateManager._saveTxList([
{ id: 1, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 2, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 3, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
])
const pendingTxCount = txController.getPendingTxCount()
assert.equal(pendingTxCount, 3, 'should be 3')
})
})
2017-09-23 01:15:18 +02:00
describe('#getConfirmedTransactions', function () {
let address
beforeEach(function () {
address = '0xc684832530fcbddae4b4230a47e991ddcec2831d'
const txParams = {
'from': address,
'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
}
txController.txStateManager._saveTxList([
2017-09-26 04:47:03 +02:00
{id: 0, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams},
2017-09-23 01:15:18 +02:00
{id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams},
{id: 2, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams},
2017-09-26 04:47:03 +02:00
{id: 3, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams},
{id: 4, status: 'rejected', metamaskNetworkId: currentNetworkId, txParams},
{id: 5, status: 'approved', metamaskNetworkId: currentNetworkId, txParams},
{id: 6, status: 'signed', metamaskNetworkId: currentNetworkId, txParams},
{id: 7, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams},
{id: 8, status: 'failed', metamaskNetworkId: currentNetworkId, txParams},
2017-09-23 01:15:18 +02:00
])
})
2017-09-26 04:47:03 +02:00
2017-09-23 01:15:18 +02:00
it('should return the number of confirmed txs', function () {
assert.equal(txController.nonceTracker.getConfirmedTransactions(address).length, 3)
})
})
2017-09-13 23:07:22 +02:00
describe('#newUnapprovedTransaction', function () {
let stub, txMeta, txParams
beforeEach(function () {
txParams = {
2017-09-23 01:15:18 +02:00
'from': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
2017-09-26 04:47:03 +02:00
}
txMeta = {
status: 'unapproved',
id: 1,
metamaskNetworkId: currentNetworkId,
txParams,
history: [],
}
2017-08-11 23:19:35 +02:00
txController.txStateManager._saveTxList([txMeta])
stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txController.txStateManager.addTx(txMeta)))
})
afterEach(function () {
2017-08-11 23:19:35 +02:00
txController.txStateManager._saveTxList([])
stub.restore()
})
it('should emit newUnaprovedTx event and pass txMeta as the first argument', function (done) {
txController.once('newUnaprovedTx', (txMetaFromEmit) => {
assert(txMetaFromEmit, 'txMeta is falsey')
assert.equal(txMetaFromEmit.id, 1, 'the right txMeta was passed')
done()
})
txController.newUnapprovedTransaction(txParams)
.catch(done)
})
it('should resolve when finished and status is submitted and resolve with the hash', function (done) {
txController.once('newUnaprovedTx', (txMetaFromEmit) => {
setTimeout(() => {
txController.setTxHash(txMetaFromEmit.id, '0x0')
2017-08-11 23:19:35 +02:00
txController.txStateManager.setTxStatusSubmitted(txMetaFromEmit.id)
}, 10)
})
txController.newUnapprovedTransaction(txParams)
.then((hash) => {
assert(hash, 'newUnapprovedTransaction needs to return the hash')
done()
})
.catch(done)
})
it('should reject when finished and status is rejected', function (done) {
txController.once('newUnaprovedTx', (txMetaFromEmit) => {
setTimeout(() => {
2017-08-11 23:19:35 +02:00
txController.txStateManager.setTxStatusRejected(txMetaFromEmit.id)
}, 10)
})
txController.newUnapprovedTransaction(txParams)
.catch((err) => {
if (err.message === 'MetaMask Tx Signature: User denied transaction signature.') done()
else done(err)
})
})
})
describe('#addUnapprovedTransaction', function () {
it('should add an unapproved transaction and return a valid txMeta', function (done) {
2017-08-03 01:07:18 +02:00
const addTxDefaultsStub = sinon.stub(txController, 'addTxDefaults').callsFake(() => Promise.resolve())
txController.addUnapprovedTransaction({})
.then((txMeta) => {
assert(('id' in txMeta), 'should have a id')
assert(('time' in txMeta), 'should have a time stamp')
assert(('metamaskNetworkId' in txMeta), 'should have a metamaskNetworkId')
assert(('txParams' in txMeta), 'should have a txParams')
assert(('history' in txMeta), 'should have a history')
2017-08-11 23:19:35 +02:00
const memTxMeta = txController.txStateManager.getTx(txMeta.id)
assert.deepEqual(txMeta, memTxMeta, `txMeta should be stored in txController after adding it\n expected: ${txMeta} \n got: ${memTxMeta}`)
addTxDefaultsStub.restore()
done()
}).catch(done)
})
})
2017-08-02 17:34:45 +02:00
describe('#addTxDefaults', function () {
it('should add the tx defaults if their are none', function (done) {
2017-09-23 01:15:18 +02:00
const txMeta = {
2017-08-02 17:34:45 +02:00
'txParams': {
2017-09-23 01:15:18 +02:00
'from': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
2017-08-02 17:34:45 +02:00
},
}
2017-08-09 00:30:22 +02:00
providerResultStub.eth_gasPrice = '4a817c800'
providerResultStub.eth_getBlockByNumber = { gasLimit: '47b784' }
providerResultStub.eth_estimateGas = '5209'
2017-08-02 17:34:45 +02:00
txController.addTxDefaults(txMeta)
.then((txMetaWithDefaults) => {
2017-09-23 01:15:18 +02:00
assert(txMetaWithDefaults.txParams.value, '0x0', 'should have added 0x0 as the value')
2017-08-02 17:34:45 +02:00
assert(txMetaWithDefaults.txParams.gasPrice, 'should have added the gas price')
assert(txMetaWithDefaults.txParams.gas, 'should have added the gas field')
done()
})
.catch(done)
})
})
describe('#validateTxParams', function () {
it('does not throw for positive values', function (done) {
var sample = {
2017-05-04 23:35:10 +02:00
value: '0x01',
}
txController.txProviderUtils.validateTxParams(sample).then(() => {
done()
}).catch(done)
})
it('returns error for negative values', function (done) {
var sample = {
2017-05-04 23:35:10 +02:00
value: '-0x01',
}
txController.txProviderUtils.validateTxParams(sample)
.then(() => done('expected to thrown on negativity values but didn\'t'))
.catch((err) => {
assert.ok(err, 'error')
done()
})
})
})
2017-05-04 23:35:10 +02:00
describe('#addTx', function () {
2017-08-11 23:19:35 +02:00
it('should emit updates', function (done) {
const txMeta = {
id: '1',
status: 'unapproved',
metamaskNetworkId: currentNetworkId,
2017-09-23 01:15:18 +02:00
txParams: {},
}
const eventNames = ['update:badge', '1:unapproved']
2017-08-11 23:19:35 +02:00
const listeners = []
eventNames.forEach((eventName) => {
listeners.push(new Promise((resolve) => {
txController.once(eventName, (arg) => {
resolve(arg)
})
}))
})
Promise.all(listeners)
.then((returnValues) => {
assert.deepEqual(returnValues.pop(), txMeta, 'last event 1:unapproved should return txMeta')
done()
})
.catch(done)
txController.addTx(txMeta)
})
})
describe('#approveTransaction', function () {
let txMeta, originalValue
beforeEach(function () {
originalValue = '0x01'
txMeta = {
id: '1',
status: 'unapproved',
metamaskNetworkId: currentNetworkId,
txParams: {
nonce: originalValue,
gas: originalValue,
gasPrice: originalValue,
},
}
})
it('does not overwrite set values', function (done) {
2017-06-16 00:25:22 +02:00
this.timeout(15000)
const wrongValue = '0x05'
txController.addTx(txMeta)
2017-08-09 00:30:22 +02:00
providerResultStub.eth_gasPrice = wrongValue
providerResultStub.eth_estimateGas = '0x5209'
const signStub = sinon.stub(txController, 'signTransaction').callsFake(() => Promise.resolve())
const pubStub = sinon.stub(txController, 'publishTransaction').callsFake(() => {
txController.setTxHash('1', originalValue)
2017-08-11 23:19:35 +02:00
txController.txStateManager.setTxStatusSubmitted('1')
})
txController.approveTransaction(txMeta.id).then(() => {
2017-08-11 23:19:35 +02:00
const result = txController.txStateManager.getTx(txMeta.id)
const params = result.txParams
assert.equal(params.gas, originalValue, 'gas unmodified')
assert.equal(params.gasPrice, originalValue, 'gas price unmodified')
assert.equal(result.hash, originalValue, `hash was set \n got: ${result.hash} \n expected: ${originalValue}`)
signStub.restore()
pubStub.restore()
done()
}).catch(done)
})
})
2017-05-04 23:35:10 +02:00
describe('#sign replay-protected tx', function () {
2017-06-13 02:42:54 +02:00
it('prepares a tx with the chainId set', function (done) {
2017-05-16 19:27:41 +02:00
txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
txController.signTransaction('1').then((rawTx) => {
2017-03-30 23:23:23 +02:00
const ethTx = new EthTx(ethUtil.toBuffer(rawTx))
assert.equal(ethTx.getChainId(), currentNetworkId)
2017-06-13 02:42:54 +02:00
done()
}).catch(done)
2017-03-30 23:23:23 +02:00
})
})
2017-09-23 01:15:18 +02:00
2017-09-26 04:47:03 +02:00
describe('#updateAndApproveTransaction', function () {
let txMeta
2017-09-23 01:15:18 +02:00
beforeEach(function () {
2017-09-26 04:47:03 +02:00
txMeta = {
id: 1,
status: 'unapproved',
txParams: {
from: '0xc684832530fcbddae4b4230a47e991ddcec2831d',
to: '0x1678a085c290ebd122dc42cba69373b5953b831d',
gasPrice: '0x77359400',
gas: '0x7b0d',
nonce: '0x4b',
},
metamaskNetworkId: currentNetworkId,
}
})
it('should update and approve transactions', function () {
2017-09-23 01:15:18 +02:00
txController.txStateManager.addTx(txMeta)
2017-09-26 04:47:03 +02:00
txController.updateAndApproveTransaction(txMeta)
const tx = txController.txStateManager.getTx(1)
assert.equal(tx.status, 'approved')
2017-09-23 01:15:18 +02:00
})
2017-09-26 04:47:03 +02:00
})
2017-09-23 01:15:18 +02:00
2017-09-26 04:47:03 +02:00
describe('#getChainId', function () {
it('returns 0 when the chainId is NaN', function () {
txController.networkStore = new ObservableStore(NaN)
assert.equal(txController.getChainId(), 0)
2017-09-23 01:15:18 +02:00
})
})
describe('#cancelTransaction', function () {
beforeEach(function () {
2017-09-26 04:47:03 +02:00
txController.txStateManager._saveTxList([
{ id: 0, status: 'unapproved', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
{ id: 1, status: 'rejected', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
{ id: 2, status: 'approved', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
{ id: 3, status: 'signed', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
{ id: 4, status: 'submitted', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
{ id: 5, status: 'confirmed', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
{ id: 6, status: 'failed', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
])
2017-09-23 01:15:18 +02:00
})
it('should set the transaction to rejected from unapproved', async function () {
await txController.cancelTransaction(0)
2017-09-26 04:47:03 +02:00
assert.equal(txController.txStateManager.getTx(0).status, 'rejected')
2017-09-23 01:15:18 +02:00
})
})
describe('#publishTransaction', function () {
2017-09-26 04:47:03 +02:00
let hash, txMeta
2017-09-23 01:15:18 +02:00
beforeEach(function () {
2017-09-26 04:47:03 +02:00
hash = '0x2a5523c6fa98b47b7d9b6c8320179785150b42a16bcff36b398c5062b65657e8'
2017-09-23 01:15:18 +02:00
txMeta = {
id: 1,
2017-09-26 04:47:03 +02:00
status: 'unapproved',
2017-09-23 01:15:18 +02:00
txParams: {},
metamaskNetworkId: currentNetworkId,
}
2017-09-26 04:47:03 +02:00
providerResultStub.eth_sendRawTransaction = hash
2017-09-23 01:15:18 +02:00
})
2017-09-26 04:47:03 +02:00
2017-09-23 01:15:18 +02:00
it('should publish a tx, updates the rawTx when provided a one', async function () {
txController.txStateManager.addTx(txMeta)
2017-09-26 04:47:03 +02:00
await txController.publishTransaction(txMeta.id)
const publishedTx = txController.txStateManager.getTx(1)
2017-09-26 04:47:03 +02:00
assert.equal(publishedTx.hash, hash)
assert.equal(publishedTx.status, 'submitted')
2017-09-23 01:15:18 +02:00
})
})
describe('#getPendingTransactions', function () {
beforeEach(function () {
txController.txStateManager._saveTxList([
{ id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 2, status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 3, status: 'approved', metamaskNetworkId: currentNetworkId, txParams: {} },
2017-09-23 01:15:18 +02:00
{ id: 4, status: 'signed', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 5, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 6, status: 'confimed', metamaskNetworkId: currentNetworkId, txParams: {} },
{ id: 7, status: 'failed', metamaskNetworkId: currentNetworkId, txParams: {} },
])
})
it('should show only submitted transactions as pending transasction', function () {
assert(txController.pendingTxTracker.getPendingTransactions().length, 1)
assert(txController.pendingTxTracker.getPendingTransactions()[0].status, 'submitted')
})
})
2017-09-22 23:19:14 +02:00
})