From ea2a0163c4f3497298f36262e655dc0e19366ff6 Mon Sep 17 00:00:00 2001 From: Trevin Hofmann Date: Fri, 13 Feb 2015 21:06:51 -0600 Subject: [PATCH 1/3] Remove redundant util requirement The Transaction class was requiring bitcore.util.js twice, as `util` and `JSUtil`. It seems `JSUtil` is the standard name in other classes. --- lib/transaction/transaction.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index a98b0d6..0f5e040 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -5,7 +5,6 @@ var $ = require('../util/preconditions'); var buffer = require('buffer'); var errors = require('../errors'); -var util = require('../util/js'); var bufferUtil = require('../util/buffer'); var JSUtil = require('../util/js'); var BufferReader = require('../encoding/bufferreader'); @@ -47,9 +46,9 @@ function Transaction(serialized) { if (serialized) { if (serialized instanceof Transaction) { return Transaction.shallowCopy(serialized); - } else if (util.isHexa(serialized)) { + } else if (JSUtil.isHexa(serialized)) { this.fromString(serialized); - } else if (util.isValidJSON(serialized)) { + } else if (JSUtil.isValidJSON(serialized)) { this.fromJSON(serialized); } else if (bufferUtil.isBuffer(serialized)) { this.fromBuffer(serialized); From 59a9e40f133367ddeb4dc1289558faaa4aaef5b9 Mon Sep 17 00:00:00 2001 From: Trevin Hofmann Date: Fri, 13 Feb 2015 21:21:06 -0600 Subject: [PATCH 2/3] Properly capitalize BufferUtil everywhere Right out of CONTRIBUTING.md https://github.com/bitpay/bitcore/blob/master/CONTRIBUTING.md#g6---naming-utility-namespaces --- lib/crypto/point.js | 4 ++-- lib/encoding/bufferreader.js | 4 ++-- lib/transaction/transaction.js | 4 ++-- test/hdpublickey.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/crypto/point.js b/lib/crypto/point.js index 2bac5c8..b4155c3 100644 --- a/lib/crypto/point.js +++ b/lib/crypto/point.js @@ -1,7 +1,7 @@ 'use strict'; var BN = require('./bn'); -var bufferUtil = require('../util/buffer'); +var BufferUtil = require('../util/buffer'); var ec = require('elliptic').curves.secp256k1; var ecPoint = ec.curve.point.bind(ec.curve); var ecPointFromX = ec.curve.pointFromX.bind(ec.curve); @@ -139,7 +139,7 @@ Point.pointToCompressed = function pointToCompressed(point) { } else { prefix = new Buffer([0x02]); } - return bufferUtil.concat([prefix, xbuf]); + return BufferUtil.concat([prefix, xbuf]); }; module.exports = Point; diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 808e66e..70bc100 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -2,7 +2,7 @@ var _ = require('lodash'); var $ = require('../util/preconditions'); -var bufferUtil = require('../util/buffer'); +var BufferUtil = require('../util/buffer'); var BN = require('../crypto/bn'); var BufferReader = function BufferReader(buf) { @@ -166,7 +166,7 @@ BufferReader.prototype.readReverse = function(len) { } var buf = this.buf.slice(this.pos, this.pos + len); this.pos = this.pos + len; - return bufferUtil.reverse(buf); + return BufferUtil.reverse(buf); }; module.exports = BufferReader; diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 0f5e040..017b2db 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -5,7 +5,7 @@ var $ = require('../util/preconditions'); var buffer = require('buffer'); var errors = require('../errors'); -var bufferUtil = require('../util/buffer'); +var BufferUtil = require('../util/buffer'); var JSUtil = require('../util/js'); var BufferReader = require('../encoding/bufferreader'); var BufferWriter = require('../encoding/bufferwriter'); @@ -50,7 +50,7 @@ function Transaction(serialized) { this.fromString(serialized); } else if (JSUtil.isValidJSON(serialized)) { this.fromJSON(serialized); - } else if (bufferUtil.isBuffer(serialized)) { + } else if (BufferUtil.isBuffer(serialized)) { this.fromBuffer(serialized); } else if (_.isObject(serialized)) { this.fromObject(serialized); diff --git a/test/hdpublickey.js b/test/hdpublickey.js index 4d48f88..df4faf5 100644 --- a/test/hdpublickey.js +++ b/test/hdpublickey.js @@ -9,7 +9,7 @@ var bitcore = require('..'); var buffer = require('buffer'); var errors = bitcore.errors; var hdErrors = bitcore.errors.HDPublicKey; -var bufferUtil = bitcore.util.buffer; +var BufferUtil = bitcore.util.buffer; var HDPrivateKey = bitcore.HDPrivateKey; var HDPublicKey = bitcore.HDPublicKey; var Base58Check = bitcore.encoding.Base58Check; @@ -117,7 +117,7 @@ describe('HDPublicKey interface', function() { it('checks the checksum', function() { var buffers = new HDPublicKey(xpubkey)._buffers; - buffers.checksum = bufferUtil.integerAsBuffer(1); + buffers.checksum = BufferUtil.integerAsBuffer(1); expectFail(function() { return new HDPublicKey(buffers); }, errors.InvalidB58Checksum); From 374e43bed2838858befd329e9cab725d80154e08 Mon Sep 17 00:00:00 2001 From: Trevin Hofmann Date: Fri, 13 Feb 2015 21:40:20 -0600 Subject: [PATCH 3/3] Properly capitalize Base58Check and Base58 --- lib/address.js | 6 +++--- lib/encoding/base58check.js | 10 +++++----- lib/privatekey.js | 6 +++--- test/encoding/base58check.js | 6 +++--- test/privatekey.js | 8 ++++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/address.js b/lib/address.js index 5273fa1..bdfe7d5 100644 --- a/lib/address.js +++ b/lib/address.js @@ -2,7 +2,7 @@ var _ = require('lodash'); var $ = require('./util/preconditions'); -var base58check = require('./encoding/base58check'); +var Base58Check = require('./encoding/base58check'); var Networks = require('./networks'); var Hash = require('./crypto/hash'); var JSUtil = require('./util/js'); @@ -294,7 +294,7 @@ Address._transformString = function(data, network, type){ if( typeof(data) !== 'string' ) { throw new TypeError('Address supplied is not a string.'); } - var addressBuffer = base58check.decode(data); + var addressBuffer = Base58Check.decode(data); var info = Address._transformBuffer(addressBuffer, network, type); return info; }; @@ -485,7 +485,7 @@ Address.prototype.toJSON = function toJSON() { * @returns {String} Bitcoin address */ Address.prototype.toString = function() { - return base58check.encode(this.toBuffer()); + return Base58Check.encode(this.toBuffer()); }; /** diff --git a/lib/encoding/base58check.js b/lib/encoding/base58check.js index e7b7b43..333f350 100644 --- a/lib/encoding/base58check.js +++ b/lib/encoding/base58check.js @@ -1,7 +1,7 @@ 'use strict'; var _ = require('lodash'); -var base58 = require('./base58'); +var Base58 = require('./base58'); var buffer = require('buffer'); var sha256sha256 = require('../crypto/hash').sha256sha256; @@ -26,10 +26,10 @@ Base58Check.prototype.set = function(obj) { Base58Check.validChecksum = function validChecksum(data, checksum) { if (_.isString(data)) { - data = new buffer.Buffer(base58.decode(data)); + data = new buffer.Buffer(Base58.decode(data)); } if (_.isString(checksum)) { - checksum = new buffer.Buffer(base58.decode(checksum)); + checksum = new buffer.Buffer(Base58.decode(checksum)); } if (!checksum) { checksum = data.slice(-4); @@ -42,7 +42,7 @@ Base58Check.decode = function(s) { if (typeof s !== 'string') throw new Error('Input must be a string'); - var buf = new Buffer(base58.decode(s)); + var buf = new Buffer(Base58.decode(s)); if (buf.length < 4) throw new Error("Input string too short"); @@ -70,7 +70,7 @@ Base58Check.encode = function(buf) { var hash = Base58Check.checksum(buf); buf.copy(checkedBuf); hash.copy(checkedBuf, buf.length); - return base58.encode(checkedBuf); + return Base58.encode(checkedBuf); }; Base58Check.prototype.fromBuffer = function(buf) { diff --git a/lib/privatekey.js b/lib/privatekey.js index ecda40a..822d2f5 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -2,7 +2,7 @@ var _ = require('lodash'); var Address = require('./address'); -var base58check = require('./encoding/base58check'); +var Base58Check = require('./encoding/base58check'); var BN = require('./crypto/bn'); var JSUtil = require('./util/js'); var Networks = require('./networks'); @@ -206,7 +206,7 @@ PrivateKey._transformBNBuffer = function(buf, network) { * @private */ PrivateKey._transformWIF = function(str, network) { - return PrivateKey._transformBuffer(base58check.decode(str), network); + return PrivateKey._transformBuffer(Base58Check.decode(str), network); }; /** @@ -333,7 +333,7 @@ PrivateKey.prototype.toWIF = function() { this.bn.toBuffer({size: 32})]); } - return base58check.encode(buf); + return Base58Check.encode(buf); }; /** diff --git a/test/encoding/base58check.js b/test/encoding/base58check.js index b8b56c8..c2cc622 100644 --- a/test/encoding/base58check.js +++ b/test/encoding/base58check.js @@ -3,7 +3,7 @@ var should = require('chai').should(); var bitcore = require('../..'); var Base58Check = bitcore.encoding.Base58Check; -var base58 = bitcore.encoding.Base58; +var Base58 = bitcore.encoding.Base58; describe('Base58Check', function() { var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); @@ -72,9 +72,9 @@ describe('Base58Check', function() { }); it('should throw an error when there is a checksum mismatch', function() { - var buf2 = base58.decode(enc); + var buf2 = Base58.decode(enc); buf2[0] = buf2[0] + 1; - var enc2 = base58.encode(buf2); + var enc2 = Base58.encode(buf2); (function() { Base58Check.decode(enc2); }).should.throw('Checksum mismatch'); diff --git a/test/privatekey.js b/test/privatekey.js index efce433..5091d7d 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -9,7 +9,7 @@ var BN = bitcore.crypto.BN; var Point = bitcore.crypto.Point; var PrivateKey = bitcore.PrivateKey; var Networks = bitcore.Networks; -var base58check = bitcore.encoding.Base58Check; +var Base58Check = bitcore.encoding.Base58Check; var validbase58 = require('./data/bitcoind/base58_keys_valid.json'); var invalidbase58 = require('./data/bitcoind/base58_keys_invalid.json'); @@ -57,7 +57,7 @@ describe('PrivateKey', function() { }); it('should create a private key from WIF buffer', function() { - var a = new PrivateKey(base58check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')); + var a = new PrivateKey(Base58Check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m')); should.exist(a); should.exist(a.bn); }); @@ -100,7 +100,7 @@ describe('PrivateKey', function() { it('should not be able to instantiate private key WIF is too long', function() { expect(function() { - var buf = base58check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); + var buf = Base58Check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); var buf2 = Buffer.concat([buf, new Buffer(0x01)]); return new PrivateKey(buf2); }).to.throw('Length of buffer must be 33 (uncompressed) or 34 (compressed'); @@ -108,7 +108,7 @@ describe('PrivateKey', function() { it('should not be able to instantiate private key WIF because of unknown network byte', function() { expect(function() { - var buf = base58check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); + var buf = Base58Check.decode('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); var buf2 = Buffer.concat([new Buffer('ff', 'hex'), buf.slice(1, 33)]); return new PrivateKey(buf2); }).to.throw('Invalid network');