diff --git a/public/views/tab-scan.html b/public/views/tab-scan.html
index 906244030..0402251b9 100644
--- a/public/views/tab-scan.html
+++ b/public/views/tab-scan.html
@@ -2,7 +2,7 @@
Scan
-
+
diff --git a/src/js/controllers/tab-scan.js b/src/js/controllers/tab-scan.js
index afb6a5c55..03e81a246 100644
--- a/src/js/controllers/tab-scan.js
+++ b/src/js/controllers/tab-scan.js
@@ -1,11 +1,91 @@
'use strict';
-angular.module('copayApp.controllers').controller('tabScanController', function($scope, $timeout, $ionicModal, gettextCatalog, platformInfo) {
+angular.module('copayApp.controllers').controller('tabScanController', function($scope, $timeout, $ionicModal, $log, $ionicPopup, configService, gettextCatalog, platformInfo, go, bitcore, lodash) {
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);
+ var alertPopup = $ionicPopup.alert({
+ title: title,
+ template: msg
+ });
+
+ if (!cb) cb = function(res) {};
+
+ alertPopup.then(cb);
+ };
+
+ var _dataScanned = function(data) {
+ var parsedData = _parseFromUri(data);
+
+ if (lodash.isEmpty(parsedData)) {
+ _showAlert('Bad bitcoin address', 'Could not recognize the bitcoin address', function(res) {
+ $scope.init();
+ });
+ return;
+ }
+
+ 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) {
$timeout(function() {
window.plugins.spinnerDialog.hide();
@@ -14,6 +94,11 @@ angular.module('copayApp.controllers').controller('tabScanController', function(
$timeout(function() {
var data = isIOS ? result : result.text;
+ // Check if the current page is tabs.scan
+ if (go.is('tabs.scan')) {
+ _dataScanned(data);
+ return;
+ }
$scope.onScan({
data: data
});
@@ -103,7 +188,12 @@ angular.module('copayApp.controllers').controller('tabScanController', function(
prevResult = data;
return;
}
+ // Check if the current page is tabs.scan
_scanStop();
+ if (go.is('tabs.scan')) {
+ _dataScanned(data);
+ return;
+ }
$scope.cancel();
$scope.onScan({
data: data
diff --git a/src/js/services/go.js b/src/js/services/go.js
index b26bb6ec4..529ee1330 100644
--- a/src/js/services/go.js
+++ b/src/js/services/go.js
@@ -39,6 +39,10 @@ angular.module('copayApp.services').factory('go', function($window, $ionicSideMe
}
};
+ root.confirm = function(params) {
+ $state.transitionTo('confirm', params)
+ };
+
root.send = function() {
root.path('tabs.send');
};