Merge branch 'master' of github.com:MetaMask/metamask-plugin into platform

feature/default_network_editable
kumavis 8 years ago
commit 810f5881f6
  1. 20
      app/scripts/transaction-manager.js
  2. 2
      package.json
  3. 25
      test/unit/notice-controller-test.js
  4. 79
      test/unit/tx-manager-test.js
  5. 17
      test/unit/tx-utils-test.js

@ -205,11 +205,23 @@ module.exports = class TransactionManager extends EventEmitter {
}) })
} }
getChainId() {
const networkState = this.networkStore.getState()
const getChainId = parseInt(networkState.network)
if (Number.isNaN(getChainId)) {
return 0
} else {
return getChainId
}
}
signTransaction (txId, cb) { signTransaction (txId, cb) {
let txMeta = this.getTx(txId) const txMeta = this.getTx(txId)
let txParams = txMeta.txParams const txParams = txMeta.txParams
let fromAddress = txParams.from const fromAddress = txParams.from
let ethTx = this.txProviderUtils.buildEthTxFromParams(txParams) // add network/chain id
txParams.chainId = this.getChainId()
const ethTx = this.txProviderUtils.buildEthTxFromParams(txParams)
this.signEthTx(ethTx, fromAddress).then(() => { this.signEthTx(ethTx, fromAddress).then(() => {
this.setTxStatusSigned(txMeta.id) this.setTxStatusSigned(txMeta.id)
cb(null, ethUtil.bufferToHex(ethTx.serialize())) cb(null, ethUtil.bufferToHex(ethTx.serialize()))

@ -57,7 +57,7 @@
"eth-query": "^1.0.3", "eth-query": "^1.0.3",
"eth-sig-util": "^1.1.1", "eth-sig-util": "^1.1.1",
"eth-simple-keyring": "^1.1.1", "eth-simple-keyring": "^1.1.1",
"ethereumjs-tx": "^1.0.0", "ethereumjs-tx": "^1.2.5",
"ethereumjs-util": "ethereumjs/ethereumjs-util#ac5d0908536b447083ea422b435da27f26615de9", "ethereumjs-util": "ethereumjs/ethereumjs-util#ac5d0908536b447083ea422b435da27f26615de9",
"ethereumjs-wallet": "^0.6.0", "ethereumjs-wallet": "^0.6.0",
"ethjs-ens": "^1.0.2", "ethjs-ens": "^1.0.2",

@ -11,8 +11,6 @@ describe('notice-controller', function() {
beforeEach(function() { beforeEach(function() {
// simple localStorage polyfill // simple localStorage polyfill
window.localStorage = {}
if (window.localStorage.clear) window.localStorage.clear()
let configManager = configManagerGen() let configManager = configManagerGen()
noticeController = new NoticeController({ noticeController = new NoticeController({
configManager: configManager, configManager: configManager,
@ -21,7 +19,7 @@ describe('notice-controller', function() {
describe('notices', function() { describe('notices', function() {
describe('#getNoticesList', function() { describe('#getNoticesList', function() {
it('should return an empty array when new', function() { it('should return an empty array when new', function(done) {
var testList = [{ var testList = [{
id:0, id:0,
read:false, read:false,
@ -29,11 +27,12 @@ describe('notice-controller', function() {
}] }]
var result = noticeController.getNoticesList() var result = noticeController.getNoticesList()
assert.equal(result.length, 0) assert.equal(result.length, 0)
done()
}) })
}) })
describe('#setNoticesList', function() { describe('#setNoticesList', function() {
it('should set data appropriately', function () { it('should set data appropriately', function (done) {
var testList = [{ var testList = [{
id:0, id:0,
read:false, read:false,
@ -42,11 +41,12 @@ describe('notice-controller', function() {
noticeController.setNoticesList(testList) noticeController.setNoticesList(testList)
var testListId = noticeController.getNoticesList()[0].id var testListId = noticeController.getNoticesList()[0].id
assert.equal(testListId, 0) assert.equal(testListId, 0)
done()
}) })
}) })
describe('#updateNoticeslist', function() { describe('#updateNoticeslist', function() {
it('should integrate the latest changes from the source', function() { it('should integrate the latest changes from the source', function(done) {
var testList = [{ var testList = [{
id:55, id:55,
read:false, read:false,
@ -57,26 +57,26 @@ describe('notice-controller', function() {
var newList = noticeController.getNoticesList() var newList = noticeController.getNoticesList()
assert.ok(newList[0].id === 55) assert.ok(newList[0].id === 55)
assert.ok(newList[1]) assert.ok(newList[1])
done()
}) })
}) })
it('should not overwrite any existing fields', function () { it('should not overwrite any existing fields', function (done) {
var testList = [{ var testList = [{
id:0, id:0,
read:false, read:false,
title:"Futuristic Notice" title:"Futuristic Notice"
}] }]
noticeController.setNoticesList(testList) noticeController.setNoticesList(testList)
noticeController.updateNoticesList().then(() => {
var newList = noticeController.getNoticesList() var newList = noticeController.getNoticesList()
assert.equal(newList[0].id, 0) assert.equal(newList[0].id, 0)
assert.equal(newList[0].title, "Futuristic Notice") assert.equal(newList[0].title, "Futuristic Notice")
assert.equal(newList.length, 1) assert.equal(newList.length, 1)
}) done()
}) })
}) })
describe('#markNoticeRead', function () { describe('#markNoticeRead', function () {
it('should mark a notice as read', function () { it('should mark a notice as read', function (done) {
var testList = [{ var testList = [{
id:0, id:0,
read:false, read:false,
@ -86,11 +86,12 @@ describe('notice-controller', function() {
noticeController.markNoticeRead(testList[0]) noticeController.markNoticeRead(testList[0])
var newList = noticeController.getNoticesList() var newList = noticeController.getNoticesList()
assert.ok(newList[0].read) assert.ok(newList[0].read)
done()
}) })
}) })
describe('#getLatestUnreadNotice', function () { describe('#getLatestUnreadNotice', function () {
it('should retrieve the latest unread notice', function () { it('should retrieve the latest unread notice', function (done) {
var testList = [ var testList = [
{id:0,read:true,title:"Past Notice"}, {id:0,read:true,title:"Past Notice"},
{id:1,read:false,title:"Current Notice"}, {id:1,read:false,title:"Current Notice"},
@ -99,8 +100,9 @@ describe('notice-controller', function() {
noticeController.setNoticesList(testList) noticeController.setNoticesList(testList)
var latestUnread = noticeController.getLatestUnreadNotice() var latestUnread = noticeController.getLatestUnreadNotice()
assert.equal(latestUnread.id, 2) assert.equal(latestUnread.id, 2)
done()
}) })
it('should return undefined if no unread notices exist.', function () { it('should return undefined if no unread notices exist.', function (done) {
var testList = [ var testList = [
{id:0,read:true,title:"Past Notice"}, {id:0,read:true,title:"Past Notice"},
{id:1,read:true,title:"Current Notice"}, {id:1,read:true,title:"Current Notice"},
@ -109,6 +111,7 @@ describe('notice-controller', function() {
noticeController.setNoticesList(testList) noticeController.setNoticesList(testList)
var latestUnread = noticeController.getLatestUnreadNotice() var latestUnread = noticeController.getLatestUnreadNotice()
assert.ok(!latestUnread) assert.ok(!latestUnread)
done()
}) })
}) })
}) })

@ -1,19 +1,28 @@
const assert = require('assert') const assert = require('assert')
const extend = require('xtend') const extend = require('xtend')
const EventEmitter = require('events') const EventEmitter = require('events')
const ethUtil = require('ethereumjs-util')
const EthTx = require('ethereumjs-tx')
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const STORAGE_KEY = 'metamask-persistance-key' const STORAGE_KEY = 'metamask-persistance-key'
const TransactionManager = require('../../app/scripts/transaction-manager') const TransactionManager = require('../../app/scripts/transaction-manager')
const noop = () => true const noop = () => true
const currentNetworkId = 42
const otherNetworkId = 36
const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex')
describe('Transaction Manager', function() { describe('Transaction Manager', function() {
let txManager let txManager
beforeEach(function() { beforeEach(function() {
txManager = new TransactionManager({ txManager = new TransactionManager({
networkStore: new ObservableStore({ network: 'unit test' }), networkStore: new ObservableStore({ network: currentNetworkId }),
txHistoryLimit: 10, txHistoryLimit: 10,
blockTracker: new EventEmitter(), blockTracker: new EventEmitter(),
signTransaction: (ethTx) => new Promise((resolve) => {
ethTx.sign(privKey)
resolve()
})
}) })
}) })
@ -27,7 +36,6 @@ describe('Transaction Manager', function() {
}) })
}) })
it('returns error for negative values', function() { it('returns error for negative values', function() {
var sample = { var sample = {
value: '-0x01' value: '-0x01'
@ -51,7 +59,7 @@ describe('Transaction Manager', function() {
describe('#addTx', function() { describe('#addTx', function() {
it('adds a tx returned in getTxList', function() { it('adds a tx returned in getTxList', function() {
var tx = { id: 1, status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} } var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx, noop) txManager.addTx(tx, noop)
var result = txManager.getTxList() var result = txManager.getTxList()
assert.ok(Array.isArray(result)) assert.ok(Array.isArray(result))
@ -60,8 +68,8 @@ describe('Transaction Manager', function() {
}) })
it('does not override txs from other networks', function() { it('does not override txs from other networks', function() {
var tx = { id: 1, status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} } var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
var tx2 = { id: 2, status: 'confirmed', metamaskNetworkId: 'another net', txParams: {} } var tx2 = { id: 2, status: 'confirmed', metamaskNetworkId: otherNetworkId, txParams: {} }
txManager.addTx(tx, noop) txManager.addTx(tx, noop)
txManager.addTx(tx2, noop) txManager.addTx(tx2, noop)
var result = txManager.getFullTxList() var result = txManager.getFullTxList()
@ -73,7 +81,7 @@ describe('Transaction Manager', function() {
it('cuts off early txs beyond a limit', function() { it('cuts off early txs beyond a limit', function() {
const limit = txManager.txHistoryLimit const limit = txManager.txHistoryLimit
for (let i = 0; i < limit + 1; i++) { for (let i = 0; i < limit + 1; i++) {
let tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} } let tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx, noop) txManager.addTx(tx, noop)
} }
var result = txManager.getTxList() var result = txManager.getTxList()
@ -84,7 +92,7 @@ describe('Transaction Manager', function() {
it('cuts off early txs beyond a limit whether or not it is confirmed or rejected', function() { it('cuts off early txs beyond a limit whether or not it is confirmed or rejected', function() {
const limit = txManager.txHistoryLimit const limit = txManager.txHistoryLimit
for (let i = 0; i < limit + 1; i++) { for (let i = 0; i < limit + 1; i++) {
let tx = { id: i, time: new Date(), status: 'rejected', metamaskNetworkId: 'unit test', txParams: {} } let tx = { id: i, time: new Date(), status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx, noop) txManager.addTx(tx, noop)
} }
var result = txManager.getTxList() var result = txManager.getTxList()
@ -93,11 +101,11 @@ describe('Transaction Manager', function() {
}) })
it('cuts off early txs beyond a limit but does not cut unapproved txs', function() { it('cuts off early txs beyond a limit but does not cut unapproved txs', function() {
var unconfirmedTx = { id: 0, time: new Date(), status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} } var unconfirmedTx = { id: 0, time: new Date(), status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(unconfirmedTx, noop) txManager.addTx(unconfirmedTx, noop)
const limit = txManager.txHistoryLimit const limit = txManager.txHistoryLimit
for (let i = 1; i < limit + 1; i++) { for (let i = 1; i < limit + 1; i++) {
let tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} } let tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx, noop) txManager.addTx(tx, noop)
} }
var result = txManager.getTxList() var result = txManager.getTxList()
@ -110,7 +118,7 @@ describe('Transaction Manager', function() {
describe('#setTxStatusSigned', function() { describe('#setTxStatusSigned', function() {
it('sets the tx status to signed', function() { it('sets the tx status to signed', function() {
var tx = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} } var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx, noop) txManager.addTx(tx, noop)
txManager.setTxStatusSigned(1) txManager.setTxStatusSigned(1)
var result = txManager.getTxList() var result = txManager.getTxList()
@ -121,7 +129,7 @@ describe('Transaction Manager', function() {
it('should emit a signed event to signal the exciton of callback', (done) => { it('should emit a signed event to signal the exciton of callback', (done) => {
this.timeout(10000) this.timeout(10000)
var tx = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} } var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
let noop = function () { let noop = function () {
assert(true, 'event listener has been triggered and noop executed') assert(true, 'event listener has been triggered and noop executed')
done() done()
@ -134,7 +142,7 @@ describe('Transaction Manager', function() {
describe('#setTxStatusRejected', function() { describe('#setTxStatusRejected', function() {
it('sets the tx status to rejected', function() { it('sets the tx status to rejected', function() {
var tx = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} } var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx) txManager.addTx(tx)
txManager.setTxStatusRejected(1) txManager.setTxStatusRejected(1)
var result = txManager.getTxList() var result = txManager.getTxList()
@ -145,7 +153,7 @@ describe('Transaction Manager', function() {
it('should emit a rejected event to signal the exciton of callback', (done) => { it('should emit a rejected event to signal the exciton of callback', (done) => {
this.timeout(10000) this.timeout(10000)
var tx = { id: 1, status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} } var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
txManager.addTx(tx) txManager.addTx(tx)
let noop = function (err, txId) { let noop = function (err, txId) {
assert(true, 'event listener has been triggered and noop executed') assert(true, 'event listener has been triggered and noop executed')
@ -159,9 +167,9 @@ describe('Transaction Manager', function() {
describe('#updateTx', function() { describe('#updateTx', function() {
it('replaces the tx with the same id', function() { it('replaces the tx with the same id', function() {
txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} }, noop) txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} }, noop) txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
txManager.updateTx({ id: '1', status: 'blah', hash: 'foo', metamaskNetworkId: 'unit test', txParams: {} }) txManager.updateTx({ id: '1', status: 'blah', hash: 'foo', metamaskNetworkId: currentNetworkId, txParams: {} })
var result = txManager.getTx('1') var result = txManager.getTx('1')
assert.equal(result.hash, 'foo') assert.equal(result.hash, 'foo')
}) })
@ -169,8 +177,8 @@ describe('Transaction Manager', function() {
describe('#getUnapprovedTxList', function() { describe('#getUnapprovedTxList', function() {
it('returns unapproved txs in a hash', function() { it('returns unapproved txs in a hash', function() {
txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} }, noop) txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} }, noop) txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
let result = txManager.getUnapprovedTxList() let result = txManager.getUnapprovedTxList()
assert.equal(typeof result, 'object') assert.equal(typeof result, 'object')
assert.equal(result['1'].status, 'unapproved') assert.equal(result['1'].status, 'unapproved')
@ -180,8 +188,8 @@ describe('Transaction Manager', function() {
describe('#getTx', function() { describe('#getTx', function() {
it('returns a tx with the requested id', function() { it('returns a tx with the requested id', function() {
txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: 'unit test', txParams: {} }, noop) txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: 'unit test', txParams: {} }, noop) txManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
assert.equal(txManager.getTx('1').status, 'unapproved') assert.equal(txManager.getTx('1').status, 'unapproved')
assert.equal(txManager.getTx('2').status, 'confirmed') assert.equal(txManager.getTx('2').status, 'confirmed')
}) })
@ -190,16 +198,16 @@ describe('Transaction Manager', function() {
describe('#getFilteredTxList', function() { describe('#getFilteredTxList', function() {
it('returns a tx with the requested data', function() { it('returns a tx with the requested data', function() {
let txMetas = [ let txMetas = [
{ id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: 'unit test' }, { id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
{ id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: 'unit test' }, { id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
{ id: 2, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: 'unit test' }, { id: 2, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
{ id: 3, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: 'unit test' }, { id: 3, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
{ id: 4, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: 'unit test' }, { id: 4, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
{ id: 5, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: 'unit test' }, { id: 5, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
{ id: 6, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: 'unit test' }, { id: 6, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
{ id: 7, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: 'unit test' }, { id: 7, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
{ id: 8, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: 'unit test' }, { id: 8, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
{ id: 9, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: 'unit test' }, { id: 9, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
] ]
txMetas.forEach((txMeta) => txManager.addTx(txMeta, noop)) txMetas.forEach((txMeta) => txManager.addTx(txMeta, noop))
let filterParams let filterParams
@ -219,4 +227,15 @@ describe('Transaction Manager', function() {
}) })
}) })
describe('#sign replay-protected tx', function() {
it('prepares a tx with the chainId set', function() {
txManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
txManager.signTransaction('1', (err, rawTx) => {
if (err) return assert.fail('it should not fail')
const ethTx = new EthTx(ethUtil.toBuffer(rawTx))
assert.equal(ethTx.getChainId(), currentNetworkId)
})
})
})
}) })

@ -12,6 +12,23 @@ describe('txUtils', function() {
txUtils = new TxUtils() txUtils = new TxUtils()
}) })
describe('chain Id', function() {
it('prepares a transaction with the provided chainId', function() {
const txParams = {
to: '0x70ad465e0bab6504002ad58c744ed89c7da38524',
from: '0x69ad465e0bab6504002ad58c744ed89c7da38525',
value: '0x0',
gas: '0x7b0c',
gasPrice: '0x199c82cc00',
data: '0x',
nonce: '0x3',
chainId: 42,
}
const ethTx = txUtils.buildEthTxFromParams(txParams)
assert.equal(ethTx.getChainId(), 42, 'chainId is set from tx params')
})
})
describe('addGasBuffer', function() { describe('addGasBuffer', function() {
it('multiplies by 1.5, when within block gas limit', function() { it('multiplies by 1.5, when within block gas limit', function() {
// naive estimatedGas: 0x16e360 (1.5 mil) // naive estimatedGas: 0x16e360 (1.5 mil)

Loading…
Cancel
Save