objects.getType

This commit is contained in:
Kagami Hiiragi 2015-01-28 16:31:18 +03:00
parent 70f7054bbf
commit ceceaee721
4 changed files with 63 additions and 4 deletions

View File

@ -19,7 +19,7 @@ var message = structs.message;
var ServicesBitfield = structs.ServicesBitfield;
/**
* Try to get command for the given encoded message.
* Try to get command of the given encoded message.
* Note that this function doesn't do any validation because it is
* already provided by `message.decode` routine. Normally you call this
* for each incoming message and then call decode function of the

View File

@ -1,7 +1,7 @@
/**
* Working with objects.
* NOTE: All operations with objects in this module are asynchronous and
* return promises.
* NOTE: Most operations with objects in this module are asynchronous
* and return promises.
* @see {@link https://bitmessage.org/wiki/Protocol_specification#Object_types}
* @module bitmessage/objects
*/
@ -25,6 +25,41 @@ var PubkeyBitfield = structs.PubkeyBitfield;
var message = structs.message;
var object = structs.object;
/**
* Try to get type of the given encoded object message.
* Note that this function doesn't do any validation because it is
* already provided by `object.decode` routine. Normally you call this
* for each incoming object message and then call decode function of the
* appropriate object handler.
* @param {Buffer} buf - Buffer that starts with encoded object message
* @return {?integer} Object's type if any
*/
exports.getType = function(buf) {
// Message header: 4 + 12 + 4 + 4
// Object header: 8 + 8 + 4
if (buf.length < 44) {
return;
}
return buf.readUInt32BE(40, true);
};
/**
* Try to get type of the given object message payoad.
* Note that this function doesn't do any validation because it is
* already provided by `object.decodePayload` routine. Normally you call
* this for each incoming object message and then call decode function
* of the appropriate object handler.
* @param {Buffer} buf - Buffer that starts with object message payload
* @return {?integer} Object's type if any
*/
exports.getPayloadType = function(buf) {
// Object header: 8 + 8 + 4
if (buf.length < 20) {
return;
}
return buf.readUInt32BE(16, true);
};
/**
* `getpubkey` object. When a node has the hash of a public key (from an
* address) but not the public key itself, it must send out a request

View File

@ -50,7 +50,7 @@
"bn.js": "^1.0.0",
"bs58": "^2.0.0",
"buffer-equal": "~0.0.1",
"eccrypto": "^0.9.4",
"eccrypto": "^0.9.5",
"es6-promise": "^2.0.1",
"hash.js": "^1.0.2",
"nan": "^1.4.1",

24
test.js
View File

@ -495,6 +495,30 @@ describe("Message types", function() {
// TODO(Kagami): Add tests for encodePayloadAsync/decodePayloadAsync as well.
describe("Object types", function() {
it("should get type of the encoded object message", function() {
var encoded = object.encode({
nonce: Buffer(8),
ttl: 100,
type: object.BROADCAST,
version: 1,
objectPayload: Buffer("test"),
});
expect(objects.getType(encoded)).to.equal(object.BROADCAST);
expect(objects.getType(Buffer(4))).to.be.undefined;
});
it("should get type of the object message payload", function() {
var encoded = object.encodePayload({
nonce: Buffer(8),
ttl: 333,
type: object.MSG,
version: 1,
objectPayload: Buffer("test"),
});
expect(objects.getPayloadType(encoded)).to.equal(object.MSG);
expect(objects.getPayloadType(Buffer(7))).to.be.undefined;
});
describe("getpubkey", function() {
it("should encode and decode getpubkey v3", function() {
return getpubkey.encodeAsync({