Merge pull request #432 from ryanxcharles/feature/proto

Correct deprecated setting of __proto__
This commit is contained in:
Ryan X. Charles 2014-07-16 16:24:22 -07:00
commit e485d0e331
5 changed files with 56 additions and 16 deletions

View File

@ -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);

View File

@ -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);
});
});
});

View File

@ -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;

View File

@ -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);
};

View File

@ -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;