Merge pull request #1311 from braydonf/tojsonhashes

Include hash in toObject/toJSON results.
This commit is contained in:
Patrick Nagurny 2015-08-13 10:20:37 -04:00
commit 79db9cc372
7 changed files with 740 additions and 601 deletions

View File

@ -21,7 +21,22 @@ var BlockHeader = function BlockHeader(arg) {
if (!(this instanceof BlockHeader)) {
return new BlockHeader(arg);
}
_.extend(this, BlockHeader._from(arg));
var info = BlockHeader._from(arg);
this.version = info.version;
this.prevHash = info.prevHash;
this.merkleRoot = info.merkleRoot;
this.time = info.time;
this.timestamp = info.time;
this.bits = info.bits;
this.nonce = info.nonce;
if (info.hash) {
$.checkState(
this.hash === info.hash,
'Argument object hash property does not match block hash.'
);
}
return this;
};
@ -72,6 +87,7 @@ BlockHeader._fromObject = function _fromObject(data) {
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex'));
}
var info = {
hash: data.hash,
version: data.version,
prevHash: prevHash,
merkleRoot: merkleRoot,
@ -163,6 +179,7 @@ BlockHeader.fromBufferReader = function fromBufferReader(br) {
*/
BlockHeader.prototype.toObject = function toObject() {
return {
hash: this.hash,
version: this.version,
prevHash: BufferUtil.reverse(this.prevHash).toString('hex'),
merkleRoot: BufferUtil.reverse(this.merkleRoot).toString('hex'),

View File

@ -332,6 +332,7 @@ Transaction.prototype.toObject = function toObject() {
outputs.push(output.toObject());
});
var obj = {
hash: this.hash,
version: this.version,
inputs: inputs,
outputs: outputs,
@ -349,10 +350,14 @@ Transaction.prototype.toObject = function toObject() {
return obj;
};
Transaction.prototype.fromObject = function(transaction) {
Transaction.prototype.fromObject = function(arg) {
/* jshint maxstatements: 20 */
var self = this;
if (transaction instanceof Transaction) {
var transaction;
if (arg instanceof Transaction) {
transaction = transaction.toObject();
} else {
transaction = arg;
}
_.each(transaction.inputs, function(input) {
if (!input.output || !input.output.script) {
@ -386,18 +391,20 @@ Transaction.prototype.fromObject = function(transaction) {
}
this.nLockTime = transaction.nLockTime;
this.version = transaction.version;
this._checkConsistency();
this._checkConsistency(arg);
return this;
};
Transaction.prototype._checkConsistency = function() {
Transaction.prototype._checkConsistency = function(arg) {
if (!_.isUndefined(this._changeIndex)) {
$.checkState(this._changeScript);
$.checkState(this.outputs[this._changeIndex]);
$.checkState(this.outputs[this._changeIndex].script.toString() ===
this._changeScript.toString());
}
// TODO: add other checks
if (arg && arg.hash) {
$.checkState(arg.hash === this.hash, 'Hash in object does not match transaction hash');
}
};
/**

View File

@ -175,6 +175,7 @@ describe('Block', function() {
block.id.should.equal(blockOneId);
block.toObject().should.deep.equal({
header: {
hash: '00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048',
version: 1,
prevHash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
merkleRoot: '0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',
@ -183,6 +184,7 @@ describe('Block', function() {
nonce: 2573394689
},
transactions: [{
hash: '0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',
version: 1,
inputs: [{
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',

View File

@ -63,6 +63,20 @@ describe('BlockHeader', function() {
should.exist(bh.nonce);
});
it('will throw an error if the argument object hash property doesn\'t match', function() {
(function() {
var bh = new BlockHeader({
hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
}).should.throw('Argument object hash property does not match block hash.');
});
});
describe('#fromJSON', function() {

File diff suppressed because it is too large Load Diff

View File

@ -27,12 +27,13 @@ module.exports = {
JSON: [
{ // Mainnet Block 100014
header: {
hash: "000000000000b731f2eef9e8c63173adfb07e41bd53eb0ef0a6b720d6cb6dea4",
version: 1,
prevHash: "0000000000016780c81d42b7eff86974c36f5ae026e8662a4393a7f39c86bb82",
merkleRoot: "8772d9d0fdf8c1303c7b1167e3c73b095fd970e33c799c6563d98b2e96c5167f",
time: 1293629558,
bits: 453281356,
nonce: 151839121
nonce: 696601429
},
numTransactions: 7,
hashes: [
@ -45,6 +46,7 @@ module.exports = {
},
{ // Mainnet Block 12363
header: {
hash: "00000000ae81d8be56fcad40f7b2ca03612a9ab681ca5bc6628ab3c2d914ef9a",
version: 1,
prevHash: "00000000acc3e6a055e05edc7cd0cfac6187cd73adc3c06d408d05c95edaaef8",
merkleRoot: "67313e7a73b62faffe9380578a1a96727c1f0af62e61eb8aa050064007a008d0",
@ -443,6 +445,7 @@ module.exports = {
"036bf6944a47791471e9a2cb86615de837f3aa234a7d1cd024026b3e1daee79e"
],
header : {
hash: "00000000000000018eaf634bf13b7e5e50860b99466b91140538223c75b75049",
prevHash : "000000000000000124f6ce137a43bb288d63cc84f9847033cb84595ead05f9de",
merkleRoot : "792f40129c95aec653d2838ef4b031bf541f11c764ca6c3ecc2e20b396ce83cb",
time : 1389715824,

View File

@ -763,6 +763,15 @@ describe('Transaction', function() {
.should.throw('Unsupported input script type: OP_1 OP_ADD OP_2 OP_EQUAL');
});
it('will error if object hash does not match transaction hash', function() {
var tx = new Transaction(tx_1_hex);
var txObj = tx.toObject();
txObj.hash = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458';
(function() {
var tx2 = new Transaction(txObj);
}).should.throw('Hash in object does not match transaction hash');
});
describe('inputAmount + outputAmount', function() {
it('returns correct values for simple transaction', function() {
var transaction = new Transaction()