diff --git a/lib/txin.js b/lib/txin.js new file mode 100644 index 000000000..15a715f9e --- /dev/null +++ b/lib/txin.js @@ -0,0 +1,58 @@ +var BufferReader = require('./bufferreader'); +var BufferWriter = require('./bufferwriter'); +var Varint = require('./varint'); +var Script = require('./script'); + +var Txin = function Txin(txidbuf, txoutnum, varint, script, seqnum) { + if (!(this instanceof Txin)) + return new Txin(txidbuf, txoutnum, varint, script, seqnum); + if (Buffer.isBuffer(txidbuf)) { + this.txidbuf = txidbuf; + this.txoutnum = txoutnum; + this.varint = varint; + this.script = script; + this.seqnum = seqnum; + } else if (txidbuf) { + var obj = txidbuf; + this.set(obj); + } +}; + +Txin.prototype.set = function(obj) { + this.txidbuf = obj.txidbuf || this.txidbuf; + this.txoutnum = typeof obj.txoutnum !== 'undefined' ? obj.txoutnum : this.txoutnum; + this.varint = typeof obj.varint !== 'undefined' ? obj.varint : this.varint; + this.script = obj.script || this.script; + this.seqnum = typeof obj.seqnum !== 'undefined' ? obj.seqnum : this.seqnum; + return this; +}; + +Txin.prototype.fromBuffer = function(buf) { + return this.fromBufferReader(BufferReader(buf)); +}; + +Txin.prototype.fromBufferReader = function(br) { + this.txidbuf = br.buffer(32); + this.txoutnum = br.readUInt32LE(); + this.varint = Varint(br.readVarintBuf()); + this.script = Script().fromBuffer(br.buffer(this.varint.toNumber())); + this.seqnum = br.readUInt32LE(); + return this; +}; + +Txin.prototype.toBuffer = function() { + return this.toBufferWriter().concat(); +}; + +Txin.prototype.toBufferWriter = function(bw) { + if (!bw) + bw = new BufferWriter(); + bw.write(this.txidbuf); + bw.writeUInt32LE(this.txoutnum); + bw.write(this.varint.buf); + bw.write(this.script.toBuffer()); + bw.writeUInt32LE(this.seqnum); + return bw; +}; + +module.exports = Txin; diff --git a/test/txin.js b/test/txin.js new file mode 100644 index 000000000..ec3e1e8e1 --- /dev/null +++ b/test/txin.js @@ -0,0 +1,90 @@ +var should = require('chai').should(); +var Script = require('../lib/script'); +var Txin = require('../lib/txin'); +var Varint = require('../lib/varint'); +var BufferReader = require('../lib/bufferreader'); + +describe('Txin', function() { + + it('should make a new txin', function() { + var txin = new Txin(); + should.exist(txin); + txin = Txin(); + should.exist(txin); + }); + + var txidbuf = new Buffer(32); + txidbuf.fill(0); + var txoutnum = 0; + var script = Script().fromString("OP_CHECKMULTISIG"); + var varint = Varint(script.toBuffer().length); + var seqnum = 0; + var txin = Txin().set({ + txidbuf: txidbuf, + txoutnum: txoutnum, + varint: varint, + script: script, + seqnum: seqnum + }); + + describe('#set', function() { + + it('should set these vars', function() { + var txin = Txin().set({ + txidbuf: txidbuf, + txoutnum: txoutnum, + varint: varint, + script: script, + seqnum: seqnum + }); + should.exist(txin.txidbuf); + should.exist(txin.txoutnum); + should.exist(txin.varint); + should.exist(txin.script); + should.exist(txin.seqnum); + }); + + }); + + describe('#fromBuffer', function() { + + it('should convert this known buffer', function() { + var hex = '00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000'; + var buf = new Buffer(hex, 'hex'); + var txin = Txin().fromBuffer(buf); + txin.varint.toNumber().should.equal(1); + txin.script.toString().should.equal('OP_CHECKMULTISIG'); + }); + + }); + + describe('#fromBufferReader', function() { + + it('should convert this known buffer', function() { + var hex = '00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000'; + var buf = new Buffer(hex, 'hex'); + var br = BufferReader(buf); + var txin = Txin().fromBufferReader(br); + txin.varint.toNumber().should.equal(1); + txin.script.toString().should.equal('OP_CHECKMULTISIG'); + }); + + }); + + describe('#toBuffer', function() { + + it('should convert this known buffer', function() { + txin.toBuffer().toString('hex').should.equal('00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000'); + }); + + }); + + describe('#toBufferWriter', function() { + + it('should convert this known buffer', function() { + txin.toBufferWriter().concat().toString('hex').should.equal('00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000'); + }); + + }); + +});