Wallet/src/js/services/addressbookService.js

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-10-22 18:43:32 -03:00
'use strict';
angular.module('copayApp.services').factory('addressbookService', function(bitcore, storageService, lodash) {
2015-10-22 18:43:32 -03:00
var root = {};
2016-09-12 11:57:20 -03:00
root.get = function(addr, cb) {
2016-08-23 10:18:43 -03:00
storageService.getAddressbook('testnet', function(err, ab) {
2016-09-12 11:57:20 -03:00
if (err) return cb(err);
if (ab) ab = JSON.parse(ab);
if (ab && ab[addr]) return cb(null, ab[addr]);
2016-08-23 10:18:43 -03:00
2016-09-12 11:57:20 -03:00
storageService.getAddressbook('livenet', function(err, ab) {
if (err) return cb(err);
if (ab) ab = JSON.parse(ab);
if (ab && ab[addr]) return cb(null, ab[addr]);
2016-08-23 10:18:43 -03:00
return cb();
});
2015-10-22 18:43:32 -03:00
});
};
root.list = function(cb) {
2016-08-15 18:11:36 -03:00
storageService.getAddressbook('testnet', function(err, ab) {
2015-10-22 18:43:32 -03:00
if (err) return cb('Could not get the Addressbook');
2016-08-15 18:11:36 -03:00
2015-10-22 18:43:32 -03:00
if (ab) ab = JSON.parse(ab);
2016-08-15 18:11:36 -03:00
ab = ab || {};
storageService.getAddressbook('livenet', function(err, ab2) {
if (ab2) ab2 = JSON.parse(ab2);
ab2 = ab2 || {};
2016-08-19 13:09:27 -03:00
return cb(err, lodash.defaults(ab2, ab));
2016-08-15 18:11:36 -03:00
});
2015-10-22 18:43:32 -03:00
});
};
root.add = function(entry, cb) {
2016-08-25 18:18:38 -03:00
var network = (new bitcore.Address(entry.address)).network.name;
storageService.getAddressbook(network, function(err, ab) {
2015-10-22 18:43:32 -03:00
if (err) return cb(err);
if (ab) ab = JSON.parse(ab);
ab = ab || {};
2016-08-25 18:18:38 -03:00
if (lodash.isArray(ab)) ab = {}; // No array
2015-10-22 18:43:32 -03:00
if (ab[entry.address]) return cb('Entry already exist');
2016-09-12 11:57:20 -03:00
ab[entry.address] = entry;
storageService.setAddressbook(network, JSON.stringify(ab), function(err, ab) {
2015-10-22 18:43:32 -03:00
if (err) return cb('Error adding new entry');
root.list(function(err, ab) {
return cb(err, ab);
});
});
});
};
2016-08-19 13:09:27 -03:00
2015-10-22 18:43:32 -03:00
root.remove = function(addr, cb) {
2016-08-25 18:18:38 -03:00
var network = (new bitcore.Address(addr)).network.name;
storageService.getAddressbook(network, function(err, ab) {
2015-10-22 18:43:32 -03:00
if (err) return cb(err);
if (ab) ab = JSON.parse(ab);
ab = ab || {};
if (lodash.isEmpty(ab)) return cb('Addressbook is empty');
2015-10-22 18:43:32 -03:00
if (!ab[addr]) return cb('Entry does not exist');
delete ab[addr];
storageService.setAddressbook(network, JSON.stringify(ab), function(err) {
2015-10-22 18:43:32 -03:00
if (err) return cb('Error deleting entry');
root.list(function(err, ab) {
return cb(err, ab);
});
});
2016-08-19 13:09:27 -03:00
});
2015-10-22 18:43:32 -03:00
};
root.removeAll = function() {
storageService.removeAddressbook('livenet', function(err) {
storageService.removeAddressbook('testnet', function(err) {
if (err) return cb('Error deleting addressbook');
return cb();
});
2015-10-22 18:43:32 -03:00
});
};
return root;
});