From 89b0fca5cb167bc705eb6d14be3af5531b65af1c Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Wed, 17 Aug 2016 19:25:07 -0300 Subject: [PATCH 1/3] QR Scanner for bitcoin URI --- public/views/tab-scan.html | 2 +- src/js/controllers/tab-scan.js | 86 +++++++++++++++++++++++++++++++++- src/js/services/go.js | 4 ++ 3 files changed, 90 insertions(+), 2 deletions(-) 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..a4833e3ca 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,8 @@ 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); $scope.onScan({ data: data }); @@ -103,7 +185,9 @@ 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); $scope.cancel(); $scope.onScan({ data: data diff --git a/src/js/services/go.js b/src/js/services/go.js index 398f20c33..abe815a12 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'); }; From 1032aa202f3098420aedbc694387792b83b9d606 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Wed, 17 Aug 2016 19:35:07 -0300 Subject: [PATCH 2/3] Fix undefined variable on device --- src/js/controllers/tab-scan.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/js/controllers/tab-scan.js b/src/js/controllers/tab-scan.js index a4833e3ca..03e81a246 100644 --- a/src/js/controllers/tab-scan.js +++ b/src/js/controllers/tab-scan.js @@ -95,7 +95,10 @@ 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); + if (go.is('tabs.scan')) { + _dataScanned(data); + return; + } $scope.onScan({ data: data }); @@ -187,7 +190,10 @@ angular.module('copayApp.controllers').controller('tabScanController', function( } // Check if the current page is tabs.scan _scanStop(); - if (go.is('tabs.scan')) _dataScanned(data); + if (go.is('tabs.scan')) { + _dataScanned(data); + return; + } $scope.cancel(); $scope.onScan({ data: data From 855b22a1137deff861603b256ab5bfdfdde5d82e Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Thu, 18 Aug 2016 00:27:23 -0300 Subject: [PATCH 3/3] Create-Join-Import --- public/views/create.html | 12 +++++++----- public/views/import.html | 16 ++++++++-------- public/views/join.html | 17 +++++++++-------- src/js/controllers/copayers.js | 2 +- src/js/controllers/create.js | 6 +++--- src/js/controllers/import.js | 26 +++++++++++++------------- src/js/controllers/index.js | 4 ++++ src/js/controllers/join.js | 8 ++++---- src/js/services/go.js | 4 ++-- src/js/services/profileService.js | 2 +- 10 files changed, 52 insertions(+), 45 deletions(-) diff --git a/public/views/create.html b/public/views/create.html index 1d427e392..cd3ed2b7a 100644 --- a/public/views/create.html +++ b/public/views/create.html @@ -1,9 +1,11 @@ - - - Add - + + + + Add + + - +
diff --git a/public/views/import.html b/public/views/import.html index 9c40af245..fc1b8312d 100644 --- a/public/views/import.html +++ b/public/views/import.html @@ -1,12 +1,11 @@ - - - Add - - - - + + + + Add + + - +
@@ -57,6 +56,7 @@
+