bitcore-wallet-service/lib/common/utils.js

126 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-02-02 10:29:14 -08:00
var $ = require('preconditions').singleton();
var _ = require('lodash');
2015-10-30 11:24:47 -07:00
var Bitcore = require('bitcore-lib');
var crypto = Bitcore.crypto;
var encoding = Bitcore.encoding;
2015-02-02 11:00:32 -08:00
var Utils = {};
2015-02-02 10:29:14 -08:00
Utils.checkRequired = function(obj, args) {
2015-02-02 12:07:18 -08:00
args = [].concat(args);
if (!_.isObject(obj)) return false;
for (var i = 0; i < args.length; i++) {
if (!obj.hasOwnProperty(args[i])) return false;
}
return true;
2015-02-02 10:29:14 -08:00
};
2015-02-03 18:17:06 -08:00
/**
*
2015-02-03 18:17:06 -08:00
* @desc rounds a JAvascript number
* @param number
* @return {number}
*/
Utils.strip = function(number) {
2016-03-08 10:24:58 -08:00
return parseFloat(number.toPrecision(12));
2015-02-03 18:17:06 -08:00
}
2015-10-30 06:34:13 -07:00
/* TODO: It would be nice to be compatible with bitcoind signmessage. How
* the hash is calculated there? */
Utils.hashMessage = function(text) {
$.checkArgument(text);
var buf = new Buffer(text);
var ret = crypto.Hash.sha256sha256(buf);
ret = new Bitcore.encoding.BufferReader(ret).readReverse();
return ret;
};
Utils.verifyMessage = function(text, signature, pubKey) {
$.checkArgument(text);
$.checkArgument(pubKey);
if (!signature)
return false;
2015-10-30 11:24:47 -07:00
var pub = new Bitcore.PublicKey(pubKey);
2015-10-30 06:34:13 -07:00
var hash = Utils.hashMessage(text);
try {
var sig = new crypto.Signature.fromString(signature);
return crypto.ECDSA.verify(hash, sig, pub, 'little');
} catch (e) {
return false;
}
};
2015-10-30 11:24:47 -07:00
Utils.formatAmount = function(satoshis, unit, opts) {
var UNITS = {
btc: {
toSatoshis: 100000000,
maxDecimals: 6,
minDecimals: 2,
},
bit: {
toSatoshis: 100,
maxDecimals: 0,
minDecimals: 0,
},
2016-03-08 10:24:58 -08:00
sat: {
toSatoshis: 1,
maxDecimals: 0,
minDecimals: 0,
}
2015-10-30 11:24:47 -07:00
};
$.shouldBeNumber(satoshis);
$.checkArgument(_.contains(_.keys(UNITS), unit));
function addSeparators(nStr, thousands, decimal, minDecimals) {
nStr = nStr.replace('.', decimal);
var x = nStr.split(decimal);
var x0 = x[0];
var x1 = x[1];
x1 = _.dropRightWhile(x1, function(n, i) {
return n == '0' && i >= minDecimals;
}).join('');
var x2 = x.length > 1 ? decimal + x1 : '';
x0 = x0.replace(/\B(?=(\d{3})+(?!\d))/g, thousands);
return x0 + x2;
}
opts = opts || {};
2016-03-08 10:24:58 -08:00
var u = _.assign(UNITS[unit], opts);
2015-10-30 11:24:47 -07:00
var amount = (satoshis / u.toSatoshis).toFixed(u.maxDecimals);
return addSeparators(amount, opts.thousandsSeparator || ',', opts.decimalSeparator || '.', u.minDecimals);
};
2016-03-07 08:00:53 -08:00
Utils.formatAmountInBtc = function(amount) {
2016-03-08 10:24:58 -08:00
return Utils.formatAmount(amount, 'btc', {
minDecimals: 8,
maxDecimals: 8,
}) + 'btc';
2016-03-07 08:00:53 -08:00
};
Utils.formatUtxos = function(utxos) {
if (_.isEmpty(utxos)) return 'none';
return _.map([].concat(utxos), function(i) {
var amount = Utils.formatAmountInBtc(i.satoshis);
var confirmations = i.confirmations ? i.confirmations + 'c' : 'u';
return amount + '/' + confirmations;
}).join(', ');
};
Utils.formatRatio = function(ratio) {
return (ratio * 100.).toFixed(4) + '%';
};
Utils.formatSize = function(size) {
return (size / 1000.).toFixed(4) + 'kB';
};
2015-10-30 06:34:13 -07:00
2015-02-02 11:00:32 -08:00
module.exports = Utils;