Skip to content

Commit

Permalink
Merge 71f181e into 34dcd1c
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan X. Charles committed Jul 16, 2014
2 parents 34dcd1c + 71f181e commit 46db5c5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/SIN.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 42 additions & 7 deletions test/test.EncodedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});

});


Expand Down
15 changes: 10 additions & 5 deletions util/EncodedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ 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;
}
};

// get or set the encoding used (transforms data)
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;
};
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion util/VersionedData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
4 changes: 2 additions & 2 deletions util/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit 46db5c5

Please sign in to comment.