1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/app/scripts/controllers/address-book.js

99 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-03-09 22:58:42 +01:00
const ObservableStore = require('obs-store')
const extend = require('xtend')
class AddressBookController {
2017-03-10 18:34:13 +01:00
/**
* 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({
2017-03-09 22:58:42 +01:00
addressBook: [],
}, initState))
this._preferencesStore = preferencesStore
2017-03-09 22:58:42 +01:00
}
//
// 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<void>} Promise resolves with undefined
*
*/
2017-03-10 00:09:50 +01:00
setAddressBook (address, name) {
2017-03-10 18:34:13 +01:00
return this._addToAddressBook(address, name)
2017-03-09 22:58:42 +01:00
.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<array>} Promises the updated addressBook array
*
*/
2017-03-10 18:34:13 +01:00
_addToAddressBook (address, name) {
2017-04-27 06:05:45 +02:00
const addressBook = this._getAddressBook()
const {identities} = this._preferencesStore.getState()
2017-04-27 06:05:45 +02:00
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()
2017-03-09 22:58:42 +01:00
}
2017-03-09 22:58:42 +01:00
addressBook.push({
address: address,
2017-03-09 22:58:42 +01:00
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.
*
*/
2017-03-10 18:34:13 +01:00
_getAddressBook () {
2017-03-09 22:58:42 +01:00
return this.store.getState().addressBook
}
}
module.exports = AddressBookController