From 39a84c0e47f35bc0e7c2a632683dc9ae802900b3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 27 Apr 2016 14:43:09 -0700 Subject: [PATCH 1/4] Fix explorer link generation --- test/unit/explorer-link-test.js | 11 +++++++++++ ui/lib/explorer-link.js | 5 ++++- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/unit/explorer-link-test.js diff --git a/test/unit/explorer-link-test.js b/test/unit/explorer-link-test.js new file mode 100644 index 000000000..961b400fd --- /dev/null +++ b/test/unit/explorer-link-test.js @@ -0,0 +1,11 @@ +var assert = require('assert') +var linkGen = require('../../ui/lib/explorer-link') + +describe('explorer-link', function() { + + it('adds testnet prefix to morden test network', function() { + var result = linkGen('hash', '2') + assert.notEqual(result.indexOf('testnet'), -1, 'testnet injected') + }) + +}) diff --git a/ui/lib/explorer-link.js b/ui/lib/explorer-link.js index a2e7872f9..92b3376fa 100644 --- a/ui/lib/explorer-link.js +++ b/ui/lib/explorer-link.js @@ -1,10 +1,13 @@ module.exports = function(hash, network) { + const net = parseInt(network) let prefix - switch (network) { + switch (net) { case 1: // main net prefix = '' + break case 2: // morden test net prefix = 'testnet.' + break default: prefix = '' } From 29718a82b6bae5957b6f772585419ebdc2c851cb Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 27 Apr 2016 15:29:10 -0700 Subject: [PATCH 2/4] Record current network on each persisted transaction --- app/scripts/background.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 1519f63db..cf5d0ff26 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -11,6 +11,7 @@ const createTxNotification = require('./lib/tx-notification.js') const configManager = require('./lib/config-manager-singleton') const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex const HostStore = require('./lib/remote-store.js').HostStore +const Web3 = require('web3') // // connect to other contexts @@ -62,6 +63,7 @@ var providerOpts = { signTransaction: idStore.signTransaction.bind(idStore), } var provider = MetaMaskProvider(providerOpts) +var web3 = new Web3(provider) // log new blocks provider.on('block', function(block){ @@ -205,12 +207,18 @@ function updateBadge(state){ // function addUnconfirmedTx(txParams, cb){ - var txId = idStore.addUnconfirmedTransaction(txParams, cb) - createTxNotification({ - title: 'New Unsigned Transaction', - txParams: txParams, - confirm: idStore.approveTransaction.bind(idStore, txId, noop), - cancel: idStore.cancelTransaction.bind(idStore, txId), + + web3.version.getNetwork(function(err, network) { + if (err) return cb(err) + + txParams.metamaskNetworkId = network + var txId = idStore.addUnconfirmedTransaction(txParams, cb) + createTxNotification({ + title: 'New Unsigned Transaction', + txParams: txParams, + confirm: idStore.approveTransaction.bind(idStore, txId, noop), + cancel: idStore.cancelTransaction.bind(idStore, txId), + }) }) } From d017c2844165939f41613e1ae6141c8531422c44 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 27 Apr 2016 18:04:33 -0700 Subject: [PATCH 3/4] Filter transaction list for current blockchain network When starting up, we now create a `web3` inside the `background.js` process, which we pass to the `idStore` and ask for the current `network`. We include the `network` on `app.metamask.network` in the state object. We re-request the network when changing provider. We filter the transaction list for transactions that match the current network. --- app/scripts/background.js | 22 ++++++++++------------ app/scripts/lib/idStore.js | 15 +++++++++++++-- app/scripts/popup.js | 9 +-------- ui/app/account-detail.js | 3 ++- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index cf5d0ff26..172e3819d 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -52,6 +52,7 @@ function setupTrustedCommunication(connectionStream){ var providerConfig = configManager.getProvider() var idStore = new IdentityStore() + var providerOpts = { rpcUrl: configManager.getCurrentRpcAddress(), getAccounts: function(cb){ @@ -64,6 +65,8 @@ var providerOpts = { } var provider = MetaMaskProvider(providerOpts) var web3 = new Web3(provider) +idStore.web3 = web3 +idStore.getNetwork(3) // log new blocks provider.on('block', function(block){ @@ -207,18 +210,12 @@ function updateBadge(state){ // function addUnconfirmedTx(txParams, cb){ - - web3.version.getNetwork(function(err, network) { - if (err) return cb(err) - - txParams.metamaskNetworkId = network - var txId = idStore.addUnconfirmedTransaction(txParams, cb) - createTxNotification({ - title: 'New Unsigned Transaction', - txParams: txParams, - confirm: idStore.approveTransaction.bind(idStore, txId, noop), - cancel: idStore.cancelTransaction.bind(idStore, txId), - }) + var txId = idStore.addUnconfirmedTransaction(txParams, cb) + createTxNotification({ + title: 'New Unsigned Transaction', + txParams: txParams, + confirm: idStore.approveTransaction.bind(idStore, txId, noop), + cancel: idStore.cancelTransaction.bind(idStore, txId), }) } @@ -230,6 +227,7 @@ function addUnconfirmedTx(txParams, cb){ function setRpcTarget(rpcTarget){ configManager.setRpcTarget(rpcTarget) chrome.runtime.reload() + idStore.getNetwork(3) // 3 retry attempts } function useEtherscanProvider() { diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index 92d0f9668..e9aaed82e 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -16,11 +16,11 @@ module.exports = IdentityStore inherits(IdentityStore, EventEmitter) -function IdentityStore(ethStore) { +function IdentityStore(opts = {}) { EventEmitter.call(this) // we just use the ethStore to auto-add accounts - this._ethStore = ethStore + this._ethStore = opts.ethStore // lightwallet key store this._keyStore = null // lightwallet wrapper @@ -110,6 +110,16 @@ IdentityStore.prototype.setSelectedAddress = function(address){ this._didUpdate() } +IdentityStore.prototype.getNetwork = function(tries) { + if (tries === 0) return + this.web3.version.getNetwork((err, network) => { + if (err) { + return this.getNetwork(tries - 1, cb) + } + this._currentState.network = network + }) +} + IdentityStore.prototype.setLocked = function(cb){ delete this._keyStore delete this._idmgmt @@ -137,6 +147,7 @@ IdentityStore.prototype.addUnconfirmedTransaction = function(txParams, cb){ var time = (new Date()).getTime() var txId = createId() txParams.metamaskId = txId + txParams.metamaskNetworkId = this._currentState.network var txData = { id: txId, txParams: txParams, diff --git a/app/scripts/popup.js b/app/scripts/popup.js index 6a39da661..e9ca7cd71 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -17,7 +17,7 @@ injectCss(css) async.parallel({ currentDomain: getCurrentDomain, accountManager: connectToAccountManager, -}, getNetworkVersion) +}, setupApp) function connectToAccountManager(cb){ // setup communication with background @@ -65,13 +65,6 @@ function getCurrentDomain(cb){ }) } -function getNetworkVersion(cb, results) { - web3.version.getNetwork(function(err, result) { - results.networkVersion = result - setupApp(err, results) - }) -} - function setupApp(err, opts){ if (err) { alert(err.stack) diff --git a/ui/app/account-detail.js b/ui/app/account-detail.js index 57f932a2b..250e7318c 100644 --- a/ui/app/account-detail.js +++ b/ui/app/account-detail.js @@ -16,7 +16,7 @@ function mapStateToProps(state) { address: state.appState.currentView.context, accountDetail: state.appState.accountDetail, transactions: state.metamask.transactions, - networkVersion: state.networkVersion, + networkVersion: state.metamask.network, } } @@ -79,6 +79,7 @@ AccountDetailScreen.prototype.render = function() { transactionList(transactions .filter(tx => tx.txParams.from === state.address) + .filter(tx => tx.txParams.metamaskNetworkId === state.networkVersion) .sort((a, b) => b.time - a.time), state.networkVersion), this.exportedAccount(accountDetail), From bd660d9aeb5638372605377fb92ce1362c3d8230 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Wed, 27 Apr 2016 18:14:59 -0700 Subject: [PATCH 4/4] Fix test --- test/unit/idStore-test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/unit/idStore-test.js b/test/unit/idStore-test.js index d3fabfe9d..e9611d7e8 100644 --- a/test/unit/idStore-test.js +++ b/test/unit/idStore-test.js @@ -15,7 +15,9 @@ describe('IdentityStore', function() { window.localStorage = {} // Hacking localStorage support into JSDom idStore = new IdentityStore({ - addAccount(acct) { accounts.push(acct) }, + ethStore: { + addAccount(acct) { accounts.push(acct) }, + }, }) idStore.createNewVault(password, entropy, (err, seeds) => { @@ -32,7 +34,9 @@ describe('IdentityStore', function() { window.localStorage = {} // Hacking localStorage support into JSDom idStore = new IdentityStore({ - addAccount(acct) { newAccounts.push(acct) }, + ethStore: { + addAccount(acct) { newAccounts.push(acct) }, + }, }) }) @@ -61,8 +65,8 @@ describe('IdentityStore', function() { window.localStorage = {} // Hacking localStorage support into JSDom idStore = new IdentityStore({ - addAccount(acct) { - accounts.push(acct) + ethStore: { + addAccount(acct) { accounts.push(acct) }, }, }) })