From 19f57e614c689a2cdf9f408636af9f4dfc341ce1 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Fri, 25 Jul 2014 11:08:29 -0300 Subject: [PATCH] Add native QR scanner to send form --- index.html | 3 +++ js/controllers/send.js | 20 ++++++++++++++++++++ js/models/core/Structure.js | 17 ++++++++++------- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 3aeba981c..636918f06 100644 --- a/index.html +++ b/index.html @@ -692,6 +692,9 @@ +
+ +
diff --git a/js/controllers/send.js b/js/controllers/send.js index e9f3d92ec..d028d34c0 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -32,6 +32,7 @@ angular.module('copayApp.controllers').controller('SendController', // Detect protocol $scope.isHttp = ($window.location.protocol.indexOf('http') === 0); + $scope.isCordova = typeof(window.cordova) != 'undefined'; navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL; @@ -194,6 +195,25 @@ angular.module('copayApp.controllers').controller('SendController', }, 500); }; + $scope.scannerIntent = function() { + cordova.plugins.barcodeScanner.scan( + function onSuccess(result) { + if (result.cancelled) return; + + var bip21 = copay.Structure.parseBitcoinURI(result.text); + $scope.address = bip21.address; + + if (bip21.amount) { + $scope.amount = bip21.amount * bitcore.util.COIN * satToUnit; + } + + $rootScope.$digest(); + }, + function onError(error) { + alert('Scanning error'); + }); + } + $scope.toggleAddressBookEntry = function(key) { var w = $rootScope.wallet; w.toggleAddressBookEntry(key); diff --git a/js/models/core/Structure.js b/js/models/core/Structure.js index ff47c43f0..54b51e4da 100644 --- a/js/models/core/Structure.js +++ b/js/models/core/Structure.js @@ -59,13 +59,16 @@ Structure.parseBitcoinURI = function(uri) { data = splitDots[1]; var splitQuestion = data.split('?'); ret.address = splitQuestion[0]; - var search = splitQuestion[1]; - data = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}', - function(key, value) { - return key === "" ? value : decodeURIComponent(value); - }); - ret.amount = parseFloat(data.amount); - ret.message = data.message; + + if (splitQuestion.length > 1) { + var search = splitQuestion[1]; + data = JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g, '":"') + '"}', + function(key, value) { + return key === "" ? value : decodeURIComponent(value); + }); + ret.amount = parseFloat(data.amount); + ret.message = data.message; + } return ret; };