aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorFrankie <frankie.diamond@gmail.com>2017-03-17 02:00:12 +0800
committerGitHub <noreply@github.com>2017-03-17 02:00:12 +0800
commit57d1a1f1860e50837104a10b7b9f86d398c795ec (patch)
treeda6648b5a74055e497f75eed7dca559968c593b5 /app
parent53efc53a4016d464d03ad7f1b474e1b09b0b4b9b (diff)
parent00f1a2d78d01301f51157e9c8b8352dc0796d034 (diff)
downloadtangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.tar
tangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.tar.gz
tangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.tar.bz2
tangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.tar.lz
tangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.tar.xz
tangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.tar.zst
tangerine-wallet-browser-57d1a1f1860e50837104a10b7b9f86d398c795ec.zip
Merge branch 'master' into i#1203MainNetSwitch
Diffstat (limited to 'app')
-rw-r--r--app/scripts/controllers/address-book.js79
-rw-r--r--app/scripts/controllers/preferences.js4
-rw-r--r--app/scripts/metamask-controller.js15
3 files changed, 97 insertions, 1 deletions
diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js
new file mode 100644
index 000000000..c66eb2bd4
--- /dev/null
+++ b/app/scripts/controllers/address-book.js
@@ -0,0 +1,79 @@
+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.
+ constructor (opts = {}, keyringController) {
+ const initState = extend({
+ addressBook: [],
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+ this.keyringController = keyringController
+ }
+
+ //
+ // PUBLIC METHODS
+ //
+
+ // Sets a new address book in store by accepting a new address and nickname.
+ setAddressBook (address, name) {
+ return this._addToAddressBook(address, name)
+ .then((addressBook) => {
+ this.store.updateState({
+ addressBook,
+ })
+ return Promise.resolve()
+ })
+ }
+
+ //
+ // 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 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)
+ } 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.
+ _getAddressBook () {
+ 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/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)
}
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js
index 2eaa53200..25f9d9e5d 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')
@@ -80,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,
@@ -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
@@ -240,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
@@ -267,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),