Script().writeXX convenience methods
Script().writeOp('OP_CHECKMULTISIG'), or... Script().writeOp(174), or... Script().writeBuffer([push data buffer]), or... Script().write([op string, number, or push data buffer]) These convenience methods let you easily write a script.
This commit is contained in:
parent
54818c0bd8
commit
b37e39abca
|
@ -234,4 +234,46 @@ Script.prototype.isScripthashIn = function() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Script.prototype.write = function(obj) {
|
||||||
|
if (typeof obj === 'string')
|
||||||
|
this.writeOp(obj);
|
||||||
|
else if (typeof obj === 'number')
|
||||||
|
this.writeOp(obj);
|
||||||
|
else if (Buffer.isBuffer(obj))
|
||||||
|
this.writeBuffer(obj);
|
||||||
|
else if (typeof obj === 'object')
|
||||||
|
this.chunks.push(obj);
|
||||||
|
else
|
||||||
|
throw new Error('Invalid script chunk');
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Script.prototype.writeOp = function(str) {
|
||||||
|
if (typeof str === 'number')
|
||||||
|
this.chunks.push(str);
|
||||||
|
else
|
||||||
|
this.chunks.push(Opcode(str).toNumber());
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Script.prototype.writeBuffer = function(buf) {
|
||||||
|
var opcodenum;
|
||||||
|
var len = buf.length;
|
||||||
|
if (buf.length > 0 && buf.length < Opcode.map.OP_PUSHDATA1) {
|
||||||
|
opcodenum = buf.length;
|
||||||
|
} else if (buf.length < Math.pow(2, 8)) {
|
||||||
|
opcodenum = Opcode.map.OP_PUSHDATA1;
|
||||||
|
} else if (buf.length < Math.pow(2, 16)) {
|
||||||
|
opcodenum = Opcode.map.OP_PUSHDATA2;
|
||||||
|
} else if (buf.length < Math.pow(2, 32)) {
|
||||||
|
opcodenum = Opcode.map.OP_PUSHDATA4;
|
||||||
|
}
|
||||||
|
this.chunks.push({
|
||||||
|
buf: buf,
|
||||||
|
len: len,
|
||||||
|
opcodenum: opcodenum
|
||||||
|
});
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = Script;
|
module.exports = Script;
|
||||||
|
|
|
@ -265,4 +265,44 @@ describe('Script', function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#writeOp', function() {
|
||||||
|
|
||||||
|
it('should write these ops', function() {
|
||||||
|
Script().writeOp('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
|
||||||
|
Script().writeOp(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#writeBuffer', function() {
|
||||||
|
|
||||||
|
it('should write these push data', function() {
|
||||||
|
var buf = new Buffer(1);
|
||||||
|
buf.fill(0);
|
||||||
|
Script().writeBuffer(buf).toString().should.equal('1 0x00');
|
||||||
|
buf = new Buffer(255);
|
||||||
|
buf.fill(0);
|
||||||
|
Script().writeBuffer(buf).toString().should.equal('OP_PUSHDATA1 255 0x' + buf.toString('hex'));
|
||||||
|
buf = new Buffer(256);
|
||||||
|
buf.fill(0);
|
||||||
|
Script().writeBuffer(buf).toString().should.equal('OP_PUSHDATA2 256 0x' + buf.toString('hex'));
|
||||||
|
buf = new Buffer(Math.pow(2, 16));
|
||||||
|
buf.fill(0);
|
||||||
|
Script().writeBuffer(buf).toString().should.equal('OP_PUSHDATA4 ' + Math.pow(2, 16) + ' 0x' + buf.toString('hex'));
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#write', function() {
|
||||||
|
|
||||||
|
it('should write both pushdata and non-pushdata chunks', function() {
|
||||||
|
Script().write('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
|
||||||
|
Script().write(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG');
|
||||||
|
var buf = new Buffer(1);
|
||||||
|
buf.fill(0);
|
||||||
|
Script().write(buf).toString().should.equal('1 0x00');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue