2015-01-03 11:29:22 +01:00
|
|
|
/**
|
|
|
|
* Node.js implementation of platform-specific routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-01-09 22:36:42 +01:00
|
|
|
var os = require("os");
|
2015-01-03 11:29:22 +01:00
|
|
|
var crypto = require("crypto");
|
2015-01-09 22:36:42 +01:00
|
|
|
var promise = typeof Promise === "undefined" ?
|
|
|
|
require("es6-promise").Promise :
|
|
|
|
Promise;
|
2015-01-07 22:28:13 +01:00
|
|
|
var bignum = require("bignum");
|
2015-01-22 01:00:58 +01:00
|
|
|
var assert = require("./_util").assert;
|
2015-01-09 22:36:42 +01:00
|
|
|
var worker = require("./worker");
|
2015-01-07 22:28:13 +01:00
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
var createHash = crypto.createHash;
|
2015-01-03 11:29:22 +01:00
|
|
|
|
2015-01-16 16:35:40 +01:00
|
|
|
exports.sha1 = function(buf) {
|
|
|
|
return createHash("sha1").update(buf).digest();
|
2015-01-03 11:29:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.sha256 = function(buf) {
|
2015-01-03 15:52:27 +01:00
|
|
|
return createHash("sha256").update(buf).digest();
|
2015-01-03 11:29:22 +01:00
|
|
|
};
|
|
|
|
|
2015-01-16 16:35:40 +01:00
|
|
|
exports.sha512 = function(buf) {
|
|
|
|
return createHash("sha512").update(buf).digest();
|
|
|
|
};
|
|
|
|
|
2015-01-03 11:29:22 +01:00
|
|
|
exports.ripemd160 = function(buf) {
|
2015-01-03 15:52:27 +01:00
|
|
|
return createHash("ripemd160").update(buf).digest();
|
2015-01-03 11:29:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.randomBytes = crypto.randomBytes;
|
2015-01-07 22:28:13 +01:00
|
|
|
|
2015-01-30 16:15:20 +01:00
|
|
|
// 2^80.
|
|
|
|
var B80 = bignum("1208925819614629174706176");
|
2015-01-07 22:28:13 +01:00
|
|
|
|
|
|
|
// NOTE(Kagami): We can't calculate entire target in JavaScript but the
|
|
|
|
// result can be represented in native number type without losing
|
|
|
|
// precision (targets mainly much less than 2^53).
|
|
|
|
exports.getTarget = function(opts) {
|
2015-01-30 16:15:20 +01:00
|
|
|
// Slightly rearrange calculations and compute it bottom-up,
|
|
|
|
// right-to-left. See also:
|
|
|
|
// <https://github.com/Bitmessage/PyBitmessage/issues/758>.
|
2015-01-30 18:13:09 +01:00
|
|
|
var length = bignum(opts.payloadLength).add(opts.payloadLengthExtraBytes);
|
2015-01-07 22:28:13 +01:00
|
|
|
var denominator = bignum(opts.ttl)
|
2015-01-30 16:15:20 +01:00
|
|
|
.add(65536)
|
2015-01-07 22:28:13 +01:00
|
|
|
.mul(length)
|
|
|
|
.mul(opts.nonceTrialsPerByte);
|
2015-01-30 16:15:20 +01:00
|
|
|
var target = B80.div(denominator).toNumber();
|
2015-01-07 22:28:13 +01:00
|
|
|
assert(target <= 9007199254740991, "Unsafe target");
|
|
|
|
return target;
|
|
|
|
};
|
2015-01-09 22:36:42 +01:00
|
|
|
|
2015-01-10 14:03:14 +01:00
|
|
|
exports.pow = function(opts) {
|
2015-01-09 22:36:42 +01:00
|
|
|
var poolSize = opts.poolSize || os.cpus().length;
|
|
|
|
// TODO(Kagami): Allow to cancel a POW (see `platform.browser.js`).
|
|
|
|
return new promise(function(resolve, reject) {
|
|
|
|
worker.powAsync(
|
|
|
|
poolSize,
|
|
|
|
opts.target,
|
|
|
|
opts.initialHash,
|
|
|
|
function(err, nonce) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(nonce);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
2015-01-16 16:35:40 +01:00
|
|
|
|
|
|
|
exports.promise = promise;
|