QR Scanner for bitcoin URI

This commit is contained in:
Gustavo Maximiliano Cortez 2016-08-17 19:25:07 -03:00
parent 4ee69ce0a1
commit 89b0fca5cb
No known key found for this signature in database
GPG Key ID: 15EDAD8D9F2EB1AF
3 changed files with 90 additions and 2 deletions

View File

@ -2,7 +2,7 @@
<ion-nav-bar class="bar-stable">
<ion-nav-title>Scan</ion-nav-title>
</ion-nav-bar>
<ion-content ng-controller="tabScanController" ng-init="init()">
<ion-content class="padding" ng-controller="tabScanController" ng-init="init()">
<canvas id="qr-canvas" width="200" height="150"></canvas>
<video id="qrcode-scanner-video" width="300" height="225"></video>

View File

@ -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

View File

@ -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');
};