Browse Source

Clean up script.js formatting. ref #41

hk-custom-address
Andreas Brekken 11 years ago
parent
commit
354d31a297
  1. 394
      src/script.js

394
src/script.js

@ -3,100 +3,100 @@ var util = require('./util');
var conv = require('./convert'); var conv = require('./convert');
var Address = require('./address'); var Address = require('./address');
var Script = function (data) { var Script = function(data) {
if (!data) { if (!data) {
this.buffer = []; this.buffer = [];
} else if ("string" == typeof data) { } else if ('string' == typeof data) {
this.buffer = conv.hexToBytes(data); this.buffer = conv.hexToBytes(data);
} else if (util.isArray(data)) { } else if (util.isArray(data)) {
this.buffer = data; this.buffer = data;
} else if (data instanceof Script) { } else if (data instanceof Script) {
this.buffer = data.buffer; this.buffer = data.buffer;
} else { } else {
throw new Error("Invalid script"); throw new Error('Invalid script');
} }
this.parse(); this.parse();
}; };
Script.fromPubKey = function(str) { Script.fromPubKey = function(str) {
var script = new Script(); var script = new Script();
var s = str.split(" "); var s = str.split(' ');
for (var i in s) { for (var i in s) {
if (Opcode.map.hasOwnProperty(s[i])){ if (Opcode.map.hasOwnProperty(s[i])) {
script.writeOp(Opcode.map[s[i]]); script.writeOp(Opcode.map[s[i]]);
} else { } else {
script.writeBytes(conv.hexToBytes(s[i])); script.writeBytes(conv.hexToBytes(s[i]));
}
} }
} return script;
return script;
}; };
Script.fromScriptSig = function(str) { Script.fromScriptSig = function(str) {
var script = new Script(); var script = new Script();
var s = str.split(" "); var s = str.split(' ');
for (var i in s) { for (var i in s) {
if (Opcode.map.hasOwnProperty(s[i])){ if (Opcode.map.hasOwnProperty(s[i])) {
script.writeOp(Opcode.map[s[i]]); script.writeOp(Opcode.map[s[i]]);
} else { } else {
script.writeBytes(conv.hexToBytes(s[i])); script.writeBytes(conv.hexToBytes(s[i]));
}
} }
} return script;
return script;
}; };
/** /**
* Update the parsed script representation. * Update the parsed script representation.
* *
* Each Script object stores the script in two formats. First as a raw byte * Each Script object stores the script in two formats. First as a raw byte
* array and second as an array of "chunks", such as opcodes and pieces of * array and second as an array of 'chunks', such as opcodes and pieces of
* data. * data.
* *
* This method updates the chunks cache. Normally this is called by the * This method updates the chunks cache. Normally this is called by the
* constructor and you don't need to worry about it. However, if you change * constructor and you don't need to worry about it. However, if you change
* the script buffer manually, you should update the chunks using this method. * the script buffer manually, you should update the chunks using this method.
*/ */
Script.prototype.parse = function () { Script.prototype.parse = function() {
var self = this; var self = this;
this.chunks = [];
// Cursor this.chunks = [];
var i = 0;
// Read n bytes and store result as a chunk // Cursor
function readChunk(n) { var i = 0;
self.chunks.push(self.buffer.slice(i, i + n));
i += n;
};
while (i < this.buffer.length) { // Read n bytes and store result as a chunk
var opcode = this.buffer[i++]; function readChunk(n) {
if (opcode >= 0xF0) { self.chunks.push(self.buffer.slice(i, i + n));
// Two byte opcode i += n;
opcode = (opcode << 8) | this.buffer[i++];
} }
var len; while (i < this.buffer.length) {
if (opcode > 0 && opcode < Opcode.map.OP_PUSHDATA1) { var opcode = this.buffer[i++];
// Read some bytes of data, opcode value is the length of data if (opcode >= 0xF0) {
readChunk(opcode); // Two byte opcode
} else if (opcode == Opcode.map.OP_PUSHDATA1) { opcode = (opcode << 8) | this.buffer[i++];
len = this.buffer[i++]; }
readChunk(len);
} else if (opcode == Opcode.map.OP_PUSHDATA2) { var len;
len = (this.buffer[i++] << 8) | this.buffer[i++]; if (opcode > 0 && opcode < Opcode.map.OP_PUSHDATA1) {
readChunk(len); // Read some bytes of data, opcode value is the length of data
} else if (opcode == Opcode.map.OP_PUSHDATA4) { readChunk(opcode);
len = (this.buffer[i++] << 24) | } else if (opcode == Opcode.map.OP_PUSHDATA1) {
(this.buffer[i++] << 16) | len = this.buffer[i++];
(this.buffer[i++] << 8) | readChunk(len);
this.buffer[i++]; } else if (opcode == Opcode.map.OP_PUSHDATA2) {
readChunk(len); len = (this.buffer[i++] << 8) | this.buffer[i++];
} else { readChunk(len);
this.chunks.push(opcode); } else if (opcode == Opcode.map.OP_PUSHDATA4) {
len = (this.buffer[i++] << 24) |
(this.buffer[i++] << 16) |
(this.buffer[i++] << 8) |
this.buffer[i++];
readChunk(len);
} else {
this.chunks.push(opcode);
}
} }
}
}; };
/** /**
@ -113,47 +113,58 @@ Script.prototype.parse = function () {
* Pubkey: * Pubkey:
* Paying to a public key directly. * Paying to a public key directly.
* [pubKey] OP_CHECKSIG * [pubKey] OP_CHECKSIG
* *
* Strange: * Strange:
* Any other script (no template matched). * Any other script (no template matched).
*/ */
Script.prototype.getOutType = function () { Script.prototype.getOutType = function() {
if (this.chunks[this.chunks.length-1] == Opcode.map.OP_EQUAL && if (this.chunks[this.chunks.length - 1] == Opcode.map.OP_EQUAL &&
this.chunks[0] == Opcode.map.OP_HASH160 && this.chunks[0] == Opcode.map.OP_HASH160 &&
this.chunks.length == 3) { this.chunks.length == 3) {
// Transfer to M-OF-N // Transfer to M-OF-N
return 'P2SH'; return 'P2SH';
} } else if (this.chunks.length == 5 &&
else if (this.chunks.length == 5 &&
this.chunks[0] == Opcode.map.OP_DUP && this.chunks[0] == Opcode.map.OP_DUP &&
this.chunks[1] == Opcode.map.OP_HASH160 && this.chunks[1] == Opcode.map.OP_HASH160 &&
this.chunks[3] == Opcode.map.OP_EQUALVERIFY && this.chunks[3] == Opcode.map.OP_EQUALVERIFY &&
this.chunks[4] == Opcode.map.OP_CHECKSIG) { this.chunks[4] == Opcode.map.OP_CHECKSIG) {
// Transfer to Bitcoin address // Transfer to Bitcoin address
return 'Pubkey'; return 'Pubkey';
} } else {
else {
return 'Strange'; return 'Strange';
} }
} }
/** /**
* Returns the address corresponding to this output in hash160 form. * Returns the address corresponding to this output in hash160 form.
* Assumes strange scripts are P2SH * Assumes strange scripts are P2SH
*/ */
Script.prototype.toScriptHash = function () { Script.prototype.toScriptHash = function() {
var outType = this.getOutType(); var outType = this.getOutType();
return outType == 'Pubkey' ? this.chunks[2] if (outType == 'Pubkey') {
: outType == 'P2SH' ? util.sha256ripe160(this.buffer) return this.chunks[2]
: util.sha256ripe160(this.buffer) }
};
if (outType == 'P2SH') {
return util.sha256ripe160(this.buffer)
}
return util.sha256ripe160(this.buffer)
}
Script.prototype.toAddress = function() { Script.prototype.toAddress = function() {
var outType = this.getOutType(); var outType = this.getOutType();
return outType == 'Pubkey' ? new Address(this.chunks[2])
: outType == 'P2SH' ? new Address(this.chunks[1],5) if (outType == 'Pubkey') {
: new Address(this.chunks[1],5) return new Address(this.chunks[2])
}
if (outType == 'P2SH') {
return new Address(this.chunks[1], 5)
}
return new Address(this.chunks[1], 5)
} }
/** /**
@ -177,30 +188,28 @@ Script.prototype.toAddress = function() {
* *
* Multisig: * Multisig:
* Paying to M-of-N public keys. * Paying to M-of-N public keys.
* *
* Strange: * Strange:
* Any other script (no template matched). * Any other script (no template matched).
*/ */
Script.prototype.getInType = function () Script.prototype.getInType = function() {
{ if (this.chunks.length == 1 &&
if (this.chunks.length == 1 && util.isArray(this.chunks[0])) {
util.isArray(this.chunks[0])) { // Direct IP to IP transactions only have the signature in their scriptSig.
// Direct IP to IP transactions only have the signature in their scriptSig. // TODO: We could also check that the length of the data is correct.
// TODO: We could also check that the length of the data is correct. return 'Pubkey';
return 'Pubkey'; } else if (this.chunks.length == 2 &&
} else if (this.chunks.length == 2 && util.isArray(this.chunks[0]) &&
util.isArray(this.chunks[0]) && util.isArray(this.chunks[1])) {
util.isArray(this.chunks[1])) { return 'Address';
return 'Address'; } else if (this.chunks[0] == Opcode.map.OP_0 &&
} else if (this.chunks[0] == Opcode.map.OP_0 && this.chunks.slice(1).reduce(function(t, chunk, i) {
this.chunks.slice(1).reduce(function(t,chunk,i) { return t && util.isArray(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1);
return t && util.isArray(chunk) }, true)) {
&& (chunk[0] == 48 || i == this.chunks.length - 1); return 'Multisig';
},true)) { } else {
return 'Multisig'; return 'Strange';
} else { }
return 'Strange';
}
}; };
/** /**
@ -216,18 +225,17 @@ Script.prototype.getInType = function ()
* *
* @deprecated * @deprecated
*/ */
Script.prototype.simpleInPubKey = function () Script.prototype.simpleInPubKey = function() {
{ switch (this.getInType()) {
switch (this.getInType()) { case 'Address':
case 'Address': return this.chunks[1];
return this.chunks[1]; case 'Pubkey':
case 'Pubkey': // TODO: Theoretically, we could recover the pubkey from the sig here.
// TODO: Theoretically, we could recover the pubkey from the sig here. // See https://bitcointalk.org/?topic=6430.0
// See https://bitcointalk.org/?topic=6430.0 throw new Error('Script does not contain pubkey');
throw new Error("Script does not contain pubkey."); default:
default: throw new Error('Encountered non-standard scriptSig');
throw new Error("Encountered non-standard scriptSig"); }
}
}; };
/** /**
@ -245,9 +253,8 @@ Script.prototype.simpleInPubKey = function ()
* *
* This method is useful for indexing transactions. * This method is useful for indexing transactions.
*/ */
Script.prototype.simpleInHash = function () Script.prototype.simpleInHash = function() {
{ return util.sha256ripe160(this.simpleInPubKey());
return util.sha256ripe160(this.simpleInPubKey());
}; };
/** /**
@ -260,59 +267,56 @@ Script.prototype.simpleInPubKeyHash = Script.prototype.simpleInHash;
/** /**
* Add an op code to the script. * Add an op code to the script.
*/ */
Script.prototype.writeOp = function (opcode) Script.prototype.writeOp = function(opcode) {
{ this.buffer.push(opcode);
this.buffer.push(opcode); this.chunks.push(opcode);
this.chunks.push(opcode);
}; };
/** /**
* Add a data chunk to the script. * Add a data chunk to the script.
*/ */
Script.prototype.writeBytes = function (data) Script.prototype.writeBytes = function(data) {
{ if (data.length < Opcode.map.OP_PUSHDATA1) {
if (data.length < Opcode.map.OP_PUSHDATA1) { this.buffer.push(data.length);
this.buffer.push(data.length); } else if (data.length <= 0xff) {
} else if (data.length <= 0xff) { this.buffer.push(Opcode.map.OP_PUSHDATA1);
this.buffer.push(Opcode.map.OP_PUSHDATA1); this.buffer.push(data.length);
this.buffer.push(data.length); } else if (data.length <= 0xffff) {
} else if (data.length <= 0xffff) { this.buffer.push(Opcode.map.OP_PUSHDATA2);
this.buffer.push(Opcode.map.OP_PUSHDATA2); this.buffer.push(data.length & 0xff);
this.buffer.push(data.length & 0xff); this.buffer.push((data.length >>> 8) & 0xff);
this.buffer.push((data.length >>> 8) & 0xff); } else {
} else { this.buffer.push(Opcode.map.OP_PUSHDATA4);
this.buffer.push(Opcode.map.OP_PUSHDATA4); this.buffer.push(data.length & 0xff);
this.buffer.push(data.length & 0xff); this.buffer.push((data.length >>> 8) & 0xff);
this.buffer.push((data.length >>> 8) & 0xff); this.buffer.push((data.length >>> 16) & 0xff);
this.buffer.push((data.length >>> 16) & 0xff); this.buffer.push((data.length >>> 24) & 0xff);
this.buffer.push((data.length >>> 24) & 0xff); }
} this.buffer = this.buffer.concat(data);
this.buffer = this.buffer.concat(data); this.chunks.push(data);
this.chunks.push(data);
}; };
/** /**
* Create an output for an address * Create an output for an address
*/ */
Script.createOutputScript = function (address) Script.createOutputScript = function(address) {
{ var script = new Script();
var script = new Script(); address = new Address(address);
address = new Address(address); // Standard pay-to-pubkey-hash
// Standard pay-to-pubkey-hash if (!address.version) {
if (!address.version) { script.writeOp(Opcode.map.OP_DUP);
script.writeOp(Opcode.map.OP_DUP); script.writeOp(Opcode.map.OP_HASH160);
script.writeOp(Opcode.map.OP_HASH160); script.writeBytes(address.hash);
script.writeBytes(address.hash); script.writeOp(Opcode.map.OP_EQUALVERIFY);
script.writeOp(Opcode.map.OP_EQUALVERIFY); script.writeOp(Opcode.map.OP_CHECKSIG);
script.writeOp(Opcode.map.OP_CHECKSIG); }
} // Standard pay-to-script-hash
// Standard pay-to-script-hash else {
else { script.writeOp(Opcode.map.OP_HASH160);
script.writeOp(Opcode.map.OP_HASH160); script.writeBytes(address.hash);
script.writeBytes(address.hash); script.writeOp(Opcode.map.OP_EQUAL);
script.writeOp(Opcode.map.OP_EQUAL); }
} return script;
return script;
}; };
/** /**
@ -321,62 +325,62 @@ Script.createOutputScript = function (address)
Script.prototype.extractPubkeys = function() { Script.prototype.extractPubkeys = function() {
return this.chunks.filter(function(chunk) { return this.chunks.filter(function(chunk) {
return (chunk[0] == 4 && chunk.length == 65 return(chunk[0] == 4 && chunk.length == 65 || chunk[0] < 4 && chunk.length == 33)
|| chunk[0] < 4 && chunk.length == 33)
}); });
} }
/** /**
* Create an m-of-n output script * Create an m-of-n output script
*/ */
Script.createMultiSigOutputScript = function (m, pubkeys) Script.createMultiSigOutputScript = function(m, pubkeys) {
{ var script = new Script();
var script = new Script();
pubkeys = pubkeys.sort();
pubkeys = pubkeys.sort();
script.writeOp(Opcode.map.OP_1 + m - 1);
script.writeOp(Opcode.map.OP_1 + m - 1);
for (var i = 0; i < pubkeys.length; ++i) {
for (var i = 0; i < pubkeys.length; ++i) { script.writeBytes(pubkeys[i]);
script.writeBytes(pubkeys[i]); }
}
script.writeOp(Opcode.map.OP_1 + pubkeys.length - 1);
script.writeOp(Opcode.map.OP_1 + pubkeys.length - 1);
script.writeOp(Opcode.map.OP_CHECKMULTISIG);
script.writeOp(Opcode.map.OP_CHECKMULTISIG);
return script;
return script;
}; };
/** /**
* Create a standard payToPubKeyHash input. * Create a standard payToPubKeyHash input.
*/ */
Script.createInputScript = function (signature, pubKey) Script.createInputScript = function(signature, pubKey) {
{ var script = new Script();
var script = new Script(); script.writeBytes(signature);
script.writeBytes(signature); script.writeBytes(pubKey);
script.writeBytes(pubKey); return script;
return script;
}; };
/** /**
* Create a multisig input * Create a multisig input
*/ */
Script.createMultiSigInputScript = function(signatures, script) Script.createMultiSigInputScript = function(signatures, script) {
{
script = new Script(script); script = new Script(script);
var k = script.chunks[0][0]; var k = script.chunks[0][0];
if (signatures.length < k) return false; //Not enough sigs
//Not enough sigs
if (signatures.length < k) return false;
var inScript = new Script(); var inScript = new Script();
inScript.writeOp(Opcode.map.OP_0); inScript.writeOp(Opcode.map.OP_0);
signatures.map(function(sig) { inScript.writeBytes(sig) }); signatures.map(function(sig) {
inScript.writeBytes(sig)
});
inScript.writeBytes(script.buffer); inScript.writeBytes(script.buffer);
return inScript; return inScript;
} }
Script.prototype.clone = function () Script.prototype.clone = function() {
{ return new Script(this.buffer);
return new Script(this.buffer);
}; };
module.exports = Script; module.exports = Script;

Loading…
Cancel
Save