QR Scanner for bitcoin URI
This commit is contained in:
parent
4ee69ce0a1
commit
89b0fca5cb
3 changed files with 90 additions and 2 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
<ion-nav-bar class="bar-stable">
|
<ion-nav-bar class="bar-stable">
|
||||||
<ion-nav-title>Scan</ion-nav-title>
|
<ion-nav-title>Scan</ion-nav-title>
|
||||||
</ion-nav-bar>
|
</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>
|
<canvas id="qr-canvas" width="200" height="150"></canvas>
|
||||||
<video id="qrcode-scanner-video" width="300" height="225"></video>
|
<video id="qrcode-scanner-video" width="300" height="225"></video>
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,91 @@
|
||||||
'use strict';
|
'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 isCordova = platformInfo.isCordova;
|
||||||
var isWP = platformInfo.isWP;
|
var isWP = platformInfo.isWP;
|
||||||
var isIOS = platformInfo.isIOS;
|
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) {
|
var onSuccess = function(result) {
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
window.plugins.spinnerDialog.hide();
|
window.plugins.spinnerDialog.hide();
|
||||||
|
|
@ -14,6 +94,8 @@ angular.module('copayApp.controllers').controller('tabScanController', function(
|
||||||
|
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
var data = isIOS ? result : result.text;
|
var data = isIOS ? result : result.text;
|
||||||
|
// Check if the current page is tabs.scan
|
||||||
|
if (go.is('tabs.scan')) _dataScanned(data);
|
||||||
$scope.onScan({
|
$scope.onScan({
|
||||||
data: data
|
data: data
|
||||||
});
|
});
|
||||||
|
|
@ -103,7 +185,9 @@ angular.module('copayApp.controllers').controller('tabScanController', function(
|
||||||
prevResult = data;
|
prevResult = data;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Check if the current page is tabs.scan
|
||||||
_scanStop();
|
_scanStop();
|
||||||
|
if (go.is('tabs.scan')) _dataScanned(data);
|
||||||
$scope.cancel();
|
$scope.cancel();
|
||||||
$scope.onScan({
|
$scope.onScan({
|
||||||
data: data
|
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.send = function() {
|
||||||
root.path('tabs.send');
|
root.path('tabs.send');
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue