mirror of https://github.com/BTCPrivate/copay.git
QR Scanner for bitcoin URI
This commit is contained in:
parent
4ee69ce0a1
commit
89b0fca5cb
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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');
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue