bitcore-lib-zcash/test/script.js

320 lines
14 KiB
JavaScript
Raw Normal View History

2014-11-20 13:33:26 -08:00
'use strict';
2014-09-01 15:45:03 -07:00
var should = require('chai').should();
2014-11-20 13:33:26 -08:00
var bitcore = require('..');
var Script = bitcore.Script;
var Opcode = bitcore.Opcode;
2014-09-01 15:45:03 -07:00
describe('Script', function() {
2014-11-27 13:30:16 -08:00
2014-09-01 15:45:03 -07:00
it('should make a new script', function() {
var script = new Script();
2014-11-20 13:33:26 -08:00
should.exist(script);
2014-09-01 15:45:03 -07:00
});
describe('#fromBuffer', function() {
2014-11-27 13:30:16 -08:00
it('should parse this buffer containing an OP code', function() {
var buf = new Buffer(1);
buf[0] = Opcode('OP_0').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]);
});
it('should parse this buffer containing another OP code', function() {
var buf = new Buffer(1);
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]);
});
it('should parse this buffer containing three bytes of data', function() {
var buf = new Buffer([3, 1, 2, 3]);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
});
it('should parse this buffer containing OP_PUSHDATA1 and three bytes of data', function() {
var buf = new Buffer([0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA1').toNumber();
buf.writeUInt8(3, 1);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
});
it('should parse this buffer containing OP_PUSHDATA2 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA2').toNumber();
buf.writeUInt16LE(3, 1);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
});
it('should parse this buffer containing OP_PUSHDATA4 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA4').toNumber();
buf.writeUInt16LE(3, 1);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
});
it('should parse this buffer an OP code, data, and another OP code', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
buf[0] = Opcode('OP_0').toNumber();
buf[1] = Opcode('OP_PUSHDATA4').toNumber();
buf.writeUInt16LE(3, 2);
buf[buf.length - 1] = Opcode('OP_0').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3);
script.chunks[0].should.equal(buf[0]);
script.chunks[1].buf.toString('hex').should.equal('010203');
script.chunks[2].should.equal(buf[buf.length - 1]);
});
});
2014-09-01 18:01:17 -07:00
describe('#toBuffer', function() {
2014-11-27 13:30:16 -08:00
2014-09-01 18:01:17 -07:00
it('should output this buffer containing an OP code', function() {
var buf = new Buffer(1);
buf[0] = Opcode('OP_0').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]);
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
it('should output this buffer containing another OP code', function() {
var buf = new Buffer(1);
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(1);
script.chunks[0].should.equal(buf[0]);
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
it('should output this buffer containing three bytes of data', function() {
var buf = new Buffer([3, 1, 2, 3]);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
it('should output this buffer containing OP_PUSHDATA1 and three bytes of data', function() {
var buf = new Buffer([0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA1').toNumber();
buf.writeUInt8(3, 1);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
it('should output this buffer containing OP_PUSHDATA2 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA2').toNumber();
buf.writeUInt16LE(3, 1);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
it('should output this buffer containing OP_PUSHDATA4 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA4').toNumber();
buf.writeUInt16LE(3, 1);
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(1);
script.chunks[0].buf.toString('hex').should.equal('010203');
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
it('should output this buffer an OP code, data, and another OP code', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
buf[0] = Opcode('OP_0').toNumber();
buf[1] = Opcode('OP_PUSHDATA4').toNumber();
buf.writeUInt16LE(3, 2);
buf[buf.length - 1] = Opcode('OP_0').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:01:17 -07:00
script.chunks.length.should.equal(3);
script.chunks[0].should.equal(buf[0]);
script.chunks[1].buf.toString('hex').should.equal('010203');
script.chunks[2].should.equal(buf[buf.length - 1]);
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
});
describe('#fromString', function() {
it('should parse these known scripts', function() {
2014-11-27 13:30:16 -08:00
Script.fromString('OP_0 OP_PUSHDATA4 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0');
Script.fromString('OP_0 OP_PUSHDATA2 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA2 3 0x010203 OP_0');
Script.fromString('OP_0 OP_PUSHDATA1 3 0x010203 OP_0').toString().should.equal('OP_0 OP_PUSHDATA1 3 0x010203 OP_0');
Script.fromString('OP_0 3 0x010203 OP_0').toString().should.equal('OP_0 3 0x010203 OP_0');
});
});
2014-09-01 18:31:02 -07:00
describe('#toString', function() {
it('should output this buffer an OP code, data, and another OP code', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
buf[0] = Opcode('OP_0').toNumber();
buf[1] = Opcode('OP_PUSHDATA4').toNumber();
buf.writeUInt16LE(3, 2);
buf[buf.length - 1] = Opcode('OP_0').toNumber();
2014-11-27 13:30:16 -08:00
var script = Script.fromBuffer(buf);
2014-09-01 18:31:02 -07:00
script.chunks.length.should.equal(3);
script.chunks[0].should.equal(buf[0]);
script.chunks[1].buf.toString('hex').should.equal('010203');
script.chunks[2].should.equal(buf[buf.length - 1]);
script.toString().toString('hex').should.equal('OP_0 OP_PUSHDATA4 3 0x010203 OP_0');
});
});
describe('#isOpReturn', function() {
2014-11-27 13:30:16 -08:00
it('should know this is a (blank) OP_RETURN script', function() {
Script('OP_RETURN').isOpReturn().should.equal(true);
});
it('should know this is an OP_RETURN script', function() {
var buf = new Buffer(40);
buf.fill(0);
Script('OP_RETURN 40 0x' + buf.toString('hex')).isOpReturn().should.equal(true);
});
it('should know this is not an OP_RETURN script', function() {
var buf = new Buffer(40);
buf.fill(0);
Script('OP_CHECKMULTISIG 40 0x' + buf.toString('hex')).isOpReturn().should.equal(false);
});
});
describe('#isPublicKeyHashIn', function() {
2014-11-27 13:30:16 -08:00
it('should classify this known pubkeyhashin', function() {
Script('73 0x3046022100bb3c194a30e460d81d34be0a230179c043a656f67e3c5c8bf47eceae7c4042ee0221008bf54ca11b2985285be0fd7a212873d243e6e73f5fad57e8eb14c4f39728b8c601 65 0x04e365859b3c78a8b7c202412b949ebca58e147dba297be29eee53cd3e1d300a6419bc780cc9aec0dc94ed194e91c8f6433f1b781ee00eac0ead2aae1e8e0712c6').isPublicKeyHashIn().should.equal(true);
});
it('should classify this known non-pubkeyhashin', function() {
Script('73 0x3046022100bb3c194a30e460d81d34be0a230179c043a656f67e3c5c8bf47eceae7c4042ee0221008bf54ca11b2985285be0fd7a212873d243e6e73f5fad57e8eb14c4f39728b8c601 65 0x04e365859b3c78a8b7c202412b949ebca58e147dba297be29eee53cd3e1d300a6419bc780cc9aec0dc94ed194e91c8f6433f1b781ee00eac0ead2aae1e8e0712c6 OP_CHECKSIG').isPublicKeyHashIn().should.equal(false);
});
});
describe('#isPublicKeyHashOut', function() {
it('should classify this known pubkeyhashout as pubkeyhashout', function() {
Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000 OP_EQUALVERIFY OP_CHECKSIG').isPublicKeyHashOut().should.equal(true);
});
it('should classify this known non-pubkeyhashout as not pubkeyhashout', function() {
2014-11-22 12:50:08 -08:00
Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000').isPublicKeyHashOut().should.equal(false);
});
});
describe('#isScripthashIn', function() {
2014-11-27 13:30:16 -08:00
it('should classify this known scripthashin', function() {
Script('20 0000000000000000000000000000000000000000').isScriptHashIn().should.equal(true);
});
it('should classify this known non-scripthashin', function() {
Script('20 0000000000000000000000000000000000000000 OP_CHECKSIG').isScriptHashIn().should.equal(false);
});
});
describe('#isScripthashOut', function() {
it('should classify this known pubkeyhashout as pubkeyhashout', function() {
Script('OP_HASH160 20 0x0000000000000000000000000000000000000000 OP_EQUAL').isScriptHashOut().should.equal(true);
});
it('should classify these known non-pubkeyhashout as not pubkeyhashout', function() {
Script('OP_HASH160 20 0x0000000000000000000000000000000000000000 OP_EQUAL OP_EQUAL').isScriptHashOut().should.equal(false);
Script('OP_HASH160 21 0x000000000000000000000000000000000000000000 OP_EQUAL').isScriptHashOut().should.equal(false);
});
});
2014-11-28 11:19:07 -08:00
describe('#isMultisigOut', function() {
it('should classify known multisig out 1 as multisig out', function() {
Script('OP_2 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 OP_2 OP_CHECKMULTISIG').isMultisigOut().should.equal(true);
});
it('should classify known multisig out 2 as multisig out', function() {
Script('OP_1 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 OP_2 OP_CHECKMULTISIG').isMultisigOut().should.equal(true);
});
it('should classify known multisig out 3 as multisig out', function() {
Script('OP_2 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 21 0x03363d90d447b00c9c99ceac05b6262ee053441c7e55552ffe526bad8f83ff4640 OP_3 OP_CHECKMULTISIG').isMultisigOut().should.equal(true);
});
it('should classify non-multisig out 1 as non-multisig out', function() {
Script('OP_2 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 OP_2 OP_CHECKMULTISIG OP_EQUAL').isMultisigOut().should.equal(false);
});
it('should classify non-multisig out 2 as non-multisig out', function() {
Script('OP_2').isMultisigOut().should.equal(false);
});
it('should classify non-multisig out 3 as non-multisig out', function() {
Script('OP_2 OP_2 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 OP_2 OP_CHECKMULTISIG OP_EQUAL').isMultisigOut().should.equal(false);
});
});
2014-11-28 07:57:19 -08:00
describe('#add and #prepend', function() {
2014-11-27 13:30:16 -08:00
it('should add these ops', function() {
Script().add('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
2014-11-28 07:57:19 -08:00
Script().add('OP_1').add('OP_2').toString().should.equal('OP_1 OP_2');
Script().add(new Opcode('OP_CHECKMULTISIG')).toString().should.equal('OP_CHECKMULTISIG');
2014-11-27 13:30:16 -08:00
Script().add(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG');
});
2014-11-28 07:57:19 -08:00
it('should prepend these ops', function() {
Script().prepend('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
Script().prepend('OP_1').prepend('OP_2').toString().should.equal('OP_2 OP_1');
});
2014-11-28 07:57:19 -08:00
it('should add and prepend correctly', function() {
Script().add('OP_1').prepend('OP_2').add('OP_3').prepend('OP_4').toString()
.should.equal('OP_4 OP_2 OP_1 OP_3');
});
2014-11-28 07:57:19 -08:00
it('should add these push data', function() {
var buf = new Buffer(1);
buf.fill(0);
Script().add(buf).toString().should.equal('1 0x00');
buf = new Buffer(255);
buf.fill(0);
Script().add(buf).toString().should.equal('OP_PUSHDATA1 255 0x' + buf.toString('hex'));
buf = new Buffer(256);
buf.fill(0);
Script().add(buf).toString().should.equal('OP_PUSHDATA2 256 0x' + buf.toString('hex'));
buf = new Buffer(Math.pow(2, 16));
buf.fill(0);
Script().add(buf).toString().should.equal('OP_PUSHDATA4 ' + Math.pow(2, 16) + ' 0x' + buf.toString('hex'));
});
2014-11-28 07:57:19 -08:00
it('should add both pushdata and non-pushdata chunks', function() {
Script().add('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
Script().add(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG');
var buf = new Buffer(1);
buf.fill(0);
Script().add(buf).toString().should.equal('1 0x00');
});
});
2014-11-27 13:30:16 -08:00
2014-09-01 15:45:03 -07:00
});