bitcore/lib/script.js

109 lines
2.5 KiB
JavaScript
Raw Normal View History

2014-09-01 15:45:03 -07:00
var BufferReader = require('./bufferreader');
2014-09-01 18:01:17 -07:00
var BufferWriter = require('./bufferwriter');
2014-09-01 15:45:03 -07:00
var Opcode = require('./opcode');
var Script = function Script(buf) {
if (!(this instanceof Script))
return new Script(buf);
this.chunks = [];
if (Buffer.isBuffer(buf)) {
this.fromBuffer(buf);
}
else if (typeof buf === 'string') {
var str = buf;
this.fromString(str);
2014-09-01 15:45:03 -07:00
}
else if (typeof buf !== 'undefined') {
var obj = buf;
this.set(obj);
}
};
Script.prototype.set = function(obj) {
this.chunks = obj.chunks || this.chunks;
return this;
};
Script.prototype.fromBuffer = function(buf) {
this.chunks = [];
var br = new BufferReader(buf);
while (!br.eof()) {
var opcodenum = br.readUInt8();
var len, buf;
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) {
len = opcodenum;
this.chunks.push({
buf: br.buffer(len),
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
len = br.readUInt8();
var buf = br.buffer(len);
this.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA2) {
len = br.readUInt16LE();
buf = br.buffer(len);
this.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA4) {
len = br.readUInt32LE();
buf = br.buffer(len);
this.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else {
this.chunks.push(opcodenum);
}
}
return this;
};
Script.prototype.toBuffer = function() {
var bw = new BufferWriter();
2014-09-01 18:01:17 -07:00
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);
}
}
}
2014-09-01 18:01:17 -07:00
return bw.concat();
};
2014-09-01 15:45:03 -07:00
module.exports = Script;