Merge pull request #6718 from cmgustavo/feat/bitcoin-cash-handle-url

Feat/bitcoin cash url support
This commit is contained in:
Matias Alejo Garcia 2017-09-09 20:42:06 -03:00 committed by GitHub
commit 7dadf7e321
9 changed files with 60 additions and 26 deletions

View file

@ -66,6 +66,7 @@
<plugin name="cordova-plugin-customurlscheme" spec="https://github.com/cmgustavo/Custom-URL-scheme.git"> <plugin name="cordova-plugin-customurlscheme" spec="https://github.com/cmgustavo/Custom-URL-scheme.git">
<variable name="URL_SCHEME" value="bitcoin" /> <variable name="URL_SCHEME" value="bitcoin" />
<variable name="SECOND_URL_SCHEME" value="*APPURI*" /> <variable name="SECOND_URL_SCHEME" value="*APPURI*" />
<variable name="THIRD_URL_SCHEME" value="bitcoincash" />
</plugin> </plugin>
<plugin name="cordova-custom-config" spec="~3.0.5" /> <plugin name="cordova-custom-config" spec="~3.0.5" />
<plugin name="cordova-plugin-queries-schemes" spec="~0.1.5" /> <plugin name="cordova-plugin-queries-schemes" spec="~0.1.5" />

View file

