@@ -12,33 +12,27 @@ var errors = require('../errors');
var MAX_SAFE_INTEGER = 0x1fffffffffffff ;
-function Output (params ) {
+function Output (args ) {
if (! (this instanceof Output)) {
- return new Output (params );
+ return new Output (args );
}
- if (params) {
- if (JSUtil .isValidJSON (params)) {
- return Output .fromJSON (params);
+ if (JSUtil .isValidJSON (args)) {
+ return Output .fromJSON (args);
+ } else if (_ .isObject (args)) {
+ this .satoshis = args .satoshis ;
+ if (_ .isString (args .script ) && JSUtil .isHexa (args .script )) {
+ args .script = new buffer.Buffer (args .script , ' hex' );
}
- return this ._fromObject (params);
+ this .setScript (args .script );
+ } else {
+ throw new TypeError (' Unrecognized argument for Output' );
}
}
Object .defineProperty (Output .prototype , ' script' , {
configurable: false ,
enumerable: true ,
get : function () {
- if (! this ._script ) {
- try {
- this ._script = Script .fromBuffer (this ._scriptBuffer );
- } catch (e) {
- if (e instanceof errors .Script .InvalidBuffer ) {
- this ._script = null ;
- } else {
- throw e;
- }
- }
- }
return this ._script ;
}
});
@@ -84,65 +78,75 @@ Output.prototype.invalidSatoshis = function() {
return false ;
};
-Output .prototype ._fromObject = function (param ) {
- this .satoshis = param .satoshis ;
- if (param .script || param .scriptBuffer ) {
- this .setScript (param .script || param .scriptBuffer );
- }
- return this ;
-};
-
Output .prototype .toObject = function toObject () {
- return {
- satoshis: this .satoshis ,
- script: this .script .toString ()
+ var obj = {
+ satoshis: this .satoshis
};
+ obj .script = this ._scriptBuffer .toString (' hex' );
+ return obj;
};
Output .prototype .toJSON = function toJSON () {
return JSON .stringify (this .toObject ());
};
-Output .fromJSON = function (json ) {
- if (JSUtil .isValidJSON (json)) {
- json = JSON .parse (json);
- }
+Output .fromJSON = function (data ) {
+ $ .checkArgument (JSUtil .isValidJSON (data), ' data must be valid JSON' );
+ var json = JSON .parse (data);
return new Output ({
- satoshis: json .satoshis || + json . valuebn ,
+ satoshis: Number ( json .satoshis ) ,
script: new Script (json .script )
});
};
+Output .prototype .setScriptFromBuffer = function (buffer ) {
+ this ._scriptBuffer = buffer;
+ try {
+ this ._script = Script .fromBuffer (this ._scriptBuffer );
+ } catch (e) {
+ if (e instanceof errors .Script .InvalidBuffer ) {
+ this ._script = null ;
+ } else {
+ throw e;
+ }
+ }
+};
+
Output .prototype .setScript = function (script ) {
if (script instanceof Script) {
this ._scriptBuffer = script .toBuffer ();
this ._script = script;
} else if (_ .isString (script)) {
- this ._script = new Script (script);
+ this ._script = Script . fromString (script);
this ._scriptBuffer = this ._script .toBuffer ();
} else if (bufferUtil .isBuffer (script)) {
- this ._scriptBuffer = script;
- this ._script = null ;
+ this .setScriptFromBuffer (script);
} else {
throw new TypeError (' Invalid argument type: script' );
}
return this ;
};
Output .prototype .inspect = function () {
- return ' <Output (' + this .satoshis + ' sats) ' + this .script .inspect () + ' >' ;
+ var scriptStr;
+ if (this .script ) {
+ scriptStr = this .script .inspect ();
+ } else {
+ scriptStr = this ._scriptBuffer .toString (' hex' );
+ }
+ return ' <Output (' + this .satoshis + ' sats) ' + scriptStr + ' >' ;
};
Output .fromBufferReader = function (br ) {
- var output = new Output () ;
- output .satoshis = br .readUInt64LEBN ();
+ var obj = {} ;
+ obj .satoshis = br .readUInt64LEBN ();
var size = br .readVarintNum ();
if (size !== 0 ) {
- output . _scriptBuffer = br .read (size);
+ obj . script = br .read (size);
} else {
- output . _scriptBuffer = new buffer.Buffer ([]);
+ obj . script = new buffer.Buffer ([]);
}
- return output ;
+ return new Output (obj) ;
};
Output .prototype .toBufferWriter = function (writer ) {
0 comments on commit
458abe0