From 28235011fd1dde6d527bdd0426370673b5479056 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 24 Aug 2016 19:28:20 -0300 Subject: [PATCH] qr Scanner --- src/js/controllers/tab-scan.js | 64 ++------------------------------ src/js/controllers/tab-send.js | 54 ++------------------------- src/js/services/walletService.js | 47 ++++++++++++++++++++++- 3 files changed, 54 insertions(+), 111 deletions(-) diff --git a/src/js/controllers/tab-scan.js b/src/js/controllers/tab-scan.js index 3615b9778..8faf3aa85 100644 --- a/src/js/controllers/tab-scan.js +++ b/src/js/controllers/tab-scan.js @@ -1,18 +1,12 @@ 'use strict'; -angular.module('copayApp.controllers').controller('tabScanController', function($scope, $timeout, $ionicModal, $log, $ionicPopup, configService, gettextCatalog, platformInfo, bitcore, lodash, $state) { +angular.module('copayApp.controllers').controller('tabScanController', function($scope, $timeout, $ionicModal, $log, $ionicPopup, configService, gettextCatalog, platformInfo, bitcore, lodash, $state, walletService) { var isCordova = platformInfo.isCordova; var isWP = platformInfo.isWP; var isIOS = platformInfo.isIOS; - var config = configService.getSync(); - var configWallet = config.wallet; - var walletSettings = configWallet.settings; - var unitToSatoshi = walletSettings.unitToSatoshi; - var unitDecimals = walletSettings.unitDecimals; - var satToUnit = 1 / unitToSatoshi; var _showAlert = function(title, msg, cb) { $log.warn(title + ":"+ msg); @@ -27,63 +21,13 @@ angular.module('copayApp.controllers').controller('tabScanController', function( }; var _dataScanned = function(data) { - var parsedData = _parseFromUri(data); - - if (lodash.isEmpty(parsedData)) { + $log.debug('Scanned:' + data); + if (!walletService.redirFromUri(data)) { + $log.warn('Fail to process scanned data'); _showAlert('Bad bitcoin address', 'Could not recognize the bitcoin address', function(res) { $scope.init(); }); - return; } - - $state.go('confirm', parsedData); - }; - - var _parseFromUri = function(uri) { - - function sanitizeUri(uri) { - // Fixes when a region uses comma to separate decimals - var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i; - var match = regex.exec(uri); - if (!match || match.length === 0) { - return uri; - } - var value = match[0].replace(',', '.'); - var newUri = uri.replace(regex, value); - return newUri; - }; - - var satToUnit = 1 / unitToSatoshi; - - // URI extensions for Payment Protocol with non-backwards-compatible request - if ((/^bitcoin:\?r=[\w+]/).exec(uri)) { - // TODO: PAYPRO - } else { - uri = sanitizeUri(uri); - - if (!bitcore.URI.isValid(uri)) { - return uri; - } - var parsed = new bitcore.URI(uri); - - var addr = parsed.address ? parsed.address.toString() : ''; - var message = parsed.message; - - var amount = parsed.amount ? - (parsed.amount.toFixed(0) * satToUnit).toFixed(unitDecimals) : 0; - - - if (parsed.r) { - // TODO: PAYPRO - } else { - // TODO: message - return { - toAddress: addr, - toAmount: amount - }; - } - } - }; var onSuccess = function(result) { diff --git a/src/js/controllers/tab-send.js b/src/js/controllers/tab-send.js index 7cbf30980..f15d33198 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, $ionicModal, $log, $timeout, addressbookService, profileService, lodash, $state, walletService, bitcore ) { +angular.module('copayApp.controllers').controller('tabSendController', function($scope, $ionicModal, $log, $timeout, addressbookService, profileService, lodash, $state, walletService ) { var originalList; @@ -51,60 +51,14 @@ angular.module('copayApp.controllers').controller('tabSendController', function( }; - var setFromUri = function(uri) { - - function sanitizeUri(uri) { - // Fixes when a region uses comma to separate decimals - var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i; - var match = regex.exec(uri); - if (!match || match.length === 0) { - return uri; - } - var value = match[0].replace(',', '.'); - var newUri = uri.replace(regex, value); - return newUri; - }; - - // URI extensions for Payment Protocol with non-backwards-compatible request - if ((/^bitcoin:\?r=[\w+]/).exec(uri)) { - uri = decodeURIComponent(uri.replace('bitcoin:?r=', '')); - setFromPayPro(uri, function(err) { - if (err) { - return err; - } - }); - } else { - uri = sanitizeUri(uri); - - if (!bitcore.URI.isValid(uri)) { - return uri; - } - var parsed = new bitcore.URI(uri); - - var addr = parsed.address ? parsed.address.toString() : ''; - var message = parsed.message; - - var amount = parsed.amount ? parsed.amount : ''; - - if (parsed.r) { - $state.go('send.confirm', {paypro: parsed.r}) - } else { - if (amount) { - $state.go('send.confirm', {toAmount: amount, toAddress: addr, description:message}) - } else { - $state.go('send.amount', {toAddress: addr}) - } - } - } - }; - - $scope.findContact = function(search, opts) { opts = opts || {}; if (search.indexOf('bitcoin:') === 0) { - return setFromUri(search); + if (!walletService.redirFromUri(search)) { + $log.error(err); + } } else if (/^https?:\/\//.test(search)) { return $state.go('send.confirm', {paypro: search}) } diff --git a/src/js/services/walletService.js b/src/js/services/walletService.js index 3165a89d8..8cc238a31 100644 --- a/src/js/services/walletService.js +++ b/src/js/services/walletService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('walletService', function($log, $timeout, lodash, trezor, ledger, storageService, configService, rateService, uxLanguage, $filter, gettextCatalog, bwcError, $ionicPopup, fingerprintService, ongoingProcess, gettext, $rootScope, txStatus, txFormatService, $ionicModal, $state, bwcService) { +angular.module('copayApp.services').factory('walletService', function($log, $timeout, lodash, trezor, ledger, storageService, configService, rateService, uxLanguage, $filter, gettextCatalog, bwcError, $ionicPopup, fingerprintService, ongoingProcess, gettext, $rootScope, txStatus, txFormatService, $ionicModal, $state, bwcService, bitcore) { // `wallet` is a decorated version of client. var root = {}; @@ -1123,5 +1123,50 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim return finale; }; + // Here? + root.redirFromUri = function(uri) { + + function sanitizeUri(uri) { + // Fixes when a region uses comma to separate decimals + var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i; + var match = regex.exec(uri); + if (!match || match.length === 0) { + return uri; + } + var value = match[0].replace(',', '.'); + var newUri = uri.replace(regex, value); + return newUri; + }; + + // URI extensions for Payment Protocol with non-backwards-compatible request + if ((/^bitcoin:\?r=[\w+]/).exec(uri)) { + uri = decodeURIComponent(uri.replace('bitcoin:?r=', '')); + $state.go('send.confirm', {paypro: uri}) + } else { + uri = sanitizeUri(uri); + + if (!bitcore.URI.isValid(uri)) { + return false; + } + var parsed = new bitcore.URI(uri); + + var addr = parsed.address ? parsed.address.toString() : ''; + var message = parsed.message; + + var amount = parsed.amount ? parsed.amount : ''; + + if (parsed.r) { + $state.go('send.confirm', {paypro: parsed.r}); + } else { + if (amount) { + $state.go('send.confirm', {toAmount: amount, toAddress: addr, description:message}) + } else { + $state.go('send.amount', {toAddress: addr}) + } + } + return true; + } + }; + return root; });