Block: Added toObject method and changed toJSON to return a string

This commit is contained in:
Braydon Fuller 2014-12-12 18:34:43 -05:00
parent 354c03987a
commit 19a17017a9
6 changed files with 32 additions and 18 deletions

View File

@ -70,7 +70,7 @@ Block._fromJSON = function _fromJSON(data) {
magicnum: data.magicnum,
blocksize: data.blocksize,
blockheader: BlockHeader.fromJSON(data.blockheader),
txsvi: Varint().fromJSON(data.txsvi),
txsvi: Varint().fromString(data.txsvi),
txs: txs
};
return info;
@ -144,9 +144,9 @@ Block.fromRawBlock = function fromRawBlock(data) {
};
/**
* @returns {Object} - A JSON object with the block properties
* @returns {Object} - A plain object with the block properties
*/
Block.prototype.toJSON = function toJSON() {
Block.prototype.toObject = function toObject() {
var txs = [];
this.txs.forEach(function(tx) {
txs.push(tx.toJSON());
@ -154,12 +154,19 @@ Block.prototype.toJSON = function toJSON() {
return {
magicnum: this.magicnum,
blocksize: this.blocksize,
blockheader: this.blockheader.toJSON(),
txsvi: this.txsvi.toJSON(),
blockheader: this.blockheader.toObject(),
txsvi: this.txsvi.toString(),
txs: txs
};
};
/**
* @returns {String} - A JSON string
*/
Block.prototype.toJSON = function toJSON() {
return JSON.stringify(this.toObject());
};
/**
* @returns {Buffer} - A buffer of the block
*/

View File

@ -139,9 +139,9 @@ BlockHeader.fromBufferReader = function fromBufferReader(br) {
};
/**
* @returns {Object} - A JSON object of the BlockHeader
* @returns {Object} - A plain object of the BlockHeader
*/
BlockHeader.prototype.toJSON = function toJSON() {
BlockHeader.prototype.toObject = function toObject() {
return {
version: this.version,
prevblockidbuf: this.prevblockidbuf.toString('hex'),
@ -152,6 +152,13 @@ BlockHeader.prototype.toJSON = function toJSON() {
};
};
/**
* @returns {String} - A JSON string
*/
BlockHeader.prototype.toJSON = function toJSON() {
return JSON.stringify(this.toObject());
};
/**
* @returns {Buffer} - A Buffer of the BlockHeader
*/

View File

@ -26,14 +26,14 @@ Varint.prototype.set = function(obj) {
return this;
};
Varint.prototype.fromJSON = function(json) {
Varint.prototype.fromString = function(str) {
this.set({
buf: new Buffer(json, 'hex')
buf: new Buffer(str, 'hex')
});
return this;
};
Varint.prototype.toJSON = function() {
Varint.prototype.toString = function() {
return this.buf.toString('hex');
};

View File

@ -107,8 +107,8 @@ describe('Block', function() {
describe('#toJSON', function() {
it('should recover these known values', function() {
var block = Block(json);
var b = block.toJSON();
var block = Block.fromJSON(json);
var b = JSON.parse(block.toJSON());
should.exist(b.magicnum);
should.exist(b.blocksize);
should.exist(b.blockheader);

View File

@ -87,7 +87,7 @@ describe('BlockHeader', function() {
describe('#toJSON', function() {
it('should set all the variables', function() {
var json = bh.toJSON();
var json = JSON.parse(bh.toJSON());
should.exist(json.version);
should.exist(json.prevblockidbuf);
should.exist(json.merklerootbuf);

View File

@ -36,22 +36,22 @@ describe('Varint', function() {
});
describe('#fromJSON', function() {
describe('#fromString', function() {
it('should set a buffer', function() {
var buf = BufferWriter().writeVarintNum(5).concat();
var varint = Varint().fromJSON(buf.toString('hex'));
var varint = Varint().fromString(buf.toString('hex'));
varint.toNumber().should.equal(5);
});
});
describe('#toJSON', function() {
describe('#toString', function() {
it('should return a buffer', function() {
var buf = BufferWriter().writeVarintNum(5).concat();
var varint = Varint().fromJSON(buf.toString('hex'));
varint.toJSON().should.equal('05');
var varint = Varint().fromString(buf.toString('hex'));
varint.toString().should.equal('05');
});
});