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";
|
|
|
|
|
2015-01-06 12:06:15 +01:00
|
|
|
var objectAssign = Object.assign || require("object-assign");
|
2014-12-18 17:47:18 +01:00
|
|
|
var bufferEqual = require("buffer-equal");
|
|
|
|
var bs58 = require("bs58");
|
2015-01-05 14:31:28 +01:00
|
|
|
var assert = require("./util").assert;
|
2015-01-03 11:14:39 +01:00
|
|
|
var var_int = require("./structs").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
|
2015-01-03 16:58:41 +01:00
|
|
|
* @static
|
2014-12-29 23:16:51 +01:00
|
|
|
*/
|
|
|
|
function Address(opts) {
|
|
|
|
if (!(this instanceof Address)) {
|
|
|
|
return new Address(opts);
|
|
|
|
}
|
|
|
|
opts = opts || {};
|
2015-01-06 12:06:15 +01:00
|
|
|
objectAssign(this, opts);
|
2014-12-29 23:16:51 +01:00
|
|
|
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) {
|
2015-01-03 15:52:27 +01:00
|
|
|
assertripelen(getripelen(this.ripe), this.version, this.ripe);
|
|
|
|
if (this.ripe.length < 20) {
|
|
|
|
var fullripe = new Buffer(20);
|
|
|
|
fullripe.fill(0);
|
|
|
|
this.ripe.copy(fullripe, 20 - this.ripe.length);
|
|
|
|
this.ripe = fullripe;
|
|
|
|
}
|
2014-12-29 23:16:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2015-01-03 15:52:27 +01:00
|
|
|
* @return {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);
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
var bytes = bs58.decode(str);
|
2014-12-18 17:47:18 +01:00
|
|
|
var data = new Buffer(bytes.slice(0, -4));
|
|
|
|
var checksum = new Buffer(bytes.slice(-4));
|
2015-01-08 03:00:19 +01:00
|
|
|
assert(bufferEqual(checksum, getaddrchecksum(data)), "Bad checkum");
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
var decoded = var_int.decode(data);
|
|
|
|
var version = decoded.value;
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
data = decoded.rest;
|
|
|
|
decoded = var_int.decode(data);
|
|
|
|
var stream = decoded.value;
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
var ripe = decoded.rest;
|
|
|
|
if (version === 4) {
|
|
|
|
assert(ripe[0] !== 0, "Ripe encode error");
|
|
|
|
}
|
2014-12-18 17:47:18 +01:00
|
|
|
|
2015-01-03 15:52:27 +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.
|
2015-01-08 03:00:19 +01:00
|
|
|
function getaddrchecksum(data) {
|
2015-01-03 15:52:27 +01:00
|
|
|
return bmcrypto.sha512(bmcrypto.sha512(data)).slice(0, 4);
|
2014-12-27 16:17:41 +01:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2015-01-08 14:47:29 +01:00
|
|
|
var dataToHash = Buffer.concat([signPublicKey, encPublicKey]);
|
|
|
|
return bmcrypto.ripemd160(bmcrypto.sha512(dataToHash));
|
2014-12-27 16:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-03 15:52:27 +01:00
|
|
|
* Calculate the ripe hash of the address.
|
2014-12-27 16:17:41 +01:00
|
|
|
* @param {?Object} opts - Options
|
2015-01-03 15:52:27 +01:00
|
|
|
* @return {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) {
|
2015-01-03 15:52:27 +01:00
|
|
|
var ripe;
|
|
|
|
opts = opts || {};
|
|
|
|
if (this.ripe) {
|
|
|
|
ripe = this.ripe;
|
2014-12-29 23:16:51 +01:00
|
|
|
} else {
|
2015-01-03 15:52:27 +01:00
|
|
|
var signKey = this.signPrivateKey || this.signPublicKey;
|
2014-12-29 23:16:51 +01:00
|
|
|
assert(signKey, "No signing key");
|
2015-01-03 15:52:27 +01:00
|
|
|
var encKey = this.encPrivateKey || this.encPublicKey;
|
2014-12-29 23:16:51 +01:00
|
|
|
assert(encKey, "No encryption key");
|
2015-01-03 15:52:27 +01:00
|
|
|
ripe = keys2ripe(signKey, encKey);
|
|
|
|
}
|
|
|
|
var ripelen = getripelen(ripe);
|
|
|
|
assertripelen(ripelen, this.version, ripe);
|
|
|
|
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
|
|
|
|
2015-01-03 15:52:27 +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;
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
// Do neccessary checkings of the truncated ripe hash length depending
|
2014-12-27 16:17:41 +01:00
|
|
|
// on the address version.
|
2015-01-03 15:52:27 +01:00
|
|
|
function assertripelen(ripelen, version, ripe) {
|
|
|
|
if (ripe) {
|
|
|
|
assert(ripe.length <= 20, "Bad ripe");
|
|
|
|
}
|
2014-12-27 16:17:41 +01:00
|
|
|
switch (version) {
|
|
|
|
case 1:
|
|
|
|
assert(ripelen === 20, "Bad ripe length");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2015-01-03 15:52:27 +01:00
|
|
|
assert(ripelen >= 18, "Ripe is too short");
|
|
|
|
assert(ripelen <= 20, "Ripe is too long");
|
2014-12-27 16:17:41 +01:00
|
|
|
break;
|
|
|
|
case 4:
|
2015-01-03 15:52:27 +01:00
|
|
|
assert(ripelen >= 4, "Ripe is too short");
|
|
|
|
assert(ripelen <= 20, "Ripe is too long");
|
2014-12-27 16:17:41 +01:00
|
|
|
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.
|
2015-01-03 15:52:27 +01:00
|
|
|
* @return {string} Address string.
|
2014-12-27 16:17:41 +01:00
|
|
|
*/
|
2014-12-29 23:16:51 +01:00
|
|
|
Address.prototype.encode = function() {
|
2015-01-03 15:52:27 +01:00
|
|
|
var ripe = this.getRipe({short: true});
|
|
|
|
var data = Buffer.concat([
|
|
|
|
var_int.encode(this.version),
|
|
|
|
var_int.encode(this.stream),
|
|
|
|
ripe,
|
|
|
|
]);
|
2015-01-08 03:00:19 +01:00
|
|
|
var addr = Buffer.concat([data, getaddrchecksum(data)]);
|
2015-01-03 15:52:27 +01:00
|
|
|
return "BM-" + bs58.encode(addr);
|
2014-12-27 16:17:41 +01:00
|
|
|
};
|
|
|
|
|
2015-01-03 15:52:27 +01:00
|
|
|
function popkey(obj, key) {
|
|
|
|
var value = obj[key];
|
|
|
|
delete obj[key];
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-12-27 16:17:41 +01:00
|
|
|
/**
|
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
|
2015-01-03 15:52:27 +01:00
|
|
|
* @return {Address} Generated address object.
|
2014-12-27 16:17:41 +01:00
|
|
|
*/
|
2014-12-29 23:16:51 +01:00
|
|
|
Address.fromRandom = function(opts) {
|
2015-01-06 12:06:15 +01:00
|
|
|
opts = objectAssign({}, opts);
|
2015-01-03 15:52:27 +01:00
|
|
|
var version = opts.version = opts.version || 4;
|
|
|
|
var ripelen = popkey(opts, "ripelen") || 19;
|
|
|
|
assertripelen(ripelen, version);
|
|
|
|
// Should the generated ripe length be strictly equal to the specified
|
|
|
|
// (less or equal by default).
|
|
|
|
var strictripelen = !!popkey(opts, "strictripelen");
|
|
|
|
|
|
|
|
// TODO(Kagami): Speed it up using web workers in Browser.
|
|
|
|
// TODO(Kagami): Bind to C++ version of this code in Node.
|
|
|
|
var encPrivateKey, encPublicKey, ripe;
|
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);
|
2015-01-03 15:52:27 +01:00
|
|
|
var keysbuf = Buffer(130);
|
|
|
|
signPublicKey.copy(keysbuf);
|
|
|
|
while (true) {
|
|
|
|
encPrivateKey = bmcrypto.getPrivate();
|
|
|
|
encPublicKey = bmcrypto.getPublic(encPrivateKey);
|
|
|
|
encPublicKey.copy(keysbuf, 65);
|
|
|
|
ripe = bmcrypto.ripemd160(bmcrypto.sha512(keysbuf));
|
|
|
|
var len = getripelen(ripe);
|
|
|
|
if (
|
|
|
|
(strictripelen && len === ripelen) ||
|
|
|
|
(!strictripelen && len <= ripelen && checkripelen(ripelen, version))
|
|
|
|
) {
|
|
|
|
// TODO(Kagami): Do we need to put all these properties or compute
|
|
|
|
// them manually via ECMA5 getters/setters instead?
|
|
|
|
opts.signPrivateKey = signPrivateKey;
|
|
|
|
opts.signPublicKey = signPublicKey;
|
|
|
|
opts.encPrivateKey = encPrivateKey;
|
|
|
|
opts.encPublicKey = encPublicKey;
|
|
|
|
opts.ripe = ripe;
|
|
|
|
return new Address(opts);
|
2014-12-27 16:17:41 +01:00
|
|
|
}
|
2015-01-03 15:52:27 +01:00
|
|
|
}
|
2014-12-27 16:17:41 +01:00
|
|
|
};
|
2014-12-29 23:16:51 +01:00
|
|
|
|
|
|
|
module.exports = Address;
|