@ -14,7 +14,7 @@ angular.module('copayApp.controllers').controller('addressbookAddController', fu
$timeout(function() { $timeout(function() {
var form = addressbookForm; var form = addressbookForm;
if (data && form) { if (data && form) {
data = data.replace('bitcoin:', ''); data = data.replace(/^bitcoin(cash)?:/, '');
form.address.$setViewValue(data); form.address.$setViewValue(data);
form.address.$isValid = true; form.address.$isValid = true;
form.address.$render(); form.address.$render();

View file

@ -8,6 +8,10 @@ angular.module('copayApp.controllers').controller('customAmountController', func
}); });
}; };
var setProtocolHandler = function() {
$scope.protocolHandler = walletService.getProtocolHandler($scope.wallet);
}
$scope.$on("$ionicView.beforeEnter", function(event, data) { $scope.$on("$ionicView.beforeEnter", function(event, data) {
var walletId = data.stateParams.id; var walletId = data.stateParams.id;
@ -20,6 +24,8 @@ angular.module('copayApp.controllers').controller('customAmountController', func
$scope.wallet = profileService.getWallet(walletId); $scope.wallet = profileService.getWallet(walletId);
setProtocolHandler();
walletService.getAddress($scope.wallet, false, function(err, addr) { walletService.getAddress($scope.wallet, false, function(err, addr) {
if (!addr) { if (!addr) {
showErrorAndBack('Error', 'Could not get the address'); showErrorAndBack('Error', 'Could not get the address');
@ -63,12 +69,16 @@ angular.module('copayApp.controllers').controller('customAmountController', func
$scope.shareAddress = function() { $scope.shareAddress = function() {
if (!platformInfo.isCordova) return; if (!platformInfo.isCordova) return;
var data = 'bitcoin:' + $scope.address + '?amount=' + $scope.amountBtc + '&coin=' + $scope.wallet.coin; var protocol = 'bitcoin';
if ($scope.wallet.coin == 'bch') protocol += 'cash';
var data = protocol + ':' + $scope.address + '?amount=' + $scope.amountBtc;
window.plugins.socialsharing.share(data, null, null, null); window.plugins.socialsharing.share(data, null, null, null);
} }
$scope.copyToClipboard = function() { $scope.copyToClipboard = function() {
return 'bitcoin:' + $scope.address + '?amount=' + $scope.amountBtc + '&coin=' + $scope.wallet.coin; var protocol = 'bitcoin';
if ($scope.wallet.coin == 'bch') protocol += 'cash';
return protocol + ':' + $scope.address + '?amount=' + $scope.amountBtc;
}; };
}); });

View file

@ -124,8 +124,13 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
return wallet; return wallet;
} }
var setProtocolHandler = function() {
$scope.protocolHandler = walletService.getProtocolHandler($scope.wallet);
}
$scope.onWalletSelect = function(wallet) { $scope.onWalletSelect = function(wallet) {
$scope.wallet = wallet; $scope.wallet = wallet;
setProtocolHandler();
$scope.setAddress(); $scope.setAddress();
}; };
@ -137,6 +142,8 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
$scope.shareAddress = function() { $scope.shareAddress = function() {
if (!$scope.isCordova) return; if (!$scope.isCordova) return;
window.plugins.socialsharing.share('bitcoin:' + $scope.addr, null, null, null); var protocol = 'bitcoin';
if ($scope.wallet.coin == 'bch') protocol += 'cash';
window.plugins.socialsharing.share(protocol + ':' + $scope.addr, null, null, null);
} }
}); });

View file

@ -69,8 +69,8 @@ angular.module('copayApp.services').factory('incomingData', function($log, $stat
}, 100); }, 100);
} }
// data extensions for Payment Protocol with non-backwards-compatible request // data extensions for Payment Protocol with non-backwards-compatible request
if ((/^bitcoin:\?r=[\w+]/).exec(data)) { if ((/^bitcoin(cash)?:\?r=[\w+]/).exec(data)) {
data = decodeURIComponent(data.replace('bitcoin:?r=', '')); data = decodeURIComponent(data.replace(/bitcoin(cash)?:\?r=/, ''));
$state.go('tabs.send', {}, { $state.go('tabs.send', {}, {
'reload': true, 'reload': true,
'notify': $state.current.name == 'tabs.send' ? false : true 'notify': $state.current.name == 'tabs.send' ? false : true
@ -84,27 +84,37 @@ angular.module('copayApp.services').factory('incomingData', function($log, $stat
data = sanitizeUri(data); data = sanitizeUri(data);
// BIP21 // Bitcoin or Bitcoin Cash URL
if (bitcore.URI.isValid(data)) { if ((/^bitcoin(cash)?:/).exec(data)) {
var parsed = new bitcore.URI(data); var coin = 'btc';
if ((/^bitcoincash:/).exec(data)) {
var addr = parsed.address ? parsed.address.toString() : ''; coin = 'bch';
var message = parsed.message; data = data.replace(/bitcoincash:/, 'bitcoin:');
}
var amount = parsed.amount ? parsed.amount : ''; if (bitcore.URI.isValid(data)) {
var coin = parsed.extras && parsed.extras.coin ? parsed.extras.coin : ''; var parsed = new bitcore.URI(data);
if (parsed.r) { var addr = parsed.address ? parsed.address.toString() : '';
payproService.getPayProDetails(parsed.r, function(err, details) { var message = parsed.message;
if (err) {
if (addr && amount) goSend(addr, amount, message, coin); var amount = parsed.amount ? parsed.amount : '';
else popupService.showAlert(gettextCatalog.getString('Error'), err);
} else handlePayPro(details); if (parsed.r) {
}); payproService.getPayProDetails(parsed.r, function(err, details) {
} else { if (err) {
goSend(addr, amount, message, coin); if (addr && amount) goSend(addr, amount, message, coin);
else popupService.showAlert(gettextCatalog.getString('Error'), err);
} else handlePayPro(details);
});
} else {
goSend(addr, amount, message, coin);
}
return true;
} else {
$log.error('Invalid Bitcoin URL');
return false;
} }
return true;
// Plain URL // Plain URL
} else if (/^https?:\/\//.test(data)) { } else if (/^https?:\/\//.test(data)) {

View file

@ -57,10 +57,10 @@ angular.module('copayApp.services').factory('openURLService', function($rootScop
// This event is sent to an existent instance of Copay (only for standalone apps) // This event is sent to an existent instance of Copay (only for standalone apps)
gui.App.on('open', function(pathData) { gui.App.on('open', function(pathData) {
if (pathData.indexOf('bitcoin:') != -1) { if (pathData.indexOf(/^bitcoin(cash)?:/) != -1) {
$log.debug('Bitcoin URL found'); $log.debug('Bitcoin URL found');
handleOpenURL({ handleOpenURL({
url: pathData.substring(pathData.indexOf('bitcoin:')) url: pathData.substring(pathData.indexOf(/^bitcoin(cash)?:/))
}); });
} else if (pathData.indexOf(appConfigService.name + '://') != -1) { } else if (pathData.indexOf(appConfigService.name + '://') != -1) {
$log.debug(appConfigService.name + ' URL found'); $log.debug(appConfigService.name + ' URL found');
@ -84,6 +84,7 @@ angular.module('copayApp.services').factory('openURLService', function($rootScop
if (navigator.registerProtocolHandler) { if (navigator.registerProtocolHandler) {
$log.debug('Registering Browser handlers base:' + base); $log.debug('Registering Browser handlers base:' + base);
navigator.registerProtocolHandler('bitcoin', url, 'Copay Bitcoin Handler'); navigator.registerProtocolHandler('bitcoin', url, 'Copay Bitcoin Handler');
navigator.registerProtocolHandler('web+bitcoincash', url, 'Copay Bitcoin Cash Handler');
navigator.registerProtocolHandler('web+copay', url, 'Copay Wallet Handler'); navigator.registerProtocolHandler('web+copay', url, 'Copay Wallet Handler');
navigator.registerProtocolHandler('web+bitpay', url, 'BitPay Wallet Handler'); navigator.registerProtocolHandler('web+bitpay', url, 'BitPay Wallet Handler');
} }

View file

@ -1232,5 +1232,10 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
}); });
}; };
root.getProtocolHandler = function(wallet) {
if (wallet.coin== 'bch') return 'bitcoincash';
else return 'bitcoin';
}
return root; return root;
}); });

View file

@ -21,7 +21,7 @@
<ion-content scroll="false"> <ion-content scroll="false">
<div class="address" ng-if="address && amountBtc"> <div class="address" ng-if="address && amountBtc">
<div class="qr-code" copy-to-clipboard="copyToClipboard()"> <div class="qr-code" copy-to-clipboard="copyToClipboard()">
<qrcode size="220" data="bitcoin:{{address + '?amount=' + amountBtc + '&coin=' + coin}}" color="#334"></qrcode> <qrcode size="220" data="{{ protocolHandler }}:{{address + '?amount=' + amountBtc}}" color="#334"></qrcode>
</div> </div>
<div class="info"> <div class="info">
<div class="item single-line" copy-to-clipboard="address"> <div class="item single-line" copy-to-clipboard="address">

View file

@ -34,7 +34,7 @@
<span translate>Show address</span> <span translate>Show address</span>
</button> </button>
</span> </span>
<qrcode ng-if="addr" size="220" data="bitcoin:{{addr}}" color="#334"></qrcode> <qrcode ng-if="addr" size="220" data="{{ protocolHandler }}:{{addr}}" color="#334"></qrcode>
<div class="address-label"> <div class="address-label">
<span class="ellipsis">{{addr}}</span> <span class="ellipsis">{{addr}}</span>
<ion-spinner ng-show="!addr" class="spinner-dark" icon="crescent"></ion-spinner> <ion-spinner ng-show="!addr" class="spinner-dark" icon="crescent"></ion-spinner>