diff --git a/lib/inventory.js b/lib/inventory.js index e5e7e96..5d4b134 100644 --- a/lib/inventory.js +++ b/lib/inventory.js @@ -7,6 +7,13 @@ var BufferReader = bitcore.encoding.BufferReader; var BufferWriter = bitcore.encoding.BufferWriter; var _ = bitcore.deps._; +/** + * A constructor for inventory related Bitcoin messages such as + * "getdata", "inv" and "notfound". + * @param {Object} - obj + * @param {Number} - obj.type - Inventory.TYPE + * @param {Buffer} - obj.hash - The hash for the inventory + */ function Inventory(obj) { this.type = obj.type; if (!BufferUtil.isBuffer(obj.hash)) { @@ -15,9 +22,14 @@ function Inventory(obj) { this.hash = obj.hash; } +/** + * A convenience constructor for Inventory. + * @param {Number} - type - Inventory.TYPE + * @param {Buffer|String} - hash - The hash for the inventory + * @returns {Inventory} - A new instance of Inventory + */ Inventory.forItem = function(type, hash) { $.checkArgument(hash); - //todo: is reversing expected behavior? if (_.isString(hash)) { hash = new Buffer(hash, 'hex'); hash = BufferUtil.reverse(hash); @@ -25,18 +37,36 @@ Inventory.forItem = function(type, hash) { return new Inventory({type: type, hash: hash}); }; +/** + * A convenience constructor for Inventory for block inventory types. + * @param {Buffer|String} - hash - The hash for the block inventory + * @returns {Inventory} - A new instance of Inventory + */ Inventory.forBlock = function(hash) { return Inventory.forItem(Inventory.TYPE.BLOCK, hash); }; +/** + * A convenience constructor for Inventory for filtered/merkle block inventory types. + * @param {Buffer|String} - hash - The hash for the filtered block inventory + * @returns {Inventory} - A new instance of Inventory + */ Inventory.forFilteredBlock = function(hash) { return Inventory.forItem(Inventory.TYPE.FILTERED_BLOCK, hash); }; +/** + * A convenience constructor for Inventory for transaction inventory types. + * @param {Buffer|String} - hash - The hash for the transaction inventory + * @returns {Inventory} - A new instance of Inventory + */ Inventory.forTransaction = function(hash) { return Inventory.forItem(Inventory.TYPE.TX, hash); }; +/** + * @returns {Buffer} - Serialized inventory + */ Inventory.prototype.toBuffer = function() { var bw = new BufferWriter(); bw.writeUInt32LE(this.type); @@ -44,12 +74,18 @@ Inventory.prototype.toBuffer = function() { return bw.concat(); }; +/** + * @param {BufferWriter} - An instance of BufferWriter + */ Inventory.prototype.toBufferWriter = function(bw) { bw.writeUInt32LE(this.type); bw.write(this.hash); return bw; }; +/** + * @param {Buffer} - Seralized buffer of the inventory + */ Inventory.fromBuffer = function(payload) { var parser = new BufferReader(payload); var obj = {}; @@ -58,6 +94,9 @@ Inventory.fromBuffer = function(payload) { return new Inventory(obj); }; +/** + * @param {BufferWriter} - An instance of BufferWriter + */ Inventory.fromBufferReader = function(br) { var obj = {}; obj.type = br.readUInt32LE();