Implement messages.addr

This commit is contained in:
Kagami Hiiragi 2015-01-15 21:11:33 +03:00
parent 808fc5eaf1
commit 75d1c1f2f4
3 changed files with 66 additions and 7 deletions

View File

@ -37,8 +37,8 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/
- [x] pubkey features
- [ ] Message types
- [x] version
- [ ] verack
- [ ] addr
- [x] verack
- [x] addr
- [ ] inv
- [ ] getdata
- [ ] error

View File

@ -21,11 +21,11 @@ exports.version = {
NONCE: new Buffer("20bde0a3355dad78", "hex"),
/**
* Decode version payload.
* Decode `version` payload.
* NOTE: `nonce` is copied.
* @param {Buffer} buf - Buffer that starts with encoded version
* @param {Buffer} buf - Buffer that starts with encoded `version`
* payload
* @return {Object} Decoded version structure.
* @return {Object} Decoded `version` structure.
*/
decode: function(payload) {
// 4 + 8 + 8 + 26 + 26 + 8 + (1+) + (1+)
@ -59,9 +59,9 @@ exports.version = {
},
/**
* Encode version payload.
* Encode `version` payload.
* @param {Object} opts - Version options
* @return {Buffer} Encoded version payload.
* @return {Buffer} Encoded `version` payload.
*/
encode: function(opts) {
// Deal with default options.
@ -97,3 +97,42 @@ exports.version = {
]);
},
};
/**
* Addresses message. Provide information on known nodes of the network.
* @see {@link https://bitmessage.org/wiki/Protocol_specification#addr}
* @namespace
*/
exports.addr = {
/**
* Decode `addr` payload.
* @param {Buffer} buf - Buffer that starts with encoded `addr` payload
* @return {Object} Decoded `addr` structure.
*/
decode: function(buf) {
var decoded = structs.var_int.decode(buf);
var listLength = decoded.value;
var length = decoded.length + listLength * 38;
assert(buf.length >= length, "Buffer is too small");
var rest = decoded.rest;
var addrs = new Array(listLength);
for (var i = 0; i < listLength; i++) {
addrs[i] = structs.net_addr.decode(rest.slice(i*38, (i+1)*38));
}
return {
addrs: addrs,
// Real data length.
length: length,
};
},
/**
* Encode `addr` payload.
* @param {Object[]} addrs - Network addresses
* @return {Buffer} Encoded `addr` payload.
*/
encode: function(addrs) {
var addrsBuf = Buffer.concat(addrs.map(structs.net_addr.encode));
return Buffer.concat([structs.var_int.encode(addrs.length), addrsBuf]);
},
};

20
test.js
View File

@ -18,6 +18,7 @@ var serviceFeatures = structs.serviceFeatures;
var pubkeyFeatures = structs.pubkeyFeatures;
var messages = bitmessage.messages;
var version = messages.version;
var addr = messages.addr;
var WIF = bitmessage.WIF;
var POW = bitmessage.POW;
var Address = bitmessage.Address;
@ -301,6 +302,25 @@ describe("Message types", function() {
expect(res.length).to.equal(101);
});
});
describe("addr", function() {
it("should encode and decode", function() {
var res = addr.decode(addr.encode([]));
expect(res.length).to.equal(1);
expect(res.addrs).to.deep.equal([]);
res = addr.decode(addr.encode([
{host: "1.2.3.4", port: 8444},
{host: "ff::1", port: 18444},
]));
expect(res.length).to.equal(77);
expect(res.addrs.length).to.equal(2);
expect(res.addrs[0].host).to.equal("1.2.3.4");
expect(res.addrs[0].port).to.equal(8444);
expect(res.addrs[1].host).to.equal("ff:0:0:0:0:0:0:1");
expect(res.addrs[1].port).to.equal(18444);
});
});
});
describe("WIF", function() {