From 1c8990fc08c8e561884a4b3eefefbac6a62119f5 Mon Sep 17 00:00:00 2001 From: Kadir Sekha Date: Thu, 11 Jan 2018 19:07:12 +0900 Subject: [PATCH] got address book working with bitcoin cash addresses --- src/js/controllers/addressbook.js | 14 +++++++++++--- src/js/controllers/addressbookAdd.js | 8 ++++++-- src/js/controllers/addressbookView.js | 11 +++++++++-- src/js/controllers/addresses.js | 7 ++----- src/js/controllers/tab-receive.js | 4 +--- src/js/directives/directives.js | 8 ++++++-- src/js/routes.js | 2 +- src/js/services/addressbookService.js | 6 +++--- www/views/addressbook.html | 2 +- www/views/addressbook.view.html | 2 +- 10 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/js/controllers/addressbook.js b/src/js/controllers/addressbook.js index 657ef1671..4f3d166db 100644 --- a/src/js/controllers/addressbook.js +++ b/src/js/controllers/addressbook.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('addressbookListController', function($scope, $log, $timeout, addressbookService, lodash, popupService, gettextCatalog, platformInfo) { +angular.module('copayApp.controllers').controller('addressbookListController', function($scope, $log, $timeout, addressbookService, lodash, popupService, gettextCatalog, platformInfo, bitcoinCashJsService) { var contacts; @@ -15,10 +15,18 @@ angular.module('copayApp.controllers').controller('addressbookListController', f contacts = []; lodash.each(ab, function(v, k) { + var c = lodash.isObject(v) ? v.coin : null; + var a = null; + if (c && c == 'bch') { + a = bitcoinCashJsService.readAddress(v.address).cashaddr.replace('bitcoincash:', ''); + } else { + a = v.address; + } contacts.push({ name: lodash.isObject(v) ? v.name : v, - address: k, - email: lodash.isObject(v) ? v.email : null + address: a, + email: lodash.isObject(v) ? v.email : null, + coin: c }); }); diff --git a/src/js/controllers/addressbookAdd.js b/src/js/controllers/addressbookAdd.js index f5730c8e2..a57839aa1 100644 --- a/src/js/controllers/addressbookAdd.js +++ b/src/js/controllers/addressbookAdd.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('addressbookAddController', function($scope, $state, $stateParams, $timeout, $ionicHistory, gettextCatalog, addressbookService, popupService, configService) { +angular.module('copayApp.controllers').controller('addressbookAddController', function($scope, $state, $stateParams, $timeout, $ionicHistory, gettextCatalog, addressbookService, popupService, configService, bitcoinCashJsService) { var config = configService.getSync(); var defaults = configService.getDefaults(); @@ -14,7 +14,7 @@ angular.module('copayApp.controllers').controller('addressbookAddController', fu 'address': $stateParams.addressbookEntry || '', 'name': '', 'email': '', - 'coin': 'btc' + 'coin': 'bch' }; $scope.onQrCodeScannedAddressBook = function(data, addressbookForm) { @@ -31,6 +31,10 @@ angular.module('copayApp.controllers').controller('addressbookAddController', fu }; $scope.add = function(addressbook) { + if ($scope.addressbookEntry.coin == 'bch') { + var translated = bitcoinCashJsService.readAddress(addressbook.address); + addressbook.address = translated.legacy; + } $timeout(function() { addressbookService.add(addressbook, function(err, ab) { if (err) { diff --git a/src/js/controllers/addressbookView.js b/src/js/controllers/addressbookView.js index a3c2c3551..06bf68fd4 100644 --- a/src/js/controllers/addressbookView.js +++ b/src/js/controllers/addressbookView.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('addressbookViewController', function($scope, $state, $timeout, lodash, addressbookService, popupService, $ionicHistory, platformInfo, gettextCatalog, configService) { +angular.module('copayApp.controllers').controller('addressbookViewController', function($scope, $state, $timeout, lodash, addressbookService, popupService, $ionicHistory, platformInfo, gettextCatalog, configService, bitcoinCashJsService) { var config = configService.getSync(); var defaults = configService.getDefaults(); @@ -24,8 +24,15 @@ angular.module('copayApp.controllers').controller('addressbookViewController', f $ionicHistory.removeBackView(); $state.go('tabs.send'); $timeout(function() { + var to = ''; + if ($scope.addressbookEntry.coin == 'bch') { + var a = 'bitcoincash:' + $scope.addressbookEntry.address; + to = bitcoinCashJsService.readAddress(a).legacy; + } else { + to = $scope.addressbookEntry.address; + } $state.transitionTo('tabs.send.amount', { - toAddress: $scope.addressbookEntry.address, + toAddress: to, toName: $scope.addressbookEntry.name, toEmail: $scope.addressbookEntry.email, coin: $scope.addressbookEntry.coin diff --git a/src/js/controllers/addresses.js b/src/js/controllers/addresses.js index 921b4a3f3..66e46750c 100644 --- a/src/js/controllers/addresses.js +++ b/src/js/controllers/addresses.js @@ -63,11 +63,8 @@ angular.module('copayApp.controllers').controller('addressesController', functio a.translatedAddresses = bitcoinCashJsService.translateAddresses(a.address); }); - var cashaddrDate = new Date(2018, 0, 15); - var currentDate = new Date(); - $scope.addressType = { - type: currentDate >= cashaddrDate ? 'cashaddr' : 'legacy' + type: 'cashaddr' }; $scope.showAddressTypes = true; } @@ -141,7 +138,7 @@ angular.module('copayApp.controllers').controller('addressesController', functio if ($scope.wallet.coin == 'bch') { _addr[0].translatedAddresses = bitcoinCashJsService.translateAddresses(_addr[0].address); } - + $scope.noBalance = [_addr[0]].concat($scope.noBalance); $scope.latestUnused = lodash.slice($scope.noBalance, 0, UNUSED_ADDRESS_LIMIT); $scope.viewAll = { diff --git a/src/js/controllers/tab-receive.js b/src/js/controllers/tab-receive.js index f9838f0a1..031def614 100644 --- a/src/js/controllers/tab-receive.js +++ b/src/js/controllers/tab-receive.js @@ -3,9 +3,7 @@ angular.module('copayApp.controllers').controller('tabReceiveController', function($rootScope, $scope, $timeout, $log, $ionicModal, $state, $ionicHistory, $ionicPopover, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService, bwcError, bitcoinCashJsService) { var listeners = []; - var cashaddrDate = new Date(2018, 0, 15); - var currentDate = new Date(); - $scope.bchAddressType = currentDate >= cashaddrDate ? 'cashaddr' : 'legacy'; + $scope.bchAddressType = 'cashaddr'; var bchAddresses = {}; $scope.isCordova = platformInfo.isCordova; diff --git a/src/js/directives/directives.js b/src/js/directives/directives.js index ac14da110..eed8ec104 100644 --- a/src/js/directives/directives.js +++ b/src/js/directives/directives.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.directives') - .directive('validAddress', ['$rootScope', 'bitcore', 'bitcoreCash', - function($rootScope, bitcore, bitcoreCash) { + .directive('validAddress', ['$rootScope', 'bitcore', 'bitcoreCash', 'bitcoinCashJsService', + function($rootScope, bitcore, bitcoreCash, bitcoinCashJsService) { return { require: 'ngModel', link: function(scope, elem, attrs, ctrl) { @@ -15,6 +15,10 @@ angular.module('copayApp.directives') var validator = function(value) { + if (value.indexOf('bitcoincash:') >= 0 || value[0] == 'C' || value[0] == 'H') { + value = bitcoinCashJsService.readAddress(value).legacy; + } + // Regular url if (/^https?:\/\//.test(value)) { ctrl.$setValidity('validAddress', true); diff --git a/src/js/routes.js b/src/js/routes.js index 49e153654..93a409837 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -620,7 +620,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr } }) .state('tabs.addressbook.view', { - url: '/view/:address/:email/:name', + url: '/view/:address/:email/:name/:coin', views: { 'tab-settings@tabs': { templateUrl: 'views/addressbook.view.html', diff --git a/src/js/services/addressbookService.js b/src/js/services/addressbookService.js index 6f8c1d106..f98bda41d 100644 --- a/src/js/services/addressbookService.js +++ b/src/js/services/addressbookService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('addressbookService', function($log, bitcore, bitcoreCash, storageService, lodash) { +angular.module('copayApp.services').factory('addressbookService', function($log, bitcore, bitcoreCash, storageService, lodash, bitcoinCashJsService) { var root = {}; var getNetwork = function(address) { @@ -53,8 +53,8 @@ angular.module('copayApp.services').factory('addressbookService', function($log, if (ab) ab = JSON.parse(ab); ab = ab || {}; if (lodash.isArray(ab)) ab = {}; // No array - if (ab[entry.address]) return cb('Entry already exist'); - ab[entry.address] = entry; + if (ab[entry.coin + entry.address]) return cb('Entry already exist'); + ab[entry.coin + entry.address] = entry; storageService.setAddressbook(network, JSON.stringify(ab), function(err, ab) { if (err) return cb('Error adding new entry'); root.list(function(err, ab) { diff --git a/www/views/addressbook.html b/www/views/addressbook.html index 416e93f0f..4ddd94d21 100644 --- a/www/views/addressbook.html +++ b/www/views/addressbook.html @@ -36,7 +36,7 @@ + ui-sref="tabs.addressbook.view({address:addrEntry.address, email: addrEntry.email, name: addrEntry.name, coin: addrEntry.coin})">

{{addrEntry.name}}

diff --git a/www/views/addressbook.view.html b/www/views/addressbook.view.html index b2c9fdfdc..143a97828 100644 --- a/www/views/addressbook.view.html +++ b/www/views/addressbook.view.html @@ -22,7 +22,7 @@ Email {{addressbookEntry.email}} -
+
Address {{addressbookEntry.address}}