bitcore/test/txin.js

128 lines
3.4 KiB
JavaScript
Raw Normal View History

2014-11-20 13:37:33 -08:00
'use strict';
2014-09-15 17:08:09 -07:00
var should = require('chai').should();
2014-11-20 13:37:33 -08:00
var bitcore = require('..');
2014-11-21 08:26:30 -08:00
var Varint = bitcore.encoding.Varint;
var BufferReader = bitcore.encoding.BufferReader;
2014-11-20 13:37:33 -08:00
var Script = bitcore.Script;
var Txin = bitcore.Txin;
2014-09-15 17:08:09 -07:00
describe('Txin', function() {
var txidbuf = new Buffer(32);
txidbuf.fill(0);
var txoutnum = 0;
2014-11-27 13:30:16 -08:00
var script = Script.fromString('OP_CHECKMULTISIG');
2014-09-17 16:48:23 -07:00
var scriptvi = Varint(script.toBuffer().length);
2014-09-15 17:08:09 -07:00
var seqnum = 0;
var txin = Txin().set({
txidbuf: txidbuf,
txoutnum: txoutnum,
2014-09-17 16:48:23 -07:00
scriptvi: scriptvi,
2014-09-15 17:08:09 -07:00
script: script,
seqnum: seqnum
});
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);
Txin(txidbuf).txidbuf.length.should.equal(32);
(function() {
var txidbuf2 = new Buffer(33);
txidbuf2.fill(0);
Txin(txidbuf2);
}).should.throw('txidbuf must be 32 bytes');
});
2014-09-15 17:08:09 -07:00
describe('#set', function() {
it('should set these vars', function() {
var txin = Txin().set({
txidbuf: txidbuf,
txoutnum: txoutnum,
2014-09-17 16:48:23 -07:00
scriptvi: scriptvi,
2014-09-15 17:08:09 -07:00
script: script,
seqnum: seqnum
});
should.exist(txin.txidbuf);
should.exist(txin.txoutnum);
2014-09-17 16:48:23 -07:00
should.exist(txin.scriptvi);
2014-09-15 17:08:09 -07:00
should.exist(txin.script);
should.exist(txin.seqnum);
});
});
describe('#fromJSON', function() {
it('should set these vars', function() {
var txin2 = Txin().fromJSON(txin.toJSON());
should.exist(txin2.txidbuf);
should.exist(txin2.txoutnum);
should.exist(txin2.scriptvi);
should.exist(txin2.script);
should.exist(txin2.seqnum);
});
});
describe('#toJSON', function() {
it('should set these vars', function() {
2014-11-22 12:50:08 -08:00
var json = txin.toJSON();
should.exist(json.txidbuf);
should.exist(json.txoutnum);
should.exist(json.scriptvi);
should.exist(json.script);
should.exist(json.seqnum);
});
});
2014-09-15 17:08:09 -07:00
describe('#fromBuffer', function() {
it('should convert this known buffer', function() {
var hex = '00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000';
var buf = new Buffer(hex, 'hex');
var txin = Txin().fromBuffer(buf);
2014-09-17 16:48:23 -07:00
txin.scriptvi.toNumber().should.equal(1);
2014-09-15 17:08:09 -07:00
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);
2014-09-17 16:48:23 -07:00
txin.scriptvi.toNumber().should.equal(1);
2014-09-15 17:08:09 -07:00
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');
});
});
});