From 5ef80495cfd47a8f5e4caf4b16842155420de62e Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Fri, 10 Aug 2018 21:54:34 -0400 Subject: refactor to support multiple hw wallets --- app/scripts/eth-ledger-keyring-listener.js | 105 +++++++++++++++++++++++ app/scripts/metamask-controller.js | 130 ++++++++++++----------------- 2 files changed, 160 insertions(+), 75 deletions(-) create mode 100644 app/scripts/eth-ledger-keyring-listener.js (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js new file mode 100644 index 000000000..a6db49772 --- /dev/null +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -0,0 +1,105 @@ + const extension = require('extensionizer') +const {EventEmitter} = require('events') + + +// HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger +const hdPathString = `m/44'/60'/0'` +const type = 'Ledger Hardware Keyring' + +class LedgerKeyring extends EventEmitter { + constructor (opts = {}) { + super() + this.type = type + this.deserialize(opts) + } + + serialize () { + return Promise.resolve({hdPath: this.hdPath, accounts: this.accounts}) + } + + deserialize (opts = {}) { + this.hdPath = opts.hdPath || hdPathString + this.accounts = opts.accounts || [] + return Promise.resolve() + } + + async addAccounts (n = 1) { + return new Promise((resolve, reject) => { + extension.runtime.sendMessage({ + action: 'ledger-add-account', + n, + }) + + extension.runtime.onMessage.addListener(({action, success, payload}) => { + if (action === 'ledger-sign-transaction') { + if (success) { + resolve(payload) + } else { + reject(payload) + } + } + }) + }) + } + + async getAccounts () { + return this.accounts.slice() + } + + // tx is an instance of the ethereumjs-transaction class. + async signTransaction (address, tx) { + return new Promise((resolve, reject) => { + extension.runtime.sendMessage({ + action: 'ledger-sign-transaction', + address, + tx, + }) + + extension.runtime.onMessage.addListener(({action, success, payload}) => { + if (action === 'ledger-sign-transaction') { + if (success) { + resolve(payload) + } else { + reject(payload) + } + } + }) + }) + } + + async signMessage (withAccount, data) { + throw new Error('Not supported on this device') + } + + // For personal_sign, we need to prefix the message: + async signPersonalMessage (withAccount, message) { + return new Promise((resolve, reject) => { + extension.runtime.sendMessage({ + action: 'ledger-sign-personal-message', + withAccount, + message, + }) + + extension.runtime.onMessage.addListener(({action, success, payload}) => { + if (action === 'ledger-sign-personal-message') { + if (success) { + resolve(payload) + } else { + reject(payload) + } + } + }) + }) + } + + async signTypedData (withAccount, typedData) { + throw new Error('Not supported on this device') + } + + async exportAccount (address) { + throw new Error('Not supported on this device') + } +} + +LedgerKeyring.type = type +module.exports = LedgerKeyring diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index db323e3fe..593b722ff 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -49,6 +49,7 @@ const seedPhraseVerifier = require('./lib/seed-phrase-verifier') const cleanErrorStack = require('./lib/cleanErrorStack') const log = require('loglevel') const TrezorKeyring = require('eth-trezor-keyring') +const LedgerKeyring = require('./eth-ledger-keyring-listener') module.exports = class MetamaskController extends EventEmitter { @@ -127,7 +128,7 @@ module.exports = class MetamaskController extends EventEmitter { }) // key mgmt - const additionalKeyrings = [TrezorKeyring] + const additionalKeyrings = [TrezorKeyring, LedgerKeyring] this.keyringController = new KeyringController({ keyringTypes: additionalKeyrings, initState: initState.KeyringController, @@ -377,9 +378,7 @@ module.exports = class MetamaskController extends EventEmitter { connectHardware: nodeify(this.connectHardware, this), forgetDevice: nodeify(this.forgetDevice, this), checkHardwareStatus: nodeify(this.checkHardwareStatus, this), - - // TREZOR - unlockTrezorAccount: nodeify(this.unlockTrezorAccount, this), + unlockHardwareWalletAccount: nodeify(this.unlockHardwareWalletAccount, this), // vault management submitPassword: nodeify(this.submitPassword, this), @@ -540,6 +539,28 @@ module.exports = class MetamaskController extends EventEmitter { // Hardware // + async getKeyringForDevice (deviceName) { + let keyringName = null + switch (deviceName) { + case 'trezor': + keyringName = TrezorKeyring.type + break + case 'ledger': + keyringName = TrezorKeyring.type + break + default: + throw new Error('MetamaskController:connectHardware - Unknown device') + } + + let keyring = await this.keyringController.getKeyringsByType(keyringName)[0] + if (!keyring) { + keyring = await this.keyringController.addNewKeyring(keyringName) + } + + return keyring + + } + /** * Fetch account list from a trezor device. * @@ -547,38 +568,26 @@ module.exports = class MetamaskController extends EventEmitter { */ async connectHardware (deviceName, page) { - switch (deviceName) { - case 'trezor': - const keyringController = this.keyringController - const oldAccounts = await keyringController.getAccounts() - let keyring = await keyringController.getKeyringsByType( - 'Trezor Hardware' - )[0] - if (!keyring) { - keyring = await this.keyringController.addNewKeyring('Trezor Hardware') - } - let accounts = [] - - switch (page) { - case -1: - accounts = await keyring.getPreviousPage() - break - case 1: - accounts = await keyring.getNextPage() - break - default: - accounts = await keyring.getFirstPage() - } - - // Merge with existing accounts - // and make sure addresses are not repeated - const accountsToTrack = [...new Set(oldAccounts.concat(accounts.map(a => a.address.toLowerCase())))] - this.accountTracker.syncWithAddresses(accountsToTrack) - return accounts - - default: - throw new Error('MetamaskController:connectHardware - Unknown device') + const oldAccounts = await this.keyringController.getAccounts() + const keyring = await this.getKeyringForDevice(deviceName) + let accounts = [] + + switch (page) { + case -1: + accounts = await keyring.getPreviousPage() + break + case 1: + accounts = await keyring.getNextPage() + break + default: + accounts = await keyring.getFirstPage() } + + // Merge with existing accounts + // and make sure addresses are not repeated + const accountsToTrack = [...new Set(oldAccounts.concat(accounts.map(a => a.address.toLowerCase())))] + this.accountTracker.syncWithAddresses(accountsToTrack) + return accounts } /** @@ -587,20 +596,8 @@ module.exports = class MetamaskController extends EventEmitter { * @returns {Promise} */ async checkHardwareStatus (deviceName) { - - switch (deviceName) { - case 'trezor': - const keyringController = this.keyringController - const keyring = await keyringController.getKeyringsByType( - 'Trezor Hardware' - )[0] - if (!keyring) { - return false - } - return keyring.isUnlocked() - default: - throw new Error('MetamaskController:checkHardwareStatus - Unknown device') - } + const keyring = await this.getKeyringForDevice(deviceName) + return keyring.isUnlocked() } /** @@ -610,20 +607,9 @@ module.exports = class MetamaskController extends EventEmitter { */ async forgetDevice (deviceName) { - switch (deviceName) { - case 'trezor': - const keyringController = this.keyringController - const keyring = await keyringController.getKeyringsByType( - 'Trezor Hardware' - )[0] - if (!keyring) { - throw new Error('MetamaskController:forgetDevice - Trezor Hardware keyring not found') - } - keyring.forgetDevice() - return true - default: - throw new Error('MetamaskController:forgetDevice - Unknown device') - } + const keyring = await this.getKeyringForDevice(deviceName) + keyring.forgetDevice() + return true } /** @@ -631,23 +617,17 @@ module.exports = class MetamaskController extends EventEmitter { * * @returns {} keyState */ - async unlockTrezorAccount (index) { - const keyringController = this.keyringController - const keyring = await keyringController.getKeyringsByType( - 'Trezor Hardware' - )[0] - if (!keyring) { - throw new Error('MetamaskController - No Trezor Hardware Keyring found') - } + async unlockHardwareWalletAccount (deviceName, index) { + const keyring = await this.getKeyringForDevice(deviceName) keyring.setAccountToUnlock(index) - const oldAccounts = await keyringController.getAccounts() - const keyState = await keyringController.addNewAccount(keyring) - const newAccounts = await keyringController.getAccounts() + const oldAccounts = await this.keyringController.getAccounts() + const keyState = await this.keyringController.addNewAccount(keyring) + const newAccounts = await this.keyringController.getAccounts() this.preferencesController.setAddresses(newAccounts) newAccounts.forEach(address => { if (!oldAccounts.includes(address)) { - this.preferencesController.setAccountLabel(address, `TREZOR #${parseInt(index, 10) + 1}`) + this.preferencesController.setAccountLabel(address, `${deviceName.toUpperCase()} #${parseInt(index, 10) + 1}`) this.preferencesController.setSelectedAddress(address) } }) -- cgit From 78a1cd3314455d6e4e08839a3eea3ec2868f4f59 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 02:35:20 -0400 Subject: iframe communication working --- app/scripts/background.js | 3 +- app/scripts/contentscript.js | 2 +- app/scripts/eth-ledger-keyring-listener.js | 203 +++++++++++++++++++++++------ app/scripts/lib/setupLedgerIframe.js | 40 ++++++ app/scripts/metamask-controller.js | 6 +- 5 files changed, 208 insertions(+), 46 deletions(-) create mode 100644 app/scripts/lib/setupLedgerIframe.js (limited to 'app/scripts') diff --git a/app/scripts/background.js b/app/scripts/background.js index 3d3afdd4e..bff559469 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -68,8 +68,6 @@ initialize().catch(log.error) // setup metamask mesh testing container setupMetamaskMeshMetrics() - - /** * An object representing a transaction, in whatever state it is in. * @typedef TransactionMeta @@ -446,3 +444,4 @@ extension.runtime.onInstalled.addListener(function (details) { extension.tabs.create({url: 'https://metamask.io/#how-it-works'}) } }) + diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index e0a2b0061..01df9e327 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -200,4 +200,4 @@ function redirectToPhishingWarning () { console.log('MetaMask - routing to Phishing Warning component') const extensionURL = extension.runtime.getURL('phishing.html') window.location.href = extensionURL -} +} \ No newline at end of file diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index a6db49772..1c02f0610 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -1,68 +1,182 @@ - const extension = require('extensionizer') +const extension = require('extensionizer') const {EventEmitter} = require('events') // HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger const hdPathString = `m/44'/60'/0'` const type = 'Ledger Hardware Keyring' +const ORIGIN = 'http://localhost:9000' class LedgerKeyring extends EventEmitter { constructor (opts = {}) { super() this.type = type + this.page = 0 + this.perPage = 5 + this.unlockedAccount = 0 + this.paths = {} + this.iframe = null + this.setupIframe() this.deserialize(opts) } + setupIframe(){ + this.iframe = document.createElement('iframe') + this.iframe.src = ORIGIN + console.log('Injecting ledger iframe') + document.head.appendChild(this.iframe) + + + /* + Passing messages from iframe to background script + */ + console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY') + + } + + sendMessage(msg, cb) { + console.log('[LEDGER]: SENDING MESSAGE TO IFRAME', msg) + this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') + window.addEventListener('message', event => { + if(event.origin !== ORIGIN) return false + if (event.data && event.data.action && event.data.action.search(name) !== -1) { + console.log('[LEDGER]: GOT MESAGE FROM IFRAME', event.data) + cb(event.data) + } + }) + } + serialize () { return Promise.resolve({hdPath: this.hdPath, accounts: this.accounts}) } deserialize (opts = {}) { this.hdPath = opts.hdPath || hdPathString + this.unlocked = opts.unlocked || false this.accounts = opts.accounts || [] return Promise.resolve() } - async addAccounts (n = 1) { + isUnlocked () { + return this.unlocked + } + + setAccountToUnlock (index) { + this.unlockedAccount = parseInt(index, 10) + } + + unlock () { + + if (this.isUnlocked()) return Promise.resolve('already unlocked') + return new Promise((resolve, reject) => { - extension.runtime.sendMessage({ - action: 'ledger-add-account', - n, + this.sendMessage({ + action: 'ledger-unlock', + params: { + hdPath: this.hdPath, + }, + }, + ({action, success, payload}) => { + if (success) { + resolve(payload) + } else { + reject(payload) + } }) + }) + } - extension.runtime.onMessage.addListener(({action, success, payload}) => { - if (action === 'ledger-sign-transaction') { + async addAccounts (n = 1) { + return new Promise((resolve, reject) => { + this.unlock() + .then(_ => { + this.sendMessage({ + action: 'ledger-add-account', + params: { + n, + }, + }, + ({action, success, payload}) => { if (success) { resolve(payload) } else { reject(payload) - } - } + } + }) }) }) } - async getAccounts () { - return this.accounts.slice() + getFirstPage () { + this.page = 0 + return this.__getPage(1) } - // tx is an instance of the ethereumjs-transaction class. - async signTransaction (address, tx) { + getNextPage () { + return this.__getPage(1) + } + + getPreviousPage () { + return this.__getPage(-1) + } + + __getPage (increment) { + + this.page += increment + + if (this.page <= 0) { this.page = 1 } + return new Promise((resolve, reject) => { - extension.runtime.sendMessage({ - action: 'ledger-sign-transaction', - address, - tx, + this.unlock() + .then(_ => { + this.sendMessage({ + action: 'ledger-get-page', + params: { + page: this.page, + }, + }, + ({action, success, payload}) => { + if (success) { + resolve(payload) + } else { + reject(payload) + } + }) }) + }) + } - extension.runtime.onMessage.addListener(({action, success, payload}) => { - if (action === 'ledger-sign-transaction') { - if (success) { - resolve(payload) - } else { - reject(payload) - } - } + getAccounts () { + return Promise.resolve(this.accounts.slice()) + } + + removeAccount (address) { + if (!this.accounts.map(a => a.toLowerCase()).includes(address.toLowerCase())) { + throw new Error(`Address ${address} not found in this keyring`) + } + this.accounts = this.accounts.filter(a => a.toLowerCase() !== address.toLowerCase()) + } + + // tx is an instance of the ethereumjs-transaction class. + async signTransaction (address, tx) { + return new Promise((resolve, reject) => { + this.unlock() + .then(_ => { + console.log('[LEDGER]: sending message ', 'ledger-sign-transaction') + this.sendMessage({ + action: 'ledger-sign-transaction', + params: { + address, + tx, + }, + }, + ({action, success, payload}) => { + if (success) { + resolve(payload) + } else { + reject(payload) + } + }) }) }) } @@ -74,20 +188,23 @@ class LedgerKeyring extends EventEmitter { // For personal_sign, we need to prefix the message: async signPersonalMessage (withAccount, message) { return new Promise((resolve, reject) => { - extension.runtime.sendMessage({ - action: 'ledger-sign-personal-message', - withAccount, - message, - }) - - extension.runtime.onMessage.addListener(({action, success, payload}) => { - if (action === 'ledger-sign-personal-message') { - if (success) { - resolve(payload) - } else { - reject(payload) - } - } + this.unlock() + .then(_ => { + console.log('[LEDGER]: sending message ', 'ledger-sign-personal-message') + this.sendMessage({ + action: 'ledger-sign-personal-message', + params: { + withAccount, + message, + }, + }, + ({action, success, payload}) => { + if (success) { + resolve(payload) + } else { + reject(payload) + } + }) }) }) } @@ -99,6 +216,14 @@ class LedgerKeyring extends EventEmitter { async exportAccount (address) { throw new Error('Not supported on this device') } + + forgetDevice () { + this.accounts = [] + this.unlocked = false + this.page = 0 + this.unlockedAccount = 0 + this.paths = {} + } } LedgerKeyring.type = type diff --git a/app/scripts/lib/setupLedgerIframe.js b/app/scripts/lib/setupLedgerIframe.js new file mode 100644 index 000000000..2831d072e --- /dev/null +++ b/app/scripts/lib/setupLedgerIframe.js @@ -0,0 +1,40 @@ +const extension = require('extensionizer') +module.exports = setupLedgerIframe +/** + * Injects an iframe into the current document to + * enable the interaction with ledger devices + */ +function setupLedgerIframe () { + const ORIGIN = 'http://localhost:9000' + const ledgerIframe = document.createElement('iframe') + ledgerIframe.src = ORIGIN + console.log('Injecting ledger iframe') + document.head.appendChild(ledgerIframe) + + console.log('[LEDGER]: LEDGER BG LISTENER READY') + extension.runtime.onMessage.addListener(({action, params}) => { + console.log('[LEDGER]: GOT MSG FROM THE KEYRING', action, params) + if (action.search('ledger-') !== -1) { + //Forward messages from the keyring to the iframe + sendMessage({action, params}) + } + }) + + function sendMessage(msg) { + ledgerIframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') + } + + /* + Passing messages from iframe to background script + */ + console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY') + window.addEventListener('message', event => { + if(event.origin !== ORIGIN) return false + if (event.data && event.data.action && event.data.action.search('ledger-') !== -1) { + // Forward messages from the iframe to the keyring + console.log('[LEDGER] : forwarding msg', event.data) + extension.runtime.sendMessage(event.data) + } + }) + + } \ No newline at end of file diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 593b722ff..58bab9789 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -546,12 +546,11 @@ module.exports = class MetamaskController extends EventEmitter { keyringName = TrezorKeyring.type break case 'ledger': - keyringName = TrezorKeyring.type + keyringName = LedgerKeyring.type break default: throw new Error('MetamaskController:connectHardware - Unknown device') } - let keyring = await this.keyringController.getKeyringsByType(keyringName)[0] if (!keyring) { keyring = await this.keyringController.addNewKeyring(keyringName) @@ -568,10 +567,8 @@ module.exports = class MetamaskController extends EventEmitter { */ async connectHardware (deviceName, page) { - const oldAccounts = await this.keyringController.getAccounts() const keyring = await this.getKeyringForDevice(deviceName) let accounts = [] - switch (page) { case -1: accounts = await keyring.getPreviousPage() @@ -585,6 +582,7 @@ module.exports = class MetamaskController extends EventEmitter { // Merge with existing accounts // and make sure addresses are not repeated + const oldAccounts = await this.keyringController.getAccounts() const accountsToTrack = [...new Set(oldAccounts.concat(accounts.map(a => a.address.toLowerCase())))] this.accountTracker.syncWithAddresses(accountsToTrack) return accounts -- cgit From 8e842a8947997b7ea47cf35d1348788d21f2f467 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 05:02:02 -0400 Subject: able to add accounts --- app/scripts/eth-ledger-keyring-listener.js | 139 +++++++++++++++++++++-------- app/scripts/metamask-controller.js | 2 +- 2 files changed, 104 insertions(+), 37 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index 1c02f0610..b9b8eb3c8 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -1,11 +1,16 @@ const extension = require('extensionizer') const {EventEmitter} = require('events') +const HDKey = require('hdkey') +const ethUtil = require('ethereumjs-util') +const sigUtil = require('eth-sig-util') +const Transaction = require('ethereumjs-tx') // HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger const hdPathString = `m/44'/60'/0'` const type = 'Ledger Hardware Keyring' -const ORIGIN = 'http://localhost:9000' +const ORIGIN = 'https://localhost:3000' +const pathBase = 'm' class LedgerKeyring extends EventEmitter { constructor (opts = {}) { @@ -14,6 +19,7 @@ class LedgerKeyring extends EventEmitter { this.page = 0 this.perPage = 5 this.unlockedAccount = 0 + this.hdk = new HDKey() this.paths = {} this.iframe = null this.setupIframe() @@ -26,10 +32,6 @@ class LedgerKeyring extends EventEmitter { console.log('Injecting ledger iframe') document.head.appendChild(this.iframe) - - /* - Passing messages from iframe to background script - */ console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY') } @@ -78,32 +80,39 @@ class LedgerKeyring extends EventEmitter { }, ({action, success, payload}) => { if (success) { - resolve(payload) + this.hdk.publicKey = new Buffer(payload.publicKey, 'hex') + this.hdk.chainCode = new Buffer(payload.chainCode, 'hex') + resolve('just unlocked') } else { - reject(payload) + reject(payload.error || 'Unknown error') } }) }) } - async addAccounts (n = 1) { + setAccountToUnlock (index) { + this.unlockedAccount = parseInt(index, 10) + } + + addAccounts (n = 1) { + return new Promise((resolve, reject) => { this.unlock() - .then(_ => { - this.sendMessage({ - action: 'ledger-add-account', - params: { - n, - }, - }, - ({action, success, payload}) => { - if (success) { - resolve(payload) - } else { - reject(payload) - } + .then(_ => { + const from = this.unlockedAccount + const to = from + n + this.accounts = [] + + for (let i = from; i < to; i++) { + const address = this._addressFromIndex(pathBase, i) + this.accounts.push(address) + this.page = 0 + } + resolve(this.accounts) + }) + .catch(e => { + reject(e) }) - }) }) } @@ -129,20 +138,27 @@ class LedgerKeyring extends EventEmitter { return new Promise((resolve, reject) => { this.unlock() .then(_ => { - this.sendMessage({ - action: 'ledger-get-page', - params: { - page: this.page, - }, - }, - ({action, success, payload}) => { - if (success) { - resolve(payload) - } else { - reject(payload) - } - }) - }) + + const from = (this.page - 1) * this.perPage + const to = from + this.perPage + + const accounts = [] + + for (let i = from; i < to; i++) { + const address = this._addressFromIndex(pathBase, i) + accounts.push({ + address: address, + balance: null, + index: i, + }) + this.paths[ethUtil.toChecksumAddress(address)] = i + + } + resolve(accounts) + }) + .catch(e => { + reject(e) + }) }) } @@ -157,6 +173,7 @@ class LedgerKeyring extends EventEmitter { this.accounts = this.accounts.filter(a => a.toLowerCase() !== address.toLowerCase()) } + // tx is an instance of the ethereumjs-transaction class. async signTransaction (address, tx) { return new Promise((resolve, reject) => { @@ -224,6 +241,56 @@ class LedgerKeyring extends EventEmitter { this.unlockedAccount = 0 this.paths = {} } + + /* PRIVATE METHODS */ + + _padLeftEven (hex) { + return hex.length % 2 !== 0 ? `0${hex}` : hex + } + + _normalize (buf) { + return this._padLeftEven(ethUtil.bufferToHex(buf).substring(2).toLowerCase()) + } + + _addressFromIndex (pathBase, i) { + const dkey = this.hdk.derive(`${pathBase}/${i}`) + const address = ethUtil + .publicToAddress(dkey.publicKey, true) + .toString('hex') + return ethUtil.toChecksumAddress(address) + } + + _pathFromAddress (address) { + const checksummedAddress = ethUtil.toChecksumAddress(address) + let index = this.paths[checksummedAddress] + if (typeof index === 'undefined') { + for (let i = 0; i < MAX_INDEX; i++) { + if (checksummedAddress === this._addressFromIndex(pathBase, i)) { + index = i + break + } + } + } + + if (typeof index === 'undefined') { + throw new Error('Unknown address') + } + return `${this.hdPath}/${index}` + } + + _toAscii (hex) { + let str = '' + let i = 0; const l = hex.length + if (hex.substring(0, 2) === '0x') { + i = 2 + } + for (; i < l; i += 2) { + const code = parseInt(hex.substr(i, 2), 16) + str += String.fromCharCode(code) + } + + return str + } } LedgerKeyring.type = type diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 58bab9789..c79e5141e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -615,7 +615,7 @@ module.exports = class MetamaskController extends EventEmitter { * * @returns {} keyState */ - async unlockHardwareWalletAccount (deviceName, index) { + async unlockHardwareWalletAccount (index, deviceName) { const keyring = await this.getKeyringForDevice(deviceName) keyring.setAccountToUnlock(index) -- cgit From aa6a42e3deda1725cb5e4e0df97549f9facc18f5 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 05:11:21 -0400 Subject: rename keyring --- app/scripts/eth-ledger-keyring-listener.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index b9b8eb3c8..1b30c4d6d 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -8,7 +8,7 @@ const Transaction = require('ethereumjs-tx') // HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger const hdPathString = `m/44'/60'/0'` -const type = 'Ledger Hardware Keyring' +const type = 'Ledger Hardware' const ORIGIN = 'https://localhost:3000' const pathBase = 'm' -- cgit From 011cc141b3f2971522a1d77cea7370f61bbc08d1 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 16:00:29 -0400 Subject: tx signing should work --- app/scripts/eth-ledger-keyring-listener.js | 49 ++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 10 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index 1b30c4d6d..ad214138f 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -7,10 +7,11 @@ const Transaction = require('ethereumjs-tx') // HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger -const hdPathString = `m/44'/60'/0'` +const hdPathString = `44'/60'/0'` const type = 'Ledger Hardware' const ORIGIN = 'https://localhost:3000' const pathBase = 'm' +const MAX_INDEX = 1000 class LedgerKeyring extends EventEmitter { constructor (opts = {}) { @@ -39,11 +40,11 @@ class LedgerKeyring extends EventEmitter { sendMessage(msg, cb) { console.log('[LEDGER]: SENDING MESSAGE TO IFRAME', msg) this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') - window.addEventListener('message', event => { - if(event.origin !== ORIGIN) return false - if (event.data && event.data.action && event.data.action.search(name) !== -1) { - console.log('[LEDGER]: GOT MESAGE FROM IFRAME', event.data) - cb(event.data) + window.addEventListener('message', ({ origin, data }) => { + if(origin !== ORIGIN) return false + if (data && data.action && data.action === `${msg.action}-reply`) { + console.log('[LEDGER]: GOT MESAGE FROM IFRAME', data) + cb(data) } }) } @@ -176,20 +177,41 @@ class LedgerKeyring extends EventEmitter { // tx is an instance of the ethereumjs-transaction class. async signTransaction (address, tx) { + return new Promise((resolve, reject) => { this.unlock() .then(_ => { console.log('[LEDGER]: sending message ', 'ledger-sign-transaction') + this.sendMessage({ action: 'ledger-sign-transaction', params: { - address, - tx, + tx: { + to: this._normalize(tx.to), + value: this._normalize(tx.value), + data: this._normalize(tx.data), + chainId: tx._chainId, + nonce: this._fixNonce(this._normalize(tx.nonce)), + gasLimit: this._normalize(tx.gasLimit), + gasPrice: this._normalize(tx.gasPrice), + }, + path: this._pathFromAddress(address) }, }, ({action, success, payload}) => { if (success) { - resolve(payload) + console.log('[LEDGER]: got tx signed!', payload.txData) + const signedTx = new Transaction(payload.txData) + // Validate that the signature matches the right address + const addressSignedWith = ethUtil.toChecksumAddress(`0x${signedTx.from.toString('hex')}`) + const correctAddress = ethUtil.toChecksumAddress(address) + if (addressSignedWith !== correctAddress) { + reject('signature doesnt match the right address') + } + console.log('[LEDGER]: all good!', signedTx.toJSON()) + console.log('[LEDGER]: signedTX', `0x${signedTx.serialize().toString('hex')}`) + + resolve(signedTx) } else { reject(payload) } @@ -249,7 +271,7 @@ class LedgerKeyring extends EventEmitter { } _normalize (buf) { - return this._padLeftEven(ethUtil.bufferToHex(buf).substring(2).toLowerCase()) + return this._padLeftEven(ethUtil.bufferToHex(buf).toLowerCase()) } _addressFromIndex (pathBase, i) { @@ -291,6 +313,13 @@ class LedgerKeyring extends EventEmitter { return str } + + _fixNonce(nonce){ + if(nonce === '0x'){ + return `${nonce}0` + } + return nonce + } } LedgerKeyring.type = type -- cgit From 12b41b8fc213b930eb96f77f49157e78b141ff43 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 18:20:50 -0400 Subject: tx signature is valid --- app/scripts/eth-ledger-keyring-listener.js | 1 + 1 file changed, 1 insertion(+) (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index ad214138f..92c85f600 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -187,6 +187,7 @@ class LedgerKeyring extends EventEmitter { action: 'ledger-sign-transaction', params: { tx: { + from: this._normalize(address), to: this._normalize(tx.to), value: this._normalize(tx.value), data: this._normalize(tx.data), -- cgit From 068bf43615fa0d0038f43798fbf9e04d03369e68 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 19:27:24 -0400 Subject: working --- app/scripts/controllers/transactions/index.js | 4 ++-- app/scripts/eth-ledger-keyring-listener.js | 34 ++++++++++----------------- 2 files changed, 14 insertions(+), 24 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 8e2288aed..7ac6ec2e6 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -289,10 +289,10 @@ class TransactionController extends EventEmitter { // sign tx const fromAddress = txParams.from const ethTx = new Transaction(txParams) - await this.signEthTx(ethTx, fromAddress) + const signedTx = await this.signEthTx(ethTx, fromAddress) // set state to signed this.txStateManager.setTxStatusSigned(txMeta.id) - const rawTx = ethUtil.bufferToHex(ethTx.serialize()) + const rawTx = ethUtil.bufferToHex(signedTx.serialize()) return rawTx } diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index 92c85f600..0043058e6 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -9,7 +9,7 @@ const Transaction = require('ethereumjs-tx') // HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger const hdPathString = `44'/60'/0'` const type = 'Ledger Hardware' -const ORIGIN = 'https://localhost:3000' +const ORIGIN = 'https://localhost:3000' const pathBase = 'm' const MAX_INDEX = 1000 @@ -27,21 +27,18 @@ class LedgerKeyring extends EventEmitter { this.deserialize(opts) } - setupIframe(){ + setupIframe () { this.iframe = document.createElement('iframe') this.iframe.src = ORIGIN console.log('Injecting ledger iframe') document.head.appendChild(this.iframe) - - console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY') - } - sendMessage(msg, cb) { + sendMessage (msg, cb) { console.log('[LEDGER]: SENDING MESSAGE TO IFRAME', msg) this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') window.addEventListener('message', ({ origin, data }) => { - if(origin !== ORIGIN) return false + if (origin !== ORIGIN) return false if (data && data.action && data.action === `${msg.action}-reply`) { console.log('[LEDGER]: GOT MESAGE FROM IFRAME', data) cb(data) @@ -79,7 +76,7 @@ class LedgerKeyring extends EventEmitter { hdPath: this.hdPath, }, }, - ({action, success, payload}) => { + ({action, success, payload}) => { if (success) { this.hdk.publicKey = new Buffer(payload.publicKey, 'hex') this.hdk.chainCode = new Buffer(payload.chainCode, 'hex') @@ -177,12 +174,10 @@ class LedgerKeyring extends EventEmitter { // tx is an instance of the ethereumjs-transaction class. async signTransaction (address, tx) { - + return new Promise((resolve, reject) => { this.unlock() .then(_ => { - console.log('[LEDGER]: sending message ', 'ledger-sign-transaction') - this.sendMessage({ action: 'ledger-sign-transaction', params: { @@ -196,26 +191,22 @@ class LedgerKeyring extends EventEmitter { gasLimit: this._normalize(tx.gasLimit), gasPrice: this._normalize(tx.gasPrice), }, - path: this._pathFromAddress(address) + path: this._pathFromAddress(address), }, }, ({action, success, payload}) => { if (success) { - console.log('[LEDGER]: got tx signed!', payload.txData) const signedTx = new Transaction(payload.txData) // Validate that the signature matches the right address const addressSignedWith = ethUtil.toChecksumAddress(`0x${signedTx.from.toString('hex')}`) const correctAddress = ethUtil.toChecksumAddress(address) if (addressSignedWith !== correctAddress) { reject('signature doesnt match the right address') - } - console.log('[LEDGER]: all good!', signedTx.toJSON()) - console.log('[LEDGER]: signedTX', `0x${signedTx.serialize().toString('hex')}`) - + } resolve(signedTx) } else { reject(payload) - } + } }) }) }) @@ -230,7 +221,6 @@ class LedgerKeyring extends EventEmitter { return new Promise((resolve, reject) => { this.unlock() .then(_ => { - console.log('[LEDGER]: sending message ', 'ledger-sign-personal-message') this.sendMessage({ action: 'ledger-sign-personal-message', params: { @@ -243,7 +233,7 @@ class LedgerKeyring extends EventEmitter { resolve(payload) } else { reject(payload) - } + } }) }) }) @@ -315,8 +305,8 @@ class LedgerKeyring extends EventEmitter { return str } - _fixNonce(nonce){ - if(nonce === '0x'){ + _fixNonce (nonce) { + if (nonce === '0x') { return `${nonce}0` } return nonce -- cgit From e6d64cecf84d91f1db3aab5777db3530c9cd8ab7 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sat, 11 Aug 2018 20:26:34 -0400 Subject: message signing works --- app/scripts/eth-ledger-keyring-listener.js | 22 +++++++++++++++++----- app/scripts/lib/createLoggerMiddleware.js | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index 0043058e6..bc5801106 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -1,4 +1,3 @@ -const extension = require('extensionizer') const {EventEmitter} = require('events') const HDKey = require('hdkey') const ethUtil = require('ethereumjs-util') @@ -202,7 +201,7 @@ class LedgerKeyring extends EventEmitter { const correctAddress = ethUtil.toChecksumAddress(address) if (addressSignedWith !== correctAddress) { reject('signature doesnt match the right address') - } + } resolve(signedTx) } else { reject(payload) @@ -218,19 +217,32 @@ class LedgerKeyring extends EventEmitter { // For personal_sign, we need to prefix the message: async signPersonalMessage (withAccount, message) { + const humanReadableMsg = this._toAscii(message) + const bufferMsg = Buffer.from(humanReadableMsg).toString('hex') return new Promise((resolve, reject) => { this.unlock() .then(_ => { this.sendMessage({ action: 'ledger-sign-personal-message', params: { - withAccount, - message, + path: this._pathFromAddress(withAccount ), + message: bufferMsg, }, }, ({action, success, payload}) => { if (success) { - resolve(payload) + const { result } = payload + let v = result['v'] - 27 + v = v.toString(16) + if (v.length < 2) { + v = `0${v}` + } + const signature = `0x${result['r']}${result['s']}${v}` + const addressSignedWith = sigUtil.recoverPersonalSignature({data: message, sig: signature}) + if (ethUtil.toChecksumAddress(addressSignedWith) !== ethUtil.toChecksumAddress(withAccount)) { + reject('signature doesnt match the right address') + } + resolve(signature) } else { reject(payload) } diff --git a/app/scripts/lib/createLoggerMiddleware.js b/app/scripts/lib/createLoggerMiddleware.js index 996c3477c..53913921c 100644 --- a/app/scripts/lib/createLoggerMiddleware.js +++ b/app/scripts/lib/createLoggerMiddleware.js @@ -14,7 +14,7 @@ function createLoggerMiddleware (opts) { log.error('Error in RPC response:\n', res) } if (req.isMetamaskInternal) return - log.info(`RPC (${opts.origin}):`, req, '->', res) + //log.info(`RPC (${opts.origin}):`, req, '->', res) cb() }) } -- cgit From 2355573340d0287582d0cd6a467e13126fbddd7c Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sun, 12 Aug 2018 00:06:36 -0400 Subject: clean up --- app/scripts/eth-ledger-keyring-listener.js | 81 +++++++++++++----------------- 1 file changed, 36 insertions(+), 45 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js index bc5801106..91507455a 100644 --- a/app/scripts/eth-ledger-keyring-listener.js +++ b/app/scripts/eth-ledger-keyring-listener.js @@ -4,11 +4,9 @@ const ethUtil = require('ethereumjs-util') const sigUtil = require('eth-sig-util') const Transaction = require('ethereumjs-tx') - -// HD path differs from eth-hd-keyring - MEW, Parity, Geth and Official Ledger clients use same unusual derivation for Ledger const hdPathString = `44'/60'/0'` const type = 'Ledger Hardware' -const ORIGIN = 'https://localhost:3000' +const BRIDGE_URL = 'https://localhost:3000' const pathBase = 'm' const MAX_INDEX = 1000 @@ -28,18 +26,15 @@ class LedgerKeyring extends EventEmitter { setupIframe () { this.iframe = document.createElement('iframe') - this.iframe.src = ORIGIN - console.log('Injecting ledger iframe') + this.iframe.src = BRIDGE_URL document.head.appendChild(this.iframe) } sendMessage (msg, cb) { - console.log('[LEDGER]: SENDING MESSAGE TO IFRAME', msg) this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') window.addEventListener('message', ({ origin, data }) => { - if (origin !== ORIGIN) return false + if (origin !== BRIDGE_URL) return false if (data && data.action && data.action === `${msg.action}-reply`) { - console.log('[LEDGER]: GOT MESAGE FROM IFRAME', data) cb(data) } }) @@ -75,7 +70,7 @@ class LedgerKeyring extends EventEmitter { hdPath: this.hdPath, }, }, - ({action, success, payload}) => { + ({success, payload}) => { if (success) { this.hdk.publicKey = new Buffer(payload.publicKey, 'hex') this.hdk.chainCode = new Buffer(payload.chainCode, 'hex') @@ -87,10 +82,6 @@ class LedgerKeyring extends EventEmitter { }) } - setAccountToUnlock (index) { - this.unlockedAccount = parseInt(index, 10) - } - addAccounts (n = 1) { return new Promise((resolve, reject) => { @@ -173,36 +164,44 @@ class LedgerKeyring extends EventEmitter { // tx is an instance of the ethereumjs-transaction class. async signTransaction (address, tx) { - return new Promise((resolve, reject) => { this.unlock() .then(_ => { + + const newTx = new Transaction({ + from: this._normalize(address), + to: this._normalize(tx.to), + value: this._normalize(tx.value), + data: this._normalize(tx.data), + chainId: tx._chainId, + nonce: this._normalize(tx.nonce), + gasLimit: this._normalize(tx.gasLimit), + gasPrice: this._normalize(tx.gasPrice), + v: ethUtil.bufferToHex(tx.getChainId()), + r: '0x00', + s: '0x00', + }) + this.sendMessage({ action: 'ledger-sign-transaction', params: { - tx: { - from: this._normalize(address), - to: this._normalize(tx.to), - value: this._normalize(tx.value), - data: this._normalize(tx.data), - chainId: tx._chainId, - nonce: this._fixNonce(this._normalize(tx.nonce)), - gasLimit: this._normalize(tx.gasLimit), - gasPrice: this._normalize(tx.gasPrice), - }, - path: this._pathFromAddress(address), + tx: newTx.serialize().toString('hex'), + hdPath: this._pathFromAddress(address), }, }, - ({action, success, payload}) => { + ({success, payload}) => { if (success) { - const signedTx = new Transaction(payload.txData) - // Validate that the signature matches the right address - const addressSignedWith = ethUtil.toChecksumAddress(`0x${signedTx.from.toString('hex')}`) - const correctAddress = ethUtil.toChecksumAddress(address) - if (addressSignedWith !== correctAddress) { - reject('signature doesnt match the right address') + + newTx.v = Buffer.from(payload.v, 'hex') + newTx.r = Buffer.from(payload.r, 'hex') + newTx.s = Buffer.from(payload.s, 'hex') + + const valid = newTx.verifySignature() + if (valid) { + resolve(newTx) + } else { + reject('The transaction signature is not valid') } - resolve(signedTx) } else { reject(payload) } @@ -225,19 +224,18 @@ class LedgerKeyring extends EventEmitter { this.sendMessage({ action: 'ledger-sign-personal-message', params: { - path: this._pathFromAddress(withAccount ), + hdPath: this._pathFromAddress(withAccount), message: bufferMsg, }, }, - ({action, success, payload}) => { + ({success, payload}) => { if (success) { - const { result } = payload - let v = result['v'] - 27 + let v = payload['v'] - 27 v = v.toString(16) if (v.length < 2) { v = `0${v}` } - const signature = `0x${result['r']}${result['s']}${v}` + const signature = `0x${payload['r']}${payload['s']}${v}` const addressSignedWith = sigUtil.recoverPersonalSignature({data: message, sig: signature}) if (ethUtil.toChecksumAddress(addressSignedWith) !== ethUtil.toChecksumAddress(withAccount)) { reject('signature doesnt match the right address') @@ -316,13 +314,6 @@ class LedgerKeyring extends EventEmitter { return str } - - _fixNonce (nonce) { - if (nonce === '0x') { - return `${nonce}0` - } - return nonce - } } LedgerKeyring.type = type -- cgit From 0b9b892c6b27c55a016378387cb8d227957f8528 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sun, 12 Aug 2018 01:34:01 -0400 Subject: this should be ready to go --- app/scripts/eth-ledger-keyring-listener.js | 320 ----------------------------- app/scripts/metamask-controller.js | 6 +- 2 files changed, 3 insertions(+), 323 deletions(-) delete mode 100644 app/scripts/eth-ledger-keyring-listener.js (limited to 'app/scripts') diff --git a/app/scripts/eth-ledger-keyring-listener.js b/app/scripts/eth-ledger-keyring-listener.js deleted file mode 100644 index 91507455a..000000000 --- a/app/scripts/eth-ledger-keyring-listener.js +++ /dev/null @@ -1,320 +0,0 @@ -const {EventEmitter} = require('events') -const HDKey = require('hdkey') -const ethUtil = require('ethereumjs-util') -const sigUtil = require('eth-sig-util') -const Transaction = require('ethereumjs-tx') - -const hdPathString = `44'/60'/0'` -const type = 'Ledger Hardware' -const BRIDGE_URL = 'https://localhost:3000' -const pathBase = 'm' -const MAX_INDEX = 1000 - -class LedgerKeyring extends EventEmitter { - constructor (opts = {}) { - super() - this.type = type - this.page = 0 - this.perPage = 5 - this.unlockedAccount = 0 - this.hdk = new HDKey() - this.paths = {} - this.iframe = null - this.setupIframe() - this.deserialize(opts) - } - - setupIframe () { - this.iframe = document.createElement('iframe') - this.iframe.src = BRIDGE_URL - document.head.appendChild(this.iframe) - } - - sendMessage (msg, cb) { - this.iframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') - window.addEventListener('message', ({ origin, data }) => { - if (origin !== BRIDGE_URL) return false - if (data && data.action && data.action === `${msg.action}-reply`) { - cb(data) - } - }) - } - - serialize () { - return Promise.resolve({hdPath: this.hdPath, accounts: this.accounts}) - } - - deserialize (opts = {}) { - this.hdPath = opts.hdPath || hdPathString - this.unlocked = opts.unlocked || false - this.accounts = opts.accounts || [] - return Promise.resolve() - } - - isUnlocked () { - return this.unlocked - } - - setAccountToUnlock (index) { - this.unlockedAccount = parseInt(index, 10) - } - - unlock () { - - if (this.isUnlocked()) return Promise.resolve('already unlocked') - - return new Promise((resolve, reject) => { - this.sendMessage({ - action: 'ledger-unlock', - params: { - hdPath: this.hdPath, - }, - }, - ({success, payload}) => { - if (success) { - this.hdk.publicKey = new Buffer(payload.publicKey, 'hex') - this.hdk.chainCode = new Buffer(payload.chainCode, 'hex') - resolve('just unlocked') - } else { - reject(payload.error || 'Unknown error') - } - }) - }) - } - - addAccounts (n = 1) { - - return new Promise((resolve, reject) => { - this.unlock() - .then(_ => { - const from = this.unlockedAccount - const to = from + n - this.accounts = [] - - for (let i = from; i < to; i++) { - const address = this._addressFromIndex(pathBase, i) - this.accounts.push(address) - this.page = 0 - } - resolve(this.accounts) - }) - .catch(e => { - reject(e) - }) - }) - } - - getFirstPage () { - this.page = 0 - return this.__getPage(1) - } - - getNextPage () { - return this.__getPage(1) - } - - getPreviousPage () { - return this.__getPage(-1) - } - - __getPage (increment) { - - this.page += increment - - if (this.page <= 0) { this.page = 1 } - - return new Promise((resolve, reject) => { - this.unlock() - .then(_ => { - - const from = (this.page - 1) * this.perPage - const to = from + this.perPage - - const accounts = [] - - for (let i = from; i < to; i++) { - const address = this._addressFromIndex(pathBase, i) - accounts.push({ - address: address, - balance: null, - index: i, - }) - this.paths[ethUtil.toChecksumAddress(address)] = i - - } - resolve(accounts) - }) - .catch(e => { - reject(e) - }) - }) - } - - getAccounts () { - return Promise.resolve(this.accounts.slice()) - } - - removeAccount (address) { - if (!this.accounts.map(a => a.toLowerCase()).includes(address.toLowerCase())) { - throw new Error(`Address ${address} not found in this keyring`) - } - this.accounts = this.accounts.filter(a => a.toLowerCase() !== address.toLowerCase()) - } - - - // tx is an instance of the ethereumjs-transaction class. - async signTransaction (address, tx) { - return new Promise((resolve, reject) => { - this.unlock() - .then(_ => { - - const newTx = new Transaction({ - from: this._normalize(address), - to: this._normalize(tx.to), - value: this._normalize(tx.value), - data: this._normalize(tx.data), - chainId: tx._chainId, - nonce: this._normalize(tx.nonce), - gasLimit: this._normalize(tx.gasLimit), - gasPrice: this._normalize(tx.gasPrice), - v: ethUtil.bufferToHex(tx.getChainId()), - r: '0x00', - s: '0x00', - }) - - this.sendMessage({ - action: 'ledger-sign-transaction', - params: { - tx: newTx.serialize().toString('hex'), - hdPath: this._pathFromAddress(address), - }, - }, - ({success, payload}) => { - if (success) { - - newTx.v = Buffer.from(payload.v, 'hex') - newTx.r = Buffer.from(payload.r, 'hex') - newTx.s = Buffer.from(payload.s, 'hex') - - const valid = newTx.verifySignature() - if (valid) { - resolve(newTx) - } else { - reject('The transaction signature is not valid') - } - } else { - reject(payload) - } - }) - }) - }) - } - - async signMessage (withAccount, data) { - throw new Error('Not supported on this device') - } - - // For personal_sign, we need to prefix the message: - async signPersonalMessage (withAccount, message) { - const humanReadableMsg = this._toAscii(message) - const bufferMsg = Buffer.from(humanReadableMsg).toString('hex') - return new Promise((resolve, reject) => { - this.unlock() - .then(_ => { - this.sendMessage({ - action: 'ledger-sign-personal-message', - params: { - hdPath: this._pathFromAddress(withAccount), - message: bufferMsg, - }, - }, - ({success, payload}) => { - if (success) { - let v = payload['v'] - 27 - v = v.toString(16) - if (v.length < 2) { - v = `0${v}` - } - const signature = `0x${payload['r']}${payload['s']}${v}` - const addressSignedWith = sigUtil.recoverPersonalSignature({data: message, sig: signature}) - if (ethUtil.toChecksumAddress(addressSignedWith) !== ethUtil.toChecksumAddress(withAccount)) { - reject('signature doesnt match the right address') - } - resolve(signature) - } else { - reject(payload) - } - }) - }) - }) - } - - async signTypedData (withAccount, typedData) { - throw new Error('Not supported on this device') - } - - async exportAccount (address) { - throw new Error('Not supported on this device') - } - - forgetDevice () { - this.accounts = [] - this.unlocked = false - this.page = 0 - this.unlockedAccount = 0 - this.paths = {} - } - - /* PRIVATE METHODS */ - - _padLeftEven (hex) { - return hex.length % 2 !== 0 ? `0${hex}` : hex - } - - _normalize (buf) { - return this._padLeftEven(ethUtil.bufferToHex(buf).toLowerCase()) - } - - _addressFromIndex (pathBase, i) { - const dkey = this.hdk.derive(`${pathBase}/${i}`) - const address = ethUtil - .publicToAddress(dkey.publicKey, true) - .toString('hex') - return ethUtil.toChecksumAddress(address) - } - - _pathFromAddress (address) { - const checksummedAddress = ethUtil.toChecksumAddress(address) - let index = this.paths[checksummedAddress] - if (typeof index === 'undefined') { - for (let i = 0; i < MAX_INDEX; i++) { - if (checksummedAddress === this._addressFromIndex(pathBase, i)) { - index = i - break - } - } - } - - if (typeof index === 'undefined') { - throw new Error('Unknown address') - } - return `${this.hdPath}/${index}` - } - - _toAscii (hex) { - let str = '' - let i = 0; const l = hex.length - if (hex.substring(0, 2) === '0x') { - i = 2 - } - for (; i < l; i += 2) { - const code = parseInt(hex.substr(i, 2), 16) - str += String.fromCharCode(code) - } - - return str - } -} - -LedgerKeyring.type = type -module.exports = LedgerKeyring diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c79e5141e..fed00077e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -49,7 +49,7 @@ const seedPhraseVerifier = require('./lib/seed-phrase-verifier') const cleanErrorStack = require('./lib/cleanErrorStack') const log = require('loglevel') const TrezorKeyring = require('eth-trezor-keyring') -const LedgerKeyring = require('./eth-ledger-keyring-listener') +const LedgerBridgeKeyring = require('eth-ledger-bridge-keyring') module.exports = class MetamaskController extends EventEmitter { @@ -128,7 +128,7 @@ module.exports = class MetamaskController extends EventEmitter { }) // key mgmt - const additionalKeyrings = [TrezorKeyring, LedgerKeyring] + const additionalKeyrings = [TrezorKeyring, LedgerBridgeKeyring] this.keyringController = new KeyringController({ keyringTypes: additionalKeyrings, initState: initState.KeyringController, @@ -546,7 +546,7 @@ module.exports = class MetamaskController extends EventEmitter { keyringName = TrezorKeyring.type break case 'ledger': - keyringName = LedgerKeyring.type + keyringName = LedgerBridgeKeyring.type break default: throw new Error('MetamaskController:connectHardware - Unknown device') -- cgit From 8f9a0a535cbe904b0816c954c170c71843f62da9 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Sun, 12 Aug 2018 01:43:41 -0400 Subject: clean up --- app/scripts/background.js | 3 ++- app/scripts/contentscript.js | 2 +- app/scripts/lib/createLoggerMiddleware.js | 2 +- app/scripts/lib/setupLedgerIframe.js | 40 ------------------------------- 4 files changed, 4 insertions(+), 43 deletions(-) delete mode 100644 app/scripts/lib/setupLedgerIframe.js (limited to 'app/scripts') diff --git a/app/scripts/background.js b/app/scripts/background.js index bff559469..3d3afdd4e 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -68,6 +68,8 @@ initialize().catch(log.error) // setup metamask mesh testing container setupMetamaskMeshMetrics() + + /** * An object representing a transaction, in whatever state it is in. * @typedef TransactionMeta @@ -444,4 +446,3 @@ extension.runtime.onInstalled.addListener(function (details) { extension.tabs.create({url: 'https://metamask.io/#how-it-works'}) } }) - diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js index 01df9e327..e0a2b0061 100644 --- a/app/scripts/contentscript.js +++ b/app/scripts/contentscript.js @@ -200,4 +200,4 @@ function redirectToPhishingWarning () { console.log('MetaMask - routing to Phishing Warning component') const extensionURL = extension.runtime.getURL('phishing.html') window.location.href = extensionURL -} \ No newline at end of file +} diff --git a/app/scripts/lib/createLoggerMiddleware.js b/app/scripts/lib/createLoggerMiddleware.js index 53913921c..996c3477c 100644 --- a/app/scripts/lib/createLoggerMiddleware.js +++ b/app/scripts/lib/createLoggerMiddleware.js @@ -14,7 +14,7 @@ function createLoggerMiddleware (opts) { log.error('Error in RPC response:\n', res) } if (req.isMetamaskInternal) return - //log.info(`RPC (${opts.origin}):`, req, '->', res) + log.info(`RPC (${opts.origin}):`, req, '->', res) cb() }) } diff --git a/app/scripts/lib/setupLedgerIframe.js b/app/scripts/lib/setupLedgerIframe.js deleted file mode 100644 index 2831d072e..000000000 --- a/app/scripts/lib/setupLedgerIframe.js +++ /dev/null @@ -1,40 +0,0 @@ -const extension = require('extensionizer') -module.exports = setupLedgerIframe -/** - * Injects an iframe into the current document to - * enable the interaction with ledger devices - */ -function setupLedgerIframe () { - const ORIGIN = 'http://localhost:9000' - const ledgerIframe = document.createElement('iframe') - ledgerIframe.src = ORIGIN - console.log('Injecting ledger iframe') - document.head.appendChild(ledgerIframe) - - console.log('[LEDGER]: LEDGER BG LISTENER READY') - extension.runtime.onMessage.addListener(({action, params}) => { - console.log('[LEDGER]: GOT MSG FROM THE KEYRING', action, params) - if (action.search('ledger-') !== -1) { - //Forward messages from the keyring to the iframe - sendMessage({action, params}) - } - }) - - function sendMessage(msg) { - ledgerIframe.contentWindow.postMessage({...msg, target: 'LEDGER-IFRAME'}, '*') - } - - /* - Passing messages from iframe to background script - */ - console.log('[LEDGER]: LEDGER FROM-IFRAME LISTENER READY') - window.addEventListener('message', event => { - if(event.origin !== ORIGIN) return false - if (event.data && event.data.action && event.data.action.search('ledger-') !== -1) { - // Forward messages from the iframe to the keyring - console.log('[LEDGER] : forwarding msg', event.data) - extension.runtime.sendMessage(event.data) - } - }) - - } \ No newline at end of file -- cgit From 4e1d8ba19db729f5c282c4e3c6b43433b562a45e Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Mon, 13 Aug 2018 19:29:43 -0400 Subject: good progress adding paths --- app/scripts/metamask-controller.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index fed00077e..beaf04c0d 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -539,7 +539,7 @@ module.exports = class MetamaskController extends EventEmitter { // Hardware // - async getKeyringForDevice (deviceName) { + async getKeyringForDevice (deviceName, hdPath = null) { let keyringName = null switch (deviceName) { case 'trezor': @@ -555,6 +555,10 @@ module.exports = class MetamaskController extends EventEmitter { if (!keyring) { keyring = await this.keyringController.addNewKeyring(keyringName) } + if (hdPath) { + console.log('[LEDGER]: HDPATH set', hdPath) + keyring.hdPath = hdPath + } return keyring @@ -565,9 +569,8 @@ module.exports = class MetamaskController extends EventEmitter { * * @returns [] accounts */ - async connectHardware (deviceName, page) { - - const keyring = await this.getKeyringForDevice(deviceName) + async connectHardware (deviceName, page, hdPath) { + const keyring = await this.getKeyringForDevice(deviceName, hdPath) let accounts = [] switch (page) { case -1: @@ -593,8 +596,8 @@ module.exports = class MetamaskController extends EventEmitter { * * @returns {Promise} */ - async checkHardwareStatus (deviceName) { - const keyring = await this.getKeyringForDevice(deviceName) + async checkHardwareStatus (deviceName, hdPath) { + const keyring = await this.getKeyringForDevice(deviceName, hdPath) return keyring.isUnlocked() } @@ -615,8 +618,8 @@ module.exports = class MetamaskController extends EventEmitter { * * @returns {} keyState */ - async unlockHardwareWalletAccount (index, deviceName) { - const keyring = await this.getKeyringForDevice(deviceName) + async unlockHardwareWalletAccount (index, deviceName, hdPath) { + const keyring = await this.getKeyringForDevice(deviceName, hdPath) keyring.setAccountToUnlock(index) const oldAccounts = await this.keyringController.getAccounts() -- cgit From 61a279204a804ddf4815d0db6103c19c5640bbb9 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 14 Aug 2018 01:26:18 -0400 Subject: legacy and new hd path working --- app/scripts/metamask-controller.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app/scripts') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index beaf04c0d..b1473390b 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -556,10 +556,11 @@ module.exports = class MetamaskController extends EventEmitter { keyring = await this.keyringController.addNewKeyring(keyringName) } if (hdPath) { - console.log('[LEDGER]: HDPATH set', hdPath) keyring.hdPath = hdPath } + keyring.network = this.networkController.getProviderConfig().type + return keyring } -- cgit From b77cc3d9690304362471fbfe207bdad461c2ca3a Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 14 Aug 2018 03:42:23 -0400 Subject: fix tx tests --- app/scripts/controllers/transactions/index.js | 4 ++-- app/scripts/metamask-controller.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 7ac6ec2e6..8e2288aed 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -289,10 +289,10 @@ class TransactionController extends EventEmitter { // sign tx const fromAddress = txParams.from const ethTx = new Transaction(txParams) - const signedTx = await this.signEthTx(ethTx, fromAddress) + await this.signEthTx(ethTx, fromAddress) // set state to signed this.txStateManager.setTxStatusSigned(txMeta.id) - const rawTx = ethUtil.bufferToHex(signedTx.serialize()) + const rawTx = ethUtil.bufferToHex(ethTx.serialize()) return rawTx } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b1473390b..c813c58ac 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -549,14 +549,14 @@ module.exports = class MetamaskController extends EventEmitter { keyringName = LedgerBridgeKeyring.type break default: - throw new Error('MetamaskController:connectHardware - Unknown device') + throw new Error('MetamaskController:getKeyringForDevice - Unknown device') } let keyring = await this.keyringController.getKeyringsByType(keyringName)[0] if (!keyring) { keyring = await this.keyringController.addNewKeyring(keyringName) } - if (hdPath) { - keyring.hdPath = hdPath + if (hdPath && keyring.setHdPath) { + keyring.setHdPath(hdPath) } keyring.network = this.networkController.getProviderConfig().type -- cgit From fdf202efb066008f6625ba15cec8bcaef1edfec6 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 14 Aug 2018 21:19:01 -0400 Subject: fixed unit tests --- app/scripts/metamask-controller.js | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'app/scripts') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index c813c58ac..ac188c4df 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -551,15 +551,21 @@ module.exports = class MetamaskController extends EventEmitter { default: throw new Error('MetamaskController:getKeyringForDevice - Unknown device') } + console.log('getting keyring for device ', deviceName, hdPath) let keyring = await this.keyringController.getKeyringsByType(keyringName)[0] + console.log('got Keyring', keyring) if (!keyring) { + console.log('we did not so lets add it', keyringName) keyring = await this.keyringController.addNewKeyring(keyringName) + console.log('what about now', keyring) } + console.log('setting hdPath', hdPath) if (hdPath && keyring.setHdPath) { keyring.setHdPath(hdPath) } keyring.network = this.networkController.getProviderConfig().type + console.log('setting network', keyring.network) return keyring -- cgit From 82a5ed1e03e0a6a9bc19b946bc178b236d9aaa98 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Tue, 14 Aug 2018 22:40:29 -0400 Subject: remove console logs --- app/scripts/metamask-controller.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'app/scripts') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index ac188c4df..1e1aa035f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -551,21 +551,15 @@ module.exports = class MetamaskController extends EventEmitter { default: throw new Error('MetamaskController:getKeyringForDevice - Unknown device') } - console.log('getting keyring for device ', deviceName, hdPath) let keyring = await this.keyringController.getKeyringsByType(keyringName)[0] - console.log('got Keyring', keyring) if (!keyring) { - console.log('we did not so lets add it', keyringName) keyring = await this.keyringController.addNewKeyring(keyringName) - console.log('what about now', keyring) } - console.log('setting hdPath', hdPath) if (hdPath && keyring.setHdPath) { keyring.setHdPath(hdPath) } keyring.network = this.networkController.getProviderConfig().type - console.log('setting network', keyring.network) return keyring @@ -635,7 +629,7 @@ module.exports = class MetamaskController extends EventEmitter { this.preferencesController.setAddresses(newAccounts) newAccounts.forEach(address => { if (!oldAccounts.includes(address)) { - this.preferencesController.setAccountLabel(address, `${deviceName.toUpperCase()} #${parseInt(index, 10) + 1}`) + this.preferencesController.setAccountLabel(address, `${deviceName.toUpperCase()} ${parseInt(index, 10) + 1}`) this.preferencesController.setSelectedAddress(address) } }) -- cgit From 992e7f1b5aae5ae4a96c67dd40b6626f181b51c1 Mon Sep 17 00:00:00 2001 From: brunobar79 Date: Fri, 17 Aug 2018 12:56:07 -0400 Subject: fix merge conflicts --- app/scripts/background.js | 5 +- app/scripts/controllers/balance.js | 2 +- app/scripts/controllers/currency.js | 2 +- .../controllers/network/createInfuraClient.js | 25 +++++ .../controllers/network/createJsonRpcClient.js | 25 +++++ .../controllers/network/createLocalhostClient.js | 21 ++++ .../network/createMetamaskMiddleware.js | 43 +++++++ app/scripts/controllers/network/network.js | 122 ++++++++++---------- app/scripts/controllers/recent-blocks.js | 56 ++++------ app/scripts/controllers/transactions/index.js | 67 +++++++++-- .../controllers/transactions/nonce-tracker.js | 28 +---- .../controllers/transactions/pending-tx-tracker.js | 80 ++++--------- .../controllers/transactions/tx-gas-utils.js | 2 +- app/scripts/lib/account-tracker.js | 124 ++++++++++----------- app/scripts/lib/events-proxy.js | 42 ------- app/scripts/lib/message-manager.js | 31 +++++- app/scripts/lib/personal-message-manager.js | 35 +++++- app/scripts/lib/setupRaven.js | 4 +- app/scripts/lib/typed-message-manager.js | 31 +++++- app/scripts/lib/util.js | 14 +++ app/scripts/metamask-controller.js | 99 ++++++---------- 21 files changed, 489 insertions(+), 369 deletions(-) create mode 100644 app/scripts/controllers/network/createInfuraClient.js create mode 100644 app/scripts/controllers/network/createJsonRpcClient.js create mode 100644 app/scripts/controllers/network/createLocalhostClient.js create mode 100644 app/scripts/controllers/network/createMetamaskMiddleware.js delete mode 100644 app/scripts/lib/events-proxy.js (limited to 'app/scripts') diff --git a/app/scripts/background.js b/app/scripts/background.js index 3d3afdd4e..c7395c810 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -19,7 +19,7 @@ const PortStream = require('./lib/port-stream.js') const createStreamSink = require('./lib/createStreamSink') const NotificationManager = require('./lib/notification-manager.js') const MetamaskController = require('./metamask-controller') -const firstTimeState = require('./first-time-state') +const rawFirstTimeState = require('./first-time-state') const setupRaven = require('./lib/setupRaven') const reportFailedTxToSentry = require('./lib/reportFailedTxToSentry') const setupMetamaskMeshMetrics = require('./lib/setupMetamaskMeshMetrics') @@ -34,6 +34,9 @@ const { ENVIRONMENT_TYPE_FULLSCREEN, } = require('./lib/enums') +// METAMASK_TEST_CONFIG is used in e2e tests to set the default network to localhost +const firstTimeState = Object.assign({}, rawFirstTimeState, global.METAMASK_TEST_CONFIG) + const STORAGE_KEY = 'metamask-config' const METAMASK_DEBUG = process.env.METAMASK_DEBUG diff --git a/app/scripts/controllers/balance.js b/app/scripts/controllers/balance.js index 4c97810a3..465751e61 100644 --- a/app/scripts/controllers/balance.js +++ b/app/scripts/controllers/balance.js @@ -80,7 +80,7 @@ class BalanceController { } }) this.accountTracker.store.subscribe(update) - this.blockTracker.on('block', update) + this.blockTracker.on('latest', update) } /** diff --git a/app/scripts/controllers/currency.js b/app/scripts/controllers/currency.js index a93aff49b..d5bc5fe2b 100644 --- a/app/scripts/controllers/currency.js +++ b/app/scripts/controllers/currency.js @@ -1,4 +1,4 @@ - const ObservableStore = require('obs-store') +const ObservableStore = require('obs-store') const extend = require('xtend') const log = require('loglevel') diff --git a/app/scripts/controllers/network/createInfuraClient.js b/app/scripts/controllers/network/createInfuraClient.js new file mode 100644 index 000000000..41af4d9f9 --- /dev/null +++ b/app/scripts/controllers/network/createInfuraClient.js @@ -0,0 +1,25 @@ +const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') +const createBlockReEmitMiddleware = require('eth-json-rpc-middleware/block-reemit') +const createBlockCacheMiddleware = require('eth-json-rpc-middleware/block-cache') +const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache') +const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') +const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') +const createInfuraMiddleware = require('eth-json-rpc-infura') +const BlockTracker = require('eth-block-tracker') + +module.exports = createInfuraClient + +function createInfuraClient ({ network }) { + const infuraMiddleware = createInfuraMiddleware({ network }) + const blockProvider = providerFromMiddleware(infuraMiddleware) + const blockTracker = new BlockTracker({ provider: blockProvider }) + + const networkMiddleware = mergeMiddleware([ + createBlockCacheMiddleware({ blockTracker }), + createInflightMiddleware(), + createBlockReEmitMiddleware({ blockTracker, provider: blockProvider }), + createBlockTrackerInspectorMiddleware({ blockTracker }), + infuraMiddleware, + ]) + return { networkMiddleware, blockTracker } +} diff --git a/app/scripts/controllers/network/createJsonRpcClient.js b/app/scripts/controllers/network/createJsonRpcClient.js new file mode 100644 index 000000000..40c353f7f --- /dev/null +++ b/app/scripts/controllers/network/createJsonRpcClient.js @@ -0,0 +1,25 @@ +const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') +const createFetchMiddleware = require('eth-json-rpc-middleware/fetch') +const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref') +const createBlockCacheMiddleware = require('eth-json-rpc-middleware/block-cache') +const createInflightMiddleware = require('eth-json-rpc-middleware/inflight-cache') +const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') +const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') +const BlockTracker = require('eth-block-tracker') + +module.exports = createJsonRpcClient + +function createJsonRpcClient ({ rpcUrl }) { + const fetchMiddleware = createFetchMiddleware({ rpcUrl }) + const blockProvider = providerFromMiddleware(fetchMiddleware) + const blockTracker = new BlockTracker({ provider: blockProvider }) + + const networkMiddleware = mergeMiddleware([ + createBlockRefMiddleware({ blockTracker }), + createBlockCacheMiddleware({ blockTracker }), + createInflightMiddleware(), + createBlockTrackerInspectorMiddleware({ blockTracker }), + fetchMiddleware, + ]) + return { networkMiddleware, blockTracker } +} diff --git a/app/scripts/controllers/network/createLocalhostClient.js b/app/scripts/controllers/network/createLocalhostClient.js new file mode 100644 index 000000000..fecc512e8 --- /dev/null +++ b/app/scripts/controllers/network/createLocalhostClient.js @@ -0,0 +1,21 @@ +const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') +const createFetchMiddleware = require('eth-json-rpc-middleware/fetch') +const createBlockRefMiddleware = require('eth-json-rpc-middleware/block-ref') +const createBlockTrackerInspectorMiddleware = require('eth-json-rpc-middleware/block-tracker-inspector') +const providerFromMiddleware = require('eth-json-rpc-middleware/providerFromMiddleware') +const BlockTracker = require('eth-block-tracker') + +module.exports = createLocalhostClient + +function createLocalhostClient () { + const fetchMiddleware = createFetchMiddleware({ rpcUrl: 'http://localhost:8545/' }) + const blockProvider = providerFromMiddleware(fetchMiddleware) + const blockTracker = new BlockTracker({ provider: blockProvider, pollingInterval: 1000 }) + + const networkMiddleware = mergeMiddleware([ + createBlockRefMiddleware({ blockTracker }), + createBlockTrackerInspectorMiddleware({ blockTracker }), + fetchMiddleware, + ]) + return { networkMiddleware, blockTracker } +} diff --git a/app/scripts/controllers/network/createMetamaskMiddleware.js b/app/scripts/controllers/network/createMetamaskMiddleware.js new file mode 100644 index 000000000..8b17829b7 --- /dev/null +++ b/app/scripts/controllers/network/createMetamaskMiddleware.js @@ -0,0 +1,43 @@ +const mergeMiddleware = require('json-rpc-engine/src/mergeMiddleware') +const createScaffoldMiddleware = require('json-rpc-engine/src/createScaffoldMiddleware') +const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware') +const createWalletSubprovider = require('eth-json-rpc-middleware/wallet') + +module.exports = createMetamaskMiddleware + +function createMetamaskMiddleware ({ + version, + getAccounts, + processTransaction, + processEthSignMessage, + processTypedMessage, + processPersonalMessage, + getPendingNonce, +}) { + const metamaskMiddleware = mergeMiddleware([ + createScaffoldMiddleware({ + // staticSubprovider + eth_syncing: false, + web3_clientVersion: `MetaMask/v${version}`, + }), + createWalletSubprovider({ + getAccounts, + processTransaction, + processEthSignMessage, + processTypedMessage, + processPersonalMessage, + }), + createPendingNonceMiddleware({ getPendingNonce }), + ]) + return metamaskMiddleware +} + +function createPendingNonceMiddleware ({ getPendingNonce }) { + return createAsyncMiddleware(async (req, res, next) => { + if (req.method !== 'eth_getTransactionCount') return next() + const address = req.params[0] + const blockRef = req.params[1] + if (blockRef !== 'pending') return next() + req.result = await getPendingNonce(address) + }) +} diff --git a/app/scripts/controllers/network/network.js b/app/scripts/controllers/network/network.js index b6f7705b5..76fdc3391 100644 --- a/app/scripts/controllers/network/network.js +++ b/app/scripts/controllers/network/network.js @@ -1,15 +1,17 @@ const assert = require('assert') const EventEmitter = require('events') -const createMetamaskProvider = require('web3-provider-engine/zero.js') -const SubproviderFromProvider = require('web3-provider-engine/subproviders/provider.js') -const createInfuraProvider = require('eth-json-rpc-infura/src/createProvider') const ObservableStore = require('obs-store') const ComposedStore = require('obs-store/lib/composed') -const extend = require('xtend') const EthQuery = require('eth-query') -const createEventEmitterProxy = require('../../lib/events-proxy.js') +const JsonRpcEngine = require('json-rpc-engine') +const providerFromEngine = require('eth-json-rpc-middleware/providerFromEngine') const log = require('loglevel') -const urlUtil = require('url') +const createMetamaskMiddleware = require('./createMetamaskMiddleware') +const createInfuraClient = require('./createInfuraClient') +const createJsonRpcClient = require('./createJsonRpcClient') +const createLocalhostClient = require('./createLocalhostClient') +const { createSwappableProxy, createEventEmitterProxy } = require('swappable-obj-proxy') + const { ROPSTEN, RINKEBY, @@ -17,7 +19,6 @@ const { MAINNET, LOCALHOST, } = require('./enums') -const LOCALHOST_RPC_URL = 'http://localhost:8545' const INFURA_PROVIDER_TYPES = [ROPSTEN, RINKEBY, KOVAN, MAINNET] const env = process.env.METAMASK_ENV @@ -39,21 +40,27 @@ module.exports = class NetworkController extends EventEmitter { this.providerStore = new ObservableStore(providerConfig) this.networkStore = new ObservableStore('loading') this.store = new ComposedStore({ provider: this.providerStore, network: this.networkStore }) - // create event emitter proxy - this._proxy = createEventEmitterProxy() - this.on('networkDidChange', this.lookupNetwork) + // provider and block tracker + this._provider = null + this._blockTracker = null + // provider and block tracker proxies - because the network changes + this._providerProxy = null + this._blockTrackerProxy = null } - initializeProvider (_providerParams) { - this._baseProviderParams = _providerParams + initializeProvider (providerParams) { + this._baseProviderParams = providerParams const { type, rpcTarget } = this.providerStore.getState() this._configureProvider({ type, rpcTarget }) - this._proxy.on('block', this._logBlock.bind(this)) - this._proxy.on('error', this.verifyNetwork.bind(this)) - this.ethQuery = new EthQuery(this._proxy) this.lookupNetwork() - return this._proxy + } + + // return the proxies so the references will always be good + getProviderAndBlockTracker () { + const provider = this._providerProxy + const blockTracker = this._blockTrackerProxy + return { provider, blockTracker } } verifyNetwork () { @@ -75,10 +82,11 @@ module.exports = class NetworkController extends EventEmitter { lookupNetwork () { // Prevent firing when provider is not defined. - if (!this.ethQuery || !this.ethQuery.sendAsync) { - return log.warn('NetworkController - lookupNetwork aborted due to missing ethQuery') + if (!this._provider) { + return log.warn('NetworkController - lookupNetwork aborted due to missing provider') } - this.ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { + const ethQuery = new EthQuery(this._provider) + ethQuery.sendAsync({ method: 'net_version' }, (err, network) => { if (err) return this.setNetworkState('loading') log.info('web3.getNetwork returned ' + network) this.setNetworkState(network) @@ -131,7 +139,7 @@ module.exports = class NetworkController extends EventEmitter { this._configureInfuraProvider(opts) // other type-based rpc endpoints } else if (type === LOCALHOST) { - this._configureStandardProvider({ rpcUrl: LOCALHOST_RPC_URL }) + this._configureLocalhostProvider() // url-based rpc endpoints } else if (type === 'rpc') { this._configureStandardProvider({ rpcUrl: rpcTarget }) @@ -141,49 +149,47 @@ module.exports = class NetworkController extends EventEmitter { } _configureInfuraProvider ({ type }) { - log.info('_configureInfuraProvider', type) - const infuraProvider = createInfuraProvider({ network: type }) - const infuraSubprovider = new SubproviderFromProvider(infuraProvider) - const providerParams = extend(this._baseProviderParams, { - engineParams: { - pollingInterval: 8000, - blockTrackerProvider: infuraProvider, - }, - dataSubprovider: infuraSubprovider, - }) - const provider = createMetamaskProvider(providerParams) - this._setProvider(provider) + log.info('NetworkController - configureInfuraProvider', type) + const networkClient = createInfuraClient({ network: type }) + this._setNetworkClient(networkClient) + } + + _configureLocalhostProvider () { + log.info('NetworkController - configureLocalhostProvider') + const networkClient = createLocalhostClient() + this._setNetworkClient(networkClient) } _configureStandardProvider ({ rpcUrl }) { - // urlUtil handles malformed urls - rpcUrl = urlUtil.parse(rpcUrl).format() - const providerParams = extend(this._baseProviderParams, { - rpcUrl, - engineParams: { - pollingInterval: 8000, - }, - }) - const provider = createMetamaskProvider(providerParams) - this._setProvider(provider) - } - - _setProvider (provider) { - // collect old block tracker events - const oldProvider = this._provider - let blockTrackerHandlers - if (oldProvider) { - // capture old block handlers - blockTrackerHandlers = oldProvider._blockTracker.proxyEventHandlers - // tear down - oldProvider.removeAllListeners() - oldProvider.stop() + log.info('NetworkController - configureStandardProvider', rpcUrl) + const networkClient = createJsonRpcClient({ rpcUrl }) + this._setNetworkClient(networkClient) + } + + _setNetworkClient ({ networkMiddleware, blockTracker }) { + const metamaskMiddleware = createMetamaskMiddleware(this._baseProviderParams) + const engine = new JsonRpcEngine() + engine.push(metamaskMiddleware) + engine.push(networkMiddleware) + const provider = providerFromEngine(engine) + this._setProviderAndBlockTracker({ provider, blockTracker }) + } + + _setProviderAndBlockTracker ({ provider, blockTracker }) { + // update or intialize proxies + if (this._providerProxy) { + this._providerProxy.setTarget(provider) + } else { + this._providerProxy = createSwappableProxy(provider) + } + if (this._blockTrackerProxy) { + this._blockTrackerProxy.setTarget(blockTracker) + } else { + this._blockTrackerProxy = createEventEmitterProxy(blockTracker) } - // override block tracler - provider._blockTracker = createEventEmitterProxy(provider._blockTracker, blockTrackerHandlers) - // set as new provider + // set new provider and blockTracker this._provider = provider - this._proxy.setTarget(provider) + this._blockTracker = blockTracker } _logBlock (block) { diff --git a/app/scripts/controllers/recent-blocks.js b/app/scripts/controllers/recent-blocks.js index 926268691..d270f6f44 100644 --- a/app/scripts/controllers/recent-blocks.js +++ b/app/scripts/controllers/recent-blocks.js @@ -1,14 +1,14 @@ const ObservableStore = require('obs-store') const extend = require('xtend') -const BN = require('ethereumjs-util').BN const EthQuery = require('eth-query') const log = require('loglevel') +const pify = require('pify') class RecentBlocksController { /** * Controller responsible for storing, updating and managing the recent history of blocks. Blocks are back filled - * upon the controller's construction and then the list is updated when the given block tracker gets a 'block' event + * upon the controller's construction and then the list is updated when the given block tracker gets a 'latest' event * (indicating that there is a new block to process). * * @typedef {Object} RecentBlocksController @@ -16,7 +16,7 @@ class RecentBlocksController { * @param {BlockTracker} opts.blockTracker Contains objects necessary for tracking blocks and querying the blockchain * @param {BlockTracker} opts.provider The provider used to create a new EthQuery instance. * @property {BlockTracker} blockTracker Points to the passed BlockTracker. On RecentBlocksController construction, - * listens for 'block' events so that new blocks can be processed and added to storage. + * listens for 'latest' events so that new blocks can be processed and added to storage. * @property {EthQuery} ethQuery Points to the EthQuery instance created with the passed provider * @property {number} historyLength The maximum length of blocks to track * @property {object} store Stores the recentBlocks @@ -34,7 +34,13 @@ class RecentBlocksController { }, opts.initState) this.store = new ObservableStore(initState) - this.blockTracker.on('block', this.processBlock.bind(this)) + this.blockTracker.on('latest', async (newBlockNumberHex) => { + try { + await this.processBlock(newBlockNumberHex) + } catch (err) { + log.error(err) + } + }) this.backfill() } @@ -55,7 +61,11 @@ class RecentBlocksController { * @param {object} newBlock The new block to modify and add to the recentBlocks array * */ - processBlock (newBlock) { + async processBlock (newBlockNumberHex) { + const newBlockNumber = Number.parseInt(newBlockNumberHex, 16) + const newBlock = await this.getBlockByNumber(newBlockNumber, true) + if (!newBlock) return + const block = this.mapTransactionsToPrices(newBlock) const state = this.store.getState() @@ -108,9 +118,9 @@ class RecentBlocksController { } /** - * On this.blockTracker's first 'block' event after this RecentBlocksController's instantiation, the store.recentBlocks + * On this.blockTracker's first 'latest' event after this RecentBlocksController's instantiation, the store.recentBlocks * array is populated with this.historyLength number of blocks. The block number of the this.blockTracker's first - * 'block' event is used to iteratively generate all the numbers of the previous blocks, which are obtained by querying + * 'latest' event is used to iteratively generate all the numbers of the previous blocks, which are obtained by querying * the blockchain. These blocks are backfilled so that the recentBlocks array is ordered from oldest to newest. * * Each iteration over the block numbers is delayed by 100 milliseconds. @@ -118,18 +128,17 @@ class RecentBlocksController { * @returns {Promise} Promises undefined */ async backfill () { - this.blockTracker.once('block', async (block) => { - const currentBlockNumber = Number.parseInt(block.number, 16) + this.blockTracker.once('latest', async (blockNumberHex) => { + const currentBlockNumber = Number.parseInt(blockNumberHex, 16) const blocksToFetch = Math.min(currentBlockNumber, this.historyLength) const prevBlockNumber = currentBlockNumber - 1 const targetBlockNumbers = Array(blocksToFetch).fill().map((_, index) => prevBlockNumber - index) await Promise.all(targetBlockNumbers.map(async (targetBlockNumber) => { try { - const newBlock = await this.getBlockByNumber(targetBlockNumber) + const newBlock = await this.getBlockByNumber(targetBlockNumber, true) + if (!newBlock) return - if (newBlock) { - this.backfillBlock(newBlock) - } + this.backfillBlock(newBlock) } catch (e) { log.error(e) } @@ -137,18 +146,6 @@ class RecentBlocksController { }) } - /** - * A helper for this.backfill. Provides an easy way to ensure a 100 millisecond delay using await - * - * @returns {Promise} Promises undefined - * - */ - async wait () { - return new Promise((resolve) => { - setTimeout(resolve, 100) - }) - } - /** * Uses EthQuery to get a block that has a given block number. * @@ -157,13 +154,8 @@ class RecentBlocksController { * */ async getBlockByNumber (number) { - const bn = new BN(number) - return new Promise((resolve, reject) => { - this.ethQuery.getBlockByNumber('0x' + bn.toString(16), true, (err, block) => { - if (err) reject(err) - resolve(block) - }) - }) + const blockNumberHex = '0x' + number.toString(16) + return await pify(this.ethQuery.getBlockByNumber).call(this.ethQuery, blockNumberHex, true) } } diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index 8e2288aed..5d7d6d6da 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -65,6 +65,7 @@ class TransactionController extends EventEmitter { this.store = this.txStateManager.store this.nonceTracker = new NonceTracker({ provider: this.provider, + blockTracker: this.blockTracker, getPendingTransactions: this.txStateManager.getPendingTransactions.bind(this.txStateManager), getConfirmedTransactions: this.txStateManager.getConfirmedTransactions.bind(this.txStateManager), }) @@ -78,13 +79,17 @@ class TransactionController extends EventEmitter { }) this.txStateManager.store.subscribe(() => this.emit('update:badge')) - this._setupListners() + this._setupListeners() // memstore is computed from a few different stores this._updateMemstore() this.txStateManager.store.subscribe(() => this._updateMemstore()) this.networkStore.subscribe(() => this._updateMemstore()) this.preferencesStore.subscribe(() => this._updateMemstore()) + + // request state update to finalize initialization + this._updatePendingTxsAfterFirstBlock() } + /** @returns {number} the chainId*/ getChainId () { const networkState = this.networkStore.getState() @@ -311,6 +316,11 @@ class TransactionController extends EventEmitter { this.txStateManager.setTxStatusSubmitted(txId) } + confirmTransaction (txId) { + this.txStateManager.setTxStatusConfirmed(txId) + this._markNonceDuplicatesDropped(txId) + } + /** Convenience method for the ui thats sets the transaction to rejected @param txId {number} - the tx's Id @@ -354,6 +364,14 @@ class TransactionController extends EventEmitter { this.getFilteredTxList = (opts) => this.txStateManager.getFilteredTxList(opts) } + // called once on startup + async _updatePendingTxsAfterFirstBlock () { + // wait for first block so we know we're ready + await this.blockTracker.getLatestBlock() + // get status update for all pending transactions (for the current network) + await this.pendingTxTracker.updatePendingTxs() + } + /** If transaction controller was rebooted with transactions that are uncompleted in steps of the transaction signing or user confirmation process it will either @@ -386,14 +404,14 @@ class TransactionController extends EventEmitter { is called in constructor applies the listeners for pendingTxTracker txStateManager and blockTracker */ - _setupListners () { + _setupListeners () { this.txStateManager.on('tx:status-update', this.emit.bind(this, 'tx:status-update')) + this._setupBlockTrackerListener() this.pendingTxTracker.on('tx:warning', (txMeta) => { this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:warning') }) - this.pendingTxTracker.on('tx:confirmed', (txId) => this.txStateManager.setTxStatusConfirmed(txId)) - this.pendingTxTracker.on('tx:confirmed', (txId) => this._markNonceDuplicatesDropped(txId)) this.pendingTxTracker.on('tx:failed', this.txStateManager.setTxStatusFailed.bind(this.txStateManager)) + this.pendingTxTracker.on('tx:confirmed', (txId) => this.confirmTransaction(txId)) this.pendingTxTracker.on('tx:block-update', (txMeta, latestBlockNumber) => { if (!txMeta.firstRetryBlockNumber) { txMeta.firstRetryBlockNumber = latestBlockNumber @@ -405,13 +423,6 @@ class TransactionController extends EventEmitter { txMeta.retryCount++ this.txStateManager.updateTx(txMeta, 'transactions/pending-tx-tracker#event: tx:retry') }) - - this.blockTracker.on('block', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker)) - // this is a little messy but until ethstore has been either - // removed or redone this is to guard against the race condition - this.blockTracker.on('latest', this.pendingTxTracker.resubmitPendingTxs.bind(this.pendingTxTracker)) - this.blockTracker.on('sync', this.pendingTxTracker.queryPendingTxs.bind(this.pendingTxTracker)) - } /** @@ -435,6 +446,40 @@ class TransactionController extends EventEmitter { }) } + _setupBlockTrackerListener () { + let listenersAreActive = false + const latestBlockHandler = this._onLatestBlock.bind(this) + const blockTracker = this.blockTracker + const txStateManager = this.txStateManager + + txStateManager.on('tx:status-update', updateSubscription) + updateSubscription() + + function updateSubscription () { + const pendingTxs = txStateManager.getPendingTransactions() + if (!listenersAreActive && pendingTxs.length > 0) { + blockTracker.on('latest', latestBlockHandler) + listenersAreActive = true + } else if (listenersAreActive && !pendingTxs.length) { + blockTracker.removeListener('latest', latestBlockHandler) + listenersAreActive = false + } + } + } + + async _onLatestBlock (blockNumber) { + try { + await this.pendingTxTracker.updatePendingTxs() + } catch (err) { + log.error(err) + } + try { + await this.pendingTxTracker.resubmitPendingTxs(blockNumber) + } catch (err) { + log.error(err) + } + } + /** Updates the memStore in transaction controller */ diff --git a/app/scripts/controllers/transactions/nonce-tracker.js b/app/scripts/controllers/transactions/nonce-tracker.js index 06f336eaa..421036368 100644 --- a/app/scripts/controllers/transactions/nonce-tracker.js +++ b/app/scripts/controllers/transactions/nonce-tracker.js @@ -12,8 +12,9 @@ const Mutex = require('await-semaphore').Mutex */ class NonceTracker { - constructor ({ provider, getPendingTransactions, getConfirmedTransactions }) { + constructor ({ provider, blockTracker, getPendingTransactions, getConfirmedTransactions }) { this.provider = provider + this.blockTracker = blockTracker this.ethQuery = new EthQuery(provider) this.getPendingTransactions = getPendingTransactions this.getConfirmedTransactions = getConfirmedTransactions @@ -34,7 +35,7 @@ class NonceTracker { * @typedef NonceDetails * @property {number} highestLocallyConfirmed - A hex string of the highest nonce on a confirmed transaction. * @property {number} nextNetworkNonce - The next nonce suggested by the eth_getTransactionCount method. - * @property {number} highetSuggested - The maximum between the other two, the number returned. + * @property {number} highestSuggested - The maximum between the other two, the number returned. */ /** @@ -80,15 +81,6 @@ class NonceTracker { } } - async _getCurrentBlock () { - const blockTracker = this._getBlockTracker() - const currentBlock = blockTracker.getCurrentBlock() - if (currentBlock) return currentBlock - return await new Promise((reject, resolve) => { - blockTracker.once('latest', resolve) - }) - } - async _globalMutexFree () { const globalMutex = this._lookupMutex('global') const releaseLock = await globalMutex.acquire() @@ -114,9 +106,8 @@ class NonceTracker { // calculate next nonce // we need to make sure our base count // and pending count are from the same block - const currentBlock = await this._getCurrentBlock() - const blockNumber = currentBlock.blockNumber - const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber || 'latest') + const blockNumber = await this.blockTracker.getLatestBlock() + const baseCountBN = await this.ethQuery.getTransactionCount(address, blockNumber) const baseCount = baseCountBN.toNumber() assert(Number.isInteger(baseCount), `nonce-tracker - baseCount is not an integer - got: (${typeof baseCount}) "${baseCount}"`) const nonceDetails = { blockNumber, baseCount } @@ -165,15 +156,6 @@ class NonceTracker { return { name: 'local', nonce: highest, details: { startPoint, highest } } } - // this is a hotfix for the fact that the blockTracker will - // change when the network changes - - /** - @returns {Object} the current blockTracker - */ - _getBlockTracker () { - return this.provider._blockTracker - } } module.exports = NonceTracker diff --git a/app/scripts/controllers/transactions/pending-tx-tracker.js b/app/scripts/controllers/transactions/pending-tx-tracker.js index 4e41cdaf8..70cac096b 100644 --- a/app/scripts/controllers/transactions/pending-tx-tracker.js +++ b/app/scripts/controllers/transactions/pending-tx-tracker.js @@ -1,6 +1,7 @@ const EventEmitter = require('events') const log = require('loglevel') const EthQuery = require('ethjs-query') + /** Event emitter utility class for tracking the transactions as they
@@ -23,55 +24,26 @@ class PendingTransactionTracker extends EventEmitter { super() this.query = new EthQuery(config.provider) this.nonceTracker = config.nonceTracker - // default is one day this.getPendingTransactions = config.getPendingTransactions this.getCompletedTransactions = config.getCompletedTransactions this.publishTransaction = config.publishTransaction - this._checkPendingTxs() + this.confirmTransaction = config.confirmTransaction } /** - checks if a signed tx is in a block and - if it is included emits tx status as 'confirmed' - @param block {object}, a full block - @emits tx:confirmed - @emits tx:failed - */ - checkForTxInBlock (block) { - const signedTxList = this.getPendingTransactions() - if (!signedTxList.length) return - signedTxList.forEach((txMeta) => { - const txHash = txMeta.hash - const txId = txMeta.id - - if (!txHash) { - const noTxHashErr = new Error('We had an error while submitting this transaction, please try again.') - noTxHashErr.name = 'NoTxHashError' - this.emit('tx:failed', txId, noTxHashErr) - return - } - - - block.transactions.forEach((tx) => { - if (tx.hash === txHash) this.emit('tx:confirmed', txId) - }) - }) - } - - /** - asks the network for the transaction to see if a block number is included on it - if we have skipped/missed blocks - @param object - oldBlock newBlock + checks the network for signed txs and releases the nonce global lock if it is */ - queryPendingTxs ({ oldBlock, newBlock }) { - // check pending transactions on start - if (!oldBlock) { - this._checkPendingTxs() - return + async updatePendingTxs () { + // in order to keep the nonceTracker accurate we block it while updating pending transactions + const nonceGlobalLock = await this.nonceTracker.getGlobalLock() + try { + const pendingTxs = this.getPendingTransactions() + await Promise.all(pendingTxs.map((txMeta) => this._checkPendingTx(txMeta))) + } catch (err) { + log.error('PendingTransactionTracker - Error updating pending transactions') + log.error(err) } - // if we synced by more than one block, check for missed pending transactions - const diff = Number.parseInt(newBlock.number, 16) - Number.parseInt(oldBlock.number, 16) - if (diff > 1) this._checkPendingTxs() + nonceGlobalLock.releaseLock() } /** @@ -79,11 +51,11 @@ class PendingTransactionTracker extends EventEmitter { @param block {object} - a block object @emits tx:warning */ - resubmitPendingTxs (block) { + resubmitPendingTxs (blockNumber) { const pending = this.getPendingTransactions() // only try resubmitting if their are transactions to resubmit if (!pending.length) return - pending.forEach((txMeta) => this._resubmitTx(txMeta, block.number).catch((err) => { + pending.forEach((txMeta) => this._resubmitTx(txMeta, blockNumber).catch((err) => { /* Dont marked as failed if the error is a "known" transaction warning "there is already a transaction with the same sender-nonce @@ -145,6 +117,7 @@ class PendingTransactionTracker extends EventEmitter { this.emit('tx:retry', txMeta) return txHash } + /** Ask the network for the transaction to see if it has been include in a block @param txMeta {Object} - the txMeta object @@ -174,9 +147,8 @@ class PendingTransactionTracker extends EventEmitter { } // get latest transaction status - let txParams try { - txParams = await this.query.getTransactionByHash(txHash) + const txParams = await this.query.getTransactionByHash(txHash) if (!txParams) return if (txParams.blockNumber) { this.emit('tx:confirmed', txId) @@ -190,27 +162,13 @@ class PendingTransactionTracker extends EventEmitter { } } - /** - checks the network for signed txs and releases the nonce global lock if it is - */ - async _checkPendingTxs () { - const signedTxList = this.getPendingTransactions() - // in order to keep the nonceTracker accurate we block it while updating pending transactions - const { releaseLock } = await this.nonceTracker.getGlobalLock() - try { - await Promise.all(signedTxList.map((txMeta) => this._checkPendingTx(txMeta))) - } catch (err) { - log.error('PendingTransactionWatcher - Error updating pending transactions') - log.error(err) - } - releaseLock() - } - /** checks to see if a confirmed txMeta has the same nonce @param txMeta {Object} - txMeta object @returns {boolean} */ + + async _checkIfNonceIsTaken (txMeta) { const address = txMeta.txParams.from const completed = this.getCompletedTransactions(address) diff --git a/app/scripts/controllers/transactions/tx-gas-utils.js b/app/scripts/controllers/transactions/tx-gas-utils.js index 5cd0f5407..3dd45507f 100644 --- a/app/scripts/controllers/transactions/tx-gas-utils.js +++ b/app/scripts/controllers/transactions/tx-gas-utils.js @@ -25,7 +25,7 @@ class TxGasUtil { @returns {object} the txMeta object with the gas written to the txParams */ async analyzeGasUsage (txMeta) { - const block = await this.query.getBlockByNumber('latest', true) + const block = await this.query.getBlockByNumber('latest', false) let estimatedGasHex try { estimatedGasHex = await this.estimateTxGas(txMeta, block.gasLimit) diff --git a/app/scripts/lib/account-tracker.js b/app/scripts/lib/account-tracker.js index 0f7b3d865..b7e2c7cbe 100644 --- a/app/scripts/lib/account-tracker.js +++ b/app/scripts/lib/account-tracker.js @@ -7,14 +7,13 @@ * on each new block. */ -const async = require('async') const EthQuery = require('eth-query') const ObservableStore = require('obs-store') -const EventEmitter = require('events').EventEmitter -function noop () {} +const log = require('loglevel') +const pify = require('pify') -class AccountTracker extends EventEmitter { +class AccountTracker { /** * This module is responsible for tracking any number of accounts and caching their current balances & transaction @@ -35,8 +34,6 @@ class AccountTracker extends EventEmitter { * */ constructor (opts = {}) { - super() - const initState = { accounts: {}, currentBlockGasLimit: '', @@ -44,12 +41,12 @@ class AccountTracker extends EventEmitter { this.store = new ObservableStore(initState) this._provider = opts.provider - this._query = new EthQuery(this._provider) + this._query = pify(new EthQuery(this._provider)) this._blockTracker = opts.blockTracker // subscribe to latest block - this._blockTracker.on('block', this._updateForBlock.bind(this)) + this._blockTracker.on('latest', this._updateForBlock.bind(this)) // blockTracker.currentBlock may be null - this._currentBlockNumber = this._blockTracker.currentBlock + this._currentBlockNumber = this._blockTracker.getCurrentBlock() } /** @@ -67,49 +64,57 @@ class AccountTracker extends EventEmitter { const accounts = this.store.getState().accounts const locals = Object.keys(accounts) - const toAdd = [] + const accountsToAdd = [] addresses.forEach((upstream) => { if (!locals.includes(upstream)) { - toAdd.push(upstream) + accountsToAdd.push(upstream) } }) - const toRemove = [] + const accountsToRemove = [] locals.forEach((local) => { if (!addresses.includes(local)) { - toRemove.push(local) + accountsToRemove.push(local) } }) - toAdd.forEach(upstream => this.addAccount(upstream)) - toRemove.forEach(local => this.removeAccount(local)) - this._updateAccounts() + this.addAccounts(accountsToAdd) + this.removeAccount(accountsToRemove) } /** - * Adds a new address to this AccountTracker's accounts object, which points to an empty object. This object will be + * Adds new addresses to track the balances of * given a balance as long this._currentBlockNumber is defined. * - * @param {string} address A hex address of a new account to store in this AccountTracker's accounts object + * @param {array} addresses An array of hex addresses of new accounts to track * */ - addAccount (address) { + addAccounts (addresses) { const accounts = this.store.getState().accounts - accounts[address] = {} + // add initial state for addresses + addresses.forEach(address => { + accounts[address] = {} + }) + // save accounts state this.store.updateState({ accounts }) + // fetch balances for the accounts if there is block number ready if (!this._currentBlockNumber) return - this._updateAccount(address) + addresses.forEach(address => this._updateAccount(address)) } /** - * Removes an account from this AccountTracker's accounts object + * Removes accounts from being tracked * - * @param {string} address A hex address of a the account to remove + * @param {array} an array of hex addresses to stop tracking * */ - removeAccount (address) { + removeAccount (addresses) { const accounts = this.store.getState().accounts - delete accounts[address] + // remove each state object + addresses.forEach(address => { + delete accounts[address] + }) + // save accounts state this.store.updateState({ accounts }) } @@ -118,71 +123,56 @@ class AccountTracker extends EventEmitter { * via EthQuery * * @private - * @param {object} block Data about the block that contains the data to update to. + * @param {number} blockNumber the block number to update to. * @fires 'block' The updated state, if all account updates are successful * */ - _updateForBlock (block) { - this._currentBlockNumber = block.number - const currentBlockGasLimit = block.gasLimit + async _updateForBlock (blockNumber) { + this._currentBlockNumber = blockNumber + // block gasLimit polling shouldn't be in account-tracker shouldn't be here... + const currentBlock = await this._query.getBlockByNumber(blockNumber, false) + if (!currentBlock) return + const currentBlockGasLimit = currentBlock.gasLimit this.store.updateState({ currentBlockGasLimit }) - async.parallel([ - this._updateAccounts.bind(this), - ], (err) => { - if (err) return console.error(err) - this.emit('block', this.store.getState()) - }) + try { + await this._updateAccounts() + } catch (err) { + log.error(err) + } } /** * Calls this._updateAccount for each account in this.store * - * @param {Function} cb A callback to pass to this._updateAccount, called after each account is successfully updated + * @returns {Promise} after all account balances updated * */ - _updateAccounts (cb = noop) { + async _updateAccounts () { const accounts = this.store.getState().accounts const addresses = Object.keys(accounts) - async.each(addresses, this._updateAccount.bind(this), cb) + await Promise.all(addresses.map(this._updateAccount.bind(this))) } /** - * Updates the current balance of an account. Gets an updated balance via this._getAccount. + * Updates the current balance of an account. * * @private * @param {string} address A hex address of a the account to be updated - * @param {Function} cb A callback to call once the account at address is successfully update + * @returns {Promise} after the account balance is updated * */ - _updateAccount (address, cb = noop) { - this._getAccount(address, (err, result) => { - if (err) return cb(err) - result.address = address - const accounts = this.store.getState().accounts - // only populate if the entry is still present - if (accounts[address]) { - accounts[address] = result - this.store.updateState({ accounts }) - } - cb(null, result) - }) - } - - /** - * Gets the current balance of an account via EthQuery. - * - * @private - * @param {string} address A hex address of a the account to query - * @param {Function} cb A callback to call once the account at address is successfully update - * - */ - _getAccount (address, cb = noop) { - const query = this._query - async.parallel({ - balance: query.getBalance.bind(query, address), - }, cb) + async _updateAccount (address) { + // query balance + const balance = await this._query.getBalance(address) + const result = { address, balance } + // update accounts state + const { accounts } = this.store.getState() + // only populate if the entry is still present + if (!accounts[address]) return + accounts[address] = result + this.store.updateState({ accounts }) } } diff --git a/app/scripts/lib/events-proxy.js b/app/scripts/lib/events-proxy.js deleted file mode 100644 index f83773ccc..000000000 --- a/app/scripts/lib/events-proxy.js +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Returns an EventEmitter that proxies events from the given event emitter - * @param {any} eventEmitter - * @param {object} listeners - The listeners to proxy to - * @returns {any} - */ -module.exports = function createEventEmitterProxy (eventEmitter, listeners) { - let target = eventEmitter - const eventHandlers = listeners || {} - const proxy = /** @type {any} */ (new Proxy({}, { - get: (_, name) => { - // intercept listeners - if (name === 'on') return addListener - if (name === 'setTarget') return setTarget - if (name === 'proxyEventHandlers') return eventHandlers - return (/** @type {any} */ (target))[name] - }, - set: (_, name, value) => { - target[name] = value - return true - }, - })) - function setTarget (/** @type {EventEmitter} */ eventEmitter) { - target = eventEmitter - // migrate listeners - Object.keys(eventHandlers).forEach((name) => { - /** @type {Array} */ (eventHandlers[name]).forEach((handler) => target.on(name, handler)) - }) - } - /** - * Attaches a function to be called whenever the specified event is emitted - * @param {string} name - * @param {Function} handler - */ - function addListener (name, handler) { - if (!eventHandlers[name]) eventHandlers[name] = [] - eventHandlers[name].push(handler) - target.on(name, handler) - } - if (listeners) proxy.setTarget(eventEmitter) - return proxy -} diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js index 901367f04..47925b94b 100644 --- a/app/scripts/lib/message-manager.js +++ b/app/scripts/lib/message-manager.js @@ -69,10 +69,39 @@ module.exports = class MessageManager extends EventEmitter { * new Message to this.messages, and to save the unapproved Messages from that list to this.memStore. * * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. + * @param {Object} req (optional) The original request object possibly containing the origin + * @returns {promise} after signature has been + * + */ + addUnapprovedMessageAsync (msgParams, req) { + return new Promise((resolve, reject) => { + const msgId = this.addUnapprovedMessage(msgParams, req) + // await finished + this.once(`${msgId}:finished`, (data) => { + switch (data.status) { + case 'signed': + return resolve(data.rawSig) + case 'rejected': + return reject(new Error('MetaMask Message Signature: User denied message signature.')) + default: + return reject(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`)) + } + }) + }) + } + + /** + * Creates a new Message with an 'unapproved' status using the passed msgParams. this.addMsg is called to add the + * new Message to this.messages, and to save the unapproved Messages from that list to this.memStore. + * + * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. + * @param {Object} req (optional) The original request object where the origin may be specificied * @returns {number} The id of the newly created message. * */ - addUnapprovedMessage (msgParams) { + addUnapprovedMessage (msgParams, req) { + // add origin from request + if (req) msgParams.origin = req.origin msgParams.data = normalizeMsgData(msgParams.data) // create txData obj with parameters and meta data var time = (new Date()).getTime() diff --git a/app/scripts/lib/personal-message-manager.js b/app/scripts/lib/personal-message-manager.js index e96ced1f2..fc2cccdf1 100644 --- a/app/scripts/lib/personal-message-manager.js +++ b/app/scripts/lib/personal-message-manager.js @@ -73,11 +73,43 @@ module.exports = class PersonalMessageManager extends EventEmitter { * this.memStore. * * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. + * @param {Object} req (optional) The original request object possibly containing the origin + * @returns {promise} When the message has been signed or rejected + * + */ + addUnapprovedMessageAsync (msgParams, req) { + return new Promise((resolve, reject) => { + if (!msgParams.from) { + reject(new Error('MetaMask Message Signature: from field is required.')) + } + const msgId = this.addUnapprovedMessage(msgParams, req) + this.once(`${msgId}:finished`, (data) => { + switch (data.status) { + case 'signed': + return resolve(data.rawSig) + case 'rejected': + return reject(new Error('MetaMask Message Signature: User denied message signature.')) + default: + return reject(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`)) + } + }) + }) + } + + /** + * Creates a new PersonalMessage with an 'unapproved' status using the passed msgParams. this.addMsg is called to add + * the new PersonalMessage to this.messages, and to save the unapproved PersonalMessages from that list to + * this.memStore. + * + * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. + * @param {Object} req (optional) The original request object possibly containing the origin * @returns {number} The id of the newly created PersonalMessage. * */ - addUnapprovedMessage (msgParams) { + addUnapprovedMessage (msgParams, req) { log.debug(`PersonalMessageManager addUnapprovedMessage: ${JSON.stringify(msgParams)}`) + // add origin from request + if (req) msgParams.origin = req.origin msgParams.data = this.normalizeMsgData(msgParams.data) // create txData obj with parameters and meta data var time = (new Date()).getTime() @@ -257,4 +289,3 @@ module.exports = class PersonalMessageManager extends EventEmitter { } } - diff --git a/app/scripts/lib/setupRaven.js b/app/scripts/lib/setupRaven.js index 3651524f1..e6e511640 100644 --- a/app/scripts/lib/setupRaven.js +++ b/app/scripts/lib/setupRaven.js @@ -70,11 +70,11 @@ function simplifyErrorMessages (report) { function rewriteErrorMessages (report, rewriteFn) { // rewrite top level message - if (report.message) report.message = rewriteFn(report.message) + if (typeof report.message === 'string') report.message = rewriteFn(report.message) // rewrite each exception message if (report.exception && report.exception.values) { report.exception.values.forEach(item => { - item.value = rewriteFn(item.value) + if (typeof item.value === 'string') item.value = rewriteFn(item.value) }) } } diff --git a/app/scripts/lib/typed-message-manager.js b/app/scripts/lib/typed-message-manager.js index c58921610..e5e1c94b3 100644 --- a/app/scripts/lib/typed-message-manager.js +++ b/app/scripts/lib/typed-message-manager.js @@ -72,11 +72,40 @@ module.exports = class TypedMessageManager extends EventEmitter { * this.memStore. Before any of this is done, msgParams are validated * * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. + * @param {Object} req (optional) The original request object possibly containing the origin + * @returns {promise} When the message has been signed or rejected + * + */ + addUnapprovedMessageAsync (msgParams, req) { + return new Promise((resolve, reject) => { + const msgId = this.addUnapprovedMessage(msgParams, req) + this.once(`${msgId}:finished`, (data) => { + switch (data.status) { + case 'signed': + return resolve(data.rawSig) + case 'rejected': + return reject(new Error('MetaMask Message Signature: User denied message signature.')) + default: + return reject(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`)) + } + }) + }) + } + + /** + * Creates a new TypedMessage with an 'unapproved' status using the passed msgParams. this.addMsg is called to add + * the new TypedMessage to this.messages, and to save the unapproved TypedMessages from that list to + * this.memStore. Before any of this is done, msgParams are validated + * + * @param {Object} msgParams The params for the eth_sign call to be made after the message is approved. + * @param {Object} req (optional) The original request object possibly containing the origin * @returns {number} The id of the newly created TypedMessage. * */ - addUnapprovedMessage (msgParams) { + addUnapprovedMessage (msgParams, req) { this.validateParams(msgParams) + // add origin from request + if (req) msgParams.origin = req.origin log.debug(`TypedMessageManager addUnapprovedMessage: ${JSON.stringify(msgParams)}`) // create txData obj with parameters and meta data diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js index d7423f2ad..ea13b26be 100644 --- a/app/scripts/lib/util.js +++ b/app/scripts/lib/util.js @@ -127,7 +127,21 @@ function BnMultiplyByFraction (targetBN, numerator, denominator) { return targetBN.mul(numBN).div(denomBN) } +function applyListeners (listeners, emitter) { + Object.keys(listeners).forEach((key) => { + emitter.on(key, listeners[key]) + }) +} + +function removeListeners (listeners, emitter) { + Object.keys(listeners).forEach((key) => { + emitter.removeListener(key, listeners[key]) + }) +} + module.exports = { + removeListeners, + applyListeners, getPlatform, getStack, getEnvironmentType, diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 1e1aa035f..585bb005e 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -46,7 +46,6 @@ const BN = require('ethereumjs-util').BN const GWEI_BN = new BN('1000000000') const percentile = require('percentile') const seedPhraseVerifier = require('./lib/seed-phrase-verifier') -const cleanErrorStack = require('./lib/cleanErrorStack') const log = require('loglevel') const TrezorKeyring = require('eth-trezor-keyring') const LedgerBridgeKeyring = require('eth-ledger-bridge-keyring') @@ -108,8 +107,9 @@ module.exports = class MetamaskController extends EventEmitter { this.blacklistController.scheduleUpdates() // rpc provider - this.provider = this.initializeProvider() - this.blockTracker = this.provider._blockTracker + this.initializeProvider() + this.provider = this.networkController.getProviderAndBlockTracker().provider + this.blockTracker = this.networkController.getProviderAndBlockTracker().blockTracker // token exchange rate tracker this.tokenRatesController = new TokenRatesController({ @@ -253,28 +253,22 @@ module.exports = class MetamaskController extends EventEmitter { static: { eth_syncing: false, web3_clientVersion: `MetaMask/v${version}`, - eth_sendTransaction: (payload, next, end) => { - const origin = payload.origin - const txParams = payload.params[0] - nodeify(this.txController.newUnapprovedTransaction, this.txController)(txParams, { origin }, end) - }, }, // account mgmt - getAccounts: (cb) => { + getAccounts: async () => { const isUnlocked = this.keyringController.memStore.getState().isUnlocked - const result = [] const selectedAddress = this.preferencesController.getSelectedAddress() - // only show address if account is unlocked if (isUnlocked && selectedAddress) { - result.push(selectedAddress) + return [selectedAddress] + } else { + return [] } - cb(null, result) }, // tx signing - // old style msg signing - processMessage: this.newUnsignedMessage.bind(this), - // personal_sign msg signing + processTransaction: this.newUnapprovedTransaction.bind(this), + // msg signing + processEthSignMessage: this.newUnsignedMessage.bind(this), processPersonalMessage: this.newUnsignedPersonalMessage.bind(this), processTypedMessage: this.newUnsignedTypedMessage.bind(this), } @@ -791,6 +785,18 @@ module.exports = class MetamaskController extends EventEmitter { // --------------------------------------------------------------------------- // Identity Management (signature operations) + /** + * Called when a Dapp suggests a new tx to be signed. + * this wrapper needs to exist so we can provide a reference to + * "newUnapprovedTransaction" before "txController" is instantiated + * + * @param {Object} msgParams - The params passed to eth_sign. + * @param {Object} req - (optional) the original request, containing the origin + */ + async newUnapprovedTransaction (txParams, req) { + return await this.txController.newUnapprovedTransaction(txParams, req) + } + // eth_sign methods: /** @@ -802,20 +808,11 @@ module.exports = class MetamaskController extends EventEmitter { * @param {Object} msgParams - The params passed to eth_sign. * @param {Function} cb = The callback function called with the signature. */ - newUnsignedMessage (msgParams, cb) { - const msgId = this.messageManager.addUnapprovedMessage(msgParams) + newUnsignedMessage (msgParams, req) { + const promise = this.messageManager.addUnapprovedMessageAsync(msgParams, req) this.sendUpdate() this.opts.showUnconfirmedMessage() - this.messageManager.once(`${msgId}:finished`, (data) => { - switch (data.status) { - case 'signed': - return cb(null, data.rawSig) - case 'rejected': - return cb(cleanErrorStack(new Error('MetaMask Message Signature: User denied message signature.'))) - default: - return cb(cleanErrorStack(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))) - } - }) + return promise } /** @@ -869,24 +866,11 @@ module.exports = class MetamaskController extends EventEmitter { * @param {Function} cb - The callback function called with the signature. * Passed back to the requesting Dapp. */ - newUnsignedPersonalMessage (msgParams, cb) { - if (!msgParams.from) { - return cb(cleanErrorStack(new Error('MetaMask Message Signature: from field is required.'))) - } - - const msgId = this.personalMessageManager.addUnapprovedMessage(msgParams) + async newUnsignedPersonalMessage (msgParams, req) { + const promise = this.personalMessageManager.addUnapprovedMessageAsync(msgParams, req) this.sendUpdate() this.opts.showUnconfirmedMessage() - this.personalMessageManager.once(`${msgId}:finished`, (data) => { - switch (data.status) { - case 'signed': - return cb(null, data.rawSig) - case 'rejected': - return cb(cleanErrorStack(new Error('MetaMask Message Signature: User denied message signature.'))) - default: - return cb(cleanErrorStack(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))) - } - }) + return promise } /** @@ -935,26 +919,11 @@ module.exports = class MetamaskController extends EventEmitter { * @param {Object} msgParams - The params passed to eth_signTypedData. * @param {Function} cb - The callback function, called with the signature. */ - newUnsignedTypedMessage (msgParams, cb) { - let msgId - try { - msgId = this.typedMessageManager.addUnapprovedMessage(msgParams) - this.sendUpdate() - this.opts.showUnconfirmedMessage() - } catch (e) { - return cb(e) - } - - this.typedMessageManager.once(`${msgId}:finished`, (data) => { - switch (data.status) { - case 'signed': - return cb(null, data.rawSig) - case 'rejected': - return cb(cleanErrorStack(new Error('MetaMask Message Signature: User denied message signature.'))) - default: - return cb(cleanErrorStack(new Error(`MetaMask Message Signature: Unknown problem: ${JSON.stringify(msgParams)}`))) - } - }) + newUnsignedTypedMessage (msgParams, req) { + const promise = this.typedMessageManager.addUnapprovedMessageAsync(msgParams, req) + this.sendUpdate() + this.opts.showUnconfirmedMessage() + return promise } /** @@ -1219,7 +1188,7 @@ module.exports = class MetamaskController extends EventEmitter { // create filter polyfill middleware const filterMiddleware = createFilterMiddleware({ provider: this.provider, - blockTracker: this.provider._blockTracker, + blockTracker: this.blockTracker, }) engine.push(createOriginMiddleware({ origin })) -- cgit