diff --git a/src/js/controllers/addressbookView.js b/src/js/controllers/addressbookView.js index 05007dfe9..9d1c49840 100644 --- a/src/js/controllers/addressbookView.js +++ b/src/js/controllers/addressbookView.js @@ -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(); }); - }); + }); }; }); diff --git a/src/js/controllers/tab-send.js b/src/js/controllers/tab-send.js index 77a664632..229d23c2a 100644 --- a/src/js/controllers/tab-send.js +++ b/src/js/controllers/tab-send.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, bwcError, gettextCatalog, scannerService) { +angular.module('copayApp.controllers').controller('tabSendController', function($scope, $rootScope, $log, $timeout, $ionicScrollDelegate, addressbookService, profileService, lodash, $state, walletService, incomingData, popupService, platformInfo, bwcError, gettextCatalog, scannerService, bitcoreCash) { var originalList; var CONTACTS_SHOW_LIMIT; @@ -86,6 +86,14 @@ angular.module('copayApp.controllers').controller('tabSendController', function( } } + var getCoin = function(address) { + var cashAddress = bitcoreCash.Address.isValid(address, 'livenet'); + if (cashAddress) { + return 'bch'; + } + return 'btc'; + }; + var updateContactsList = function(cb) { addressbookService.list(function(err, ab) { if (err) $log.error(err); @@ -100,6 +108,7 @@ angular.module('copayApp.controllers').controller('tabSendController', function( address: k, email: lodash.isObject(v) ? v.email : null, recipientType: 'contact', + coin: getCoin(k), getAddress: function(cb) { return cb(null, k); }, diff --git a/src/js/directives/directives.js b/src/js/directives/directives.js index ed59564b7..ac14da110 100644 --- a/src/js/directives/directives.js +++ b/src/js/directives/directives.js @@ -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; }; diff --git a/src/js/services/addressbookService.js b/src/js/services/addressbookService.js index edd3237d1..6f8c1d106 100644 --- a/src/js/services/addressbookService.js +++ b/src/js/services/addressbookService.js @@ -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.warn('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);