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

This commit is contained in:
Braydon Fuller 2014-12-12 19:03:14 -05:00
parent cddd55afc4
commit 002eb3dcf5
3 changed files with 28 additions and 8 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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() {