.
This commit is contained in:
parent
246f1927dc
commit
d5d3f9ee28
13 changed files with 310 additions and 119 deletions
87
src/js/services/incomingData.js
Normal file
87
src/js/services/incomingData.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('incomingData', function($log, $ionicModal, $state, bitcore) {
|
||||
|
||||
var root = {};
|
||||
|
||||
root.redir = function(data) {
|
||||
$log.debug('Processing incoming data:' +data);
|
||||
|
||||
function sanitizeUri(data) {
|
||||
// Fixes when a region uses comma to separate decimals
|
||||
var regex = /[\?\&]amount=(\d+([\,\.]\d+)?)/i;
|
||||
var match = regex.exec(data);
|
||||
if (!match || match.length === 0) {
|
||||
return data;
|
||||
}
|
||||
var value = match[0].replace(',', '.');
|
||||
var newUri = data.replace(regex, value);
|
||||
|
||||
// mobile devices, uris like copay://glidera
|
||||
newUri.replace('://', ':');
|
||||
|
||||
return newUri;
|
||||
};
|
||||
|
||||
// data extensions for Payment Protocol with non-backwards-compatible request
|
||||
if ((/^bitcoin:\?r=[\w+]/).exec(data)) {
|
||||
data = decodeURIComponent(data.replace('bitcoin:?r=', ''));
|
||||
$state.go('send.confirm', {paypro: data})
|
||||
}
|
||||
|
||||
|
||||
data = sanitizeUri(data);
|
||||
|
||||
// BIP21
|
||||
if (bitcore.URI.isValid(data)) {
|
||||
var parsed = new bitcore.URI(data);
|
||||
|
||||
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;
|
||||
|
||||
// Plain URL
|
||||
} else if (/^https?:\/\//.test(data)) {
|
||||
return $state.go('send.confirm', {paypro: data})
|
||||
|
||||
// Plain Address
|
||||
} else if (bitcore.Address.isValid(data, 'livenet')) {
|
||||
return $state.go('send.amount', {toAddress: data})
|
||||
} else if (bitcore.Address.isValid(data, 'testnet')) {
|
||||
return $state.go('send.amount', {toAddress: data})
|
||||
|
||||
|
||||
// copay: protocol
|
||||
} else if (data.indexOf('copay:glidera')==0) {
|
||||
return $state.go('send.uriglidera', {url: data})
|
||||
} else if (data.indexOf('copay:coinbase')==0) {
|
||||
return $state.go('send.uricoinbase', {url: data})
|
||||
|
||||
// Join
|
||||
} else if (data.match(/^copay:[0-9A-HJ-NP-Za-km-z]{70,80}$/)) {
|
||||
return $state.go('add.join', {url: data})
|
||||
|
||||
// Old join
|
||||
} else if (data.match(/^[0-9A-HJ-NP-Za-km-z]{70,80}$/)) {
|
||||
return $state.go('add.join', {url: data})
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
@ -1,37 +1,17 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('openURLService', function($rootScope, $ionicHistory, $document, $log, $state, go, platformInfo, lodash, profileService) {
|
||||
angular.module('copayApp.services').factory('openURLService', function($rootScope, $ionicHistory, $document, $log, $state, go, platformInfo, lodash, profileService, incomingData) {
|
||||
var root = {};
|
||||
|
||||
root.registeredUriHandlers = [{
|
||||
name: 'Bitcoin BIP21 URL',
|
||||
startsWith: 'bitcoin:',
|
||||
transitionTo: 'uripayment',
|
||||
}, {
|
||||
name: 'Glidera Authentication Callback',
|
||||
startsWith: 'copay:glidera',
|
||||
transitionTo: 'uriglidera',
|
||||
}, {
|
||||
name: 'Coinbase Authentication Callback',
|
||||
startsWith: 'copay:coinbase',
|
||||
transitionTo: 'uricoinbase',
|
||||
}];
|
||||
|
||||
|
||||
var handleOpenURL = function(args) {
|
||||
$log.info('Handling Open URL: ' + JSON.stringify(args));
|
||||
|
||||
if (!profileService.isBound) {
|
||||
$log.warn('Profile not bound yet. Waiting');
|
||||
|
||||
return $rootScope.$on('Local/ProfileBound', function() {
|
||||
// Wait ux to settle
|
||||
setTimeout(function() {
|
||||
$log.warn('Profile ready, retrying...');
|
||||
handleOpenURL(args);
|
||||
}, 2000);
|
||||
});
|
||||
};
|
||||
profileService.whenAvailable(function() {
|
||||
// Wait ux to settle
|
||||
setTimeout(function() {
|
||||
handleOpenURL(args);
|
||||
}, 2000);
|
||||
});
|
||||
|
||||
// Stop it from caching the first view as one to return when the app opens
|
||||
$ionicHistory.nextViewOptions({
|
||||
|
|
@ -55,19 +35,7 @@ angular.module('copayApp.services').factory('openURLService', function($rootScop
|
|||
|
||||
document.addEventListener('handleopenurl', handleOpenURL, false);
|
||||
|
||||
var x = lodash.find(root.registeredUriHandlers, function(x) {
|
||||
return url.indexOf(x.startsWith) == 0 ||
|
||||
url.indexOf('web+' + x.startsWith) == 0 || // web protocols
|
||||
url.indexOf(x.startsWith.replace(':', '://')) == 0 // from mobile devices
|
||||
;
|
||||
});
|
||||
|
||||
if (x) {
|
||||
$log.debug('openURL GOT ' + x.name + ' URL');
|
||||
return $state.transitionTo(x.transitionTo, {
|
||||
url: url
|
||||
});
|
||||
} else {
|
||||
if (!incomingData.redir(url)) {
|
||||
$log.warn('Unknown URL! : ' + url);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1123,50 +1123,7 @@ 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;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue