aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2018-05-22 04:44:07 +0800
committerGitHub <noreply@github.com>2018-05-22 04:44:07 +0800
commit08d95bbafa3f952b960124c36958edbafa57cb3d (patch)
tree7935b16f4a926895b2d0efee6589fd48e8780c31 /app/scripts
parentb426b5e58cc136c429ebb7659f414155139f5020 (diff)
parent67310e151ea75c7aa4fc00bfd63bf41cd4f2db51 (diff)
downloadtangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.tar
tangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.tar.gz
tangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.tar.bz2
tangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.tar.lz
tangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.tar.xz
tangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.tar.zst
tangerine-wallet-browser-08d95bbafa3f952b960124c36958edbafa57cb3d.zip
Merge pull request #4034 from whymarrh/account-nicknames
Move account names out of KeyringController
Diffstat (limited to 'app/scripts')
-rw-r--r--app/scripts/controllers/address-book.js27
-rw-r--r--app/scripts/controllers/preferences.js30
-rw-r--r--app/scripts/metamask-controller.js25
-rw-r--r--app/scripts/migrations/026.js47
-rw-r--r--app/scripts/migrations/index.js1
5 files changed, 97 insertions, 33 deletions
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/metamask-controller.js b/app/scripts/metamask-controller.js
index a6b5d3453..807c9a0b9 100644
--- a/app/scripts/metamask-controller.js
+++ b/app/scripts/metamask-controller.js
@@ -144,7 +144,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({
@@ -363,6 +364,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
@@ -373,7 +375,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
@@ -433,7 +434,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) {
@@ -453,7 +456,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) {
@@ -471,12 +476,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)
}
@@ -502,13 +505,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'),
]