From 8e892581f5121399a76aabaa6b6cf542899d8798 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Thu, 29 Jan 2015 01:13:41 +0300 Subject: [PATCH] Improve comments; fixes --- lib/objects.js | 29 ++++++++++++++++++++++++----- lib/structs.js | 3 ++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/objects.js b/lib/objects.js index ce549da..7d984ac 100644 --- a/lib/objects.js +++ b/lib/objects.js @@ -205,17 +205,24 @@ var pubkey = exports.pubkey = { }); }, + /** + * Options for decoding pubkey + * @typedef {Object} PubkeyDecodeOpts + * @property {?(Address[]|Address|Object)} neededPubkeys - Address + * objects which represent pubkeys that we are interested in; this is + * used only for pubkeys v4 + */ + /** * Decode `pubkey` object message payload. * @param {Buffer} buf - Message payload - * @param {?Object} opts - Decoding options + * @param {?PubkeyDecodeOpts} opts - Decoding options * @return {Promise.} A promise that contains decoded `pubkey` * object structure when fulfilled. */ decodePayloadAsync: function(buf, opts) { return new promise(function(resolve) { opts = opts || {}; - var neededPubkeys = opts.neededPubkeys || {}; var decoded = object.decodePayload(buf); assert(decoded.type === object.PUBKEY, "Wrong object type"); var version = decoded.version; @@ -259,6 +266,7 @@ var pubkey = exports.pubkey = { // `neededPubkeys` is either single address or addresses array or // Object key-by-tag. Time to match the tag is O(1), O(n), O(1) // respectfully. + var neededPubkeys = opts.neededPubkeys || {}; if (Address.isAddress(neededPubkeys)) { addr = neededPubkeys; neededPubkeys = {}; @@ -489,21 +497,32 @@ var msg = exports.msg = { }); }, + /** + * Options for decoding pubkey + * @typedef {Object} MsgDecodeOpts + * @property {(Address[]|Address)} identities - Our identities used to + * decrypt the message + */ + /** * Decode `msg` object message payload. * @param {Buffer} buf - Message payload - * @param {Object} opts - Decoding options + * @param {MsgDecodeOpts} opts - Decoding options * @return {Promise.} A promise that contains decoded `msg` * object structure when fulfilled. */ decodePayloadAsync: function(buf, opts) { return new promise(function(resolve) { + var identities = opts.identities; + if (Address.isAddress(identities)) { + identities = [identities]; + } var decoded = object.decodePayload(buf); assert(decoded.type === object.MSG, "Wrong object type"); assert(decoded.version === 1, "Wrong msg version"); var objectPayload = util.popkey(decoded, "objectPayload"); - var msgp = tryDecrypt(opts.identities, objectPayload) + var msgp = tryDecrypt(identities, objectPayload) .then(function(decInfo) { var decrypted = decInfo.decrypted; @@ -538,7 +557,7 @@ var msg = exports.msg = { decoded.ripe = rest.slice(0, 20); assert( bufferEqual(decoded.ripe, decInfo.addr.ripe), - "Message was decrypted but the destination ripe differs"); + "msg was decrypted but the destination ripe differs"); decoded.length += 20; var decodedEncoding = var_int.decode(rest.slice(20)); var encoding = decoded.encoding = decodedEncoding.value; diff --git a/lib/structs.js b/lib/structs.js index f6b0534..db6ee1d 100644 --- a/lib/structs.js +++ b/lib/structs.js @@ -129,6 +129,7 @@ var object = exports.object = { * @return {Object} Decoded `object` structure. */ // FIXME(Kagami): Check a POW. + // TODO(Kagami): Allow lower POW for friends. // TODO(Kagami): Option to not fail on bad POW (may be useful for // bitchan). // TODO(Kagami): Option to not fail on expired objects (would be @@ -245,7 +246,7 @@ var var_int = exports.var_int = { // , // for details. // TODO(Kagami): We may want to return raw Buffer for - // 2^53 <= value <= 2^64 - 1 range. Possibly using the optional + // 2^53 <= value <= 2^64 - 1 range. Probably using the optional // argument because most of the code expect to get a number when // calling `var_int.decode`. assert(hi <= 2097151, "Unsafe integer");