add Script validation tests

This commit is contained in:
Manuel Araoz 2014-07-28 14:03:22 -03:00
parent bed6ccaac0
commit b727cc0d7c
2 changed files with 63 additions and 31 deletions

View File

@ -116,7 +116,6 @@ Script.prototype.isMultiSig = function() {
}; };
Script.prototype.isPubkeyHashScriptSig = function() { Script.prototype.isPubkeyHashScriptSig = function() {
// TODO: add more restrictions to chunks?
return (this.chunks.length == 2 && return (this.chunks.length == 2 &&
Buffer.isBuffer(this.chunks[0]) && Buffer.isBuffer(this.chunks[0]) &&
Buffer.isBuffer(this.chunks[1])); Buffer.isBuffer(this.chunks[1]));
@ -423,6 +422,7 @@ Script.prototype.writeOp = function(opcode) {
this.buffer = buf; this.buffer = buf;
this.chunks.push(opcode); this.chunks.push(opcode);
return this;
}; };
Script.prototype.writeN = function(n) { Script.prototype.writeN = function(n) {

View File

@ -7,6 +7,7 @@ var should = chai.should();
var ScriptModule = bitcore.Script; var ScriptModule = bitcore.Script;
var Address = bitcore.Address; var Address = bitcore.Address;
var Opcode = bitcore.Opcode;
var networks = bitcore.networks; var networks = bitcore.networks;
var Script; var Script;
var testdata = testdata || require('./testdata'); var testdata = testdata || require('./testdata');
@ -139,7 +140,9 @@ describe('Script', function() {
}); });
// 3 of 5 multisig, unsorted // 3 of 5 multisig, unsorted
// test case generated with: bitcoind createmultisig 3 '["02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0", "02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758", "0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea","02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70", "02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793"]' // test case generated with: bitcoind createmultisig 3 '["02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0", "02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758", "0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea","02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70", "02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793"]'
var s1 = Script.createMultisig(3,pubs, {noSorting: true}); var s1 = Script.createMultisig(3, pubs, {
noSorting: true
});
s1.getBuffer().toString('hex').should.equal('532102c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae02102b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758210266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea2102ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e702102c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af79355ae'); s1.getBuffer().toString('hex').should.equal('532102c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae02102b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758210266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea2102ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e702102c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af79355ae');
// 3 of 5 multisig, sorted // 3 of 5 multisig, sorted
@ -180,7 +183,9 @@ describe('Script', function() {
var pubs = testPubKeysHex.map(function(hex) { var pubs = testPubKeysHex.map(function(hex) {
return new Buffer(hex, 'hex'); return new Buffer(hex, 'hex');
}); });
var s1 = Script.createMultisig(3,pubs, {noSorting: true}); var s1 = Script.createMultisig(3, pubs, {
noSorting: true
});
s1.isMultiSig().should.equal(true); s1.isMultiSig().should.equal(true);
}); });
it('should return false for invalid multisig scripts', function() { it('should return false for invalid multisig scripts', function() {
@ -191,11 +196,21 @@ describe('Script', function() {
}); });
}); });
describe('#isPubkeyHashScriptSig', function() { describe.only('ScriptSig validations', function() {
var testPKHSS = function(raw, result) { var pkhss = '4730440220150eccaec1e5d9104434544bf820b1e24c94e0da7a768d62260b57b9f02877db02204d5d193e833099adb0bf38a610d314936fb70671383d2fa6e09586bc77abe3f9012103146226860c4f62b1ab79bdbb0d3145bf1dc1a0cfa7bf35f2aa30e8432717ac72'
var s = new Script(new Buffer(raw, 'hex'); var p2shss = '004930460221008d36f82425396aff3797aed0651954b5bd2bf8768baf358fbeef9994a282d639022100e3967e55972a99b37da210e9a01c580dc3e0e4df8dc9f5a87ba4338c8fc9e5ba0147304402201aafdf74d2dc5d9d78baadd3beb2e565b0ed14489ad2f1434f9b51ad9b4fa7df02204de4400a1e6817c0883cae056baab77986e65a65aac885020d29d4ddafe30960014c69522103909e13a508df9edd35c806b4d0993bca644e69963041aa93dc209105cfd39b282103b3805706833fab77ae3ad3be1117bf797b460bd58c901f5e12721975d89aff8f2103d442f2fe27171b5d1404a9d7ca943e01951fdc103a25bd89089eb88b5a3e743a53ae'
s.isPubkeyHashScriptSig().should.equal(result); var pkss = '0313220402bc01062f503253482f';
var msss = '004830450220582cd0d8c0f42113ef036af9b5b26d500447eb47dd737e129b0d1b9f870166fa022100e6794cc9158cb2347ff440cec6c017ab5043bb71f1cff55baf9df4888902e26a0149304602210089c912fa687304f82634fe4e02f86ad721c3f9b8a6e7a2c06a7b0ba7a891ac18022100ff4f47c88c752a9e2e1ad8d450c7d5c06628159ea2e614f260dcf28c1c7333b101483045022100dd0c15876575df2e9973f3cd57c4f5e9e84d94277d2f4d82cebfb10fe2b25d62022060eb86654f538a5e5c55288de828bdd854e5d1434050a6b54d7d5402b59528ae01';
var createTestF = function(f) {
var testF = function(raw, result) {
var s = new Script(new Buffer(raw, 'hex'));
f.bind(s)().should.equal(result);
}; };
return testF;
};
describe('#isPubkeyHashScriptSig', function() {
var isPubkeyHashScriptSig = new Script().isPubkeyHashScriptSig;
var testPKHSS = createTestF(isPubkeyHashScriptSig);
it('should identify pubkeyhash scriptsig', function() { it('should identify pubkeyhash scriptsig', function() {
testPKHSS(pkhss, true); testPKHSS(pkhss, true);
}); });
@ -209,5 +224,22 @@ describe('Script', function() {
testPKHSS(msss, false); testPKHSS(msss, false);
}); });
}); });
describe('#isP2shScriptSig', function() {
var isP2shScriptSig = new Script().isP2shScriptSig;
var testP2SHSS = createTestF(isP2shScriptSig);
it('should not identify pubkeyhash scriptsig', function() {
testP2SHSS(pkhss, false);
});
it('should not identify pubkey scriptsig', function() {
testP2SHSS(pkss, false);
});
it('should identify p2sh scriptsig', function() {
testP2SHSS(p2shss, true);
});
it('should not identify multisig scriptsig', function() {
testP2SHSS(msss, false);
});
});
}); });
});