diff options
author | frankiebee <frankie.diamond@gmail.com> | 2018-03-08 09:16:35 +0800 |
---|---|---|
committer | frankiebee <frankie.diamond@gmail.com> | 2018-03-08 09:16:35 +0800 |
commit | 8adb03074c6315b4bdcffdd22abca2c84c7a6985 (patch) | |
tree | 28ef1c54f5287206f89db5276df3a82bba00ae78 /app/scripts | |
parent | 62febac87659ddf78a34dd0dac1ee8a38d8c8e77 (diff) | |
parent | 303801d2768a264a27a51916e5debf778739ee0c (diff) | |
download | tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.tar tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.tar.gz tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.tar.bz2 tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.tar.lz tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.tar.xz tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.tar.zst tangerine-wallet-browser-8adb03074c6315b4bdcffdd22abca2c84c7a6985.zip |
Merge branch 'master' into retry-tx-refractor
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/background.js | 18 | ||||
-rw-r--r-- | app/scripts/edge-encryptor.js | 69 | ||||
-rw-r--r-- | app/scripts/lib/reportFailedTxToSentry.js | 38 | ||||
-rw-r--r-- | app/scripts/lib/setupRaven.js (renamed from app/scripts/setupRaven.js) | 2 | ||||
-rw-r--r-- | app/scripts/popup.js | 2 |
5 files changed, 121 insertions, 8 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js index 476d073d1..601ae0372 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -13,8 +13,11 @@ const PortStream = require('./lib/port-stream.js') const NotificationManager = require('./lib/notification-manager.js') const MetamaskController = require('./metamask-controller') const firstTimeState = require('./first-time-state') -const setupRaven = require('./setupRaven') +const setupRaven = require('./lib/setupRaven') +const reportFailedTxToSentry = require('./lib/reportFailedTxToSentry') const setupMetamaskMeshMetrics = require('./lib/setupMetamaskMeshMetrics') +const EdgeEncryptor = require('./edge-encryptor') + const STORAGE_KEY = 'metamask-config' const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' @@ -30,6 +33,12 @@ global.METAMASK_NOTIFIER = notificationManager const release = platform.getVersion() const raven = setupRaven({ release }) +// browser check if it is Edge - https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser +// Internet Explorer 6-11 +const isIE = !!document.documentMode +// Edge 20+ +const isEdge = !isIE && !!window.StyleMedia + let popupIsOpen = false let openMetamaskTabsIDs = {} @@ -79,6 +88,7 @@ function setupController (initState) { initState, // platform specific api platform, + encryptor: isEdge ? new EdgeEncryptor() : undefined, }) global.metamaskController = controller @@ -86,11 +96,7 @@ function setupController (initState) { controller.txController.on(`tx:status-update`, (txId, status) => { if (status !== 'failed') return const txMeta = controller.txController.txStateManager.getTx(txId) - const errorMessage = `Transaction Failed: ${txMeta.err.message}` - raven.captureMessage(errorMessage, { - // "extra" key is required by Sentry - extra: txMeta, - }) + reportFailedTxToSentry({ raven, txMeta }) }) // setup state persistence diff --git a/app/scripts/edge-encryptor.js b/app/scripts/edge-encryptor.js new file mode 100644 index 000000000..24c0c93a8 --- /dev/null +++ b/app/scripts/edge-encryptor.js @@ -0,0 +1,69 @@ +const asmcrypto = require('asmcrypto.js') +const Unibabel = require('browserify-unibabel') + +class EdgeEncryptor { + + encrypt (password, dataObject) { + + var salt = this._generateSalt() + return this._keyFromPassword(password, salt) + .then(function (key) { + + var data = JSON.stringify(dataObject) + var dataBuffer = Unibabel.utf8ToBuffer(data) + var vector = global.crypto.getRandomValues(new Uint8Array(16)) + var resultbuffer = asmcrypto.AES_GCM.encrypt(dataBuffer, key, vector) + + var buffer = new Uint8Array(resultbuffer) + var vectorStr = Unibabel.bufferToBase64(vector) + var vaultStr = Unibabel.bufferToBase64(buffer) + return JSON.stringify({ + data: vaultStr, + iv: vectorStr, + salt: salt, + }) + }) + } + + decrypt (password, text) { + + const payload = JSON.parse(text) + const salt = payload.salt + return this._keyFromPassword(password, salt) + .then(function (key) { + const encryptedData = Unibabel.base64ToBuffer(payload.data) + const vector = Unibabel.base64ToBuffer(payload.iv) + return new Promise((resolve, reject) => { + var result + try { + result = asmcrypto.AES_GCM.decrypt(encryptedData, key, vector) + } catch (err) { + return reject(new Error('Incorrect password')) + } + const decryptedData = new Uint8Array(result) + const decryptedStr = Unibabel.bufferToUtf8(decryptedData) + const decryptedObj = JSON.parse(decryptedStr) + resolve(decryptedObj) + }) + }) + } + + _keyFromPassword (password, salt) { + + var passBuffer = Unibabel.utf8ToBuffer(password) + var saltBuffer = Unibabel.base64ToBuffer(salt) + return new Promise((resolve) => { + var key = asmcrypto.PBKDF2_HMAC_SHA256.bytes(passBuffer, saltBuffer, 10000) + resolve(key) + }) + } + + _generateSalt (byteCount = 32) { + var view = new Uint8Array(byteCount) + global.crypto.getRandomValues(view) + var b64encoded = btoa(String.fromCharCode.apply(null, view)) + return b64encoded + } +} + +module.exports = EdgeEncryptor diff --git a/app/scripts/lib/reportFailedTxToSentry.js b/app/scripts/lib/reportFailedTxToSentry.js new file mode 100644 index 000000000..ee73f6845 --- /dev/null +++ b/app/scripts/lib/reportFailedTxToSentry.js @@ -0,0 +1,38 @@ +const ethJsRpcSlug = 'Error: [ethjs-rpc] rpc error with payload ' +const errorLabelPrefix = 'Error: ' + +module.exports = reportFailedTxToSentry + +// +// utility for formatting failed transaction messages +// for sending to sentry +// + +function reportFailedTxToSentry({ raven, txMeta }) { + const errorMessage = extractErrorMessage(txMeta.err.message) + raven.captureMessage(errorMessage, { + // "extra" key is required by Sentry + extra: txMeta, + }) +} + +// +// ethjs-rpc provides overly verbose error messages +// if we detect this type of message, we extract the important part +// Below is an example input and output +// +// Error: [ethjs-rpc] rpc error with payload {"id":3947817945380,"jsonrpc":"2.0","params":["0xf8eb8208708477359400830398539406012c8cf97bead5deae237070f9587f8e7a266d80b8843d7d3f5a0000000000000000000000000000000000000000000000000000000000081d1a000000000000000000000000000000000000000000000000001ff973cafa800000000000000000000000000000000000000000000000000000038d7ea4c68000000000000000000000000000000000000000000000000000000000000003f48025a04c32a9b630e0d9e7ff361562d850c86b7a884908135956a7e4a336fa0300d19ca06830776423f25218e8d19b267161db526e66895567147015b1f3fc47aef9a3c7"],"method":"eth_sendRawTransaction"} Error: replacement transaction underpriced +// +// Transaction Failed: replacement transaction underpriced +// + +function extractErrorMessage(errorMessage) { + const isEthjsRpcError = errorMessage.includes(ethJsRpcSlug) + if (isEthjsRpcError) { + const payloadAndError = errorMessage.slice(ethJsRpcSlug.length) + const originalError = payloadAndError.slice(payloadAndError.indexOf(errorLabelPrefix) + errorLabelPrefix.length) + return `Transaction Failed: ${originalError}` + } else { + return `Transaction Failed: ${errorMessage}` + } +} diff --git a/app/scripts/setupRaven.js b/app/scripts/lib/setupRaven.js index 7beffeff9..42e48cb90 100644 --- a/app/scripts/setupRaven.js +++ b/app/scripts/lib/setupRaven.js @@ -1,4 +1,4 @@ -const Raven = require('./vendor/raven.min.js') +const Raven = require('../vendor/raven.min.js') const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG' const PROD = 'https://3567c198f8a8412082d32655da2961d0@sentry.io/273505' const DEV = 'https://f59f3dd640d2429d9d0e2445a87ea8e1@sentry.io/273496' diff --git a/app/scripts/popup.js b/app/scripts/popup.js index 53ab00e00..11d50ee87 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -8,7 +8,7 @@ const extension = require('extensionizer') const ExtensionPlatform = require('./platforms/extension') const NotificationManager = require('./lib/notification-manager') const notificationManager = new NotificationManager() -const setupRaven = require('./setupRaven') +const setupRaven = require('./lib/setupRaven') // create platform global global.platform = new ExtensionPlatform() |