aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2018-05-23 04:20:04 +0800
committerkumavis <aaron@kumavis.me>2018-05-23 04:20:04 +0800
commit3a80f3dd836889b7e4f4d4db8eaf42883623d9bf (patch)
treebcde7f3a6acbdf311bf66b70be6bfac2b3ed088a /app/scripts
parent3084dc47d10e3e455c924e5aad0b0961c500ec8d (diff)
parentcee55b47d03006630b1dbe038c6008654ca8f674 (diff)
downloadtangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar
tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.gz
tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.bz2
tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.lz
tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.xz
tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.zst
tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.zip
Merge branch 'develop' of github.com:MetaMask/metamask-extension into network-remove-provider-engine
Diffstat (limited to 'app/scripts')
-rw-r--r--app/scripts/background.js2
-rw-r--r--app/scripts/contentscript.js3
-rw-r--r--app/scripts/controllers/address-book.js27
-rw-r--r--app/scripts/controllers/preferences.js30
-rw-r--r--app/scripts/controllers/transactions/index.js15
-rw-r--r--app/scripts/lib/createErrorMiddleware.js66
-rw-r--r--app/scripts/lib/inpage-provider.js2
-rw-r--r--app/scripts/metamask-controller.js27
-rw-r--r--app/scripts/migrations/026.js47
-rw-r--r--app/scripts/migrations/index.js1
10 files changed, 169 insertions, 51 deletions
diff --git a/app/scripts/background.js b/app/scripts/background.js
index 69d549c85..686296329 100644
--- a/app/scripts/background.js
+++ b/app/scripts/background.js
@@ -322,7 +322,7 @@ function setupController (initState, initLangCode) {
/**
* A runtime.Port object, as provided by the browser:
- * @link https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/Port
+ * @see https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/Port
* @typedef Port
* @type Object
*/
diff --git a/app/scripts/contentscript.js b/app/scripts/contentscript.js
index ddf1a9432..555902ddf 100644
--- a/app/scripts/contentscript.js
+++ b/app/scripts/contentscript.js
@@ -166,7 +166,7 @@ function documentElementCheck () {
/**
* Checks if the current domain is blacklisted
- *
+ *
* @returns {boolean} {@code true} if the current domain is blacklisted
*/
function blacklistedDomainCheck () {
@@ -175,6 +175,7 @@ function blacklistedDomainCheck () {
'dropbox.com',
'webbyawards.com',
'cdn.shopify.com/s/javascripts/tricorder/xtld-read-only-frame.html',
+ 'adyen.com',
]
var currentUrl = window.location.href
var currentRegex
diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js
index c91e6b2e4..4697e074c 100644
--- a/app/scripts/controllers/address-book.js
+++ b/app/scripts/controllers/address-book.js
@@ -13,19 +13,17 @@ class AddressBookController {
* @param {object} opts Overrides the defaults for the initial state of this.store
* @property {array} opts.initState initializes the the state of the AddressBookController. Can contain an
* addressBook property to initialize the addressBook array
- * @param {KeyringController} keyringController (Soon to be deprecated) The keyringController used in the current
- * MetamaskController. Contains the identities used in this AddressBookController.
+ * @property {object} opts.preferencesStore the {@code PreferencesController} store
* @property {object} store The the store of the current users address book
* @property {array} store.addressBook An array of addresses and nicknames. These are set by the user when sending
* to a new address.
*
*/
- constructor (opts = {}, keyringController) {
- const initState = extend({
+ constructor ({initState, preferencesStore}) {
+ this.store = new ObservableStore(extend({
addressBook: [],
- }, opts.initState)
- this.store = new ObservableStore(initState)
- this.keyringController = keyringController
+ }, initState))
+ this._preferencesStore = preferencesStore
}
//
@@ -62,7 +60,7 @@ class AddressBookController {
*/
_addToAddressBook (address, name) {
const addressBook = this._getAddressBook()
- const identities = this._getIdentities()
+ const {identities} = this._preferencesStore.getState()
const addressBookIndex = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name })
const identitiesIndex = Object.keys(identities).findIndex((element) => { return element.toLowerCase() === address.toLowerCase() })
@@ -95,19 +93,6 @@ class AddressBookController {
_getAddressBook () {
return this.store.getState().addressBook
}
-
- /**
- * Retrieves identities from the keyring controller in order to avoid
- * duplication
- *
- * @deprecated
- * @returns {array} Returns the identies array from the keyringContoller's state
- *
- */
- _getIdentities () {
- return this.keyringController.memStore.getState().identities
- }
-
}
module.exports = AddressBookController
diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js
index 1d3308d36..a4ff1207e 100644
--- a/app/scripts/controllers/preferences.js
+++ b/app/scripts/controllers/preferences.js
@@ -27,6 +27,7 @@ class PreferencesController {
useBlockie: false,
featureFlags: {},
currentLocale: opts.initLangCode,
+ identities: {},
}, opts.initState)
this.store = new ObservableStore(initState)
}
@@ -62,6 +63,16 @@ class PreferencesController {
this.store.updateState({ currentLocale: key })
}
+ setAddresses (addresses) {
+ const oldIdentities = this.store.getState().identities
+ const identities = addresses.reduce((ids, address, index) => {
+ const oldId = oldIdentities[address] || {}
+ ids[address] = {name: `Account ${index + 1}`, address, ...oldId}
+ return ids
+ }, {})
+ this.store.updateState({ identities })
+ }
+
/**
* Setter for the `selectedAddress` property
*
@@ -156,6 +167,21 @@ class PreferencesController {
}
/**
+ * Sets a custom label for an account
+ * @param {string} account the account to set a label for
+ * @param {string} label the custom label for the account
+ * @return {Promise<string>}
+ */
+ setAccountLabel (account, label) {
+ const address = normalizeAddress(account)
+ const {identities} = this.store.getState()
+ identities[address] = identities[address] || {}
+ identities[address].name = label
+ this.store.updateState({ identities })
+ return Promise.resolve(label)
+ }
+
+ /**
* Gets an updated rpc list from this.addToFrequentRpcList() and sets the `frequentRpcList` to this update list.
*
* @param {string} _url The the new rpc url to add to the updated list
@@ -189,8 +215,8 @@ class PreferencesController {
* The returned list will have a max length of 2. If the _url currently exists it the list, it will be moved to the
* end of the list. The current list is modified and returned as a promise.
*
- * @param {string} _url The rpc url to add to the frequentRpcList.
- * @returns {Promise<array>} The updated frequentRpcList.
+ * @param {string} _url The rpc url to add to the frequentRpcList.
+ * @returns {Promise<array>} The updated frequentRpcList.
*
*/
addToFrequentRpcList (_url) {
diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js
index cb3d28f1d..128f5b86f 100644
--- a/app/scripts/controllers/transactions/index.js
+++ b/app/scripts/controllers/transactions/index.js
@@ -113,21 +113,6 @@ class TransactionController extends EventEmitter {
}
/**
- Check if a txMeta in the list with the same nonce has been confirmed in a block
- if the txParams dont have a nonce will return false
- @returns {boolean} whether the nonce has been used in a transaction confirmed in a block
- @param {object} txMeta - the txMeta object
- */
- async isNonceTaken (txMeta) {
- const { from, nonce } = txMeta.txParams
- if ('nonce' in txMeta.txParams) {
- const sameNonceTxList = this.txStateManager.getFilteredTxList({from, nonce, status: 'confirmed'})
- return (sameNonceTxList.length >= 1)
- }
- return false
- }
-
- /**
add a new unapproved transaction to the pipeline
@returns {Promise<string>} the hash of the transaction after being submitted to the network
diff --git a/app/scripts/lib/createErrorMiddleware.js b/app/scripts/lib/createErrorMiddleware.js
new file mode 100644
index 000000000..baed99e45
--- /dev/null
+++ b/app/scripts/lib/createErrorMiddleware.js
@@ -0,0 +1,66 @@
+const log = require('loglevel')
+
+/**
+ * JSON-RPC error object
+ *
+ * @typedef {Object} RpcError
+ * @property {number} code - Indicates the error type that occurred
+ * @property {Object} [data] - Contains additional information about the error
+ * @property {string} [message] - Short description of the error
+ */
+
+/**
+ * Middleware configuration object
+ *
+ * @typedef {Object} MiddlewareConfig
+ * @property {boolean} [override] - Use RPC_ERRORS message in place of provider message
+ */
+
+/**
+ * Map of standard and non-standard RPC error codes to messages
+ */
+const RPC_ERRORS = {
+ 1: 'An unauthorized action was attempted.',
+ 2: 'A disallowed action was attempted.',
+ 3: 'An execution error occurred.',
+ [-32600]: 'The JSON sent is not a valid Request object.',
+ [-32601]: 'The method does not exist / is not available.',
+ [-32602]: 'Invalid method parameter(s).',
+ [-32603]: 'Internal JSON-RPC error.',
+ [-32700]: 'Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.',
+ internal: 'Internal server error.',
+ unknown: 'Unknown JSON-RPC error.',
+}
+
+/**
+ * Modifies a JSON-RPC error object in-place to add a human-readable message,
+ * optionally overriding any provider-supplied message
+ *
+ * @param {RpcError} error - JSON-RPC error object
+ * @param {boolean} override - Use RPC_ERRORS message in place of provider message
+ */
+function sanitizeRPCError (error, override) {
+ if (error.message && !override) { return error }
+ const message = error.code > -31099 && error.code < -32100 ? RPC_ERRORS.internal : RPC_ERRORS[error.code]
+ error.message = message || RPC_ERRORS.unknown
+}
+
+/**
+ * json-rpc-engine middleware that both logs standard and non-standard error
+ * messages and ends middleware stack traversal if an error is encountered
+ *
+ * @param {MiddlewareConfig} [config={override:true}] - Middleware configuration
+ * @returns {Function} json-rpc-engine middleware function
+ */
+function createErrorMiddleware ({ override = true } = {}) {
+ return (req, res, next) => {
+ next(done => {
+ const { error } = res
+ if (!error) { return done() }
+ sanitizeRPCError(error)
+ log.error(`MetaMask - RPC Error: ${error.message}`, error)
+ })
+ }
+}
+
+module.exports = createErrorMiddleware \ No newline at end of file
diff --git a/app/scripts/lib/inpage-provider.js b/app/scripts/lib/inpage-provider.js
index 99cc5d2cf..4e65f0a23 100644
--- a/app/scripts/lib/inpage-provider.js
+++ b/app/scripts/lib/inpage-provider.js
@@ -1,5 +1,6 @@
const pump = require('pump')
const RpcEngine = require('json-rpc-engine')
+const createErrorMiddleware = require('./createErrorMiddleware')
const createIdRemapMiddleware = require('json-rpc-engine/src/idRemapMiddleware')
const createStreamMiddleware = require('json-rpc-middleware-stream')
const LocalStorageStore = require('obs-store')
@@ -44,6 +45,7 @@ function MetamaskInpageProvider (connectionStream) {
// handle sendAsync requests via dapp-side rpc engine
const rpcEngine = new RpcEngine()
rpcEngine.push(createIdRemapMiddleware())
+ rpcEngine.push(createErrorMiddleware())
rpcEngine.push(streamMiddleware)
self.rpcEngine = rpcEngine
}
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 7f4f2fc9b..14eb749e7 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -145,7 +145,8 @@ module.exports = class MetamaskController extends EventEmitter {
// address book controller
this.addressBookController = new AddressBookController({
initState: initState.AddressBookController,
- }, this.keyringController)
+ preferencesStore: this.preferencesController.store,
+ })
// tx mgmt
this.txController = new TransactionController({
@@ -364,6 +365,7 @@ module.exports = class MetamaskController extends EventEmitter {
addToken: nodeify(preferencesController.addToken, preferencesController),
removeToken: nodeify(preferencesController.removeToken, preferencesController),
setCurrentAccountTab: nodeify(preferencesController.setCurrentAccountTab, preferencesController),
+ setAccountLabel: nodeify(preferencesController.setAccountLabel, preferencesController),
setFeatureFlag: nodeify(preferencesController.setFeatureFlag, preferencesController),
// AddressController
@@ -374,7 +376,6 @@ module.exports = class MetamaskController extends EventEmitter {
createNewVaultAndKeychain: nodeify(this.createNewVaultAndKeychain, this),
createNewVaultAndRestore: nodeify(this.createNewVaultAndRestore, this),
addNewKeyring: nodeify(keyringController.addNewKeyring, keyringController),
- saveAccountLabel: nodeify(keyringController.saveAccountLabel, keyringController),
exportAccount: nodeify(keyringController.exportAccount, keyringController),
// txController
@@ -382,7 +383,7 @@ module.exports = class MetamaskController extends EventEmitter {
updateTransaction: nodeify(txController.updateTransaction, txController),
updateAndApproveTransaction: nodeify(txController.updateAndApproveTransaction, txController),
retryTransaction: nodeify(this.retryTransaction, this),
- isNonceTaken: nodeify(txController.isNonceTaken, txController),
+ getFilteredTxList: nodeify(txController.getFilteredTxList, txController),
// messageManager
signMessage: nodeify(this.signMessage, this),
@@ -434,7 +435,9 @@ module.exports = class MetamaskController extends EventEmitter {
} else {
vault = await this.keyringController.createNewVaultAndKeychain(password)
- this.selectFirstIdentity(vault)
+ const accounts = await this.keyringController.getAccounts()
+ this.preferencesController.setAddresses(accounts)
+ this.selectFirstIdentity()
}
release()
} catch (err) {
@@ -454,7 +457,9 @@ module.exports = class MetamaskController extends EventEmitter {
const release = await this.createVaultMutex.acquire()
try {
const vault = await this.keyringController.createNewVaultAndRestore(password, seed)
- this.selectFirstIdentity(vault)
+ const accounts = await this.keyringController.getAccounts()
+ this.preferencesController.setAddresses(accounts)
+ this.selectFirstIdentity()
release()
return vault
} catch (err) {
@@ -472,12 +477,10 @@ module.exports = class MetamaskController extends EventEmitter {
*/
/**
- * Retrieves the first Identiy from the passed Vault and selects the related address
- *
- * @param {} vault
+ * Sets the first address in the state to the selected address
*/
- selectFirstIdentity (vault) {
- const { identities } = vault
+ selectFirstIdentity () {
+ const { identities } = this.preferencesController.store.getState()
const address = Object.keys(identities)[0]
this.preferencesController.setSelectedAddress(address)
}
@@ -503,13 +506,15 @@ module.exports = class MetamaskController extends EventEmitter {
await this.verifySeedPhrase()
+ this.preferencesController.setAddresses(newAccounts)
newAccounts.forEach((address) => {
if (!oldAccounts.includes(address)) {
this.preferencesController.setSelectedAddress(address)
}
})
- return keyState
+ const {identities} = this.preferencesController.store.getState()
+ return {...keyState, identities}
}
/**
diff --git a/app/scripts/migrations/026.js b/app/scripts/migrations/026.js
new file mode 100644
index 000000000..1b8a91a45
--- /dev/null
+++ b/app/scripts/migrations/026.js
@@ -0,0 +1,47 @@
+const version = 26
+
+/*
+
+This migration moves the identities stored in the KeyringController
+ into the PreferencesController
+
+*/
+
+const clone = require('clone')
+
+module.exports = {
+ version,
+ migrate (originalVersionedData) {
+ const versionedData = clone(originalVersionedData)
+ versionedData.meta.version = version
+ try {
+ const state = versionedData.data
+ versionedData.data = transformState(state)
+ } catch (err) {
+ console.warn(`MetaMask Migration #${version}` + err.stack)
+ return Promise.reject(err)
+ }
+ return Promise.resolve(versionedData)
+ },
+}
+
+function transformState (state) {
+ if (!state.KeyringController || !state.PreferencesController) {
+ return
+ }
+
+ if (!state.KeyringController.walletNicknames) {
+ return state
+ }
+
+ state.PreferencesController.identities = Object.keys(state.KeyringController.walletNicknames)
+ .reduce((identities, address) => {
+ identities[address] = {
+ name: state.KeyringController.walletNicknames[address],
+ address,
+ }
+ return identities
+ }, {})
+ delete state.KeyringController.walletNicknames
+ return state
+}
diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js
index 6c4a51b32..04d90bfff 100644
--- a/app/scripts/migrations/index.js
+++ b/app/scripts/migrations/index.js
@@ -36,4 +36,5 @@ module.exports = [
require('./023'),
require('./024'),
require('./025'),
+ require('./026'),
]