Browse Source

Merge pull request #44 from justcoin/tidy-scripts

Tidy up script.js
hk-custom-address
Kyle Drake 11 years ago
parent
commit
e7de3540d1
  1. 125
      src/script.js

125
src/script.js

@ -3,27 +3,20 @@ var util = require('./util');
var conv = require('./convert');
var Address = require('./address');
var Script = function (data) {
if (!data) {
this.buffer = [];
} else if ("string" == typeof data) {
this.buffer = conv.hexToBytes(data);
} else if (util.isArray(data)) {
this.buffer = data;
} else if (data instanceof Script) {
this.buffer = data.buffer;
} else {
throw new Error("Invalid script");
}
var Script = function(data) {
this.buffer = data || [];
this.parse();
};
Script.fromHex = function(data) {
return new Script(conv.hexToBytes(data))
};
Script.fromPubKey = function(str) {
var script = new Script();
var s = str.split(" ");
var s = str.split(' ');
for (var i in s) {
if (Opcode.map.hasOwnProperty(s[i])){
if (Opcode.map.hasOwnProperty(s[i])) {
script.writeOp(Opcode.map[s[i]]);
} else {
script.writeBytes(conv.hexToBytes(s[i]));
@ -34,9 +27,9 @@ Script.fromPubKey = function(str) {
Script.fromScriptSig = function(str) {
var script = new Script();
var s = str.split(" ");
var s = str.split(' ');
for (var i in s) {
if (Opcode.map.hasOwnProperty(s[i])){
if (Opcode.map.hasOwnProperty(s[i])) {
script.writeOp(Opcode.map[s[i]]);
} else {
script.writeBytes(conv.hexToBytes(s[i]));
@ -49,14 +42,14 @@ Script.fromScriptSig = function(str) {
* Update the parsed script representation.
*
* 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.
*
* 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
* the script buffer manually, you should update the chunks using this method.
*/
Script.prototype.parse = function () {
Script.prototype.parse = function() {
var self = this;
this.chunks = [];
@ -68,7 +61,7 @@ Script.prototype.parse = function () {
function readChunk(n) {
self.chunks.push(self.buffer.slice(i, i + n));
i += n;
};
}
while (i < this.buffer.length) {
var opcode = this.buffer[i++];
@ -117,22 +110,20 @@ Script.prototype.parse = function () {
* Strange:
* Any other script (no template matched).
*/
Script.prototype.getOutType = function () {
if (this.chunks[this.chunks.length-1] == Opcode.map.OP_EQUAL &&
Script.prototype.getOutType = function() {
if (this.chunks[this.chunks.length - 1] == Opcode.map.OP_EQUAL &&
this.chunks[0] == Opcode.map.OP_HASH160 &&
this.chunks.length == 3) {
// Transfer to M-OF-N
return 'P2SH';
}
else if (this.chunks.length == 5 &&
} else if (this.chunks.length == 5 &&
this.chunks[0] == Opcode.map.OP_DUP &&
this.chunks[1] == Opcode.map.OP_HASH160 &&
this.chunks[3] == Opcode.map.OP_EQUALVERIFY &&
this.chunks[4] == Opcode.map.OP_CHECKSIG) {
// Transfer to Bitcoin address
return 'Pubkey';
}
else {
} else {
return 'Strange';
}
}
@ -141,19 +132,32 @@ Script.prototype.getOutType = function () {
* Returns the address corresponding to this output in hash160 form.
* Assumes strange scripts are P2SH
*/
Script.prototype.toScriptHash = function () {
Script.prototype.toScriptHash = function() {
var outType = this.getOutType();
return outType == 'Pubkey' ? this.chunks[2]
: outType == 'P2SH' ? util.sha256ripe160(this.buffer)
: util.sha256ripe160(this.buffer)
};
if (outType == 'Pubkey') {
return this.chunks[2]
}
if (outType == 'P2SH') {
return util.sha256ripe160(this.buffer)
}
return util.sha256ripe160(this.buffer)
}
Script.prototype.toAddress = function() {
var outType = this.getOutType();
return outType == 'Pubkey' ? new Address(this.chunks[2])
: outType == 'P2SH' ? new Address(this.chunks[1],5)
: new Address(this.chunks[1],5)
if (outType == 'Pubkey') {
return new Address(this.chunks[2])
}
if (outType == 'P2SH') {
return new Address(this.chunks[1], 5)
}
return new Address(this.chunks[1], 5)
}
/**
@ -181,8 +185,7 @@ Script.prototype.toAddress = function() {
* Strange:
* Any other script (no template matched).
*/
Script.prototype.getInType = function ()
{
Script.prototype.getInType = function() {
if (this.chunks.length == 1 &&
util.isArray(this.chunks[0])) {
// Direct IP to IP transactions only have the signature in their scriptSig.
@ -193,10 +196,9 @@ Script.prototype.getInType = function ()
util.isArray(this.chunks[1])) {
return 'Address';
} else if (this.chunks[0] == Opcode.map.OP_0 &&
this.chunks.slice(1).reduce(function(t,chunk,i) {
return t && util.isArray(chunk)
&& (chunk[0] == 48 || i == this.chunks.length - 1);
},true)) {
this.chunks.slice(1).reduce(function(t, chunk, i) {
return t && util.isArray(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1);
}, true)) {
return 'Multisig';
} else {
return 'Strange';
@ -216,17 +218,16 @@ Script.prototype.getInType = function ()
*
* @deprecated
*/
Script.prototype.simpleInPubKey = function ()
{
Script.prototype.simpleInPubKey = function() {
switch (this.getInType()) {
case 'Address':
return this.chunks[1];
case 'Pubkey':
// TODO: Theoretically, we could recover the pubkey from the sig here.
// See https://bitcointalk.org/?topic=6430.0
throw new Error("Script does not contain pubkey.");
throw new Error('Script does not contain pubkey');
default:
throw new Error("Encountered non-standard scriptSig");
throw new Error('Encountered non-standard scriptSig');
}
};
@ -245,8 +246,7 @@ Script.prototype.simpleInPubKey = function ()
*
* This method is useful for indexing transactions.
*/
Script.prototype.simpleInHash = function ()
{
Script.prototype.simpleInHash = function() {
return util.sha256ripe160(this.simpleInPubKey());
};
@ -260,8 +260,7 @@ Script.prototype.simpleInPubKeyHash = Script.prototype.simpleInHash;
/**
* Add an op code to the script.
*/
Script.prototype.writeOp = function (opcode)
{
Script.prototype.writeOp = function(opcode) {
this.buffer.push(opcode);
this.chunks.push(opcode);
};
@ -269,8 +268,7 @@ Script.prototype.writeOp = function (opcode)
/**
* Add a data chunk to the script.
*/
Script.prototype.writeBytes = function (data)
{
Script.prototype.writeBytes = function(data) {
if (data.length < Opcode.map.OP_PUSHDATA1) {
this.buffer.push(data.length);
} else if (data.length <= 0xff) {
@ -294,8 +292,7 @@ Script.prototype.writeBytes = function (data)
/**
* Create an output for an address
*/
Script.createOutputScript = function (address)
{
Script.createOutputScript = function(address) {
var script = new Script();
address = new Address(address);
// Standard pay-to-pubkey-hash
@ -321,16 +318,14 @@ Script.createOutputScript = function (address)
Script.prototype.extractPubkeys = function() {
return this.chunks.filter(function(chunk) {
return (chunk[0] == 4 && chunk.length == 65
|| chunk[0] < 4 && chunk.length == 33)
return(chunk[0] == 4 && chunk.length == 65 || chunk[0] < 4 && chunk.length == 33)
});
}
/**
* Create an m-of-n output script
*/
Script.createMultiSigOutputScript = function (m, pubkeys)
{
Script.createMultiSigOutputScript = function(m, pubkeys) {
var script = new Script();
pubkeys = pubkeys.sort();
@ -351,8 +346,7 @@ Script.createMultiSigOutputScript = function (m, pubkeys)
/**
* Create a standard payToPubKeyHash input.
*/
Script.createInputScript = function (signature, pubKey)
{
Script.createInputScript = function(signature, pubKey) {
var script = new Script();
script.writeBytes(signature);
script.writeBytes(pubKey);
@ -362,20 +356,23 @@ Script.createInputScript = function (signature, pubKey)
/**
* Create a multisig input
*/
Script.createMultiSigInputScript = function(signatures, script)
{
Script.createMultiSigInputScript = function(signatures, script) {
script = new Script(script);
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();
inScript.writeOp(Opcode.map.OP_0);
signatures.map(function(sig) { inScript.writeBytes(sig) });
signatures.map(function(sig) {
inScript.writeBytes(sig)
});
inScript.writeBytes(script.buffer);
return inScript;
}
Script.prototype.clone = function ()
{
Script.prototype.clone = function() {
return new Script(this.buffer);
};

Loading…
Cancel
Save