script.toBuffer();
This commit is contained in:
parent
378dc923ef
commit
e6af57f4fa
|
@ -46,3 +46,4 @@ Features over bitcore:
|
|||
* Better test coverage
|
||||
* Proper message signing and verification
|
||||
* npm-shrinkwrap.json ensures npm install works as intended
|
||||
* byte-for-byte reading/writing scripts
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
var BufferReader = require('./bufferreader');
|
||||
var BufferWriter = require('./bufferwriter');
|
||||
var Opcode = require('./opcode');
|
||||
|
||||
var Script = function Script(buf) {
|
||||
|
@ -75,14 +76,33 @@ Script.prototype.fromBuffer = function(buf) {
|
|||
Script.prototype.toBuffer = function() {
|
||||
var bw = new BufferWriter();
|
||||
|
||||
for (var key in this.chunks) {
|
||||
if (this.chunks.hasOwnProperty(key)) {
|
||||
var chunk = this.chunks[key];
|
||||
if (typeof chunk === 'number') {
|
||||
|
||||
for (var i = 0; i < this.chunks.length; i++) {
|
||||
var chunk = this.chunks[i];
|
||||
if (typeof chunk === 'number') {
|
||||
var opcodenum = chunk;
|
||||
bw.writeUInt8(opcodenum);
|
||||
} else {
|
||||
var opcodenum = chunk.opcodenum;
|
||||
bw.writeUInt8(chunk.opcodenum);
|
||||
if (opcodenum < Opcode.map.OP_PUSHDATA1) {
|
||||
bw.write(chunk.buf);
|
||||
}
|
||||
else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
|
||||
bw.writeUInt8(chunk.len);
|
||||
bw.write(chunk.buf);
|
||||
}
|
||||
else if (opcodenum === Opcode.map.OP_PUSHDATA2) {
|
||||
bw.writeUInt16LE(chunk.len);
|
||||
bw.write(chunk.buf);
|
||||
}
|
||||
else if (opcodenum === Opcode.map.OP_PUSHDATA4) {
|
||||
bw.writeUInt32LE(chunk.len);
|
||||
bw.write(chunk.buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bw.concat();
|
||||
};
|
||||
|
||||
module.exports = Script;
|
||||
|
|
|
@ -77,4 +77,78 @@ describe('Script', function() {
|
|||
|
||||
});
|
||||
|
||||
describe('#toBuffer', function() {
|
||||
|
||||
it('should output this buffer containing an OP code', function() {
|
||||
var buf = new Buffer(1);
|
||||
buf[0] = Opcode('OP_0').toNumber();
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(1);
|
||||
script.chunks[0].should.equal(buf[0]);
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
it('should output this buffer containing another OP code', function() {
|
||||
var buf = new Buffer(1);
|
||||
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber();
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(1);
|
||||
script.chunks[0].should.equal(buf[0]);
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
it('should output this buffer containing three bytes of data', function() {
|
||||
var buf = new Buffer([3, 1, 2, 3]);
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(1);
|
||||
script.chunks[0].buf.toString('hex').should.equal('010203');
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
it('should output this buffer containing OP_PUSHDATA1 and three bytes of data', function() {
|
||||
var buf = new Buffer([0, 0, 1, 2, 3]);
|
||||
buf[0] = Opcode('OP_PUSHDATA1').toNumber();
|
||||
buf.writeUInt8(3, 1);
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(1);
|
||||
script.chunks[0].buf.toString('hex').should.equal('010203');
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
it('should output this buffer containing OP_PUSHDATA2 and three bytes of data', function() {
|
||||
var buf = new Buffer([0, 0, 0, 1, 2, 3]);
|
||||
buf[0] = Opcode('OP_PUSHDATA2').toNumber();
|
||||
buf.writeUInt16LE(3, 1);
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(1);
|
||||
script.chunks[0].buf.toString('hex').should.equal('010203');
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
it('should output this buffer containing OP_PUSHDATA4 and three bytes of data', function() {
|
||||
var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]);
|
||||
buf[0] = Opcode('OP_PUSHDATA4').toNumber();
|
||||
buf.writeUInt16LE(3, 1);
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(1);
|
||||
script.chunks[0].buf.toString('hex').should.equal('010203');
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
it('should output this buffer an OP code, data, and another OP code', function() {
|
||||
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
|
||||
buf[0] = Opcode('OP_0').toNumber();
|
||||
buf[1] = Opcode('OP_PUSHDATA4').toNumber();
|
||||
buf.writeUInt16LE(3, 2);
|
||||
buf[buf.length - 1] = Opcode('OP_0').toNumber();
|
||||
var script = Script().fromBuffer(buf);
|
||||
script.chunks.length.should.equal(3);
|
||||
script.chunks[0].should.equal(buf[0]);
|
||||
script.chunks[1].buf.toString('hex').should.equal('010203');
|
||||
script.chunks[2].should.equal(buf[buf.length - 1]);
|
||||
script.toBuffer().toString('hex').should.equal(buf.toString('hex'));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue