'use strict'; angular.module('copayApp.services').factory('addressbookService', function(bitcore, storageService, lodash) { var root = {}; var getNetwork = function(addr) { var Address = bitcore.Address; if (Address.isValid(addr, 'livenet')) { return 'livenet'; } if (Address.isValid(addr, 'testnet')) { return 'testnet'; } }; root.getLabel = function(addr, cb) { storageService.getAddressbook('testnet', function(err, ab) { if (ab && ab[addr]) return cb(ab[addr]); storageService.getAddressbook('livnet', function(err, ab) { if (ab && ab[addr]) return cb(ab[addr]); return cb(); }); }); }; root.list = function(cb) { storageService.getAddressbook('testnet', function(err, ab) { if (err) return cb('Could not get the Addressbook'); if (ab) ab = JSON.parse(ab); ab = ab || {}; storageService.getAddressbook('livenet', function(err, ab2) { if (ab2) ab2 = JSON.parse(ab2); ab2 = ab2 || {}; return cb(err, lodash.defaults(ab2, ab)); }); }); }; root.add = function(entry, cb) { var network = getNetwork(entry.address); storageService.getAddressbook(network, function(err, ab) { if (err) return cb(err); if (ab) ab = JSON.parse(ab); ab = ab || {}; if (ab[entry.address]) return cb('Entry already exist'); ab[entry.address] = entry.label; storageService.setAddressbook(network, JSON.stringify(ab), function(err, ab) { if (err) return cb('Error adding new entry'); root.list(function(err, ab) { return cb(err, ab); }); }); }); }; root.remove = function(addr, cb) { var network = getNetwork(addr); storageService.getAddressbook(network, function(err, ab) { if (err) return cb(err); if (ab) ab = JSON.parse(ab); ab = ab || {}; if (lodash.isEmpty(ab)) return cb('Addressbook is empty'); if (!ab[addr]) return cb('Entry does not exist'); delete ab[addr]; storageService.setAddressbook(network, JSON.stringify(ab), function(err) { if (err) return cb('Error deleting entry'); root.list(function(err, ab) { return cb(err, ab); }); }); }); }; root.removeAll = function() { storageService.removeAddressbook('livenet', function(err) { storageService.removeAddressbook('testnet', function(err) { if (err) return cb('Error deleting addressbook'); return cb(); }); }); }; return root; });