Merge pull request #1196 from maraoz/script/coverage
increase script test coverage
This commit is contained in:
commit
a03daab866
|
@ -182,12 +182,11 @@ Script.fromString = function(str) {
|
||||||
return script;
|
return script;
|
||||||
};
|
};
|
||||||
|
|
||||||
Script.prototype.toString = function() {
|
Script.prototype._chunkToString = function(chunk) {
|
||||||
var str = '';
|
|
||||||
for (var i = 0; i < this.chunks.length; i++) {
|
|
||||||
var chunk = this.chunks[i];
|
|
||||||
var opcodenum = chunk.opcodenum;
|
var opcodenum = chunk.opcodenum;
|
||||||
|
var str = '';
|
||||||
if (!chunk.buf) {
|
if (!chunk.buf) {
|
||||||
|
// no data chunk
|
||||||
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
|
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
|
||||||
str = str + ' ' + Opcode(opcodenum).toString();
|
str = str + ' ' + Opcode(opcodenum).toString();
|
||||||
} else {
|
} else {
|
||||||
|
@ -198,6 +197,7 @@ Script.prototype.toString = function() {
|
||||||
str = str + ' ' + '0x' + numstr;
|
str = str + ' ' + '0x' + numstr;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// data chunk
|
||||||
if (opcodenum === Opcode.OP_PUSHDATA1 ||
|
if (opcodenum === Opcode.OP_PUSHDATA1 ||
|
||||||
opcodenum === Opcode.OP_PUSHDATA2 ||
|
opcodenum === Opcode.OP_PUSHDATA2 ||
|
||||||
opcodenum === Opcode.OP_PUSHDATA4) {
|
opcodenum === Opcode.OP_PUSHDATA4) {
|
||||||
|
@ -208,6 +208,14 @@ Script.prototype.toString = function() {
|
||||||
str = str + ' ' + '0x' + chunk.buf.toString('hex');
|
str = str + ' ' + '0x' + chunk.buf.toString('hex');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return str;
|
||||||
|
};
|
||||||
|
|
||||||
|
Script.prototype.toString = function() {
|
||||||
|
var str = '';
|
||||||
|
for (var i = 0; i < this.chunks.length; i++) {
|
||||||
|
var chunk = this.chunks[i];
|
||||||
|
str += this._chunkToString(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
return str.substr(1);
|
return str.substr(1);
|
||||||
|
@ -480,10 +488,10 @@ Script.prototype._addByType = function(obj, prepend) {
|
||||||
this._addOpcode(obj, prepend);
|
this._addOpcode(obj, prepend);
|
||||||
} else if (BufferUtil.isBuffer(obj)) {
|
} else if (BufferUtil.isBuffer(obj)) {
|
||||||
this._addBuffer(obj, prepend);
|
this._addBuffer(obj, prepend);
|
||||||
} else if (typeof obj === 'object') {
|
|
||||||
this._insertAtPosition(obj, prepend);
|
|
||||||
} else if (obj instanceof Script) {
|
} else if (obj instanceof Script) {
|
||||||
this.chunks = this.chunks.concat(obj.chunks);
|
this.chunks = this.chunks.concat(obj.chunks);
|
||||||
|
} else if (typeof obj === 'object') {
|
||||||
|
this._insertAtPosition(obj, prepend);
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid script chunk');
|
throw new Error('Invalid script chunk');
|
||||||
}
|
}
|
||||||
|
@ -583,7 +591,7 @@ Script.buildMultisigOut = function(publicKeys, threshold, opts) {
|
||||||
*
|
*
|
||||||
* @param {PublicKey[]} pubkeys list of all public keys controlling the output
|
* @param {PublicKey[]} pubkeys list of all public keys controlling the output
|
||||||
* @param {number} threshold amount of required signatures to spend the output
|
* @param {number} threshold amount of required signatures to spend the output
|
||||||
* @param {Array} signatures signatures to append to the script
|
* @param {Array} signatures and array of signature buffers to append to the script
|
||||||
* @param {Object=} opts
|
* @param {Object=} opts
|
||||||
* @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
|
* @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
|
||||||
* @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
|
* @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
|
||||||
|
@ -598,6 +606,8 @@ Script.buildP2SHMultisigIn = function(pubkeys, threshold, signatures, opts) {
|
||||||
var s = new Script();
|
var s = new Script();
|
||||||
s.add(Opcode.OP_0);
|
s.add(Opcode.OP_0);
|
||||||
_.each(signatures, function(signature) {
|
_.each(signatures, function(signature) {
|
||||||
|
$.checkArgument(BufferUtil.isBuffer(signature), 'Signatures must be an array of Buffers');
|
||||||
|
// TODO: allow signatures to be an array of Signature objects
|
||||||
s.add(signature);
|
s.add(signature);
|
||||||
});
|
});
|
||||||
s.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
|
s.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
|
||||||
|
|
|
@ -430,6 +430,24 @@ describe('Script', function() {
|
||||||
it('should work for no data OP_RETURN', function() {
|
it('should work for no data OP_RETURN', function() {
|
||||||
Script().add(Opcode.OP_RETURN).add(new Buffer('')).toString().should.equal('OP_RETURN 0');
|
Script().add(Opcode.OP_RETURN).add(new Buffer('')).toString().should.equal('OP_RETURN 0');
|
||||||
});
|
});
|
||||||
|
it('works with objects', function() {
|
||||||
|
Script().add({
|
||||||
|
opcodenum: 106
|
||||||
|
}).toString().should.equal('OP_RETURN');
|
||||||
|
});
|
||||||
|
it('works with another script', function() {
|
||||||
|
var someScript = Script('OP_2 21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 ' +
|
||||||
|
'21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 OP_2 OP_CHECKMULTISIG');
|
||||||
|
var s = new Script().add(someScript);
|
||||||
|
s.toString()
|
||||||
|
.should.equal(someScript.toString());
|
||||||
|
});
|
||||||
|
it('fails with wrong type', function() {
|
||||||
|
var fails = function() {
|
||||||
|
return new Script().add(true);
|
||||||
|
};
|
||||||
|
fails.should.throw('Invalid script chunk');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#isStandard', function() {
|
describe('#isStandard', function() {
|
||||||
|
|
Loading…
Reference in New Issue