Ryan X. Charles
10 years ago
2 changed files with 132 additions and 0 deletions
@ -0,0 +1,54 @@ |
|||||
|
var BN = require('./bn'); |
||||
|
var BufferReader = require('./bufferreader'); |
||||
|
var BufferWriter = require('./bufferwriter'); |
||||
|
var Varint = require('./varint'); |
||||
|
var Script = require('./script'); |
||||
|
|
||||
|
var Txout = function Txout(valuebn, varint, script) { |
||||
|
if (!(this instanceof Txout)) |
||||
|
return new Txout(valuebn, varint, script); |
||||
|
if (valuebn instanceof BN) { |
||||
|
this.set({ |
||||
|
valuebn: valuebn, |
||||
|
varint: varint, |
||||
|
script: script |
||||
|
}); |
||||
|
} else if (valuebn) { |
||||
|
var obj = valuebn; |
||||
|
this.set(obj); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
Txout.prototype.set = function(obj) { |
||||
|
this.valuebn = obj.valuebn || this.valuebn; |
||||
|
this.varint = obj.varint || this.varint; |
||||
|
this.script = obj.script || this.script; |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
Txout.prototype.fromBuffer = function(buf) { |
||||
|
return this.fromBufferReader(BufferReader(buf)); |
||||
|
}; |
||||
|
|
||||
|
Txout.prototype.fromBufferReader = function(br) { |
||||
|
this.valuebn = br.readUInt64LEBN(); |
||||
|
this.varint = Varint(br.readVarintNum()); |
||||
|
this.script = Script().fromBuffer(br.buffer(this.varint.toNumber())); |
||||
|
return this; |
||||
|
}; |
||||
|
|
||||
|
Txout.prototype.toBuffer = function() { |
||||
|
var bw = new BufferWriter(); |
||||
|
return this.toBufferWriter(bw).concat(); |
||||
|
}; |
||||
|
|
||||
|
Txout.prototype.toBufferWriter = function(bw) { |
||||
|
if (!bw) |
||||
|
bw = new BufferWriter(); |
||||
|
bw.writeUInt64LEBN(this.valuebn); |
||||
|
bw.write(this.varint.buf); |
||||
|
bw.write(this.script.toBuffer()); |
||||
|
return bw; |
||||
|
}; |
||||
|
|
||||
|
module.exports = Txout; |
@ -0,0 +1,78 @@ |
|||||
|
var should = require('chai').should(); |
||||
|
var BN = require('../lib/bn'); |
||||
|
var Txout = require('../lib/txout'); |
||||
|
var Script = require('../lib/script'); |
||||
|
var Varint = require('../lib/varint'); |
||||
|
var BufferReader = require('../lib/bufferreader'); |
||||
|
var BufferWriter = require('../lib/bufferwriter'); |
||||
|
|
||||
|
describe('Txout', function() { |
||||
|
|
||||
|
it('should make a new txout', function() { |
||||
|
var txout = new Txout(); |
||||
|
should.exist(txout); |
||||
|
txout = Txout(); |
||||
|
should.exist(txout); |
||||
|
}); |
||||
|
|
||||
|
var valuebn = BN(5); |
||||
|
var script = Script().fromString("OP_CHECKMULTISIG"); |
||||
|
var varint = Varint(script.toBuffer().length); |
||||
|
var txout = new Txout().set({ |
||||
|
valuebn: valuebn, |
||||
|
varint: varint, |
||||
|
script: script |
||||
|
}); |
||||
|
|
||||
|
describe('#set', function() { |
||||
|
|
||||
|
it('should set this object', function() { |
||||
|
var txout = new Txout().set({ |
||||
|
valuebn: valuebn, |
||||
|
varint: varint, |
||||
|
script: script |
||||
|
}); |
||||
|
should.exist(txout.valuebn); |
||||
|
should.exist(txout.varint); |
||||
|
should.exist(txout.script); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#fromBuffer', function() { |
||||
|
|
||||
|
it('should make this txin from this known buffer', function() { |
||||
|
var txout = Txout().fromBuffer(new Buffer('050000000000000001ae', 'hex')); |
||||
|
txout.toBuffer().toString('hex').should.equal('050000000000000001ae'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#fromBufferReader', function() { |
||||
|
|
||||
|
it('should make this txin from this known buffer', function() { |
||||
|
var txout = Txout().fromBufferReader(BufferReader(new Buffer('050000000000000001ae', 'hex'))); |
||||
|
txout.toBuffer().toString('hex').should.equal('050000000000000001ae'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#toBuffer', function() { |
||||
|
|
||||
|
it('should output this known buffer', function() { |
||||
|
var txout = Txout().fromBufferReader(BufferReader(new Buffer('050000000000000001ae', 'hex'))); |
||||
|
txout.toBuffer().toString('hex').should.equal('050000000000000001ae'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
describe('#toBufferWriter', function() { |
||||
|
|
||||
|
it('should output this known buffer', function() { |
||||
|
var txout = Txout().fromBufferReader(BufferReader(new Buffer('050000000000000001ae', 'hex'))); |
||||
|
txout.toBufferWriter().concat().toString('hex').should.equal('050000000000000001ae'); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
|
||||
|
}); |
Loading…
Reference in new issue