qr Scanner

This commit is contained in:
Matias Alejo Garcia 2016-08-24 19:28:20 -03:00
commit 28235011fd
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
3 changed files with 54 additions and 111 deletions

View file

@ -1,18 +1,12 @@
'use strict';
angular.module('copayApp.controllers').controller('tabScanController', function($scope, $timeout, $ionicModal, $log, $ionicPopup, configService, gettextCatalog, platformInfo, bitcore, lodash, $state) {
angular.module('copayApp.controllers').controller('tabScanController', function($scope, $timeout, $ionicModal, $log, $ionicPopup, configService, gettextCatalog, platformInfo, bitcore, lodash, $state, walletService) {
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);
@ -27,63 +21,13 @@ angular.module('copayApp.controllers').controller('tabScanController', function(
};
var _dataScanned = function(data) {
var parsedData = _parseFromUri(data);
if (lodash.isEmpty(parsedData)) {
$log.debug('Scanned:' + data);
if (!walletService.redirFromUri(data)) {
$log.warn('Fail to process scanned data');
_showAlert('Bad bitcoin address', 'Could not recognize the bitcoin address', function(res) {
$scope.init();
});
return;
}
$state.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) {

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $ionicModal, $log, $timeout, addressbookService, profileService, lodash, $state, walletService, bitcore ) {
angular.module('copayApp.controllers').controller('tabSendController', function($scope, $ionicModal, $log, $timeout, addressbookService, profileService, lodash, $state, walletService ) {
var originalList;
@ -51,60 +51,14 @@ angular.module('copayApp.controllers').controller('tabSendController', function(
};
var setFromUri = 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;
};
// URI extensions for Payment Protocol with non-backwards-compatible request
if ((/^bitcoin:\?r=[\w+]/).exec(uri)) {
uri = decodeURIComponent(uri.replace('bitcoin:?r=', ''));
setFromPayPro(uri, function(err) {
if (err) {
return err;
}
});
} 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 : '';
if (parsed.r) {
$state.go('send.confirm', {paypro: parsed.r})
} else {
if (amount) {
$state.go('send.confirm', {toAmount: amount, toAddress: addr, description:message})
} else {
$state.go('send.amount', {toAddress: addr})
}
}
}
};
$scope.findContact = function(search, opts) {
opts = opts || {};
if (search.indexOf('bitcoin:') === 0) {
return setFromUri(search);
if (!walletService.redirFromUri(search)) {
$log.error(err);
}
} else if (/^https?:\/\//.test(search)) {
return $state.go('send.confirm', {paypro: search})
}

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services').factory('walletService', function($log, $timeout, lodash, trezor, ledger, storageService, configService, rateService, uxLanguage, $filter, gettextCatalog, bwcError, $ionicPopup, fingerprintService, ongoingProcess, gettext, $rootScope, txStatus, txFormatService, $ionicModal, $state, bwcService) {
angular.module('copayApp.services').factory('walletService', function($log, $timeout, lodash, trezor, ledger, storageService, configService, rateService, uxLanguage, $filter, gettextCatalog, bwcError, $ionicPopup, fingerprintService, ongoingProcess, gettext, $rootScope, txStatus, txFormatService, $ionicModal, $state, bwcService, bitcore) {
// `wallet` is a decorated version of client.
var root = {};
@ -1123,5 +1123,50 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
return finale;
};
// Here?
root.redirFromUri = 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;
};
// URI extensions for Payment Protocol with non-backwards-compatible request
if ((/^bitcoin:\?r=[\w+]/).exec(uri)) {
uri = decodeURIComponent(uri.replace('bitcoin:?r=', ''));
$state.go('send.confirm', {paypro: uri})
} else {
uri = sanitizeUri(uri);
if (!bitcore.URI.isValid(uri)) {
return false;
}
var parsed = new bitcore.URI(uri);
var addr = parsed.address ? parsed.address.toString() : '';
var message = parsed.message;
var amount = parsed.amount ? parsed.amount : '';
if (parsed.r) {
$state.go('send.confirm', {paypro: parsed.r});
} else {
if (amount) {
$state.go('send.confirm', {toAmount: amount, toAddress: addr, description:message})
} else {
$state.go('send.amount', {toAddress: addr})
}
}
return true;
}
};
return root;
});