Adds bitcoin cash support for the address book

This commit is contained in:
Gustavo Maximiliano Cortez 2017-09-19 10:58:58 -04:00
commit c9253c44f3
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
3 changed files with 55 additions and 14 deletions

View file

@ -1,8 +1,19 @@
'use strict';
angular.module('copayApp.services').factory('addressbookService', function(bitcore, storageService, lodash) {
angular.module('copayApp.services').factory('addressbookService', function($log, bitcore, bitcoreCash, storageService, lodash) {
var root = {};
var getNetwork = function(address) {
var network;
try {
network = (new bitcore.Address(address)).network.name;
} catch(e) {
$log.error('No valid bitcoin address. Trying bitcoin cash...');
network = (new bitcoreCash.Address(address)).network.name;
}
return network;
};
root.get = function(addr, cb) {
storageService.getAddressbook('testnet', function(err, ab) {
if (err) return cb(err);
@ -35,7 +46,8 @@ angular.module('copayApp.services').factory('addressbookService', function(bitco
};
root.add = function(entry, cb) {
var network = (new bitcore.Address(entry.address)).network.name;
var network = getNetwork(entry.address);
if (lodash.isEmpty(network)) return cb('Not valid bitcoin address');
storageService.getAddressbook(network, function(err, ab) {
if (err) return cb(err);
if (ab) ab = JSON.parse(ab);
@ -53,7 +65,8 @@ angular.module('copayApp.services').factory('addressbookService', function(bitco
};
root.remove = function(addr, cb) {
var network = (new bitcore.Address(addr)).network.name;
var network = getNetwork(addr);
if (lodash.isEmpty(network)) return cb('Not valid bitcoin address');
storageService.getAddressbook(network, function(err, ab) {
if (err) return cb(err);
if (ab) ab = JSON.parse(ab);