Browse Source

Transaction: Added toObject method and changed toJSON to return a string

patch-2
Braydon Fuller 10 years ago
parent
commit
002eb3dcf5
  1. 13
      lib/transaction/input/input.js
  2. 10
      lib/transaction/output.js
  3. 13
      lib/transaction/transaction.js

13
lib/transaction/input/input.js

@ -4,7 +4,7 @@ var _ = require('lodash');
var BufferWriter = require('../../encoding/bufferwriter'); var BufferWriter = require('../../encoding/bufferwriter');
var buffer = require('buffer'); var buffer = require('buffer');
var bufferUtil = require('../../util/buffer'); var bufferUtil = require('../../util/buffer');
var jsUtil = require('../../util/js'); var JSUtil = require('../../util/js');
var Script = require('../../script'); var Script = require('../../script');
function Input(params) { function Input(params) {
@ -28,7 +28,7 @@ Object.defineProperty(Input.prototype, 'script', {
}); });
Input.prototype._fromObject = function(params) { 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'); params.prevTxId = new buffer.Buffer(params.prevTxId, 'hex');
} }
this.prevTxId = params.prevTxId; this.prevTxId = params.prevTxId;
@ -40,7 +40,7 @@ Input.prototype._fromObject = function(params) {
return this; return this;
}; };
Input.prototype.toJSON = function() { Input.prototype.toObject = function toObject() {
return { return {
prevTxId: this.prevTxId, prevTxId: this.prevTxId,
outputIndex: this.outputIndex, 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) { Input.fromJSON = function(json) {
if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json);
}
return new Input({ return new Input({
prevTxId: json.prevTxId || json.txidbuf, prevTxId: json.prevTxId || json.txidbuf,
outputIndex: json.outputIndex || json.txoutnum, outputIndex: json.outputIndex || json.txoutnum,

10
lib/transaction/output.js

@ -4,6 +4,7 @@ var _ = require('lodash');
var BN = require('../crypto/bn'); var BN = require('../crypto/bn');
var buffer = require('buffer'); var buffer = require('buffer');
var bufferUtil = require('../util/buffer'); var bufferUtil = require('../util/buffer');
var JSUtil = require('../util/js');
var BufferWriter = require('../encoding/bufferwriter'); var BufferWriter = require('../encoding/bufferwriter');
var Script = require('../script'); var Script = require('../script');
@ -53,14 +54,21 @@ Output.prototype._fromObject = function(param) {
return this; return this;
}; };
Output.prototype.toJSON = function() { Output.prototype.toObject = function toObject() {
return { return {
satoshis: this.satoshis, satoshis: this.satoshis,
script: this._scriptBuffer.toString('hex') script: this._scriptBuffer.toString('hex')
}; };
}; };
Output.prototype.toJSON = function toJSON() {
return JSON.stringify(this.toObject());
};
Output.fromJSON = function(json) { Output.fromJSON = function(json) {
if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json);
}
return new Output({ return new Output({
satoshis: json.satoshis || -(-json.valuebn), satoshis: json.satoshis || -(-json.valuebn),
script: new Script(json.script) script: new Script(json.script)

13
lib/transaction/transaction.js

@ -6,6 +6,7 @@ var assert = require('assert');
var util = require('../util/js'); var util = require('../util/js');
var bufferUtil = require('../util/buffer'); var bufferUtil = require('../util/buffer');
var JSUtil = require('../util/js');
var BufferReader = require('../encoding/bufferreader'); var BufferReader = require('../encoding/bufferreader');
var BufferWriter = require('../encoding/bufferwriter'); var BufferWriter = require('../encoding/bufferwriter');
var Hash = require('../crypto/hash'); var Hash = require('../crypto/hash');
@ -121,6 +122,9 @@ Transaction.prototype.fromBufferReader = function(reader) {
}; };
Transaction.prototype.fromJSON = function(json) { Transaction.prototype.fromJSON = function(json) {
if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json);
}
var self = this; var self = this;
this.inputs = []; this.inputs = [];
var inputs = json.inputs || json.txins; var inputs = json.inputs || json.txins;
@ -137,7 +141,7 @@ Transaction.prototype.fromJSON = function(json) {
return this; return this;
}; };
Transaction.prototype.toJSON = function() { Transaction.prototype.toObject = function toObject() {
var inputs = []; var inputs = [];
this.inputs.forEach(function(input) { this.inputs.forEach(function(input) {
inputs.push(input.toJSON()); inputs.push(input.toJSON());
@ -154,11 +158,12 @@ Transaction.prototype.toJSON = function() {
}; };
}; };
Transaction.prototype.fromString = function(string) { Transaction.prototype.toJSON = function toJSON() {
this.fromBuffer(new buffer.Buffer(string, 'hex')); 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() { Transaction.prototype._newTransaction = function() {

Loading…
Cancel
Save