From 05b83fe1c86a2735a5357c0106af677942c543dd Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 27 Jun 2016 17:32:23 -0700 Subject: [PATCH] script fromJSON. mempool test. --- lib/bcoin/coin.js | 4 ++-- lib/bcoin/script.js | 26 ++++++++++++++++++++++++-- test/mempool-test.js | 3 ++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 43c56751..92aca874 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -160,7 +160,7 @@ Coin.prototype.toJSON = function toJSON() { version: this.version, height: this.height, value: utils.btc(this.value), - script: this.script.toRaw().toString('hex'), + script: this.script.toJSON(), coinbase: this.coinbase, hash: this.hash ? utils.revHex(this.hash) : null, index: this.index @@ -195,7 +195,7 @@ Coin.prototype.fromJSON = function fromJSON(json) { this.version = json.version; this.height = json.height; this.value = utils.satoshi(json.value); - this.script = bcoin.script.fromRaw(new Buffer(json.script, 'hex')); + this.script = bcoin.script.fromJSON(json.script); this.coinbase = json.coinbase; this.hash = json.hash ? utils.revHex(json.hash) : null; this.index = json.index; diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 66ec48f8..6e03af94 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -310,6 +310,17 @@ Witness.prototype.toJSON = function toJSON() { return this.toRaw().toString('hex'); }; +/** + * Inject properties from json object. + * @private + * @param {String} json + */ + +Witness.prototype.fromJSON = function fromJSON(json) { + assert(typeof json === 'string'); + return this.fromRaw(new Buffer(json, 'hex')); +}; + /** * Insantiate witness from a hex string. * @param {String} json @@ -317,7 +328,7 @@ Witness.prototype.toJSON = function toJSON() { */ Witness.fromJSON = function fromJSON(json) { - return Witness.fromRaw(json, 'hex'); + return new Witness().fromJSON(json); }; /** @@ -1257,6 +1268,17 @@ Script.prototype.toJSON = function toJSON() { return this.toRaw().toString('hex'); }; +/** + * Inject properties from json object. + * @private + * @param {String} json + */ + +Script.prototype.fromJSON = function fromJSON(json) { + assert(typeof json === 'string'); + return this.fromRaw(new Buffer(json, 'hex')); +}; + /** * Instantiate script from a hex string. * @params {String} json @@ -1264,7 +1286,7 @@ Script.prototype.toJSON = function toJSON() { */ Script.fromJSON = function fromJSON(json) { - return Script.fromRaw(json, 'hex'); + return new Script().fromJSON(json); }; /** diff --git a/test/mempool-test.js b/test/mempool-test.js index 20d58619..68395dbf 100644 --- a/test/mempool-test.js +++ b/test/mempool-test.js @@ -101,7 +101,8 @@ describe('Mempool', function() { w.scriptInputs(fake, function(err) { assert.ifError(err); // Fake signature - fake.inputs[0].script.code[0] = new Buffer([0,0,0,0,0,0,0,0,0]); + fake.inputs[0].script.set(0, new Buffer([0,0,0,0,0,0,0,0,0])); + fake.inputs[0].script.compile(); // balance: 11000 [t2, t3, t4, f1, fake].forEach(function(tx) { tx.inputs.forEach(function(input) {