diff --git a/lib/block.js b/lib/block.js index d0ceb42..6266727 100644 --- a/lib/block.js +++ b/lib/block.js @@ -3,11 +3,11 @@ var _ = require('lodash'); var BlockHeader = require('./blockheader'); var BN = require('./crypto/bn'); -var bufferUtil = require('./util/buffer'); +var BufferUtil = require('./util/buffer'); var BufferReader = require('./encoding/bufferreader'); var BufferWriter = require('./encoding/bufferwriter'); var Hash = require('./crypto/hash'); -var jsUtil = require('./util/js'); +var JSUtil = require('./util/js'); var Transaction = require('./transaction'); var Varint = require('./encoding/varint'); @@ -35,9 +35,9 @@ var Block = function Block(arg) { */ Block._from = function _from(arg) { var info = {}; - if (bufferUtil.isBuffer(arg)) { + if (BufferUtil.isBuffer(arg)) { info = Block._fromBufferReader(BufferReader(arg)); - } else if (jsUtil.isValidJSON(arg)) { + } else if (JSUtil.isValidJSON(arg)) { info = Block._fromJSON(arg); } else if (_.isObject(arg)) { info = { @@ -59,7 +59,7 @@ Block._from = function _from(arg) { * @private */ Block._fromJSON = function _fromJSON(data) { - if (jsUtil.isValidJSON(data)) { + if (JSUtil.isValidJSON(data)) { data = JSON.parse(data); } var txs = []; @@ -135,7 +135,7 @@ Block.fromString = function fromString(str) { * @returns {Block} - An instance of block */ Block.fromRawBlock = function fromRawBlock(data) { - if (!bufferUtil.isBuffer(data)) { + if (!BufferUtil.isBuffer(data)) { data = new Buffer(data, 'binary'); } var br = BufferReader(data); diff --git a/lib/blockheader.js b/lib/blockheader.js index 62999e8..9300e2e 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -2,11 +2,11 @@ var _ = require('lodash'); var BN = require('./crypto/bn'); -var bufferUtil = require('./util/buffer'); +var BufferUtil = require('./util/buffer'); var BufferReader = require('./encoding/bufferreader'); var BufferWriter = require('./encoding/bufferwriter'); var Hash = require('./crypto/hash'); -var jsUtil = require('./util/js'); +var JSUtil = require('./util/js'); /** * Instantiate a BlockHeader from a Buffer, JSON object, or Object with @@ -32,9 +32,9 @@ var BlockHeader = function BlockHeader(arg) { */ BlockHeader._from = function _from(arg) { var info = {}; - if (bufferUtil.isBuffer(arg)) { + if (BufferUtil.isBuffer(arg)) { info = BlockHeader._fromBufferReader(BufferReader(arg)); - } else if (jsUtil.isValidJSON(arg)) { + } else if (JSUtil.isValidJSON(arg)) { info = BlockHeader._fromJSON(arg); } else if (_.isObject(arg)) { info = { @@ -57,7 +57,7 @@ BlockHeader._from = function _from(arg) { * @private */ BlockHeader._fromJSON = function _fromJSON(data) { - if (jsUtil.isValidJSON(data)) { + if (JSUtil.isValidJSON(data)) { data = JSON.parse(data); } var info = { @@ -85,7 +85,7 @@ BlockHeader.fromJSON = function fromJSON(json) { * @returns {BlockHeader} - An instance of block header */ BlockHeader.fromRawBlock = function fromRawBlock(data) { - if (!bufferUtil.isBuffer(data)) { + if (!BufferUtil.isBuffer(data)) { data = new Buffer(data, 'binary'); } var br = BufferReader(data); diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index a336e8c..40a1978 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -17,8 +17,8 @@ var Random = require('./crypto/random'); var errors = require('./errors'); var hdErrors = errors.HDPrivateKey; -var bufferUtil = require('./util/buffer'); -var jsUtil = require('./util/js'); +var BufferUtil = require('./util/buffer'); +var JSUtil = require('./util/js'); var MINIMUM_ENTROPY_BITS = 128; var BITS_TO_BYTES = 1/8; @@ -42,10 +42,10 @@ function HDPrivateKey(arg) { return new HDPrivateKey(arg); } if (arg) { - if (_.isString(arg) || bufferUtil.isBuffer(arg)) { + if (_.isString(arg) || BufferUtil.isBuffer(arg)) { if (HDPrivateKey.isValidSerialized(arg)) { this._buildFromSerialized(arg); - } else if (jsUtil.isValidJSON(arg)) { + } else if (JSUtil.isValidJSON(arg)) { this._buildFromJSON(arg); } else { throw HDPrivateKey.getSerializedError(arg); @@ -107,12 +107,12 @@ HDPrivateKey.prototype._deriveWithNumber = function(index, hardened) { return cached; } - var indexBuffer = bufferUtil.integerAsBuffer(index); + var indexBuffer = BufferUtil.integerAsBuffer(index); var data; if (hardened) { - data = bufferUtil.concat([new buffer.Buffer([0]), this.privateKey.toBuffer(), indexBuffer]); + data = BufferUtil.concat([new buffer.Buffer([0]), this.privateKey.toBuffer(), indexBuffer]); } else { - data = bufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]); + data = BufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]); } var hash = Hash.sha512hmac(data, this._buffers.chainCode); var leftPart = BN().fromBuffer(hash.slice(0, 32), {size: 32}); @@ -177,7 +177,7 @@ HDPrivateKey.isValidSerialized = function(data, network) { */ HDPrivateKey.getSerializedError = function(data, network) { /* jshint maxcomplexity: 10 */ - if (!(_.isString(data) || bufferUtil.isBuffer(data))) { + if (!(_.isString(data) || BufferUtil.isBuffer(data))) { return new hdErrors.UnrecognizedArgument('Expected string or buffer'); } if (!Base58.validCharacters(data)) { @@ -206,7 +206,7 @@ HDPrivateKey._validateNetwork = function(data, networkArg) { return new errors.InvalidNetworkArgument(networkArg); } var version = data.slice(0, 4); - if (bufferUtil.integerFromBuffer(version) !== network.xprivkey) { + if (BufferUtil.integerFromBuffer(version) !== network.xprivkey) { return new errors.InvalidNetwork(version); } return null; @@ -220,13 +220,13 @@ HDPrivateKey.prototype._buildFromObject = function(arg) { /* jshint maxcomplexity: 12 */ // TODO: Type validation var buffers = { - version: arg.network ? bufferUtil.integerAsBuffer(Network.get(arg.network).xprivkey) : arg.version, - depth: bufferUtil.integerAsSingleByteBuffer(arg.depth), - parentFingerPrint: _.isNumber(arg.parentFingerPrint) ? bufferUtil.integerAsBuffer(arg.parentFingerPrint) : arg.parentFingerPrint, - childIndex: _.isNumber(arg.childIndex) ? bufferUtil.integerAsBuffer(arg.childIndex) : arg.childIndex, - chainCode: _.isString(arg.chainCode) ? bufferUtil.hexToBuffer(arg.chainCode) : arg.chainCode, - privateKey: (_.isString(arg.privateKey) && jsUtil.isHexa(arg.privateKey)) ? bufferUtil.hexToBuffer(arg.privateKey) : arg.privateKey, - checksum: arg.checksum ? (arg.checksum.length ? arg.checksum : bufferUtil.integerAsBuffer(arg.checksum)) : undefined + version: arg.network ? BufferUtil.integerAsBuffer(Network.get(arg.network).xprivkey) : arg.version, + depth: BufferUtil.integerAsSingleByteBuffer(arg.depth), + parentFingerPrint: _.isNumber(arg.parentFingerPrint) ? BufferUtil.integerAsBuffer(arg.parentFingerPrint) : arg.parentFingerPrint, + childIndex: _.isNumber(arg.childIndex) ? BufferUtil.integerAsBuffer(arg.childIndex) : arg.childIndex, + chainCode: _.isString(arg.chainCode) ? BufferUtil.hexToBuffer(arg.chainCode) : arg.chainCode, + privateKey: (_.isString(arg.privateKey) && JSUtil.isHexa(arg.privateKey)) ? BufferUtil.hexToBuffer(arg.privateKey) : arg.privateKey, + checksum: arg.checksum ? (arg.checksum.length ? arg.checksum : BufferUtil.integerAsBuffer(arg.checksum)) : undefined }; return this._buildFromBuffers(buffers); }; @@ -261,8 +261,8 @@ HDPrivateKey.prototype._generateRandomly = function(network) { HDPrivateKey.fromSeed = function(hexa, network) { /* jshint maxcomplexity: 8 */ - if (jsUtil.isHexaString(hexa)) { - hexa = bufferUtil.hexToBuffer(hexa); + if (JSUtil.isHexaString(hexa)) { + hexa = BufferUtil.hexToBuffer(hexa); } if (!Buffer.isBuffer(hexa)) { throw new hdErrors.InvalidEntropyArgument(hexa); @@ -313,7 +313,7 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { var sequence = [ arg.version, arg.depth, arg.parentFingerPrint, arg.childIndex, arg.chainCode, - bufferUtil.emptyBuffer(1), arg.privateKey + BufferUtil.emptyBuffer(1), arg.privateKey ]; var concat = buffer.Buffer.concat(sequence); if (!arg.checksum || !arg.checksum.length) { @@ -360,7 +360,7 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { HDPrivateKey._validateBufferArguments = function(arg) { var checkBuffer = function(name, size) { var buff = arg[name]; - assert(bufferUtil.isBuffer(buff), name + ' argument is not a buffer'); + assert(BufferUtil.isBuffer(buff), name + ' argument is not a buffer'); assert( buff.length === size, name + ' has not the expected size: found ' + buff.length + ', expected ' + size @@ -408,14 +408,14 @@ HDPrivateKey.prototype.toString = function() { */ HDPrivateKey.prototype.toJSON = function() { return { - network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name, - depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth), - fingerPrint: bufferUtil.integerFromBuffer(this.fingerPrint), - parentFingerPrint: bufferUtil.integerFromBuffer(this._buffers.parentFingerPrint), - childIndex: bufferUtil.integerFromBuffer(this._buffers.childIndex), - chainCode: bufferUtil.bufferToHex(this._buffers.chainCode), + network: Network.get(BufferUtil.integerFromBuffer(this._buffers.version)).name, + depth: BufferUtil.integerFromSingleByteBuffer(this._buffers.depth), + fingerPrint: BufferUtil.integerFromBuffer(this.fingerPrint), + parentFingerPrint: BufferUtil.integerFromBuffer(this._buffers.parentFingerPrint), + childIndex: BufferUtil.integerFromBuffer(this._buffers.childIndex), + chainCode: BufferUtil.bufferToHex(this._buffers.chainCode), privateKey: this.privateKey.toBuffer().toString('hex'), - checksum: bufferUtil.integerFromBuffer(this._buffers.checksum), + checksum: BufferUtil.integerFromBuffer(this._buffers.checksum), xprivkey: this.xprivkey }; }; diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 8f05419..7a64019 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -16,8 +16,8 @@ var errors = bitcoreErrors; var hdErrors = bitcoreErrors.HDPublicKey; var assert = require('assert'); -var jsUtil = require('./util/js'); -var bufferUtil = require('./util/buffer'); +var JSUtil = require('./util/js'); +var BufferUtil = require('./util/buffer'); /** * The representation of an hierarchically derived public key. @@ -37,11 +37,11 @@ function HDPublicKey(arg) { return new HDPublicKey(arg); } if (arg) { - if (_.isString(arg) || bufferUtil.isBuffer(arg)) { + if (_.isString(arg) || BufferUtil.isBuffer(arg)) { var error = HDPublicKey.getSerializedError(arg); if (!error) { return this._buildFromSerialized(arg); - } else if (jsUtil.isValidJSON(arg)) { + } else if (JSUtil.isValidJSON(arg)) { return this._buildFromJSON(arg); } else { if (error instanceof hdErrors.ArgumentIsPrivateExtended) { @@ -105,8 +105,8 @@ HDPublicKey.prototype._deriveWithNumber = function (index, hardened) { return cached; } - var indexBuffer = bufferUtil.integerAsBuffer(index); - var data = bufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]); + var indexBuffer = BufferUtil.integerAsBuffer(index); + var data = BufferUtil.concat([this.publicKey.toBuffer(), indexBuffer]); var hash = Hash.sha512hmac(data, this._buffers.chainCode); var leftPart = BN().fromBuffer(hash.slice(0, 32), {size: 32}); var chainCode = hash.slice(32, 64); @@ -172,7 +172,7 @@ HDPublicKey.isValidSerialized = function (data, network) { HDPublicKey.getSerializedError = function (data, network) { /* jshint maxcomplexity: 10 */ /* jshint maxstatements: 20 */ - if (!(_.isString(data) || bufferUtil.isBuffer(data))) { + if (!(_.isString(data) || BufferUtil.isBuffer(data))) { return new hdErrors.UnrecognizedArgument('expected buffer or string'); } if (!Base58.validCharacters(data)) { @@ -193,7 +193,7 @@ HDPublicKey.getSerializedError = function (data, network) { } } network = Network.get(network) || Network.defaultNetwork; - if (bufferUtil.integerFromBuffer(data.slice(0, 4)) === network.xprivkey) { + if (BufferUtil.integerFromBuffer(data.slice(0, 4)) === network.xprivkey) { return new hdErrors.ArgumentIsPrivateExtended(); } return null; @@ -205,7 +205,7 @@ HDPublicKey._validateNetwork = function (data, networkArg) { return new errors.InvalidNetworkArgument(networkArg); } var version = data.slice(HDPublicKey.VersionStart, HDPublicKey.VersionEnd); - if (bufferUtil.integerFromBuffer(version) !== network.xpubkey) { + if (BufferUtil.integerFromBuffer(version) !== network.xpubkey) { return new errors.InvalidNetwork(version); } return null; @@ -219,7 +219,7 @@ HDPublicKey.prototype._buildFromPrivate = function (arg) { var args = _.clone(arg._buffers); var point = Point.getG().mul(BN().fromBuffer(args.privateKey)); args.publicKey = Point.pointToCompressed(point); - args.version = bufferUtil.integerAsBuffer(Network.get(bufferUtil.integerFromBuffer(args.version)).xpubkey); + args.version = BufferUtil.integerAsBuffer(Network.get(BufferUtil.integerFromBuffer(args.version)).xpubkey); args.privateKey = undefined; args.checksum = undefined; args.xprivkey = undefined; @@ -230,14 +230,14 @@ HDPublicKey.prototype._buildFromObject = function (arg) { /* jshint maxcomplexity: 10 */ // TODO: Type validation var buffers = { - version: arg.network ? bufferUtil.integerAsBuffer(Network.get(arg.network).xpubkey) : arg.version, - depth: bufferUtil.integerAsSingleByteBuffer(arg.depth), - parentFingerPrint: _.isNumber(arg.parentFingerPrint) ? bufferUtil.integerAsBuffer(arg.parentFingerPrint) : arg.parentFingerPrint, - childIndex: bufferUtil.integerAsBuffer(arg.childIndex), - chainCode: _.isString(arg.chainCode) ? bufferUtil.hexToBuffer(arg.chainCode) : arg.chainCode, - publicKey: _.isString(arg.publicKey) ? bufferUtil.hexToBuffer(arg.publicKey) : - bufferUtil.isBuffer(arg.publicKey) ? arg.publicKey : arg.publicKey.toBuffer(), - checksum: _.isNumber(arg.checksum) ? bufferUtil.integerAsBuffer(arg.checksum) : arg.checksum + version: arg.network ? BufferUtil.integerAsBuffer(Network.get(arg.network).xpubkey) : arg.version, + depth: BufferUtil.integerAsSingleByteBuffer(arg.depth), + parentFingerPrint: _.isNumber(arg.parentFingerPrint) ? BufferUtil.integerAsBuffer(arg.parentFingerPrint) : arg.parentFingerPrint, + childIndex: BufferUtil.integerAsBuffer(arg.childIndex), + chainCode: _.isString(arg.chainCode) ? BufferUtil.hexToBuffer(arg.chainCode) : arg.chainCode, + publicKey: _.isString(arg.publicKey) ? BufferUtil.hexToBuffer(arg.publicKey) : + BufferUtil.isBuffer(arg.publicKey) ? arg.publicKey : arg.publicKey.toBuffer(), + checksum: _.isNumber(arg.checksum) ? BufferUtil.integerAsBuffer(arg.checksum) : arg.checksum }; return this._buildFromBuffers(buffers); }; @@ -289,7 +289,7 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { arg.version, arg.depth, arg.parentFingerPrint, arg.childIndex, arg.chainCode, arg.publicKey ]; - var concat = bufferUtil.concat(sequence); + var concat = BufferUtil.concat(sequence); var checksum = Base58Check.checksum(concat); if (!arg.checksum || !arg.checksum.length) { arg.checksum = checksum; @@ -302,7 +302,7 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { var xpubkey; if (!arg.xpubkey) { - xpubkey = Base58Check.encode(bufferUtil.concat(sequence)); + xpubkey = Base58Check.encode(BufferUtil.concat(sequence)); } else { xpubkey = arg.xpubkey; } @@ -311,10 +311,10 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { var size = HDPublicKey.ParentFingerPrintSize; var fingerPrint = Hash.sha256ripemd160(publicKey.toBuffer()).slice(0, size); - jsUtil.defineImmutable(this, { + JSUtil.defineImmutable(this, { xpubkey: xpubkey, - network: Network.get(bufferUtil.integerFromBuffer(arg.version)), - depth: bufferUtil.integerFromSingleByteBuffer(arg.depth), + network: Network.get(BufferUtil.integerFromBuffer(arg.version)), + depth: BufferUtil.integerFromSingleByteBuffer(arg.depth), publicKey: publicKey, fingerPrint: fingerPrint }); @@ -325,7 +325,7 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { HDPublicKey._validateBufferArguments = function (arg) { var checkBuffer = function(name, size) { var buff = arg[name]; - assert(bufferUtil.isBuffer(buff), name + ' argument is not a buffer, it\'s ' + typeof buff); + assert(BufferUtil.isBuffer(buff), name + ' argument is not a buffer, it\'s ' + typeof buff); assert( buff.length === size, name + ' has not the expected size: found ' + buff.length + ', expected ' + size @@ -362,20 +362,20 @@ HDPublicKey.prototype.toString = function () { * * childIndex: index with which this key was derived * * chainCode: string in hexa encoding used for derivation * * publicKey: string, hexa encoded, in compressed key format - * * checksum: bufferUtil.integerFromBuffer(this._buffers.checksum), + * * checksum: BufferUtil.integerFromBuffer(this._buffers.checksum), * * xpubkey: the string with the base58 representation of this extended key * * checksum: the base58 checksum of xpubkey */ HDPublicKey.prototype.toJSON = function () { return { - network: Network.get(bufferUtil.integerFromBuffer(this._buffers.version)).name, - depth: bufferUtil.integerFromSingleByteBuffer(this._buffers.depth), - fingerPrint: bufferUtil.integerFromBuffer(this.fingerPrint), - parentFingerPrint: bufferUtil.integerFromBuffer(this._buffers.parentFingerPrint), - childIndex: bufferUtil.integerFromBuffer(this._buffers.childIndex), - chainCode: bufferUtil.bufferToHex(this._buffers.chainCode), + network: Network.get(BufferUtil.integerFromBuffer(this._buffers.version)).name, + depth: BufferUtil.integerFromSingleByteBuffer(this._buffers.depth), + fingerPrint: BufferUtil.integerFromBuffer(this.fingerPrint), + parentFingerPrint: BufferUtil.integerFromBuffer(this._buffers.parentFingerPrint), + childIndex: BufferUtil.integerFromBuffer(this._buffers.childIndex), + chainCode: BufferUtil.bufferToHex(this._buffers.chainCode), publicKey: this.publicKey.toString(), - checksum: bufferUtil.integerFromBuffer(this._buffers.checksum), + checksum: BufferUtil.integerFromBuffer(this._buffers.checksum), xpubkey: this.xpubkey }; };