Block: Added toObject method and changed toJSON to return a string
This commit is contained in:
parent
354c03987a
commit
19a17017a9
17
lib/block.js
17
lib/block.js
@ -70,7 +70,7 @@ Block._fromJSON = function _fromJSON(data) {
|
|||||||
magicnum: data.magicnum,
|
magicnum: data.magicnum,
|
||||||
blocksize: data.blocksize,
|
blocksize: data.blocksize,
|
||||||
blockheader: BlockHeader.fromJSON(data.blockheader),
|
blockheader: BlockHeader.fromJSON(data.blockheader),
|
||||||
txsvi: Varint().fromJSON(data.txsvi),
|
txsvi: Varint().fromString(data.txsvi),
|
||||||
txs: txs
|
txs: txs
|
||||||
};
|
};
|
||||||
return info;
|
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 = [];
|
var txs = [];
|
||||||
this.txs.forEach(function(tx) {
|
this.txs.forEach(function(tx) {
|
||||||
txs.push(tx.toJSON());
|
txs.push(tx.toJSON());
|
||||||
@ -154,12 +154,19 @@ Block.prototype.toJSON = function toJSON() {
|
|||||||
return {
|
return {
|
||||||
magicnum: this.magicnum,
|
magicnum: this.magicnum,
|
||||||
blocksize: this.blocksize,
|
blocksize: this.blocksize,
|
||||||
blockheader: this.blockheader.toJSON(),
|
blockheader: this.blockheader.toObject(),
|
||||||
txsvi: this.txsvi.toJSON(),
|
txsvi: this.txsvi.toString(),
|
||||||
txs: txs
|
txs: txs
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns {String} - A JSON string
|
||||||
|
*/
|
||||||
|
Block.prototype.toJSON = function toJSON() {
|
||||||
|
return JSON.stringify(this.toObject());
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {Buffer} - A buffer of the block
|
* @returns {Buffer} - A buffer of the block
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -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 {
|
return {
|
||||||
version: this.version,
|
version: this.version,
|
||||||
prevblockidbuf: this.prevblockidbuf.toString('hex'),
|
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
|
* @returns {Buffer} - A Buffer of the BlockHeader
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -26,14 +26,14 @@ Varint.prototype.set = function(obj) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
Varint.prototype.fromJSON = function(json) {
|
Varint.prototype.fromString = function(str) {
|
||||||
this.set({
|
this.set({
|
||||||
buf: new Buffer(json, 'hex')
|
buf: new Buffer(str, 'hex')
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
Varint.prototype.toJSON = function() {
|
Varint.prototype.toString = function() {
|
||||||
return this.buf.toString('hex');
|
return this.buf.toString('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -107,8 +107,8 @@ describe('Block', function() {
|
|||||||
describe('#toJSON', function() {
|
describe('#toJSON', function() {
|
||||||
|
|
||||||
it('should recover these known values', function() {
|
it('should recover these known values', function() {
|
||||||
var block = Block(json);
|
var block = Block.fromJSON(json);
|
||||||
var b = block.toJSON();
|
var b = JSON.parse(block.toJSON());
|
||||||
should.exist(b.magicnum);
|
should.exist(b.magicnum);
|
||||||
should.exist(b.blocksize);
|
should.exist(b.blocksize);
|
||||||
should.exist(b.blockheader);
|
should.exist(b.blockheader);
|
||||||
|
|||||||
@ -87,7 +87,7 @@ describe('BlockHeader', function() {
|
|||||||
describe('#toJSON', function() {
|
describe('#toJSON', function() {
|
||||||
|
|
||||||
it('should set all the variables', 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.version);
|
||||||
should.exist(json.prevblockidbuf);
|
should.exist(json.prevblockidbuf);
|
||||||
should.exist(json.merklerootbuf);
|
should.exist(json.merklerootbuf);
|
||||||
|
|||||||
@ -36,22 +36,22 @@ describe('Varint', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#fromJSON', function() {
|
describe('#fromString', function() {
|
||||||
|
|
||||||
it('should set a buffer', function() {
|
it('should set a buffer', function() {
|
||||||
var buf = BufferWriter().writeVarintNum(5).concat();
|
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);
|
varint.toNumber().should.equal(5);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#toJSON', function() {
|
describe('#toString', function() {
|
||||||
|
|
||||||
it('should return a buffer', function() {
|
it('should return a buffer', function() {
|
||||||
var buf = BufferWriter().writeVarintNum(5).concat();
|
var buf = BufferWriter().writeVarintNum(5).concat();
|
||||||
var varint = Varint().fromJSON(buf.toString('hex'));
|
var varint = Varint().fromString(buf.toString('hex'));
|
||||||
varint.toJSON().should.equal('05');
|
varint.toString().should.equal('05');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user