Browse Source

Merge pull request #432 from ryanxcharles/feature/proto

Correct deprecated setting of __proto__
patch-2
Ryan X. Charles 11 years ago
parent
commit
e485d0e331
  1. 2
      lib/SIN.js
  2. 49
      test/test.EncodedData.js
  3. 15
      util/EncodedData.js
  4. 2
      util/VersionedData.js
  5. 4
      util/error.js

2
lib/SIN.js

@ -8,7 +8,7 @@ function SIN(type, payload) {
return; return;
}; };
this.data = new Buffer(1 + 1 + payload.length); 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.prefix(0x0F); // SIN magic number, in numberspace
this.type(type); this.type(type);
this.payload(payload); this.payload(payload);

49
test/test.EncodedData.js

@ -5,21 +5,56 @@ var bitcore = bitcore || require('../bitcore');
var should = chai.should(); var should = chai.should();
var EncodedDataModule = bitcore.EncodedData; var EncodedData = bitcore.EncodedData;
var EncodedData;
describe('EncodedData', function() { describe('EncodedData', function() {
it('should initialze the main object', function() {
should.exist(EncodedDataModule); it('should initialize the main object', function() {
});
it('should be able to create class', function() {
EncodedData = EncodedDataModule;
should.exist(EncodedData); should.exist(EncodedData);
}); });
it('should be able to create an instance', function() { it('should be able to create an instance', function() {
var ed = new EncodedData('1GMx4HdDmN78xzGvdQYkwrVqkmLDG1aMNT'); var ed = new EncodedData('1GMx4HdDmN78xzGvdQYkwrVqkmLDG1aMNT');
should.exist(ed); 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);
});
});
}); });

15
util/EncodedData.js

@ -8,9 +8,14 @@ var base58 = require('../lib/Base58').base58Check;
function EncodedData(data, encoding) { function EncodedData(data, encoding) {
this.data = data; this.data = data;
if (!encoding && (typeof data == 'string')) { 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 { } 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) { EncodedData.prototype.encoding = function(encoding) {
if (encoding && (encoding != this._encoding)) { if (encoding && (encoding != this._encoding)) {
this.data = this.as(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; return this._encoding;
}; };
@ -132,11 +138,10 @@ EncodedData.applyEncodingsTo = function(aClass) {
var tmp = {}; var tmp = {};
for (var k in encodings) { for (var k in encodings) {
var enc = encodings[k]; var enc = encodings[k];
var obj = {}; var obj = Object.create(aClass.prototype);
for (var j in enc) { for (var j in enc) {
obj[j] = enc[j]; obj[j] = enc[j];
} }
obj.__proto__ = aClass.prototype;
tmp[k] = obj; tmp[k] = obj;
} }
aClass.prototype.encodings = tmp; aClass.prototype.encodings = tmp;

2
util/VersionedData.js

@ -9,7 +9,7 @@ function VersionedData(version, payload) {
return; return;
}; };
this.data = new Buffer(payload.length + 1); this.data = new Buffer(payload.length + 1);
this.__proto__ = this.encodings['binary']; this.encoding('binary');
this.version(version); this.version(version);
this.payload(payload); this.payload(payload);
}; };

4
util/error.js

@ -15,7 +15,7 @@ function MissingSourceError(msg, missingTxHash) {
this.name = 'MissingSourceError'; this.name = 'MissingSourceError';
}; };
MissingSourceError.prototype.__proto__ = Error.prototype; MissingSourceError.prototype = Object.create(Error.prototype);
exports.MissingSourceError = MissingSourceError; exports.MissingSourceError = MissingSourceError;
@ -38,6 +38,6 @@ function VerificationError(msg, missingTxHash) {
this.name = 'VerificationError'; this.name = 'VerificationError';
}; };
VerificationError.prototype.__proto__ = Error.prototype; VerificationError.prototype = Object.create(Error.prototype);
exports.VerificationError = VerificationError; exports.VerificationError = VerificationError;

Loading…
Cancel
Save