2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
2015-04-23 16:17:18 -03:00
|
|
|
angular.module('copayApp.controllers').controller('paymentUriController',
|
|
|
|
|
function($rootScope, $stateParams, $location, $timeout, profileService, configService, lodash, bitcore, go) {
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
function strip(number) {
|
|
|
|
|
return (parseFloat(number.toPrecision(12)));
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
// Build bitcoinURI with querystring
|
|
|
|
|
this.checkBitcoinUri = function() {
|
|
|
|
|
var query = [];
|
|
|
|
|
angular.forEach($location.search(), function(value, key) {
|
|
|
|
|
query.push(key + "=" + value);
|
|
|
|
|
});
|
|
|
|
|
var queryString = query ? query.join("&") : null;
|
|
|
|
|
this.bitcoinURI = $stateParams.data + (queryString ? '?' + queryString : '');
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
var URI = bitcore.URI;
|
|
|
|
|
var isUriValid = URI.isValid(this.bitcoinURI);
|
|
|
|
|
if (!URI.isValid(this.bitcoinURI)) {
|
|
|
|
|
this.error = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var uri = new URI(this.bitcoinURI);
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
if (uri && uri.address) {
|
|
|
|
|
var config = configService.getSync().wallet.settings;
|
|
|
|
|
var unitToSatoshi = config.unitToSatoshi;
|
|
|
|
|
var satToUnit = 1 / unitToSatoshi;
|
|
|
|
|
var unitName = config.unitName;
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
uri.amount = strip(uri.amount * satToUnit) + ' ' + unitName;
|
|
|
|
|
uri.network = uri.address.network.name;
|
|
|
|
|
return uri;
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
this.getWallets = function(network) {
|
|
|
|
|
if (!profileService.profile) return;
|
|
|
|
|
var config = configService.getSync();
|
|
|
|
|
config.colorFor = config.colorFor || {};
|
|
|
|
|
var ret = lodash.map(profileService.profile.credentials, function(c) {
|
|
|
|
|
return {
|
|
|
|
|
m: c.m,
|
|
|
|
|
n: c.n,
|
|
|
|
|
name: c.walletName,
|
|
|
|
|
id: c.walletId,
|
|
|
|
|
network: c.network,
|
|
|
|
|
color: config.colorFor[c.walletId] || '#2C3E50'
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
ret = lodash.filter(ret, function(w) {
|
|
|
|
|
return (w.network == network);
|
|
|
|
|
});
|
|
|
|
|
return lodash.sortBy(ret, 'walletName');
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-04-23 16:17:18 -03:00
|
|
|
this.selectWallet = function(wid) {
|
|
|
|
|
var self = this;
|
|
|
|
|
if (wid != profileService.focusedClient.credentials.walletId) {
|
|
|
|
|
profileService.setAndStoreFocus(wid, function() {});
|
|
|
|
|
}
|
|
|
|
|
go.send();
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$rootScope.$emit('paymentUri', self.bitcoinURI);
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
});
|