From 86da48a25deafc8b9f06269b3cad64f55a6f5546 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 15 Jul 2014 15:59:19 -0700 Subject: [PATCH 1/4] replace a deprecated use of setting __proto__ --- util/EncodedData.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/util/EncodedData.js b/util/EncodedData.js index af673d0..dcd33d0 100644 --- a/util/EncodedData.js +++ b/util/EncodedData.js @@ -132,11 +132,10 @@ EncodedData.applyEncodingsTo = function(aClass) { var tmp = {}; for (var k in encodings) { var enc = encodings[k]; - var obj = {}; + var obj = Object.create(aClass.prototype); for (var j in enc) { obj[j] = enc[j]; } - obj.__proto__ = aClass.prototype; tmp[k] = obj; } aClass.prototype.encodings = tmp; From 9122807ecb3ed6d8a17933a04bd70b01cdc9381c Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 15 Jul 2014 17:14:04 -0700 Subject: [PATCH 2/4] remove __proto__ from EncodedData and family EncodedData was setting "converter" and "_encoding" by setting them on the prototype of the object. This was probably done to enable overriding these functions. However, overriding was never actually used anywhere, and setting the __proto__ is deprecated. So I have remove all instances of setting __proto__ for EncodedData-ish classes, and instead just set "convert" and "_encoding" on the object directly. --- lib/SIN.js | 2 +- util/EncodedData.js | 12 +++++++++--- util/VersionedData.js | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/SIN.js b/lib/SIN.js index f93ae90..dc69499 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -8,7 +8,7 @@ function SIN(type, payload) { return; }; this.data = new Buffer(1 + 1 + payload.length); - this.__proto__ = this.encodings['binary']; + this.encoding('binary'); this.prefix(0x0F); // SIN magic number, in numberspace this.type(type); this.payload(payload); diff --git a/util/EncodedData.js b/util/EncodedData.js index dcd33d0..87e25d2 100644 --- a/util/EncodedData.js +++ b/util/EncodedData.js @@ -8,9 +8,14 @@ var base58 = require('../lib/Base58').base58Check; function EncodedData(data, encoding) { this.data = data; if (!encoding && (typeof data == 'string')) { - this.__proto__ = this.encodings['base58']; + encoding = 'base58'; + this.converters = this.encodings[encoding].converters; + this._encoding = this.encodings[encoding]._encoding; } else { - this.__proto__ = this.encodings[encoding || 'binary']; + if (typeof this.encodings[encoding] === 'undefined') + encoding = 'binary'; + this.converters = this.encodings['binary'].converters; + this._encoding = this.encodings['binary']._encoding; } }; @@ -18,7 +23,8 @@ function EncodedData(data, encoding) { EncodedData.prototype.encoding = function(encoding) { if (encoding && (encoding != this._encoding)) { this.data = this.as(encoding); - this.__proto__ = this.encodings[encoding]; + this.converters = this.encodings[encoding].converters; + this._encoding = this.encodings[encoding]._encoding; } return this._encoding; }; diff --git a/util/VersionedData.js b/util/VersionedData.js index 8614b0f..fa0d348 100644 --- a/util/VersionedData.js +++ b/util/VersionedData.js @@ -9,7 +9,7 @@ function VersionedData(version, payload) { return; }; this.data = new Buffer(payload.length + 1); - this.__proto__ = this.encodings['binary']; + this.encoding('binary'); this.version(version); this.payload(payload); }; From cfa0c11983771a1d73840520884ae82729018cba Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 15 Jul 2014 18:04:25 -0700 Subject: [PATCH 3/4] remove replace deprecated setting of __proto__ in error.js --- util/error.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/error.js b/util/error.js index 6b64997..58f4cc9 100644 --- a/util/error.js +++ b/util/error.js @@ -15,7 +15,7 @@ function MissingSourceError(msg, missingTxHash) { this.name = 'MissingSourceError'; }; -MissingSourceError.prototype.__proto__ = Error.prototype; +MissingSourceError.prototype = Object.create(Error.prototype); exports.MissingSourceError = MissingSourceError; @@ -38,6 +38,6 @@ function VerificationError(msg, missingTxHash) { this.name = 'VerificationError'; }; -VerificationError.prototype.__proto__ = Error.prototype; +VerificationError.prototype = Object.create(Error.prototype); exports.VerificationError = VerificationError; From 71f181efee636e942c9dce26b2a70df6d3d3e4f4 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 15 Jul 2014 18:21:38 -0700 Subject: [PATCH 4/4] add tests to EncodedData and fix hex conversion bug ...making sure the new changes to __proto__ are working correctly. --- test/test.EncodedData.js | 49 ++++++++++++++++++++++++++++++++++------ util/EncodedData.js | 4 ++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/test/test.EncodedData.js b/test/test.EncodedData.js index e269661..3573972 100644 --- a/test/test.EncodedData.js +++ b/test/test.EncodedData.js @@ -5,21 +5,56 @@ var bitcore = bitcore || require('../bitcore'); var should = chai.should(); -var EncodedDataModule = bitcore.EncodedData; -var EncodedData; +var EncodedData = bitcore.EncodedData; describe('EncodedData', function() { - it('should initialze the main object', function() { - should.exist(EncodedDataModule); - }); - it('should be able to create class', function() { - EncodedData = EncodedDataModule; + + it('should initialize the main object', function() { should.exist(EncodedData); }); + it('should be able to create an instance', function() { var ed = new EncodedData('1GMx4HdDmN78xzGvdQYkwrVqkmLDG1aMNT'); should.exist(ed); }); + + describe('#as', function() { + var buf = bitcore.util.sha256('test'); + var hex = buf.toString('hex'); + var b58 = '2DFtpKRbW2nfrzgAgE25onW3vwCQwM7S1iHk34LW9cwH1kzmHp'; + + it('should convert from binary -> base58', function() { + var ed = new EncodedData(buf); + ed.as('base58').should.equal(bitcore.Base58.base58Check.encode(buf)); + }); + + it('should convert from binary -> hex', function() { + var ed = new EncodedData(buf); + ed.as('hex').should.equal(hex); + }); + + it('should convert from base58 -> binary', function() { + var ed = new EncodedData(b58); + ed.as('binary').toString('hex').should.equal(hex); + }); + + it('should convert from base58 -> hex', function() { + var ed = new EncodedData(b58); + ed.as('hex').should.equal(hex); + }); + + it('should convert from hex -> binary', function() { + var ed = new EncodedData(hex, 'hex'); + ed.as('binary').toString('hex').should.equal(hex); + }); + + it('should convert from hex -> base58', function() { + var ed = new EncodedData(hex, 'hex'); + ed.as('base58').should.equal(b58); + }); + + }); + }); diff --git a/util/EncodedData.js b/util/EncodedData.js index 87e25d2..fbd45dd 100644 --- a/util/EncodedData.js +++ b/util/EncodedData.js @@ -14,8 +14,8 @@ function EncodedData(data, encoding) { } else { if (typeof this.encodings[encoding] === 'undefined') encoding = 'binary'; - this.converters = this.encodings['binary'].converters; - this._encoding = this.encodings['binary']._encoding; + this.converters = this.encodings[encoding].converters; + this._encoding = this.encodings[encoding]._encoding; } };