diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..346b49f22 Binary files /dev/null and b/.DS_Store differ diff --git a/.jsdoc.conf b/.jsdoc.conf new file mode 100644 index 000000000..b0c6449b5 --- /dev/null +++ b/.jsdoc.conf @@ -0,0 +1,36 @@ +{ +"tags": { + "allowUnknownTags": true +}, +"source": { + "include": ["docs/README.md"], + "exclude": [], + "includePattern": "lib/.+\\.js(doc)?$", + "excludePattern": "(^|\\/|\\\\)_" +}, +"plugins": ["plugins/markdown"], +"templates": { + "cleverLinks": false, + "monospaceLinks": false +}, +"opts": { + "template": "node_modules/ink-docstrap/template", + "encoding": "utf8", + "destination": "./apiref/", + "recurse": true, + "query": "value", + "private": true, + "lenient": true +}, +"templates": { + "systemName": "bitcore", + "copyright": "© 2013-2014, BitPay Inc.", + "navType": "vertical", + "theme": "journal", + "linenums": true, + "collapseSymbols": false, + "inverseNav": false, + "outputSourceFiles": true +} + +} diff --git a/apiref.zip b/apiref.zip new file mode 100644 index 000000000..8d9ad5c5a Binary files /dev/null and b/apiref.zip differ diff --git a/gulpfile.js b/gulpfile.js index f5a5913c0..33907f225 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -120,26 +120,12 @@ gulp.task('lint', function() { .pipe(jshint.reporter('default')); }); -gulp.task('plato', shell.task[ - 'plato -d report -r -l .jshintrc -t bitcore lib' -]); +gulp.task('plato', shell.task['plato -d report -r -l .jshintrc -t bitcore lib']); + +gulp.task('jsdoc', shell.task['jsdoc -c .jsdoc.conf lib']); gulp.task('coverage', shell.task(['istanbul cover _mocha -- --recursive'])); -gulp.task('jsdoc', function() { - return gulp.src(files.concat([jsdocReadme])) - .pipe(jsdoc.parser({ - name: 'bitcore', - version: '0.8.0', - description: 'API Reference for the bitcore bitcoin javascript library', - plugins: ['plugins/markdown'] - })) - .pipe(jsdoc.generator('./apiref', { - path: 'ink-docstrap', - theme: 'journal' - })); -}); - /** * Watch tasks */ @@ -189,7 +175,7 @@ gulp.task('watch:browser', function() { */ gulp.task('default', function(callback) { return runSequence(['lint', 'jsdoc'], - ['browser:compressed', 'test'], - ['coverage', 'minify'], + ['browser:uncompressed', 'test'], + ['coverage', 'browser:compressed'], callback); }); diff --git a/index.js b/index.js index 4da2a9e40..bd055ed6b 100644 --- a/index.js +++ b/index.js @@ -20,16 +20,12 @@ bitcore.encoding.Varint = require('./lib/encoding/varint'); // utilities bitcore.util = {}; -bitcore.util.bitcoin = require('./lib/util/bitcoin'); bitcore.util.buffer = require('./lib/util/buffer'); bitcore.util.js = require('./lib/util/js'); bitcore.util.preconditions = require('./lib/util/preconditions'); // transport -bitcore.transport = {}; -bitcore.transport.Peer = require('./lib/transport/peer'); -bitcore.transport.Messages = require('./lib/transport/messages'); -bitcore.transport.Pool = require('./lib/transport/pool'); +bitcore.transport = require('./lib/transport'); // errors thrown by the library bitcore.errors = require('./lib/errors'); diff --git a/lib/block.js b/lib/block.js index 86311da06..f950fa724 100644 --- a/lib/block.js +++ b/lib/block.js @@ -12,6 +12,7 @@ var Transaction = require('./transaction'); var Varint = require('./encoding/varint'); /** + * @class Block * Instantiate a Block from a Buffer, JSON object, or Object with * the properties of the Block * @@ -42,10 +43,26 @@ Block._from = function _from(arg) { info = Block._fromJSON(arg); } else if (_.isObject(arg)) { info = { + /** + * @name Block#magicnum + * @type number + */ magicnum: arg.magicnum, + /** + * @name Block#blocksize + * @type number + */ blocksize: arg.blocksize, + /** + * @name Block#blockheader + * @type {BlockHeader} + */ blockheader: arg.blockheader, txsvi: arg.txsvi, + /** + * @name Block#txs + * @type {Transaction[]} + */ txs: arg.txs }; } else { @@ -59,18 +76,35 @@ Block._from = function _from(arg) { * easier access */ Block.prototype._setupProperties = function() { + /** + * @name Block#version + * @type {number} + */ Object.defineProperty(this, 'version', { configurable: false, value: this.blockheader.version }); + /** + * @name Block#timestamp + * @type {number} + */ Object.defineProperty(this, 'timestamp', { configurable: false, value: this.blockheader.timestamp }); + /** + * @name Block#nonce + * @type {number} + */ Object.defineProperty(this, 'nonce', { configurable: false, value: this.blockheader.nonce }); + /** + * Amount of bytes of the serialized block + * @name Block#size + * @type {number} + */ Object.defineProperty(this, 'size', { configurable: false, value: this.blockheader.size diff --git a/lib/errors/build.js b/lib/errors/build.js index 332f68aae..85a41f26c 100644 --- a/lib/errors/build.js +++ b/lib/errors/build.js @@ -35,7 +35,7 @@ var traverseRoot = function(errorsDefinition) { var fullName = 'bitcore.Error'; var path = 'Error'; var generated = '\'use strict\';\n\nvar inherits = require(\'inherits\');\n\n'; - generated += '/** AUTOGENERATED FILE. DON\'T EDIT, MODIFY "lib/errors/spec.js" INSTEAD */\n\n'; + generated += '/* AUTOGENERATED FILE. DON\'T EDIT, MODIFY "lib/errors/spec.js" INSTEAD */\n\n'; generated += 'var bitcore = {};\n\n'; generated += defineElement(fullName, path, '\'Internal error\''); generated += childDefinitions(fullName, errorsDefinition); diff --git a/lib/transaction/input/multisigscripthash.js b/lib/transaction/input/multisigscripthash.js index f876b3da5..ac26b1a45 100644 --- a/lib/transaction/input/multisigscripthash.js +++ b/lib/transaction/input/multisigscripthash.js @@ -12,6 +12,9 @@ var Signature = require('../../crypto/signature'); var Sighash = require('../sighash'); var BufferUtil = require('../../util/buffer'); +/** + * @constructor + */ function MultiSigScriptHashInput(input, pubkeys, threshold) { Input.apply(this, arguments); var self = this; diff --git a/lib/transaction/input/publickeyhash.js b/lib/transaction/input/publickeyhash.js index e17739105..bfebd83a2 100644 --- a/lib/transaction/input/publickeyhash.js +++ b/lib/transaction/input/publickeyhash.js @@ -14,6 +14,7 @@ var Signature = require('../../crypto/signature'); /** * Represents a special kind of input of PayToPublicKeyHash kind. + * @constructor */ function PublicKeyHashInput() { Input.apply(this, arguments); diff --git a/lib/transaction/sighash.js b/lib/transaction/sighash.js index 361a08bdd..0d2782f22 100644 --- a/lib/transaction/sighash.js +++ b/lib/transaction/sighash.js @@ -19,6 +19,7 @@ var BITS_64_ON = 'ffffffffffffffff'; * Returns a buffer of length 32 bytes with the hash that needs to be signed * for OP_CHECKSIG. * + * @name Signing.sighash * @param {Transaction} transaction the transaction to sign * @param {number} sighashType the type of the hash * @param {number} inputNumber the input index for the signature @@ -90,21 +91,46 @@ var sighash = function sighash(transaction, sighashType, inputNumber, subscript) return ret; }; -var sign = function sign(transaction, keypair, nhashtype, nin, subscript) { - var hashbuf = sighash(transaction, nhashtype, nin, subscript); - var sig = ECDSA.sign(hashbuf, keypair, 'little').set({ - nhashtype: nhashtype +/** + * Create a signature + * + * @name Signing.sign + * @param {Transaction} transaction + * @param {PrivateKey} privateKey + * @param {number} sighash + * @param {number} inputIndex + * @param {Script} subscript + * @return {Signature} + */ +function sign(transaction, privateKey, sighashType, inputIndex, subscript) { + var hashbuf = sighash(transaction, sighashType, inputIndex, subscript); + var sig = ECDSA.sign(hashbuf, privateKey, 'little').set({ + nhashtype: sighashType }); return sig; -}; +} -var verify = function verify(transaction, sig, pubkey, nin, subscript) { +/** + * Verify a signature + * + * @name Signing.verify + * @param {Transaction} transaction + * @param {Signature} signature + * @param {PublicKey} publicKey + * @param {number} inputIndex + * @param {Script} subscript + * @return {boolean} + */ +function verify(transaction, signature, publicKey, inputIndex, subscript) { $.checkArgument(transaction); - $.checkArgument(sig && sig.nhashtype); - var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript); - return ECDSA.verify(hashbuf, sig, pubkey, 'little'); -}; + $.checkArgument(signature && signature.nhashtype); + var hashbuf = sighash(transaction, signature.nhashtype, inputIndex, subscript); + return ECDSA.verify(hashbuf, signature, publicKey, 'little'); +} +/** + * @namespace Signing + */ module.exports = { sighash: sighash, sign: sign, diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 26fa466a5..ca8bc8a25 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -31,6 +31,7 @@ var DEFAULT_SEQNUMBER = 0xFFFFFFFF; * ownership of tokens * * @param {*} serialized + * @constructor */ function Transaction(serialized) { if (!(this instanceof Transaction)) { diff --git a/lib/transport/index.js b/lib/transport/index.js new file mode 100644 index 000000000..f1cbcbf1b --- /dev/null +++ b/lib/transport/index.js @@ -0,0 +1,8 @@ +/** + * @namespace Transport + */ +module.exports = { + Peer: require('./peer'), + Messages: require('./messages'), + Pool: require('./pool') +}; diff --git a/lib/transport/messages.js b/lib/transport/messages.js index 179940a6b..7503d4526 100644 --- a/lib/transport/messages.js +++ b/lib/transport/messages.js @@ -1,4 +1,7 @@ 'use strict'; +/** + * @namespace Transport.Message + */ /* jshint curly: false */ var Buffers = require('buffers'); @@ -19,6 +22,7 @@ var PROTOCOL_VERSION = 70000; /** * Static helper for consuming a data buffer until the next message. * + * @name Transport.Message#parseMessage * @param{Network} network - the network object * @param{Buffer} dataBuffer - the buffer to read from * @returns{Message|undefined} A message or undefined if there is nothing to read. @@ -55,7 +59,8 @@ var parseMessage = function(network, dataBuffer) { module.exports.parseMessage = parseMessage; /** - * Internal function that discards data until founds the next message. + * @desc Internal function that discards data until founds the next message. + * @name Transport.Message#discardUntilNextMessage */ function discardUntilNextMessage(network, dataBuffer) { var magicNumber = network.networkMagic; @@ -82,11 +87,20 @@ function discardUntilNextMessage(network, dataBuffer) { /** * Abstract Message that knows how to parse and serialize itself. * Concret subclases should implement {fromBuffer} and {getPayload} methods. + * @name Transport.Message */ function Message() {} +/** + * @value + * @name Transport.Message.COMMANDS + */ Message.COMMANDS = {}; +/** + * Look up a message type by command name and instantiate the correct Message + * @name Transport.Message#buildMessage + */ Message.buildMessage = function(command, payload) { try { var CommandClass = Message.COMMANDS[command]; @@ -146,6 +160,7 @@ Message.prototype.serialize = function(network) { /** * Version Message * + * @name Transport.Message.Version * @param{string} subversion - version of the client * @param{Buffer} nonce - a random 8 bytes buffer */ @@ -191,6 +206,7 @@ module.exports.Version = Message.COMMANDS.version = Version; /** * Inv Message * + * @name Transport.Message.Inventory * @param{Array} inventory - reported elements */ function Inventory(inventory) { @@ -229,6 +245,7 @@ module.exports.Inventory = Message.COMMANDS.inv = Inventory; /** * Getdata Message * + * @name Transport.Message.GetData * @param{Array} inventory - requested elements */ function GetData(inventory) { @@ -242,6 +259,7 @@ module.exports.GetData = GetData; /** * Ping Message * + * @name Transport.Message.Ping * @param{Buffer} nonce - a random 8 bytes buffer */ function Ping(nonce) { @@ -264,6 +282,7 @@ module.exports.Ping = Message.COMMANDS.ping = Ping; /** * Pong Message * + * @name Transport.Message.Pong * @param{Buffer} nonce - a random 8 bytes buffer */ function Pong(nonce) { @@ -277,6 +296,7 @@ module.exports.Pong = Message.COMMANDS.pong = Pong; /** * Addr Message * + * @name Transport.Message.Addressess * @param{Array} addresses - array of know addresses */ function Addresses(addresses) { @@ -342,6 +362,7 @@ module.exports.Addresses = Message.COMMANDS.addr = Addresses; /** * GetAddr Message * + * @name Transport.Message.GetAddresses */ function GetAddresses() { this.command = 'getaddr'; @@ -353,6 +374,7 @@ module.exports.GetAddresses = Message.COMMANDS.getaddr = GetAddresses; /** * Verack Message * + * @name Transport.Message.VerAck */ function VerAck() { this.command = 'verack'; @@ -364,6 +386,7 @@ module.exports.VerAck = Message.COMMANDS.verack = VerAck; /** * Reject Message * + * @name Transport.Message.Reject */ function Reject() { this.command = 'reject'; @@ -377,6 +400,7 @@ module.exports.Reject = Message.COMMANDS.reject = Reject; /** * Alert Message * + * @name Transport.Message.Alert */ function Alert(payload, signature) { this.command = 'alert'; @@ -408,6 +432,7 @@ module.exports.Alert = Message.COMMANDS.alert = Alert; /** * Headers Message * + * @name Transport.Message.Headers * @param{Array} blockheaders - array of block headers */ function Headers(blockheaders) { @@ -446,6 +471,7 @@ module.exports.Headers = Message.COMMANDS.headers = Headers; /** * Block Message * + * @name Transport.Message.Block * @param{Block} block */ function Block(block) { @@ -468,6 +494,7 @@ module.exports.Block = Message.COMMANDS.block = Block; /** * Tx Message * + * @name Transport.Message.Transaction * @param{Transaction} transaction */ function Transaction(transaction) { @@ -490,6 +517,7 @@ module.exports.Transaction = Message.COMMANDS.tx = Transaction; /** * Getblocks Message * + * @name Transport.Message.GetBlocks * @param{Array} starts - array of buffers with the starting block hashes * @param{Buffer} [stop] - hash of the last block */ @@ -540,6 +568,7 @@ module.exports.GetBlocks = Message.COMMANDS.getblocks = GetBlocks; /** * Getheaders Message * + * @name Transport.Message.GetHeaders * @param{Array} starts - array of buffers with the starting block hashes * @param{Buffer} [stop] - hash of the last block */ diff --git a/lib/unit.js b/lib/unit.js index d33439765..ba6baac61 100644 --- a/lib/unit.js +++ b/lib/unit.js @@ -22,8 +22,8 @@ var JSUtil = require('./util/js'); * @param {Number} amount - The amount to be represented * @param {String} code - The unit of the amount * @returns {Unit} A new instance of an Unit + * @constructor */ - function Unit(amount, code) { if (!(this instanceof Unit)) { return new Unit(amount, code); diff --git a/lib/uri.js b/lib/uri.js index a551605e8..54b4405b3 100644 --- a/lib/uri.js +++ b/lib/uri.js @@ -29,6 +29,7 @@ var JSUtil = require('./util/js'); * @throws {TypeError} Invalid amount * @throws {Error} Unknown required argument * @returns {URI} A new valid and frozen instance of URI + * @constructor */ var URI = function(data, knownParams) { this.extras = {}; diff --git a/lib/util/bitcoin.js b/lib/util/bitcoin.js deleted file mode 100644 index 821c04dbd..000000000 --- a/lib/util/bitcoin.js +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file util/bitcoin.js - * Contains utilities to handle magnitudes inside of bitcoin - */ -'use strict'; - -var SATOSHIS_PER_BTC = 1e8; - -module.exports = { - /** - * @param number satoshis - amount of satoshis to convert - * @return string an exact representation of such amount, in form of a string - * (avoids duplicate representations in ieee756 of the same number) - */ - satoshisToBitcoin: function(satoshis) { - return satoshis / SATOSHIS_PER_BTC; - } -}; diff --git a/lib/util/js.js b/lib/util/js.js index a4819879a..85c128428 100644 --- a/lib/util/js.js +++ b/lib/util/js.js @@ -5,6 +5,7 @@ var _ = require('lodash'); /** * Determines whether a string contains only hexadecimal values * + * @name JSUtil.isHexa * @param {string} value * @return {boolean} true if the string is the hexa representation of a number */ @@ -15,6 +16,9 @@ var isHexa = function isHexa(value) { return /^[0-9a-fA-F]+$/.test(value); }; +/** + * @namespace JSUtil + */ module.exports = { /** * Test if an argument is a valid JSON object. If it is, returns a truthy diff --git a/package.json b/package.json index 3dc1ac280..6bb7083b8 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "main": "index.js", "scripts": { "lint": "gulp lint", - "test": "gulp test-all", + "test": "gulp test", "coverage": "gulp coverage", "build": "gulp", "postinstall": "node ./lib/errors/build.js" @@ -79,8 +79,11 @@ "elliptic": "=0.15.14", "hash.js": "=0.3.2", "inherits": "=2.0.1", + "ink-docstrap": "^0.4.12", + "jsdoc": "^3.3.0-alpha11", "jsrsasign": "=0.0.3", "lodash": "=2.4.1", + "plato": "^1.3.0", "protobufjs": "=3.0.0", "sha512": "=0.0.1", "socks5-client": "^0.3.6"