Browse Source

add script.isMultisigOut

patch-2
Manuel Araoz 10 years ago
parent
commit
96e1451d28
  1. 53
      lib/script.js
  2. 5
      package.json
  3. 22
      test/script.js

53
lib/script.js

@ -170,15 +170,13 @@ Script.prototype.toString = function() {
return str.substr(0, str.length - 1); return str.substr(0, str.length - 1);
}; };
Script.prototype.isOpReturn = function() {
return (this.chunks[0] === Opcode('OP_RETURN').toNumber() &&
(this.chunks.length === 1 ||
(this.chunks.length === 2 &&
this.chunks[1].buf &&
this.chunks[1].buf.length <= 40 &&
this.chunks[1].length === this.chunks.len)));
};
// script classification methods
/**
* @returns true if this is a pay to pubkey hash output script
*/
Script.prototype.isPublicKeyHashOut = function() { Script.prototype.isPublicKeyHashOut = function() {
return this.chunks[0] === Opcode('OP_DUP').toNumber() && return this.chunks[0] === Opcode('OP_DUP').toNumber() &&
this.chunks[1] === Opcode('OP_HASH160').toNumber() && this.chunks[1] === Opcode('OP_HASH160').toNumber() &&
@ -187,12 +185,18 @@ Script.prototype.isPublicKeyHashOut = function() {
this.chunks[4] === Opcode('OP_CHECKSIG').toNumber(); this.chunks[4] === Opcode('OP_CHECKSIG').toNumber();
}; };
/**
* @returns true if this is a pay to public key hash input script
*/
Script.prototype.isPublicKeyHashIn = function() { Script.prototype.isPublicKeyHashIn = function() {
return !!(this.chunks.length === 2 && return !!(this.chunks.length === 2 &&
this.chunks[0].buf && this.chunks[0].buf &&
this.chunks[1].buf); this.chunks[1].buf);
}; };
/**
* @returns true if this is a p2sh output script
*/
Script.prototype.isScriptHashOut = function() { Script.prototype.isScriptHashOut = function() {
return this.chunks.length === 3 && return this.chunks.length === 3 &&
this.chunks[0] === Opcode('OP_HASH160').toNumber() && this.chunks[0] === Opcode('OP_HASH160').toNumber() &&
@ -201,13 +205,44 @@ Script.prototype.isScriptHashOut = function() {
this.chunks[2] === Opcode('OP_EQUAL').toNumber(); this.chunks[2] === Opcode('OP_EQUAL').toNumber();
}; };
//note that these are frequently indistinguishable from pubkeyhashin /**
* @returns true if this is a p2sh input script
* Note that these are frequently indistinguishable from pubkeyhashin
*/
Script.prototype.isScriptHashIn = function() { Script.prototype.isScriptHashIn = function() {
return this.chunks.every(function(chunk) { return this.chunks.every(function(chunk) {
return Buffer.isBuffer(chunk.buf); return Buffer.isBuffer(chunk.buf);
}); });
}; };
/**
* @returns true if this is a mutlsig output script
*/
Script.prototype.isMultisigOut = function() {
return (this.chunks.length > 3 &&
Opcode.isSmallIntOp(this.chunks[0]) &&
this.chunks.slice(1, this.chunks.length - 2).every(function(obj) {
return obj.buf && Buffer.isBuffer(obj.buf);
}) &&
Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2]) &&
this.chunks[this.chunks.length - 1] === Opcode.map.OP_CHECKMULTISIG);
};
/**
* @returns true if this is an OP_RETURN data script
*/
Script.prototype.isOpReturn = function() {
return (this.chunks[0] === Opcode('OP_RETURN').toNumber() &&
(this.chunks.length === 1 ||
(this.chunks.length === 2 &&
this.chunks[1].buf &&
this.chunks[1].buf.length <= 40 &&
this.chunks[1].length === this.chunks.len)));
};
// Script construction methods
/** /**
* Adds a script element at the start of the script. * Adds a script element at the start of the script.
* @param {*} obj a string, number, Opcode, Bufer, or object to add * @param {*} obj a string, number, Opcode, Bufer, or object to add

5
package.json

@ -96,8 +96,11 @@
"mocha": "~2.0.1", "mocha": "~2.0.1",
"run-sequence": "^1.0.2", "run-sequence": "^1.0.2",
"karma": "^0.12.28", "karma": "^0.12.28",
"karma-firefox-launcher": "^0.1.3",
"karma-mocha": "^0.1.9", "karma-mocha": "^0.1.9",
"karma-firefox-launcher": "^0.1.3" "lodash": "^2.4.1",
"mocha": "~2.0.1",
"run-sequence": "^1.0.2"
}, },
"license": "MIT" "license": "MIT"
} }

22
test/script.js

@ -251,6 +251,28 @@ describe('Script', function() {
}); });
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);
});
});
describe('#add and #prepend', function() { describe('#add and #prepend', function() {
it('should add these ops', function() { it('should add these ops', function() {

Loading…
Cancel
Save