From 002eb3dcf5d206037b96cddb64082f33c5277c72 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 12 Dec 2014 19:03:14 -0500 Subject: [PATCH] Transaction: Added toObject method and changed toJSON to return a string --- lib/transaction/input/input.js | 13 ++++++++++--- lib/transaction/output.js | 10 +++++++++- lib/transaction/transaction.js | 13 +++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/transaction/input/input.js b/lib/transaction/input/input.js index 5775539..3a4f5b1 100644 --- a/lib/transaction/input/input.js +++ b/lib/transaction/input/input.js @@ -4,7 +4,7 @@ var _ = require('lodash'); var BufferWriter = require('../../encoding/bufferwriter'); var buffer = require('buffer'); var bufferUtil = require('../../util/buffer'); -var jsUtil = require('../../util/js'); +var JSUtil = require('../../util/js'); var Script = require('../../script'); function Input(params) { @@ -28,7 +28,7 @@ Object.defineProperty(Input.prototype, 'script', { }); Input.prototype._fromObject = function(params) { - if (_.isString(params.prevTxId) && jsUtil.isHexa(params.prevTxId)) { + if (_.isString(params.prevTxId) && JSUtil.isHexa(params.prevTxId)) { params.prevTxId = new buffer.Buffer(params.prevTxId, 'hex'); } this.prevTxId = params.prevTxId; @@ -40,7 +40,7 @@ Input.prototype._fromObject = function(params) { return this; }; -Input.prototype.toJSON = function() { +Input.prototype.toObject = function toObject() { return { prevTxId: this.prevTxId, outputIndex: this.outputIndex, @@ -49,7 +49,14 @@ Input.prototype.toJSON = function() { }; }; +Input.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); +}; + Input.fromJSON = function(json) { + if (JSUtil.isValidJSON(json)) { + json = JSON.parse(json); + } return new Input({ prevTxId: json.prevTxId || json.txidbuf, outputIndex: json.outputIndex || json.txoutnum, diff --git a/lib/transaction/output.js b/lib/transaction/output.js index 97385f1..759aeac 100644 --- a/lib/transaction/output.js +++ b/lib/transaction/output.js @@ -4,6 +4,7 @@ var _ = require('lodash'); var BN = require('../crypto/bn'); var buffer = require('buffer'); var bufferUtil = require('../util/buffer'); +var JSUtil = require('../util/js'); var BufferWriter = require('../encoding/bufferwriter'); var Script = require('../script'); @@ -53,14 +54,21 @@ Output.prototype._fromObject = function(param) { return this; }; -Output.prototype.toJSON = function() { +Output.prototype.toObject = function toObject() { return { satoshis: this.satoshis, script: this._scriptBuffer.toString('hex') }; }; +Output.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); +}; + Output.fromJSON = function(json) { + if (JSUtil.isValidJSON(json)) { + json = JSON.parse(json); + } return new Output({ satoshis: json.satoshis || -(-json.valuebn), script: new Script(json.script) diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 9593479..654efe7 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -6,6 +6,7 @@ var assert = require('assert'); var util = require('../util/js'); var bufferUtil = require('../util/buffer'); +var JSUtil = require('../util/js'); var BufferReader = require('../encoding/bufferreader'); var BufferWriter = require('../encoding/bufferwriter'); var Hash = require('../crypto/hash'); @@ -121,6 +122,9 @@ Transaction.prototype.fromBufferReader = function(reader) { }; Transaction.prototype.fromJSON = function(json) { + if (JSUtil.isValidJSON(json)) { + json = JSON.parse(json); + } var self = this; this.inputs = []; var inputs = json.inputs || json.txins; @@ -137,7 +141,7 @@ Transaction.prototype.fromJSON = function(json) { return this; }; -Transaction.prototype.toJSON = function() { +Transaction.prototype.toObject = function toObject() { var inputs = []; this.inputs.forEach(function(input) { inputs.push(input.toJSON()); @@ -154,11 +158,12 @@ Transaction.prototype.toJSON = function() { }; }; -Transaction.prototype.fromString = function(string) { - this.fromBuffer(new buffer.Buffer(string, 'hex')); +Transaction.prototype.toJSON = function toJSON() { + return JSON.stringify(this.toObject()); }; -Transaction.prototype.fromObject = function(object) { +Transaction.prototype.fromString = function(string) { + this.fromBuffer(new buffer.Buffer(string, 'hex')); }; Transaction.prototype._newTransaction = function() {