Merge pull request #935 from eordano/feature/outputinspect
Add inspect method to output
This commit is contained in:
commit
7c87305943
@ -13,6 +13,9 @@ function Output(params) {
|
|||||||
return new Output(params);
|
return new Output(params);
|
||||||
}
|
}
|
||||||
if (params) {
|
if (params) {
|
||||||
|
if (JSUtil.isValidJSON(params)) {
|
||||||
|
return Output.fromJSON(params);
|
||||||
|
}
|
||||||
return this._fromObject(params);
|
return this._fromObject(params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,13 +37,15 @@ Object.defineProperty(Output.prototype, 'satoshis', {
|
|||||||
writeable: true,
|
writeable: true,
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
get: function() {
|
get: function() {
|
||||||
return this._satoshis.toNumber();
|
return this._satoshis;
|
||||||
},
|
},
|
||||||
set: function(num) {
|
set: function(num) {
|
||||||
if (num instanceof BN) {
|
if (num instanceof BN) {
|
||||||
this._satoshis = num;
|
this._satoshisBN = num;
|
||||||
|
this._satoshis = num.toNumber();
|
||||||
} else {
|
} else {
|
||||||
this._satoshis = BN.fromNumber(num);
|
this._satoshisBN = BN.fromNumber(num);
|
||||||
|
this._satoshis = num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -90,9 +95,13 @@ Output.prototype.setScript = function(script) {
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Output.prototype.inspect = function() {
|
||||||
|
return '<Output (' + this.satoshis + ' sats) ' + this.script.inspect() + '>';
|
||||||
|
};
|
||||||
|
|
||||||
Output.fromBufferReader = function(br) {
|
Output.fromBufferReader = function(br) {
|
||||||
var output = new Output();
|
var output = new Output();
|
||||||
output._satoshis = br.readUInt64LEBN();
|
output.satoshis = br.readUInt64LEBN();
|
||||||
var size = br.readVarintNum();
|
var size = br.readVarintNum();
|
||||||
if (size !== 0) {
|
if (size !== 0) {
|
||||||
output._scriptBuffer = br.read(size);
|
output._scriptBuffer = br.read(size);
|
||||||
@ -106,7 +115,7 @@ Output.prototype.toBufferWriter = function(writer) {
|
|||||||
if (!writer) {
|
if (!writer) {
|
||||||
writer = new BufferWriter();
|
writer = new BufferWriter();
|
||||||
}
|
}
|
||||||
writer.writeUInt64LEBN(this._satoshis);
|
writer.writeUInt64LEBN(this._satoshisBN);
|
||||||
var script = this._scriptBuffer;
|
var script = this._scriptBuffer;
|
||||||
writer.writeVarintNum(script.length);
|
writer.writeVarintNum(script.length);
|
||||||
writer.write(script);
|
writer.write(script);
|
||||||
|
|||||||
@ -681,7 +681,7 @@ Transaction.prototype.verify = function() {
|
|||||||
var valueoutbn = BN(0);
|
var valueoutbn = BN(0);
|
||||||
for (var i = 0; i < this.outputs.length; i++) {
|
for (var i = 0; i < this.outputs.length; i++) {
|
||||||
var txout = this.outputs[i];
|
var txout = this.outputs[i];
|
||||||
var valuebn = txout._satoshis;
|
var valuebn = txout._satoshisBN;
|
||||||
if (valuebn.lt(0)) {
|
if (valuebn.lt(0)) {
|
||||||
return 'transaction txout ' + i + ' negative';
|
return 'transaction txout ' + i + ' negative';
|
||||||
}
|
}
|
||||||
|
|||||||
54
test/transaction/output.js
Normal file
54
test/transaction/output.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/* jshint unused: false */
|
||||||
|
/* jshint latedef: false */
|
||||||
|
var should = require('chai').should();
|
||||||
|
var expect = require('chai').expect;
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
|
var bitcore = require('../..');
|
||||||
|
var BN = bitcore.crypto.BN;
|
||||||
|
var BufferWriter = bitcore.encoding.BufferWriter;
|
||||||
|
var BufferReader = bitcore.encoding.BufferReader;
|
||||||
|
var Output = bitcore.Transaction.Output;
|
||||||
|
var Script = bitcore.Script;
|
||||||
|
|
||||||
|
var errors = bitcore.errors;
|
||||||
|
|
||||||
|
describe('Output', function() {
|
||||||
|
|
||||||
|
var output = new Output({satoshis: 0, script: Script.empty()});
|
||||||
|
|
||||||
|
it('can be assigned a satoshi amount in big number', function() {
|
||||||
|
var newOutput = new Output({satoshis: new BN(100), script: Script.empty()});
|
||||||
|
newOutput.satoshis.should.equal(100);
|
||||||
|
});
|
||||||
|
|
||||||
|
var expectEqualOutputs = function(a, b) {
|
||||||
|
a.satoshis.should.equal(b.satoshis);
|
||||||
|
a.script.toString().should.equal(b.script.toString());
|
||||||
|
};
|
||||||
|
|
||||||
|
it('deserializes correctly a simple output', function() {
|
||||||
|
var writer = new BufferWriter();
|
||||||
|
output.toBufferWriter(writer);
|
||||||
|
var deserialized = Output.fromBufferReader(new BufferReader(writer.toBuffer()));
|
||||||
|
expectEqualOutputs(output, deserialized);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('roundtrips to/from object', function() {
|
||||||
|
var newOutput = new Output({satoshis: 50, script: new Script().add(0)});
|
||||||
|
var otherOutput = new Output(newOutput.toObject());
|
||||||
|
expectEqualOutputs(newOutput, otherOutput);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can set a script from a buffer', function() {
|
||||||
|
var newOutput = Output(output);
|
||||||
|
newOutput.setScript(Script().add(0).toBuffer());
|
||||||
|
newOutput.inspect().should.equal('<Output (0 sats) <Script: OP_0>>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('has a inspect property', function() {
|
||||||
|
output.inspect().should.equal('<Output (0 sats) <Script: >>');
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue
Block a user