Update AddressBookController to read from preferences store

feature/default_network_editable
Whymarrh Whitby 7 years ago
parent 924cc1fcf7
commit cbe4d0d88c
  1. 27
      app/scripts/controllers/address-book.js
  2. 3
      app/scripts/metamask-controller.js
  3. 26
      test/unit/address-book-controller.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

@ -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({

@ -1,26 +1,26 @@
const assert = require('assert')
const AddressBookController = require('../../app/scripts/controllers/address-book')
const mockKeyringController = {
memStore: {
getState: function () {
return {
identities: {
'0x0aaa': {
address: '0x0aaa',
name: 'owned',
},
const stubPreferencesStore = {
getState: function () {
return {
identities: {
'0x0aaa': {
address: '0x0aaa',
name: 'owned',
},
}
},
},
}
},
}
};
describe('address-book-controller', function () {
var addressBookController
beforeEach(function () {
addressBookController = new AddressBookController({}, mockKeyringController)
addressBookController = new AddressBookController({
preferencesStore: stubPreferencesStore,
})
})
describe('addres book management', function () {

Loading…
Cancel
Save