mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 01:39:44 +01:00
Create persistence address book.
This commit is contained in:
parent
d270cbc9d2
commit
9f6c040554
46
app/scripts/controllers/address-book.js
Normal file
46
app/scripts/controllers/address-book.js
Normal file
@ -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
|
@ -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
|
||||
|
@ -19,6 +19,7 @@ function reduceMetamask (state, action) {
|
||||
noActiveNotices: true,
|
||||
lastUnreadNotice: undefined,
|
||||
frequentRpcList: [],
|
||||
addressBook: [],
|
||||
}, state.metamask)
|
||||
|
||||
switch (action.type) {
|
||||
|
Loading…
Reference in New Issue
Block a user