From ddd867092c6da22002f785675fb3555711803eb0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Fri, 31 Aug 2018 18:37:04 +0900 Subject: [PATCH] Refactor incomingData --- src/js/controllers/addressbookView.js | 10 +---- src/js/directives/incomingDataMenu.js | 36 ++++++++--------- src/js/services/incomingData.js | 46 ++++++++++++++++++++-- src/js/services/send-flow-state.service.js | 10 ++++- src/js/services/send-flow.service.js | 19 +++++---- www/views/includes/incomingDataMenu.html | 24 +++++------ 6 files changed, 90 insertions(+), 55 deletions(-) diff --git a/src/js/controllers/addressbookView.js b/src/js/controllers/addressbookView.js index ecbf7299a..16df9e559 100644 --- a/src/js/controllers/addressbookView.js +++ b/src/js/controllers/addressbookView.js @@ -21,16 +21,8 @@ angular.module('copayApp.controllers').controller('addressbookViewController', f }); $scope.sendTo = function() { - var to = ''; - if ($scope.addressbookEntry.coin == 'bch') { - var a = 'bitcoincash:' + $scope.addressbookEntry.address; - to = bitcoinCashJsService.readAddress(a).legacy; - } else { - to = $scope.addressbookEntry.address; - } - var stateParams = { - toAddress: to, + data: $scope.addressbookEntry.address, toName: $scope.addressbookEntry.name, toEmail: $scope.addressbookEntry.email, coin: $scope.addressbookEntry.coin diff --git a/src/js/directives/incomingDataMenu.js b/src/js/directives/incomingDataMenu.js index 21478102b..78856e62f 100644 --- a/src/js/directives/incomingDataMenu.js +++ b/src/js/directives/incomingDataMenu.js @@ -1,23 +1,28 @@ 'use strict'; angular.module('copayApp.directives') - .directive('incomingDataMenu', function($timeout, $rootScope, $state, externalLinkService) { + .directive('incomingDataMenu', function($timeout, $rootScope, $state, externalLinkService, sendFlowService, bitcoinCashJsService) { return { restrict: 'E', templateUrl: 'views/includes/incomingDataMenu.html', link: function(scope, element, attrs) { $rootScope.$on('incomingDataMenu.showMenu', function(event, data) { $timeout(function() { - scope.data = data.data; - scope.type = data.type; - scope.showMenu = true; - scope.https = false; + scope.data = data; - if (scope.type === 'url') { - if (scope.data.indexOf('https://') === 0) { - scope.https = true; - } + if (scope.data.parsed.privateKey) { + scope.type = "privateKey"; + } else if (scope.data.parsed.url) { + scope.type = "url"; + } else if (scope.data.parsed.publicAddress) { + scope.type = "bitcoinAddress"; + var prefix = scope.data.parsed.isTestnet ? 'bchtest:' : 'bitcoincash:'; + scope.data.toAddress = (prefix + scope.data.parsed.publicAddress.cashAddr) || scope.data.parsed.publicAddress.legacy || scope.data.parsed.publicAddress.bitpay; + } else { + scope.type = "text"; } + + scope.showMenu = true; }); }); scope.hide = function() { @@ -28,18 +33,9 @@ angular.module('copayApp.directives') externalLinkService.open(url); }; scope.sendPaymentToAddress = function(bitcoinAddress) { - var noPrefixInAddress = 0; - if (bitcoinAddress.toLowerCase().indexOf('bitcoin') < 0) { - noPrefixInAddress = 1; - } scope.showMenu = false; - $state.go('tabs.send').then(function() { - $timeout(function() { - $state.transitionTo('tabs.send.amount', { - toAddress: bitcoinAddress, - noPrefix: noPrefixInAddress - }); - }, 50); + sendFlowService.start({ + data: bitcoinAddress }); }; scope.addToAddressBook = function(bitcoinAddress) { diff --git a/src/js/services/incomingData.js b/src/js/services/incomingData.js index 9d2189ab9..016e39421 100644 --- a/src/js/services/incomingData.js +++ b/src/js/services/incomingData.js @@ -8,9 +8,7 @@ angular.module('copayApp.services').factory('incomingData', function(externalLin $rootScope.$broadcast('incomingDataMenu.showMenu', data); }; - root.redir = function(data, serviceId, serviceData) { - var originalAddress = null; - var noPrefixInAddress = 0; + root.redir = function(data) { var allParsed = bitcoinUriService.parse(data); if (allParsed.isValid && allParsed.isTestnet) { @@ -19,7 +17,49 @@ angular.module('copayApp.services').factory('incomingData', function(externalLin gettextCatalog.getString('Testnet is not supported.') ); return false; + } else { + /** + * Hardcore fix, but the legacy code in the bottom needs to be removed. + * BitcoinUriService is making the job of this. + * incomingData should be an intermediate to redirect either to the sendFlow + * or to import a wallet for example. + */ + scannerService.pausePreview(); + + /** + * Strategy for the action + */ + if ( + !allParsed.isValid + || allParsed.privateKey + || (sendFlowService.state.isEmpty() && !allParsed.url && !allParsed.amount) + ) { + root.showMenu({ + original: data, + parsed: allParsed + }); + } else { + var state = sendFlowService.state.getClone(); + state.data = data; + + sendFlowService.start(state, function onError(err) { + /** + * OnError, open the menu (link not validated) + */ + root.showMenu({ + original: data, + parsed: allParsed + }); + }); + } } + + // No need to go more far + return; + + /** + * The legacy code in the bottom needs to be checked and removed if any case is forgotten. + */ if (data.toLowerCase().indexOf('bitcoin') < 0) { noPrefixInAddress = 1; diff --git a/src/js/services/send-flow-state.service.js b/src/js/services/send-flow-state.service.js index f9697dcf5..0d2912b59 100644 --- a/src/js/services/send-flow-state.service.js +++ b/src/js/services/send-flow-state.service.js @@ -29,13 +29,17 @@ angular map: map, pop: pop, push: push, + isEmpty: isEmpty }; return service; function init(params) { clear(); - push(params); + + if (params) { + push(params); + } } function clear() { @@ -94,6 +98,10 @@ angular clearCurrent(); map(params); }; + + function isEmpty() { + return service.previousStates.length == 0; + }; }; })(); \ No newline at end of file diff --git a/src/js/services/send-flow.service.js b/src/js/services/send-flow.service.js index ee7eb37a9..cac710b0c 100644 --- a/src/js/services/send-flow.service.js +++ b/src/js/services/send-flow.service.js @@ -30,7 +30,7 @@ angular /** * Clears all previous state */ - function start(params) { + function start(params, onError) { console.log('start()'); if (params && params.data) { @@ -101,11 +101,14 @@ angular } if (res.amount) { + if (res.currency) { + params.currency = res.currency; + } params.amount = res.amount; } if (res.publicAddress) { - var prefix = res.testnet ? 'bchtest:' : 'bitcoincash:'; + var prefix = res.isTestnet ? 'bchtest:' : 'bitcoincash:'; params.displayAddress = (prefix + res.publicAddress.cashAddr) || res.publicAddress.legacy || res.publicAddress.bitpay; params.toAddress = bitcoinCashJsService.readAddress(params.displayAddress).legacy; } @@ -113,7 +116,9 @@ angular _next(); } } else { - _next(); + if (onError) { + onError(); + } } } else { _next(); @@ -122,13 +127,7 @@ angular // Next used for sync the async task function _next() { - - /** - * Init the state if params is defined - */ - if (params) { - sendFlowStateService.init(params); - } + sendFlowStateService.init(params); /** * Routing strategy to -> send-flow-router.service diff --git a/www/views/includes/incomingDataMenu.html b/www/views/includes/incomingDataMenu.html index 1d66b616a..e60d7e956 100644 --- a/www/views/includes/incomingDataMenu.html +++ b/www/views/includes/incomingDataMenu.html @@ -9,21 +9,21 @@
- {{data}} + {{data.original}}
- +
Add as a contact
- +
Send payment to this address
- +
Copy to clipboard
@@ -38,11 +38,11 @@
Text
- {{data}} + {{data.original}}
-
+
Copy to clipboard
@@ -57,16 +57,16 @@
Private Key
- {{data}} + {{data.original}}
-
+
Sweep paper wallet
- +
Copy to clipboard
@@ -81,16 +81,16 @@
URL
- {{data}} + {{data.original}}
-
+
Open in web browser
- +
Copy to clipboard