diff --git a/lib/block.js b/lib/block.js index 6266727..43e90c8 100644 --- a/lib/block.js +++ b/lib/block.js @@ -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 */ diff --git a/lib/blockheader.js b/lib/blockheader.js index 9300e2e..2fdf9b4 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -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 */ diff --git a/lib/encoding/varint.js b/lib/encoding/varint.js index 0ba7f10..c434f4d 100644 --- a/lib/encoding/varint.js +++ b/lib/encoding/varint.js @@ -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'); }; diff --git a/test/block.js b/test/block.js index fdd421e..148a552 100644 --- a/test/block.js +++ b/test/block.js @@ -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); diff --git a/test/blockheader.js b/test/blockheader.js index 4fed3a1..6e226a3 100644 --- a/test/blockheader.js +++ b/test/blockheader.js @@ -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); diff --git a/test/encoding/varint.js b/test/encoding/varint.js index 9a8bb84..2ee6b2c 100644 --- a/test/encoding/varint.js +++ b/test/encoding/varint.js @@ -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'); }); });