classify pubkeyhash and scripthash scripts

...both the "in" (ScriptSig) and "out" (ScriptPubkey)
This commit is contained in:
Ryan X. Charles 2014-09-22 16:04:06 -07:00
parent 6f92775b2c
commit 792e8080c8
2 changed files with 94 additions and 0 deletions

View File

@ -188,5 +188,50 @@ Script.prototype.isOpReturn = function() {
}
};
Script.prototype.isPubkeyhashOut = function() {
if (this.chunks[0] === Opcode('OP_DUP').toNumber()
&& this.chunks[1] === Opcode('OP_HASH160').toNumber()
&& this.chunks[2].buf
&& this.chunks[3] === Opcode('OP_EQUALVERIFY').toNumber()
&& this.chunks[4] === Opcode('OP_CHECKSIG').toNumber()) {
return true;
} else {
return false;
}
};
Script.prototype.isPubkeyhashIn = function() {
if (this.chunks.length === 2
&& this.chunks[0].buf
&& this.chunks[1].buf) {
return true;
} else {
return false;
}
};
Script.prototype.isScripthashOut = function() {
if (this.chunks.length === 3
&& this.chunks[0] === Opcode('OP_HASH160').toNumber()
&& this.chunks[1].buf
&& this.chunks[1].buf.length === 20
&& this.chunks[2] === Opcode('OP_EQUAL').toNumber()) {
return true;
} else {
return false;
}
};
//note that these are frequently indistinguishable from pubkeyhashin
Script.prototype.isScripthashIn = function() {
var allpush = this.chunks.every(function(chunk) {
return Buffer.isBuffer(chunk.buf);
});
if (allpush) {
return true;
} else {
return false;
}
};
module.exports = Script;

View File

@ -216,4 +216,53 @@ describe('Script', function() {
});
describe('#isPubkeyhashIn', function() {
it('should classify this known pubkeyhashin', function() {
Script('73 0x3046022100bb3c194a30e460d81d34be0a230179c043a656f67e3c5c8bf47eceae7c4042ee0221008bf54ca11b2985285be0fd7a212873d243e6e73f5fad57e8eb14c4f39728b8c601 65 0x04e365859b3c78a8b7c202412b949ebca58e147dba297be29eee53cd3e1d300a6419bc780cc9aec0dc94ed194e91c8f6433f1b781ee00eac0ead2aae1e8e0712c6').isPubkeyhashIn().should.equal(true);
});
it('should classify this known non-pubkeyhashin', function() {
Script('73 0x3046022100bb3c194a30e460d81d34be0a230179c043a656f67e3c5c8bf47eceae7c4042ee0221008bf54ca11b2985285be0fd7a212873d243e6e73f5fad57e8eb14c4f39728b8c601 65 0x04e365859b3c78a8b7c202412b949ebca58e147dba297be29eee53cd3e1d300a6419bc780cc9aec0dc94ed194e91c8f6433f1b781ee00eac0ead2aae1e8e0712c6 OP_CHECKSIG').isPubkeyhashIn().should.equal(false);
});
});
describe('#isPubkeyhashOut', function() {
it('should classify this known pubkeyhashout as pubkeyhashout', function() {
Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000 OP_EQUALVERIFY OP_CHECKSIG').isPubkeyhashOut().should.equal(true);
});
it('should classify this known non-pubkeyhashout as not pubkeyhashout', function() {
Script('OP_DUP OP_HASH160 20 0000000000000000000000000000000000000000').isPubkeyhashOut().should.equal(false)
});
});
describe('#isScripthashIn', function() {
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);
});
});
});