From d270cbc9d2f45b6dae184efbe6c405889ee8cba5 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 9 Mar 2017 13:07:38 -0800 Subject: Create distinct labels and names for addresses. --- app/scripts/controllers/preferences.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index 18fccf11b..c7f675a41 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -5,7 +5,9 @@ const extend = require('xtend') class PreferencesController { constructor (opts = {}) { - const initState = extend({ frequentRpcList: [] }, opts.initState) + const initState = extend({ + frequentRpcList: [], + }, opts.initState) this.store = new ObservableStore(initState) } -- cgit v1.2.3 From 9f6c04055419f54a730bcbd3f1da4c5f992db94d Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 9 Mar 2017 13:58:42 -0800 Subject: Create persistence address book. --- app/scripts/controllers/address-book.js | 46 +++++++++++++++++++++++++++++++++ app/scripts/metamask-controller.js | 11 ++++++++ 2 files changed, 57 insertions(+) create mode 100644 app/scripts/controllers/address-book.js (limited to 'app') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js new file mode 100644 index 000000000..824a256d1 --- /dev/null +++ b/app/scripts/controllers/address-book.js @@ -0,0 +1,46 @@ +const ObservableStore = require('obs-store') +const extend = require('xtend') + +class AddressBookController { + + constructor (opts = {}) { + const initState = extend({ + addressBook: [], + }, opts.initState) + this.store = new ObservableStore(initState) + } + + // + // PUBLIC METHODS + // + + setAddressList (address, name) { + return this.addToAddressList(address, name) + .then((addressBook) => { + this.store.updateState({ + addressBook, + }) + return Promise.resolve() + }) + } + + addToAddressList (address, name) { + let addressBook = this.getAddressList() + let index = addressBook.findIndex((element) => { return element.address === address }) + if (index !== -1) { + addressBook.splice(index, 1) + } + addressBook.push({ + address, + name, + }) + return Promise.resolve(addressBook) + } + + getAddressList () { + return this.store.getState().addressBook + } + +} + +module.exports = AddressBookController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 536891dc6..6cdd8e96f 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -15,6 +15,7 @@ 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 MessageManager = require('./lib/message-manager') const PersonalMessageManager = require('./lib/personal-message-manager') const TxManager = require('./transaction-manager') @@ -50,6 +51,11 @@ module.exports = class MetamaskController extends EventEmitter { initState: initState.PreferencesController, }) + // address book controller + this.addressBookController = new AddressBookController({ + initState: initState.AddressBookController, + }) + // currency controller this.currencyController = new CurrencyController({ initState: initState.CurrencyController, @@ -124,6 +130,9 @@ module.exports = class MetamaskController extends EventEmitter { this.preferencesController.store.subscribe((state) => { this.store.updateState({ PreferencesController: state }) }) + this.addressBookController.store.subscribe((state) => { + this.store.updateState({ AddressBookController: state }) + }) this.currencyController.store.subscribe((state) => { this.store.updateState({ CurrencyController: state }) }) @@ -142,6 +151,7 @@ module.exports = class MetamaskController extends EventEmitter { this.personalMessageManager.memStore.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) this.preferencesController.store.subscribe(this.sendUpdate.bind(this)) + this.addressBookController.store.subscribe(this.sendUpdate.bind(this)) this.currencyController.store.subscribe(this.sendUpdate.bind(this)) this.noticeController.memStore.subscribe(this.sendUpdate.bind(this)) this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this)) @@ -219,6 +229,7 @@ module.exports = class MetamaskController extends EventEmitter { this.personalMessageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), + this.addressBookController.store.getState(), this.currencyController.store.getState(), this.noticeController.memStore.getState(), // config manager -- cgit v1.2.3 From b296640f1b2e8cb808297715146a9370ff4f41ec Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 9 Mar 2017 15:09:50 -0800 Subject: Rename functions for consistency. --- app/scripts/controllers/address-book.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js index 824a256d1..3c2a73dd7 100644 --- a/app/scripts/controllers/address-book.js +++ b/app/scripts/controllers/address-book.js @@ -14,8 +14,8 @@ class AddressBookController { // PUBLIC METHODS // - setAddressList (address, name) { - return this.addToAddressList(address, name) + setAddressBook (address, name) { + return this.addToAddressBook(address, name) .then((addressBook) => { this.store.updateState({ addressBook, @@ -24,9 +24,9 @@ class AddressBookController { }) } - addToAddressList (address, name) { - let addressBook = this.getAddressList() - let index = addressBook.findIndex((element) => { return element.address === address }) + addToAddressBook (address, name) { + let addressBook = this.getAddressBook() + let index = addressBook.findIndex((element) => { return element.address === address || element.name === name }) if (index !== -1) { addressBook.splice(index, 1) } @@ -37,7 +37,7 @@ class AddressBookController { return Promise.resolve(addressBook) } - getAddressList () { + getAddressBook () { return this.store.getState().addressBook } -- cgit v1.2.3 From b34ee4daa145c1d4eea2da6fd0cba0763e5c6483 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 9 Mar 2017 15:10:27 -0800 Subject: Allow for adding recently used addresses to address book. --- app/scripts/metamask-controller.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app') diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 6cdd8e96f..45905db72 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -251,6 +251,7 @@ module.exports = class MetamaskController extends EventEmitter { const preferencesController = this.preferencesController const txManager = this.txManager const noticeController = this.noticeController + const addressBookController = this.addressBookController return { // etc @@ -278,6 +279,9 @@ module.exports = class MetamaskController extends EventEmitter { setDefaultRpc: nodeify(this.setDefaultRpc).bind(this), setCustomRpc: nodeify(this.setCustomRpc).bind(this), + // AddressController + setAddressBook: nodeify(addressBookController.setAddressBook).bind(addressBookController), + // KeyringController setLocked: nodeify(keyringController.setLocked).bind(keyringController), createNewVaultAndKeychain: nodeify(keyringController.createNewVaultAndKeychain).bind(keyringController), -- cgit v1.2.3 From 2ab86b001dfc4ade4fc6df030175e64359b757e6 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Fri, 10 Mar 2017 09:34:13 -0800 Subject: Add comments. --- app/scripts/controllers/address-book.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js index 3c2a73dd7..a75ef06ce 100644 --- a/app/scripts/controllers/address-book.js +++ b/app/scripts/controllers/address-book.js @@ -3,6 +3,10 @@ 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. constructor (opts = {}) { const initState = extend({ addressBook: [], @@ -14,8 +18,9 @@ class AddressBookController { // PUBLIC METHODS // + // Sets a new address book in store by accepting a new address and nickname. setAddressBook (address, name) { - return this.addToAddressBook(address, name) + return this._addToAddressBook(address, name) .then((addressBook) => { this.store.updateState({ addressBook, @@ -24,8 +29,16 @@ class AddressBookController { }) } - addToAddressBook (address, name) { - let addressBook = this.getAddressBook() + // + // PRIVATE METHODS + // + + + // 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. + _addToAddressBook (address, name) { + let addressBook = this._getAddressBook() let index = addressBook.findIndex((element) => { return element.address === address || element.name === name }) if (index !== -1) { addressBook.splice(index, 1) @@ -37,7 +50,9 @@ class AddressBookController { return Promise.resolve(addressBook) } - getAddressBook () { + // Internal method to get the address book. Current persistence behavior + // should not require that this method be called from the UI directly. + _getAddressBook () { return this.store.getState().addressBook } -- cgit v1.2.3 From 7182a2be4332e7802c8d2375b9380490aed7ca0b Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Fri, 10 Mar 2017 10:05:10 -0800 Subject: Improve duplicate checking in address book. --- app/scripts/controllers/address-book.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js index a75ef06ce..914fbded2 100644 --- a/app/scripts/controllers/address-book.js +++ b/app/scripts/controllers/address-book.js @@ -39,12 +39,12 @@ class AddressBookController { // upper limit to the number of addresses. _addToAddressBook (address, name) { let addressBook = this._getAddressBook() - let index = addressBook.findIndex((element) => { return element.address === address || element.name === name }) + let index = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name }) if (index !== -1) { addressBook.splice(index, 1) } addressBook.push({ - address, + address: address, name, }) return Promise.resolve(addressBook) -- cgit v1.2.3 From 7dcab52a9e6aa0532a712cb4e502846a383efc94 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Fri, 10 Mar 2017 10:34:46 -0800 Subject: Connect keyring controller to address book to prevent additional duplicates. --- app/scripts/controllers/address-book.js | 22 ++++++++++++++++++---- app/scripts/metamask-controller.js | 10 +++++----- 2 files changed, 23 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js index 914fbded2..0e97cc477 100644 --- a/app/scripts/controllers/address-book.js +++ b/app/scripts/controllers/address-book.js @@ -7,11 +7,12 @@ 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. - constructor (opts = {}) { + constructor (opts = {}, keyringController) { const initState = extend({ addressBook: [], }, opts.initState) this.store = new ObservableStore(initState) + this.keyringController = keyringController } // @@ -39,9 +40,16 @@ class AddressBookController { // upper limit to the number of addresses. _addToAddressBook (address, name) { let addressBook = this._getAddressBook() - let index = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name }) - if (index !== -1) { - addressBook.splice(index, 1) + let identities = this._getIdentities() + + let addressBookIndex = addressBook.findIndex((element) => { return element.address.toLowerCase() === address.toLowerCase() || element.name === name }) + let 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) } addressBook.push({ address: address, @@ -56,6 +64,12 @@ class AddressBookController { return this.store.getState().addressBook } + // Retrieves identities from the keyring controller in order to avoid + // duplication + _getIdentities () { + return this.keyringController.memStore.getState().identities + } + } module.exports = AddressBookController diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 45905db72..1fcee61da 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -51,11 +51,6 @@ module.exports = class MetamaskController extends EventEmitter { initState: initState.PreferencesController, }) - // address book controller - this.addressBookController = new AddressBookController({ - initState: initState.AddressBookController, - }) - // currency controller this.currencyController = new CurrencyController({ initState: initState.CurrencyController, @@ -86,6 +81,11 @@ module.exports = class MetamaskController extends EventEmitter { autoFaucet(address) }) + // address book controller + this.addressBookController = new AddressBookController({ + initState: initState.AddressBookController, + }, this.keyringController) + // tx mgmt this.txManager = new TxManager({ initState: initState.TransactionManager, -- cgit v1.2.3 From 4933e2e2eefa103d6b0d3f3ce264aaf661b50291 Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Mon, 13 Mar 2017 16:37:53 -0700 Subject: Limit the number of addresses stored in our book. --- app/scripts/controllers/address-book.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js index 0e97cc477..c66eb2bd4 100644 --- a/app/scripts/controllers/address-book.js +++ b/app/scripts/controllers/address-book.js @@ -50,7 +50,11 @@ class AddressBookController { // 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, -- cgit v1.2.3