2014-12-18 17:47:18 +01:00
|
|
|
/**
|
|
|
|
* Working with Bitmessage addresses.
|
2014-12-30 18:00:28 +01:00
|
|
|
* @see {@link https://bitmessage.org/wiki/Address}
|
2014-12-18 17:47:18 +01:00
|
|
|
* @module bitmessage/address
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2014-12-27 16:17:41 +01:00
|
|
|
require("es6-promise").polyfill();
|
2014-12-29 23:16:51 +01:00
|
|
|
require("object.assign").shim();
|
2014-12-20 11:48:12 +01:00
|
|
|
var assert = require("assert");
|
2014-12-18 17:47:18 +01:00
|
|
|
var bufferEqual = require("buffer-equal");
|
|
|
|
var bs58 = require("bs58");
|
2015-01-01 20:26:06 +01:00
|
|
|
var var_int = require("./struct").var_int;
|
2014-12-18 17:47:18 +01:00
|
|
|
var bmcrypto = require("./crypto");
|
|
|
|
|
2014-12-29 23:16:51 +01:00
|
|
|
/**
|
|
|
|
* Create a new Bitmessage address object.
|
|
|
|
* @param {?Object} opts - Address options
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function Address(opts) {
|
|
|
|
if (!(this instanceof Address)) {
|
|
|
|
return new Address(opts);
|
|
|
|
}
|
|
|
|
opts = opts || {};
|
|
|
|
Object.assign(this, opts);
|
|
|
|
this.version = this.version || 4;
|
|
|
|
assert(this.version <= 4, "Version too high");
|
|
|
|
assert(this.version >= 1, "Version too low");
|
|
|
|
this.stream = this.stream || 1;
|
|
|
|
if (this.ripe) {
|
|
|
|
assertripelen(getripelen(this.ripe), this.version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 17:47:18 +01:00
|
|
|
/**
|
2014-12-27 16:17:41 +01:00
|
|
|
* Parse Bitmessage address into address object.
|
|
|
|
* @param {String} str - Address string (with or without `BM-` prefix)
|
2014-12-30 18:00:28 +01:00
|
|
|
* @return {Promise.<Address>} Decoded address object.
|
2014-12-18 17:47:18 +01:00
|
|
|
*/
|
2014-12-29 23:16:51 +01:00
|
|
|
Address.decode = function(str) {
|
2014-12-18 17:47:18 +01:00
|
|
|
str = str.trim();
|
|
|
|
if (str.slice(0, 3) === "BM-") {
|
|
|
|
str = str.slice(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytes;
|
|
|
|
try {
|
|
|
|
bytes = bs58.decode(str);
|
|
|
|
} catch(e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checksum validating.
|
|
|
|
var data = new Buffer(bytes.slice(0, -4));
|
|
|
|
var checksum = new Buffer(bytes.slice(-4));
|
2014-12-27 16:17:41 +01:00
|
|
|
return getchecksum(data).then(function(realchecksum) {
|
|
|
|
assert(bufferEqual(checksum, realchecksum), "Bad checkum");
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-01 20:26:06 +01:00
|
|
|
var decoded = var_int.decode(data);
|
2014-12-18 17:47:18 +01:00
|
|
|
var version = decoded.value;
|
|
|
|
|
|
|
|
data = decoded.rest;
|
2015-01-01 20:26:06 +01:00
|
|
|
decoded = var_int.decode(data);
|
2014-12-18 17:47:18 +01:00
|
|
|
var stream = decoded.value;
|
|
|
|
|
|
|
|
var ripe = decoded.rest;
|
|
|
|
var ripelen = ripe.length;
|
2014-12-27 16:17:41 +01:00
|
|
|
if (version === 4) {
|
2014-12-29 23:16:51 +01:00
|
|
|
assert(ripe[0] !== 0, "Ripe encode error");
|
2014-12-18 17:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent extra allocation. God, kill me please for premature
|
|
|
|
// optimizations.
|
|
|
|
if (ripelen < 20) {
|
|
|
|
var zeroes = new Buffer(Array(20 - ripelen));
|
|
|
|
ripe = Buffer.concat([zeroes, ripe]);
|
|
|
|
}
|
2014-12-29 23:16:51 +01:00
|
|
|
return new Address({version: version, stream: stream, ripe: ripe});
|
2014-12-18 17:47:18 +01:00
|
|
|
});
|
|
|
|
};
|
2014-12-27 16:17:41 +01:00
|
|
|
|
|
|
|
// Compute the Bitmessage checksum for the given data.
|
|
|
|
function getchecksum(data) {
|
|
|
|
return bmcrypto.sha512(data).then(bmcrypto.sha512).then(function(dhash) {
|
|
|
|
return dhash.slice(0, 4);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:04:23 +01:00
|
|
|
// Get RIPEMD160(SHA512(SIGN_PUBLIC_KEY || ENC_PUBLIC_KEY)).
|
2014-12-27 16:17:41 +01:00
|
|
|
// Arguments could be either private or public keys. Private keys are
|
|
|
|
// **always** 32 bytes in length.
|
|
|
|
function keys2ripe(signKey, encKey) {
|
|
|
|
var signPublicKey, encPublicKey;
|
|
|
|
if (signKey.length === 32) {
|
|
|
|
signPublicKey = bmcrypto.getPublic(signKey);
|
|
|
|
} else {
|
|
|
|
signPublicKey = signKey;
|
|
|
|
}
|
|
|
|
if (encKey.length === 32) {
|
|
|
|
encPublicKey = bmcrypto.getPublic(encKey);
|
|
|
|
} else {
|
|
|
|
encPublicKey = encKey;
|
|
|
|
}
|
|
|
|
var concat = Buffer.concat([signPublicKey, encPublicKey]);
|
|
|
|
return bmcrypto.sha512(concat).then(bmcrypto.ripemd160);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-29 23:16:51 +01:00
|
|
|
* Calculate the Ripe hash of the address.
|
2014-12-27 16:17:41 +01:00
|
|
|
* @param {?Object} opts - Options
|
2014-12-30 18:00:28 +01:00
|
|
|
* @return {Promise.<Buffer>} Resulting Ripe hash.
|
2014-12-27 16:17:41 +01:00
|
|
|
*/
|
2014-12-29 23:16:51 +01:00
|
|
|
Address.prototype.getRipe = function(opts) {
|
|
|
|
var self = this;
|
|
|
|
var ripepromise;
|
|
|
|
if (self.ripe) {
|
|
|
|
ripepromise = Promise.resolve(self.ripe);
|
|
|
|
} else {
|
|
|
|
opts = opts || {};
|
|
|
|
var signKey = self.signPrivateKey || self.signPublicKey;
|
|
|
|
assert(signKey, "No signing key");
|
|
|
|
var encKey = self.encPrivateKey || self.encPublicKey;
|
|
|
|
assert(encKey, "No encryption key");
|
|
|
|
ripepromise = keys2ripe(signKey, encKey);
|
|
|
|
}
|
|
|
|
return ripepromise.then(function(ripe) {
|
|
|
|
var ripelen = getripelen(ripe);
|
|
|
|
assertripelen(ripelen, self.version);
|
2014-12-27 16:17:41 +01:00
|
|
|
if (opts.short) {
|
|
|
|
return ripe.slice(20 - ripelen);
|
|
|
|
} else {
|
|
|
|
return ripe;
|
|
|
|
}
|
|
|
|
});
|
2014-12-29 23:16:51 +01:00
|
|
|
};
|
2014-12-27 16:17:41 +01:00
|
|
|
|
2014-12-27 22:04:23 +01:00
|
|
|
// Get truncated Ripe hash length.
|
2014-12-27 16:17:41 +01:00
|
|
|
function getripelen(ripe) {
|
|
|
|
var zeroes = 0;
|
|
|
|
for (var i = 0; i < 20, ripe[i] === 0; i++) {
|
|
|
|
zeroes++;
|
|
|
|
}
|
|
|
|
return 20 - zeroes;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:04:23 +01:00
|
|
|
// Do neccessary checkings of the truncated Ripe hash length depending
|
2014-12-27 16:17:41 +01:00
|
|
|
// on the address version.
|
|
|
|
function assertripelen(ripelen, version) {
|
|
|
|
switch (version) {
|
|
|
|
case 1:
|
|
|
|
assert(ripelen === 20, "Bad ripe length");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
assert(ripelen >= 18, "Ripe too short");
|
|
|
|
assert(ripelen <= 20, "Ripe too long");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
assert(ripelen >= 4, "Ripe too short");
|
|
|
|
assert(ripelen <= 20, "Ripe too long");
|
|
|
|
break;
|
|
|
|
default:
|
2014-12-29 23:16:51 +01:00
|
|
|
throw new Error("Bad version");
|
2014-12-27 16:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:04:23 +01:00
|
|
|
// The same as `assertripelen` but return boolean instead of thrown an
|
2014-12-27 16:17:41 +01:00
|
|
|
// Error.
|
|
|
|
function checkripelen(ripelen, version) {
|
|
|
|
try {
|
|
|
|
assertripelen(ripelen, version);
|
|
|
|
return true;
|
|
|
|
} catch(e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode Bitmessage address object into address string.
|
2014-12-30 18:00:28 +01:00
|
|
|
* @return {Promise.<string>} Address string.
|
2014-12-27 16:17:41 +01:00
|
|
|
*/
|
2014-12-29 23:16:51 +01:00
|
|
|
Address.prototype.encode = function() {
|
|
|
|
var self = this;
|
|
|
|
return self.getRipe({short: true}).then(function(ripe) {
|
2014-12-27 16:17:41 +01:00
|
|
|
var data = Buffer.concat([
|
2015-01-01 20:26:06 +01:00
|
|
|
var_int.encode(self.version),
|
|
|
|
var_int.encode(self.stream),
|
2014-12-27 16:17:41 +01:00
|
|
|
ripe,
|
|
|
|
]);
|
|
|
|
return getchecksum(data).then(function(checksum) {
|
|
|
|
var addr = Buffer.concat([data, checksum]);
|
|
|
|
return "BM-" + bs58.encode(addr);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-12-29 23:16:51 +01:00
|
|
|
* Create new Bitmessage address from random encryption and signing
|
2014-12-27 16:17:41 +01:00
|
|
|
* private keys.
|
|
|
|
* @param {?Object} opts - Address options
|
2014-12-30 18:00:28 +01:00
|
|
|
* @return {Promise.<Address>} Generated address object.
|
2014-12-27 16:17:41 +01:00
|
|
|
*/
|
2014-12-29 23:16:51 +01:00
|
|
|
Address.fromRandom = function(opts) {
|
2014-12-27 22:04:23 +01:00
|
|
|
opts = opts || {};
|
2014-12-29 23:16:51 +01:00
|
|
|
var version = opts.version || 4;
|
|
|
|
var ripelen = opts.ripelen || 19;
|
2014-12-27 16:17:41 +01:00
|
|
|
try {
|
|
|
|
assertripelen(ripelen, version);
|
|
|
|
} catch(e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
// Should the generated Ripe length be strictly equal to the specified
|
|
|
|
// (less-or-equal by default);
|
|
|
|
var strictripelen = !!opts.strictripelen;
|
2014-12-27 22:04:23 +01:00
|
|
|
var nextTick = typeof setImmediate === "undefined" ?
|
|
|
|
process.nextTick :
|
|
|
|
setImmediate;
|
2014-12-29 23:16:51 +01:00
|
|
|
|
|
|
|
var signPrivateKey = bmcrypto.getPrivate();
|
2014-12-27 16:17:41 +01:00
|
|
|
var signPublicKey = bmcrypto.getPublic(signPrivateKey);
|
|
|
|
|
|
|
|
// FIXME(Kagami): This function is rather slow in browsers so
|
|
|
|
// generation of ripelen=18 currently is disabled (see `test.js`). It
|
|
|
|
// should be heavilty profiled to determine the bottleneck.
|
|
|
|
// TODO(Kagami): We may want to run this in the web worker to speedup
|
|
|
|
// the search. Currently WebCryptoAPI is not available in Firefox in
|
|
|
|
// web workers (see
|
|
|
|
// <https://bugzilla.mozilla.org/show_bug.cgi?id=842818>) but is
|
|
|
|
// available in Chrome (at least in 39.0+).
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
function tryKey() {
|
2014-12-29 23:16:51 +01:00
|
|
|
var encPrivateKey = bmcrypto.getPrivate();
|
|
|
|
var encPublicKey = bmcrypto.getPublic(encPrivateKey);
|
|
|
|
return keys2ripe(signPublicKey, encPublicKey).then(function(ripe) {
|
2014-12-27 16:17:41 +01:00
|
|
|
var len = getripelen(ripe);
|
|
|
|
if (
|
|
|
|
(strictripelen && len === ripelen) ||
|
|
|
|
(!strictripelen && len <= ripelen && checkripelen(ripelen, version))
|
|
|
|
) {
|
2014-12-29 23:16:51 +01:00
|
|
|
// XXX(Kagami): Do we need to put all these properties or
|
|
|
|
// compute them manually via ECMA5 getters/setters instead?
|
|
|
|
resolve(new Address(Object.assign({
|
2014-12-27 16:17:41 +01:00
|
|
|
signPrivateKey: signPrivateKey,
|
2014-12-29 23:16:51 +01:00
|
|
|
signPublicKey: signPublicKey,
|
2014-12-27 16:17:41 +01:00
|
|
|
encPrivateKey: encPrivateKey,
|
2014-12-29 23:16:51 +01:00
|
|
|
encPublicKey: encPublicKey,
|
|
|
|
ripe: ripe,
|
|
|
|
}, opts)));
|
2014-12-27 16:17:41 +01:00
|
|
|
} else {
|
|
|
|
nextTick(tryKey);
|
|
|
|
}
|
|
|
|
}).catch(reject);
|
|
|
|
}
|
|
|
|
tryKey();
|
|
|
|
});
|
|
|
|
};
|
2014-12-29 23:16:51 +01:00
|
|
|
|
|
|
|
module.exports = Address;
|