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/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 af673d0..fbd45dd 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[encoding].converters; + this._encoding = this.encodings[encoding]._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; }; @@ -132,11 +138,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; 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); }; 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;