Improve Address API

This commit is contained in:
Kagami Hiiragi 2015-01-19 15:08:56 +03:00
parent 232e06caf2
commit dc868634ac
4 changed files with 36 additions and 13 deletions

View File

@ -68,11 +68,18 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/
## Usage ## Usage
### Address
```js ```js
// Generate a new random Bitmessage identity.
var Address = require("bitmessage").Address; var Address = require("bitmessage").Address;
var addr = Address.fromRandom();
console.log("New random Bitmessage address:", addr.encode()); // Generate a new random Bitmessage identity.
var addr1 = Address.fromRandom();
console.log("New random Bitmessage address:", addr1.encode());
// Or create it from passphrase.
var addr2 = Address.fromPassphrase("test");
console.log("Deterministic Bitmessage address:", addr2.encode());
``` ```
## License ## License

View File

@ -29,11 +29,12 @@ function Address(opts) {
this.version = popkey(opts, "version") || 4; this.version = popkey(opts, "version") || 4;
assert(this.version <= 4, "Version too high"); assert(this.version <= 4, "Version too high");
assert(this.version >= 1, "Version too low"); assert(this.version >= 1, "Version too low");
// Set defaults.
opts.stream = opts.stream || 1;
opts.behavior = opts.behavior ||
PubkeyBitfield().set(PubkeyBitfield.DOES_ACK);
// Merge remained values. // Merge remained values.
objectAssign(this, opts); objectAssign(this, opts);
this.stream = this.stream || 1;
this.pubkeyFeatures = this.pubkeyFeatures ||
PubkeyBitfield().set(PubkeyBitfield.DOES_ACK);
} }
/** /**
@ -210,7 +211,11 @@ Address.fromRandom = function(opts) {
* @return {Address} Generated address object. * @return {Address} Generated address object.
*/ */
Address.fromPassphrase = function(opts) { Address.fromPassphrase = function(opts) {
opts = objectAssign({}, opts); if (typeof opts === "string") {
opts = {passphrase: opts};
} else {
opts = objectAssign({}, opts);
}
var version = opts.version = opts.version || 4; var version = opts.version = opts.version || 4;
var ripelen = popkey(opts, "ripelen") || 19; var ripelen = popkey(opts, "ripelen") || 19;
assertripelen(ripelen, version); assertripelen(ripelen, version);

View File

@ -501,7 +501,7 @@ exports.encrypted = {
var Bitfield = function(size) { var Bitfield = function(size) {
var bytesize = size / 8; var bytesize = size / 8;
// <https://github.com/fb55/bitfield> is the source of inspiration. // Inspired by <https://github.com/fb55/bitfield>.
function BitfieldInner(buf, opts) { function BitfieldInner(buf, opts) {
if (!(this instanceof BitfieldInner)) { if (!(this instanceof BitfieldInner)) {
return new BitfieldInner(buf); return new BitfieldInner(buf);
@ -574,15 +574,15 @@ var ServicesBitfield = exports.ServicesBitfield = objectAssign(Bitfield(64), {
// invert the numberes. See // invert the numberes. See
// <https://github.com/Bitmessage/PyBitmessage/issues/769> for details. // <https://github.com/Bitmessage/PyBitmessage/issues/769> for details.
exports.PubkeyBitfield = objectAssign(Bitfield(32), { exports.PubkeyBitfield = objectAssign(Bitfield(32), {
/**
* If true, the receiving node does send acknowledgements (rather than
* dropping them).
*/
DOES_ACK: 0,
/** /**
* Receiving node expects that the RIPE hash encoded in their address * Receiving node expects that the RIPE hash encoded in their address
* preceedes the encrypted message data of msg messages bound for * preceedes the encrypted message data of msg messages bound for
* them. * them.
*/ */
INCLUDE_DESTINATION: 1, INCLUDE_DESTINATION: 1,
/**
* If true, the receiving node does send acknowledgements (rather than
* dropping them).
*/
DOES_ACK: 0,
}); });

11
test.js
View File

@ -570,6 +570,17 @@ describe("High-level classes", function() {
expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F"); expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F");
}); });
it("should accept string in Address.fromPassphrase", function() {
this.timeout(60000);
var addr = Address.fromPassphrase("test");
expect(addr.version).to.equal(4);
expect(addr.stream).to.equal(1);
expect(bufferEqual(addr.signPrivateKey, WIF.decode("5JY1CFeeyN4eyfL35guWAuUqu5VLmd7LojtkNP6wmt5msZxxZ57"))).to.be.true;
expect(bufferEqual(addr.encPrivateKey, WIF.decode("5J1oDgZDicNhUgbfzBDQqi2m5jUPnDrfZinnTqEEEaLv63jVFTM"))).to.be.true;
expect(addr.getRipe().toString("hex")).to.equal("00ac14944b00decea5628eb40d0ff4b0f9ee9eca");
expect(addr.encode()).to.equal("BM-2cWFkyuXXFw6d393RGnin2RpSXj8wxtt6F");
});
it("should calculate tag", function() { it("should calculate tag", function() {
var addr = Address.decode("2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z"); var addr = Address.decode("2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z");
expect(addr.getTag().toString("hex")).to.equal("facf1e3e6c74916203b7f714ca100d4d60604f0917696d0f09330f82f52bed1a"); expect(addr.getTag().toString("hex")).to.equal("facf1e3e6c74916203b7f714ca100d4d60604f0917696d0f09330f82f52bed1a");