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,11 +1,23 @@
'use strict';
angular.module('copayApp.controllers').controller('addressbookViewController', function($scope, $state, $timeout, $stateParams, lodash, addressbookService, popupService, $ionicHistory, platformInfo, gettextCatalog) {
angular.module('copayApp.controllers').controller('addressbookViewController', function($scope, $state, $timeout, lodash, addressbookService, popupService, $ionicHistory, platformInfo, gettextCatalog, bitcoreCash) {
$scope.isChromeApp = platformInfo.isChromeApp;
$scope.addressbookEntry = {};
$scope.addressbookEntry.name = $stateParams.name;
$scope.addressbookEntry.email = $stateParams.email;
$scope.addressbookEntry.address = $stateParams.address;
var coin;
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.addressbookEntry = {};
$scope.addressbookEntry.name = data.stateParams.name;
$scope.addressbookEntry.email = data.stateParams.email;
$scope.addressbookEntry.address = data.stateParams.address;
var cashAddress = bitcoreCash.Address.isValid($scope.addressbookEntry.address, 'livenet');
if (cashAddress) {
coin = 'bch';
} else {
coin = 'btc';
}
});
$scope.sendTo = function() {
$ionicHistory.removeBackView();
@ -14,7 +26,8 @@ angular.module('copayApp.controllers').controller('addressbookViewController', f
$state.transitionTo('tabs.send.amount', {
toAddress: $scope.addressbookEntry.address,
toName: $scope.addressbookEntry.name,
toEmail: $scope.addressbookEntry.email
toEmail: $scope.addressbookEntry.email,
coin: coin
});
}, 100);
};
@ -31,7 +44,7 @@ angular.module('copayApp.controllers').controller('addressbookViewController', f
}
$ionicHistory.goBack();
});
});
});
};
});

View file

@ -1,12 +1,18 @@
'use strict';
angular.module('copayApp.directives')
.directive('validAddress', ['$rootScope', 'bitcore',
function($rootScope, bitcore) {
.directive('validAddress', ['$rootScope', 'bitcore', 'bitcoreCash',
function($rootScope, bitcore, bitcoreCash) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
// Bitcoin address
var URI = bitcore.URI;
var Address = bitcore.Address
// Bitcoin Cash address
var URICash = bitcoreCash.URI;
var AddressCash = bitcoreCash.Address
var validator = function(value) {
// Regular url
@ -16,8 +22,8 @@ angular.module('copayApp.directives')
}
// Bip21 uri
var uri, isAddressValidLivenet, isAddressValidTestnet;
if (/^bitcoin:/.test(value)) {
var uri, isAddressValidLivenet, isAddressValidTestnet;
var isUriValid = URI.isValid(value);
if (isUriValid) {
uri = new URI(value);
@ -26,6 +32,14 @@ angular.module('copayApp.directives')
}
ctrl.$setValidity('validAddress', isUriValid && (isAddressValidLivenet || isAddressValidTestnet));
return value;
} else if (/^bitcoincash:/.test(value)) {
var isUriValid = URICash.isValid(value);
if (isUriValid) {
uri = new URICash(value);
isAddressValidLivenet = AddressCash.isValid(uri.address.toString(), 'livenet')
}
ctrl.$setValidity('validAddress', isUriValid && (isAddressValidLivenet));
return value;
}
if (typeof value == 'undefined') {
@ -33,10 +47,11 @@ angular.module('copayApp.directives')
return;
}
// Regular Address
// Regular Address: try Bitcoin and Bitcoin Cash
var regularAddressLivenet = Address.isValid(value, 'livenet');
var regularAddressTestnet = Address.isValid(value, 'testnet');
ctrl.$setValidity('validAddress', (regularAddressLivenet || regularAddressTestnet));
var regularAddressCashLivenet = AddressCash.isValid(value, 'livenet');
ctrl.$setValidity('validAddress', (regularAddressLivenet || regularAddressTestnet || regularAddressCashLivenet));
return value;
};

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);