bitcore/lib/script.js

328 lines
8.6 KiB
JavaScript
Raw Normal View History

2014-11-20 07:52:32 -08:00
'use strict';
2014-11-21 07:54:56 -08:00
var BufferReader = require('./encoding/bufferreader');
var BufferWriter = require('./encoding/bufferwriter');
2014-09-01 15:45:03 -07:00
var Opcode = require('./opcode');
2014-11-27 12:10:35 -08:00
var Script = function Script(from) {
if (!(this instanceof Script)) {
return new Script(from);
}
2014-09-01 15:45:03 -07:00
this.chunks = [];
2014-11-27 12:10:35 -08:00
if (Buffer.isBuffer(from)) {
2014-11-27 13:30:16 -08:00
return Script.fromBuffer(from);
2014-11-27 12:10:35 -08:00
} else if (typeof from === 'string') {
2014-11-27 13:30:16 -08:00
return Script.fromString(from);
2014-11-27 12:10:35 -08:00
} else if (typeof from !== 'undefined') {
this.set(from);
2014-09-01 15:45:03 -07:00
}
};
Script.prototype.set = function(obj) {
this.chunks = obj.chunks || this.chunks;
return this;
};
2014-11-27 12:10:35 -08:00
Script.fromBuffer = function(buffer) {
var script = new Script();
script.chunks = [];
2014-11-27 12:10:35 -08:00
var br = new BufferReader(buffer);
while (!br.eof()) {
var opcodenum = br.readUInt8();
var len, buf;
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) {
len = opcodenum;
2014-11-27 12:10:35 -08:00
script.chunks.push({
2014-09-17 15:49:45 -07:00
buf: br.read(len),
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
len = br.readUInt8();
2014-11-27 12:10:35 -08:00
buf = br.read(len);
script.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA2) {
len = br.readUInt16LE();
2014-09-17 15:49:45 -07:00
buf = br.read(len);
2014-11-27 12:10:35 -08:00
script.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA4) {
len = br.readUInt32LE();
2014-09-17 15:49:45 -07:00
buf = br.read(len);
2014-11-27 12:10:35 -08:00
script.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else {
2014-11-27 12:10:35 -08:00
script.chunks.push(opcodenum);
}
}
2014-11-27 12:10:35 -08:00
return script;
};
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];
2014-11-27 12:10:35 -08:00
var opcodenum;
2014-09-01 18:01:17 -07:00
if (typeof chunk === 'number') {
2014-11-27 12:10:35 -08:00
opcodenum = chunk;
2014-09-01 18:01:17 -07:00
bw.writeUInt8(opcodenum);
} else {
2014-11-27 12:10:35 -08:00
opcodenum = chunk.opcodenum;
2014-09-01 18:01:17 -07:00
bw.writeUInt8(chunk.opcodenum);
if (opcodenum < Opcode.map.OP_PUSHDATA1) {
bw.write(chunk.buf);
2014-11-27 12:10:35 -08:00
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
2014-09-01 18:01:17 -07:00
bw.writeUInt8(chunk.len);
bw.write(chunk.buf);
2014-11-27 12:10:35 -08:00
} else if (opcodenum === Opcode.map.OP_PUSHDATA2) {
2014-09-01 18:01:17 -07:00
bw.writeUInt16LE(chunk.len);
bw.write(chunk.buf);
2014-11-27 12:10:35 -08:00
} else if (opcodenum === Opcode.map.OP_PUSHDATA4) {
2014-09-01 18:01:17 -07:00
bw.writeUInt32LE(chunk.len);
bw.write(chunk.buf);
}
}
}
2014-09-01 18:01:17 -07:00
return bw.concat();
};
2014-11-27 13:30:16 -08:00
Script.fromString = function(str) {
var script = new Script();
script.chunks = [];
var tokens = str.split(' ');
var i = 0;
while (i < tokens.length) {
var token = tokens[i];
var opcode = Opcode(token);
var opcodenum = opcode.toNumber();
if (typeof opcodenum === 'undefined') {
opcodenum = parseInt(token);
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) {
2014-11-27 13:30:16 -08:00
script.chunks.push({
buf: new Buffer(tokens[i + 1].slice(2), 'hex'),
len: opcodenum,
opcodenum: opcodenum
});
i = i + 2;
2014-11-27 12:10:35 -08:00
} else {
throw new Error('Invalid script');
}
2014-11-28 06:48:06 -08:00
} else if (opcodenum === Opcode.map.OP_PUSHDATA1 ||
opcodenum === Opcode.map.OP_PUSHDATA2 ||
opcodenum === Opcode.map.OP_PUSHDATA4) {
2014-11-27 12:10:35 -08:00
if (tokens[i + 2].slice(0, 2) !== '0x') {
throw new Error('Pushdata data must start with 0x');
2014-11-27 12:10:35 -08:00
}
2014-11-27 13:30:16 -08:00
script.chunks.push({
buf: new Buffer(tokens[i + 2].slice(2), 'hex'),
len: parseInt(tokens[i + 1]),
opcodenum: opcodenum
});
i = i + 3;
} else {
2014-11-27 13:30:16 -08:00
script.chunks.push(opcodenum);
i = i + 1;
}
}
2014-11-27 13:30:16 -08:00
return script;
};
2014-09-01 18:31:02 -07:00
Script.prototype.toString = function() {
2014-11-27 12:10:35 -08:00
var str = '';
2014-09-01 18:31:02 -07:00
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
2014-11-27 12:10:35 -08:00
var opcodenum;
2014-09-01 18:31:02 -07:00
if (typeof chunk === 'number') {
2014-11-27 12:10:35 -08:00
opcodenum = chunk;
str = str + Opcode(opcodenum).toString() + ' ';
2014-09-01 18:31:02 -07:00
} else {
2014-11-27 12:10:35 -08:00
opcodenum = chunk.opcodenum;
if (opcodenum === Opcode.map.OP_PUSHDATA1 ||
opcodenum === Opcode.map.OP_PUSHDATA2 ||
opcodenum === Opcode.map.OP_PUSHDATA4) {
str = str + Opcode(opcodenum).toString() + ' ';
}
str = str + chunk.len + ' ';
str = str + '0x' + chunk.buf.toString('hex') + ' ';
2014-09-01 18:31:02 -07:00
}
}
return str.substr(0, str.length - 1);
};
2014-11-28 11:19:07 -08:00
// script classification methods
/**
* @returns true if this is a pay to pubkey hash output script
*/
Script.prototype.isPublicKeyHashOut = function() {
2014-11-28 06:45:42 -08:00
return this.chunks[0] === Opcode('OP_DUP').toNumber() &&
2014-11-28 06:08:33 -08:00
this.chunks[1] === Opcode('OP_HASH160').toNumber() &&
this.chunks[2].buf &&
this.chunks[3] === Opcode('OP_EQUALVERIFY').toNumber() &&
2014-11-28 06:45:42 -08:00
this.chunks[4] === Opcode('OP_CHECKSIG').toNumber();
};
2014-11-28 11:19:07 -08:00
/**
* @returns true if this is a pay to public key hash input script
*/
Script.prototype.isPublicKeyHashIn = function() {
2014-11-28 06:45:42 -08:00
return !!(this.chunks.length === 2 &&
2014-11-28 06:08:33 -08:00
this.chunks[0].buf &&
2014-11-28 06:45:42 -08:00
this.chunks[1].buf);
};
2014-11-28 11:19:07 -08:00
/**
* @returns true if this is a p2sh output script
*/
Script.prototype.isScriptHashOut = function() {
2014-11-28 06:45:42 -08:00
return this.chunks.length === 3 &&
2014-11-28 06:08:33 -08:00
this.chunks[0] === Opcode('OP_HASH160').toNumber() &&
this.chunks[1].buf &&
this.chunks[1].buf.length === 20 &&
2014-11-28 06:45:42 -08:00
this.chunks[2] === Opcode('OP_EQUAL').toNumber();
};
2014-11-28 11:19:07 -08:00
/**
* @returns true if this is a p2sh input script
* Note that these are frequently indistinguishable from pubkeyhashin
*/
Script.prototype.isScriptHashIn = function() {
2014-11-28 06:45:42 -08:00
return this.chunks.every(function(chunk) {
return Buffer.isBuffer(chunk.buf);
});
};
2014-11-28 11:19:07 -08:00
/**
* @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
2014-11-28 07:57:19 -08:00
/**
* Adds a script element at the start of the script.
* @param {*} obj a string, number, Opcode, Bufer, or object to add
* @returns {Script} this script instance
*/
Script.prototype.prepend = function(obj) {
this._addByType(obj, true);
return this;
};
/**
* Adds a script element to the end of the script.
*
* @param {*} obj a string, number, Opcode, Bufer, or object to add
* @returns {Script} this script instance
*
*/
2014-11-27 12:10:35 -08:00
Script.prototype.add = function(obj) {
2014-11-28 07:57:19 -08:00
this._addByType(obj, false);
return this;
};
Script.prototype._addByType = function(obj, prepend) {
2014-11-27 12:10:35 -08:00
if (typeof obj === 'string') {
2014-11-28 07:57:19 -08:00
this._addOpcode(obj, prepend);
2014-11-27 12:10:35 -08:00
} else if (typeof obj === 'number') {
2014-11-28 07:57:19 -08:00
this._addOpcode(obj, prepend);
2014-11-27 12:10:35 -08:00
} else if (obj.constructor && obj.constructor.name && obj.constructor.name === 'Opcode') {
2014-11-28 07:57:19 -08:00
this._addOpcode(obj, prepend);
2014-11-27 12:10:35 -08:00
} else if (Buffer.isBuffer(obj)) {
2014-11-28 07:57:19 -08:00
this._addBuffer(obj, prepend);
2014-11-27 12:10:35 -08:00
} else if (typeof obj === 'object') {
2014-11-28 07:57:19 -08:00
this._insertAtPosition(obj, prepend);
2014-11-27 12:10:35 -08:00
} else {
throw new Error('Invalid script chunk');
2014-11-27 12:10:35 -08:00
}
};
2014-11-28 07:57:19 -08:00
Script.prototype._insertAtPosition = function(op, prepend) {
if (prepend) {
this.chunks.unshift(op);
} else {
this.chunks.push(op);
}
};
Script.prototype._addOpcode = function(opcode, prepend) {
var op;
2014-11-27 12:10:35 -08:00
if (typeof opcode === 'number') {
2014-11-28 07:57:19 -08:00
op = opcode;
2014-11-27 12:10:35 -08:00
} else if (opcode.constructor && opcode.constructor.name && opcode.constructor.name === 'Opcode') {
2014-11-28 07:57:19 -08:00
op = opcode.toNumber();
2014-11-27 12:10:35 -08:00
} else {
2014-11-28 07:57:19 -08:00
op = Opcode(opcode).toNumber();
2014-11-27 12:10:35 -08:00
}
2014-11-28 07:57:19 -08:00
this._insertAtPosition(op, prepend);
return this;
};
2014-11-28 07:57:19 -08:00
Script.prototype._addBuffer = function(buf, prepend) {
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;
} else {
2014-11-27 12:10:35 -08:00
throw new Error('You can\'t push that much data');
}
2014-11-28 07:57:19 -08:00
this._insertAtPosition({
buf: buf,
len: len,
opcodenum: opcodenum
2014-11-28 07:57:19 -08:00
}, prepend);
return this;
};
2014-09-01 15:45:03 -07:00
module.exports = Script;