Adds bitcoin cash basic support (unit convertion, integrations, request specific amount, etc)

This commit is contained in:
Gustavo Maximiliano Cortez 2017-08-24 17:02:49 -03:00
commit 94363704ab
No known key found for this signature in database
GPG key ID: 15EDAD8D9F2EB1AF
24 changed files with 376 additions and 302 deletions

View file

@ -143,6 +143,14 @@ angular.module('copayApp.services').factory('configService', function(storageSer
configCache.bitpayAccount = defaultConfig.bitpayAccount;
}
if (configCache.wallet.settings.unitCode == 'bit') {
// Convert to BTC. Bits will be disabled
configCache.wallet.settings.unitName = defaultConfig.wallet.settings.unitName;
configCache.wallet.settings.unitToSatoshi = defaultConfig.wallet.settings.unitToSatoshi;
configCache.wallet.settings.unitDecimals = defaultConfig.wallet.settings.unitDecimals;
configCache.wallet.settings.unitCode = defaultConfig.wallet.settings.unitCode;
}
} else {
configCache = lodash.clone(defaultConfig);
};

View file

@ -64,7 +64,6 @@ angular.module('copayApp.services').factory('feeService', function($log, $timeou
}
var walletClient = bwcService.getClient();
var unitName = configService.getSync().wallet.settings.unitName;
walletClient.getFeeLevels('livenet', function(errLivenet, levelsLivenet) {
walletClient.getFeeLevels('testnet', function(errTestnet, levelsTestnet) {

View file

@ -89,6 +89,7 @@ angular.module('copayApp.services')
wallet.copayerId = wallet.credentials.copayerId;
wallet.m = wallet.credentials.m;
wallet.n = wallet.credentials.n;
wallet.chain = wallet.credentials.chain;
root.updateWalletSettings(wallet);
root.wallet[walletId] = wallet;
@ -222,11 +223,13 @@ angular.module('copayApp.services')
return ((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url);
};
var client = bwcService.getClient(JSON.stringify(credentials), {
bwsurl: getBWSURL(credentials.walletId),
});
// TODO: Should return "chain" = "BTC" or "BCH"
client.credentials.chain = 'BTC';
var skipKeyValidation = shouldSkipValidation(credentials.walletId);
if (!skipKeyValidation)
root.runValidation(client, 500);
@ -749,6 +752,12 @@ angular.module('copayApp.services')
var ret = lodash.values(root.wallet);
if (opts.chain) {
ret = lodash.filter(ret, function(x) {
return (x.credentials.chain == opts.chain);
});
}
if (opts.network) {
ret = lodash.filter(ret, function(x) {
return (x.credentials.network == opts.network);
@ -848,7 +857,7 @@ angular.module('copayApp.services')
});
};
function process(notifications) {
function process(wallet, notifications) {
if (!notifications) return [];
var shown = lodash.sortBy(notifications, 'createdOn').reverse();
@ -861,7 +870,7 @@ angular.module('copayApp.services')
x.types = [x.type];
if (x.data && x.data.amount)
x.amountStr = txFormatService.formatAmountStr(x.data.amount);
x.amountStr = txFormatService.formatAmountStr(wallet, x.data.amount);
x.action = function() {
// TODO?
@ -939,7 +948,7 @@ angular.module('copayApp.services')
notifications = lodash.sortBy(notifications, 'createdOn');
notifications = lodash.compact(lodash.flatten(notifications)).slice(0, MAX);
var total = notifications.length;
return cb(null, process(notifications), total);
return cb(null, process(wallet, notifications), total);
};
});
});

View file

@ -17,10 +17,10 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
return this.Utils.formatAmount(satoshis, config.unitCode, opts);
};
root.formatAmountStr = function(satoshis) {
root.formatAmountStr = function(wallet, satoshis) {
console.log('[txFormatService.js:20]',wallet); //TODO/
if (isNaN(satoshis)) return;
var config = configService.getSync().wallet.settings;
return root.formatAmount(satoshis) + ' ' + config.unitName;
return root.formatAmount(satoshis) + ' ' + wallet.chain;
};
root.toFiat = function(satoshis, code, cb) {
@ -86,7 +86,7 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
};
};
root.processTx = function(tx) {
root.processTx = function(wallet, tx) {
if (!tx || tx.action == 'invalid')
return tx;
@ -101,7 +101,7 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
tx.hasMultiplesOutputs = true;
}
tx.amount = lodash.reduce(tx.outputs, function(total, o) {
o.amountStr = root.formatAmountStr(o.amount);
o.amountStr = root.formatAmountStr(wallet, o.amount);
o.alternativeAmountStr = root.formatAlternativeStr(o.amount);
return total + o.amount;
}, 0);
@ -109,9 +109,9 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
tx.toAddress = tx.outputs[0].toAddress;
}
tx.amountStr = root.formatAmountStr(tx.amount);
tx.amountStr = root.formatAmountStr(wallet, tx.amount);
tx.alternativeAmountStr = root.formatAlternativeStr(tx.amount);
tx.feeStr = root.formatAmountStr(tx.fee || tx.fees);
tx.feeStr = root.formatAmountStr(wallet, tx.fee || tx.fees);
if (tx.amountStr) {
tx.amountValueStr = tx.amountStr.split(' ')[0];
@ -145,8 +145,6 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
lodash.each(txps, function(tx) {
tx = txFormatService.processTx(tx);
// no future transactions...
if (tx.createdOn > now)
tx.createdOn = now;
@ -157,6 +155,8 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
return;
}
tx = txFormatService.processTx(tx.wallet, tx);
var action = lodash.find(tx.actions, {
copayerId: tx.wallet.copayerId
});
@ -180,7 +180,7 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
return txps;
};
root.parseAmount = function(amount, currency) {
root.parseAmount = function(wallet, amount, currency) {
var config = configService.getSync().wallet.settings;
var satToBtc = 1 / 100000000;
var unitToSatoshi = config.unitToSatoshi;
@ -189,21 +189,21 @@ angular.module('copayApp.services').factory('txFormatService', function($filter,
var alternativeIsoCode = config.alternativeIsoCode;
// If fiat currency
if (currency != 'bits' && currency != 'BTC' && currency != 'sat') {
if (currency != 'BCH' && currency != 'BTC' && currency != 'sat') {
amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency;
amountSat = rateService.fromFiat(amount, currency).toFixed(0);
} else if (currency == 'sat') {
amountSat = amount;
amountUnitStr = root.formatAmountStr(amountSat);
// convert sat to BTC
amountUnitStr = root.formatAmountStr(wallet, amountSat);
// convert sat to BTC or BCH
amount = (amountSat * satToBtc).toFixed(8);
currency = 'BTC';
currency = wallet.chain;
} else {
amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
amountUnitStr = root.formatAmountStr(amountSat);
// convert unit to BTC
amountUnitStr = root.formatAmountStr(wallet, amountSat);
// convert unit to BTC or BCH
amount = (amountSat * satToBtc).toFixed(8);
currency = 'BTC';
currency = wallet.chain;
}
return {

View file

@ -2,8 +2,8 @@
angular.module('copayApp.services').factory('walletService', function($log, $timeout, lodash, trezor, ledger, intelTEE, storageService, configService, rateService, uxLanguage, $filter, gettextCatalog, bwcError, $ionicPopup, fingerprintService, ongoingProcess, gettext, $rootScope, txFormatService, $ionicModal, $state, bwcService, bitcore, popupService) {
// Ratio low amount warning (fee/amount) in incoming TX
var LOW_AMOUNT_RATIO = 0.15;
// Ratio low amount warning (fee/amount) in incoming TX
var LOW_AMOUNT_RATIO = 0.15;
// Ratio of "many utxos" warning in total balance (fee/amount)
var TOTAL_LOW_WARNING_RATIO = .3;
@ -130,7 +130,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
lodash.each(txps, function(tx) {
tx = txFormatService.processTx(tx);
tx = txFormatService.processTx(wallet, tx);
// no future transactions...
if (tx.createdOn > now)
@ -213,14 +213,13 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
// Selected unit
cache.unitToSatoshi = config.settings.unitToSatoshi;
cache.satToUnit = 1 / cache.unitToSatoshi;
cache.unitName = config.settings.unitName;
//STR
cache.totalBalanceStr = txFormatService.formatAmount(cache.totalBalanceSat) + ' ' + cache.unitName;
cache.lockedBalanceStr = txFormatService.formatAmount(cache.lockedBalanceSat) + ' ' + cache.unitName;
cache.availableBalanceStr = txFormatService.formatAmount(cache.availableBalanceSat) + ' ' + cache.unitName;
cache.spendableBalanceStr = txFormatService.formatAmount(cache.spendableAmount) + ' ' + cache.unitName;
cache.pendingBalanceStr = txFormatService.formatAmount(cache.pendingAmount) + ' ' + cache.unitName;
cache.totalBalanceStr = txFormatService.formatAmount(cache.totalBalanceSat) + ' ' + wallet.chain;
cache.lockedBalanceStr = txFormatService.formatAmount(cache.lockedBalanceSat) + ' ' + wallet.chain;
cache.availableBalanceStr = txFormatService.formatAmount(cache.availableBalanceSat) + ' ' + wallet.chain;
cache.spendableBalanceStr = txFormatService.formatAmount(cache.spendableAmount) + ' ' + wallet.chain;
cache.pendingBalanceStr = txFormatService.formatAmount(cache.pendingAmount) + ' ' + wallet.chain;
cache.alternativeName = config.settings.alternativeName;
cache.alternativeIsoCode = config.settings.alternativeIsoCode;
@ -366,7 +365,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
wallet.hasUnsafeConfirmed = false;
lodash.each(txs, function(tx) {
tx = txFormatService.processTx(tx);
tx = txFormatService.processTx(wallet, tx);
// no future transactions...
if (tx.time > now)
@ -400,7 +399,6 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
var LIMIT = 50;
var requestLimit = FIRST_LIMIT;
var walletId = wallet.credentials.walletId;
var config = configService.getSync().wallet.settings;
var opts = opts || {};
var progressFn = opts.progressFn || function() {};
@ -414,17 +412,15 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
var fixTxsUnit = function(txs) {
if (!txs || !txs[0] || !txs[0].amountStr) return;
var cacheUnit = txs[0].amountStr.split(' ')[1];
var cacheChain = txs[0].amountStr.split(' ')[1];
if (cacheUnit == config.unitName)
if (cacheChain == wallet.chain)
return;
var name = ' ' + config.unitName;
$log.debug('Fixing Tx Cache Unit to:' + name)
$log.debug('Fixing Tx Cache Unit to: ' + wallet.chain)
lodash.each(txs, function(tx) {
tx.amountStr = txFormatService.formatAmount(tx.amount) + name;
tx.feeStr = txFormatService.formatAmount(tx.fees) + name;
tx.amountStr = txFormatService.formatAmountStr(wallet, tx.amount);
tx.feeStr = txFormatService.formatAmountStr(wallet, tx.fees);
});
};
@ -788,7 +784,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
//prefs.email (may come from arguments)
prefs.email = config.emailNotifications.email;
prefs.language = uxLanguage.getCurrentLanguage();
prefs.unit = walletSettings.unitCode;
// prefs.unit = walletSettings.unitCode; // TODO: remove, not used
updateRemotePreferencesFor(lodash.clone(clients), prefs, function(err) {
if (err) return cb(err);
@ -922,7 +918,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
};
// Approx utxo amount, from which the uxto is economically redeemable
// Approx utxo amount, from which the uxto is economically redeemable
root.getMinFee = function(wallet, feeLevels, nbOutputs) {
var lowLevelRate = (lodash.find(feeLevels[wallet.network], {
level: 'normal',
@ -933,7 +929,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
};
// Approx utxo amount, from which the uxto is economically redeemable
// Approx utxo amount, from which the uxto is economically redeemable
root.getLowAmount = function(wallet, feeLevels, nbOutputs) {
var minFee = root.getMinFee(wallet,feeLevels, nbOutputs);
return parseInt( minFee / LOW_AMOUNT_RATIO);