From 5987eb360f1dd94c7ca2e4966fb4446a213059fe Mon Sep 17 00:00:00 2001 From: Paul Bouchon Date: Wed, 28 Nov 2018 11:07:08 -0500 Subject: [PATCH] gaba: integrate AddressBookController --- app/scripts/controllers/address-book.js | 98 ------------------- app/scripts/lib/ComposableObservableStore.js | 4 +- app/scripts/metamask-controller.js | 15 +-- package.json | 1 + .../controllers/address-book-controller.js | 54 ---------- 5 files changed, 9 insertions(+), 163 deletions(-) delete mode 100644 app/scripts/controllers/address-book.js delete mode 100644 test/unit/app/controllers/address-book-controller.js diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js deleted file mode 100644 index 4697e074c9bd..000000000000 --- a/app/scripts/controllers/address-book.js +++ /dev/null @@ -1,98 +0,0 @@ -const ObservableStore = require('obs-store') -const extend = require('xtend') - -class AddressBookController { - - - /** - * Controller in charge of managing the address book functionality from the - * recipients field on the send screen. Manages a history of all saved - * addresses and all currently owned addresses. - * - * @typedef {Object} 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 - * @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 ({initState, preferencesStore}) { - this.store = new ObservableStore(extend({ - addressBook: [], - }, initState)) - this._preferencesStore = preferencesStore - } - - // - // PUBLIC METHODS - // - - /** - * Sets a new address book in store by accepting a new address and nickname. - * - * @param {string} address A hex address of a new account that the user is sending to. - * @param {string} name The name the user wishes to associate with the new account - * @returns {Promise} Promise resolves with undefined - * - */ - setAddressBook (address, name) { - return this._addToAddressBook(address, name) - .then((addressBook) => { - this.store.updateState({ - addressBook, - }) - return Promise.resolve() - }) - } - - /** - * Performs the logic to add the address and name into the address book. The pushed object is an object of two - * fields. Current behavior does not set an upper limit to the number of addresses. - * - * @private - * @param {string} address A hex address of a new account that the user is sending to. - * @param {string} name The name the user wishes to associate with the new account - * @returns {Promise} Promises the updated addressBook array - * - */ - _addToAddressBook (address, name) { - const addressBook = this._getAddressBook() - 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() }) - // trigger this condition if we own this address--no need to overwrite. - if (identitiesIndex !== -1) { - return Promise.resolve(addressBook) - // trigger this condition if we've seen this address before--may need to update nickname. - } else if (addressBookIndex !== -1) { - addressBook.splice(addressBookIndex, 1) - } else if (addressBook.length > 15) { - addressBook.shift() - } - - - addressBook.push({ - address: address, - name, - }) - return Promise.resolve(addressBook) - } - - /** - * Internal method to get the address book. Current persistence behavior should not require that this method be - * called from the UI directly. - * - * @private - * @returns {array} The addressBook array from the store. - * - */ - _getAddressBook () { - return this.store.getState().addressBook - } -} - -module.exports = AddressBookController diff --git a/app/scripts/lib/ComposableObservableStore.js b/app/scripts/lib/ComposableObservableStore.js index d5ee708a1814..abb77967291b 100644 --- a/app/scripts/lib/ComposableObservableStore.js +++ b/app/scripts/lib/ComposableObservableStore.js @@ -40,7 +40,9 @@ class ComposableObservableStore extends ObservableStore { getFlatState () { let flatState = {} for (const key in this.config) { - flatState = { ...flatState, ...this.config[key].getState() } + const controller = this.config[key] + const state = controller.getState ? controller.getState() : controller.state + flatState = { ...flatState, ...state } } return flatState } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 41c3e364247b..229369d79f0c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -26,7 +26,6 @@ const PreferencesController = require('./controllers/preferences') const CurrencyController = require('./controllers/currency') const NoticeController = require('./notice-controller') const ShapeShiftController = require('./controllers/shapeshift') -const AddressBookController = require('./controllers/address-book') const InfuraController = require('./controllers/infura') const BlacklistController = require('./controllers/blacklist') const CachedBalancesController = require('./controllers/cached-balances') @@ -55,6 +54,7 @@ const HW_WALLETS_KEYRINGS = [TrezorKeyring.type, LedgerBridgeKeyring.type] const EthQuery = require('eth-query') const ethUtil = require('ethereumjs-util') const sigUtil = require('eth-sig-util') +const { AddressBookController } = require('gaba') module.exports = class MetamaskController extends EventEmitter { @@ -175,11 +175,7 @@ module.exports = class MetamaskController extends EventEmitter { keyringMemStore: this.keyringController.memStore, }) - // address book controller - this.addressBookController = new AddressBookController({ - initState: initState.AddressBookController, - preferencesStore: this.preferencesController.store, - }) + this.addressBookController = new AddressBookController(undefined, initState.AddressBookController) // tx mgmt this.txController = new TransactionController({ @@ -245,7 +241,7 @@ module.exports = class MetamaskController extends EventEmitter { TransactionController: this.txController.store, KeyringController: this.keyringController.store, PreferencesController: this.preferencesController.store, - AddressBookController: this.addressBookController.store, + AddressBookController: this.addressBookController, CurrencyController: this.currencyController.store, NoticeController: this.noticeController.store, ShapeShiftController: this.shapeshiftController.store, @@ -267,7 +263,7 @@ module.exports = class MetamaskController extends EventEmitter { KeyringController: this.keyringController.memStore, PreferencesController: this.preferencesController.store, RecentBlocksController: this.recentBlocksController.store, - AddressBookController: this.addressBookController.store, + AddressBookController: this.addressBookController, CurrencyController: this.currencyController.store, NoticeController: this.noticeController.memStore, ShapeshiftController: this.shapeshiftController.store, @@ -376,7 +372,6 @@ module.exports = class MetamaskController extends EventEmitter { const preferencesController = this.preferencesController const txController = this.txController const noticeController = this.noticeController - const addressBookController = this.addressBookController const networkController = this.networkController const providerApprovalController = this.providerApprovalController @@ -440,7 +435,7 @@ module.exports = class MetamaskController extends EventEmitter { whitelistPhishingDomain: this.whitelistPhishingDomain.bind(this), // AddressController - setAddressBook: nodeify(addressBookController.setAddressBook, addressBookController), + setAddressBook: this.addressBookController.set.bind(this.addressBookController), // KeyringController setLocked: nodeify(this.setLocked, this), diff --git a/package.json b/package.json index 66e3d49abe16..4e3be4536516 100644 --- a/package.json +++ b/package.json @@ -256,6 +256,7 @@ "file-loader": "^1.1.11", "fs-extra": "^6.0.1", "fs-promise": "^2.0.3", + "gaba": "1.0.0-beta.62", "ganache-cli": "^6.1.0", "ganache-core": "^2.3.1", "geckodriver": "^1.14.1", diff --git a/test/unit/app/controllers/address-book-controller.js b/test/unit/app/controllers/address-book-controller.js deleted file mode 100644 index 1350e1a61fea..000000000000 --- a/test/unit/app/controllers/address-book-controller.js +++ /dev/null @@ -1,54 +0,0 @@ -const assert = require('assert') -const AddressBookController = require('../../../../app/scripts/controllers/address-book') - -const stubPreferencesStore = { - getState: function () { - return { - identities: { - '0x0aaa': { - address: '0x0aaa', - name: 'owned', - }, - }, - } - }, -} - -describe('address-book-controller', function () { - var addressBookController - - beforeEach(function () { - addressBookController = new AddressBookController({ - preferencesStore: stubPreferencesStore, - }) - }) - - describe('addres book management', function () { - describe('#_getAddressBook', function () { - it('should be empty by default.', function () { - assert.equal(addressBookController._getAddressBook().length, 0) - }) - }) - describe('#setAddressBook', function () { - it('should properly set a new address.', function () { - addressBookController.setAddressBook('0x01234', 'test') - var addressBook = addressBookController._getAddressBook() - assert.equal(addressBook.length, 1, 'incorrect address book length.') - assert.equal(addressBook[0].address, '0x01234', 'incorrect addresss') - assert.equal(addressBook[0].name, 'test', 'incorrect nickname') - }) - - it('should reject duplicates.', function () { - addressBookController.setAddressBook('0x01234', 'test') - addressBookController.setAddressBook('0x01234', 'test') - var addressBook = addressBookController._getAddressBook() - assert.equal(addressBook.length, 1, 'incorrect address book length.') - }) - it('should not add any identities that are under user control', function () { - addressBookController.setAddressBook('0x0aaa', ' ') - var addressBook = addressBookController._getAddressBook() - assert.equal(addressBook.length, 0, 'incorrect address book length.') - }) - }) - }) -})