Improve comments; fixes

This commit is contained in:
Kagami Hiiragi 2015-01-29 01:13:41 +03:00
parent 2e4c91f49b
commit 8e892581f5
2 changed files with 26 additions and 6 deletions

View File

@ -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. * Decode `pubkey` object message payload.
* @param {Buffer} buf - Message payload * @param {Buffer} buf - Message payload
* @param {?Object} opts - Decoding options * @param {?PubkeyDecodeOpts} opts - Decoding options
* @return {Promise.<Object>} A promise that contains decoded `pubkey` * @return {Promise.<Object>} A promise that contains decoded `pubkey`
* object structure when fulfilled. * object structure when fulfilled.
*/ */
decodePayloadAsync: function(buf, opts) { decodePayloadAsync: function(buf, opts) {
return new promise(function(resolve) { return new promise(function(resolve) {
opts = opts || {}; opts = opts || {};
var neededPubkeys = opts.neededPubkeys || {};
var decoded = object.decodePayload(buf); var decoded = object.decodePayload(buf);
assert(decoded.type === object.PUBKEY, "Wrong object type"); assert(decoded.type === object.PUBKEY, "Wrong object type");
var version = decoded.version; var version = decoded.version;
@ -259,6 +266,7 @@ var pubkey = exports.pubkey = {
// `neededPubkeys` is either single address or addresses array or // `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) // Object key-by-tag. Time to match the tag is O(1), O(n), O(1)
// respectfully. // respectfully.
var neededPubkeys = opts.neededPubkeys || {};
if (Address.isAddress(neededPubkeys)) { if (Address.isAddress(neededPubkeys)) {
addr = neededPubkeys; addr = neededPubkeys;
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. * Decode `msg` object message payload.
* @param {Buffer} buf - Message payload * @param {Buffer} buf - Message payload
* @param {Object} opts - Decoding options * @param {MsgDecodeOpts} opts - Decoding options
* @return {Promise.<Object>} A promise that contains decoded `msg` * @return {Promise.<Object>} A promise that contains decoded `msg`
* object structure when fulfilled. * object structure when fulfilled.
*/ */
decodePayloadAsync: function(buf, opts) { decodePayloadAsync: function(buf, opts) {
return new promise(function(resolve) { return new promise(function(resolve) {
var identities = opts.identities;
if (Address.isAddress(identities)) {
identities = [identities];
}
var decoded = object.decodePayload(buf); var decoded = object.decodePayload(buf);
assert(decoded.type === object.MSG, "Wrong object type"); assert(decoded.type === object.MSG, "Wrong object type");
assert(decoded.version === 1, "Wrong msg version"); assert(decoded.version === 1, "Wrong msg version");
var objectPayload = util.popkey(decoded, "objectPayload"); var objectPayload = util.popkey(decoded, "objectPayload");
var msgp = tryDecrypt(opts.identities, objectPayload) var msgp = tryDecrypt(identities, objectPayload)
.then(function(decInfo) { .then(function(decInfo) {
var decrypted = decInfo.decrypted; var decrypted = decInfo.decrypted;
@ -538,7 +557,7 @@ var msg = exports.msg = {
decoded.ripe = rest.slice(0, 20); decoded.ripe = rest.slice(0, 20);
assert( assert(
bufferEqual(decoded.ripe, decInfo.addr.ripe), 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; decoded.length += 20;
var decodedEncoding = var_int.decode(rest.slice(20)); var decodedEncoding = var_int.decode(rest.slice(20));
var encoding = decoded.encoding = decodedEncoding.value; var encoding = decoded.encoding = decodedEncoding.value;

View File

@ -129,6 +129,7 @@ var object = exports.object = {
* @return {Object} Decoded `object` structure. * @return {Object} Decoded `object` structure.
*/ */
// FIXME(Kagami): Check a POW. // 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 // TODO(Kagami): Option to not fail on bad POW (may be useful for
// bitchan). // bitchan).
// TODO(Kagami): Option to not fail on expired objects (would be // TODO(Kagami): Option to not fail on expired objects (would be
@ -245,7 +246,7 @@ var var_int = exports.var_int = {
// <http://mdn.io/issafeinteger>, // <http://mdn.io/issafeinteger>,
// <https://stackoverflow.com/q/307179> for details. // <https://stackoverflow.com/q/307179> for details.
// TODO(Kagami): We may want to return raw Buffer for // 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 // argument because most of the code expect to get a number when
// calling `var_int.decode`. // calling `var_int.decode`.
assert(hi <= 2097151, "Unsafe integer"); assert(hi <= 2097151, "Unsafe integer");