bitcoinjs-lib/src/script.js

392 lines
10 KiB
JavaScript
Raw Normal View History

2014-03-31 11:47:47 +08:00
var Opcode = require('./opcode')
2014-04-08 22:00:28 +10:00
var crypto = require('./crypto')
2014-03-31 11:47:47 +08:00
var convert = require('./convert')
var Address = require('./address')
var network = require('./network')
2012-01-11 02:40:45 +01:00
2014-03-01 19:12:50 +01:00
var Script = function(data) {
2014-03-31 11:47:47 +08:00
this.buffer = data || []
if(!Array.isArray(this.buffer)) {
throw new Error('expect Script to be initialized with Array, but got ' + data)
}
this.parse()
}
2014-03-01 19:19:27 +01:00
Script.fromHex = function(data) {
2014-03-31 11:47:47 +08:00
return new Script(convert.hexToBytes(data))
}
2014-03-01 19:19:27 +01:00
Script.fromPubKey = function(str) {
2014-03-31 11:47:47 +08:00
var script = new Script()
var s = str.split(' ')
for (var i in s) {
if (Opcode.map.hasOwnProperty(s[i])) {
script.writeOp(Opcode.map[s[i]])
} else {
script.writeBytes(convert.hexToBytes(s[i]))
2012-01-11 02:40:45 +01:00
}
2014-03-31 11:47:47 +08:00
}
return script
}
2012-01-11 02:40:45 +01:00
Script.fromScriptSig = function(str) {
2014-03-31 11:47:47 +08:00
var script = new Script()
var s = str.split(' ')
for (var i in s) {
if (Opcode.map.hasOwnProperty(s[i])) {
script.writeOp(Opcode.map[s[i]])
} else {
script.writeBytes(convert.hexToBytes(s[i]))
}
2014-03-31 11:47:47 +08:00
}
return script
}
2012-01-11 02:40:45 +01:00
/**
* Update the parsed script representation.
*
* Each Script object stores the script in two formats. First as a raw byte
2014-03-01 19:12:50 +01:00
* 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.
*/
2014-03-01 19:12:50 +01:00
Script.prototype.parse = function() {
2014-03-31 11:47:47 +08:00
var self = this
this.chunks = []
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
// Cursor
var i = 0
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
// Read n bytes and store result as a chunk
function readChunk(n) {
self.chunks.push(self.buffer.slice(i, i + n))
i += n
}
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
while (i < this.buffer.length) {
var opcode = this.buffer[i++]
if (opcode >= 0xF0) {
// Two byte opcode
opcode = (opcode << 8) | this.buffer[i++]
}
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
var len
if (opcode > 0 && opcode < Opcode.map.OP_PUSHDATA1) {
// Read some bytes of data, opcode value is the length of data
readChunk(opcode)
} else if (opcode == Opcode.map.OP_PUSHDATA1) {
len = this.buffer[i++]
readChunk(len)
} else if (opcode == Opcode.map.OP_PUSHDATA2) {
len = (this.buffer[i++] << 8) | this.buffer[i++]
readChunk(len)
} 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)
2012-01-11 02:40:45 +01:00
}
2014-03-31 11:47:47 +08:00
}
}
2012-01-11 02:40:45 +01:00
/**
* Compare the script to known templates of scriptPubKey.
*
* This method will compare the script to a small number of standard script
* templates and return a string naming the detected type.
*
* Currently supported are:
* Address:
* Paying to a Bitcoin address which is the hash of a pubkey.
* OP_DUP OP_HASH160 [pubKeyHash] OP_EQUALVERIFY OP_CHECKSIG
*
* Pubkey:
* Paying to a public key directly.
* [pubKey] OP_CHECKSIG
2014-03-01 19:12:50 +01:00
*
* Strange:
* Any other script (no template matched).
*/
2014-03-01 19:12:50 +01:00
Script.prototype.getOutType = function() {
2014-03-31 11:47:47 +08:00
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 &&
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 {
return 'Strange'
}
}
2012-01-11 02:40:45 +01:00
/**
* Returns the address corresponding to this output in hash160 form.
* Assumes strange scripts are P2SH
*/
2014-03-01 19:12:50 +01:00
Script.prototype.toScriptHash = function() {
2014-03-31 11:47:47 +08:00
var outType = this.getOutType()
2014-03-31 11:47:47 +08:00
if (outType == 'Pubkey') {
return this.chunks[2]
}
2014-03-01 19:12:50 +01:00
2014-03-31 11:47:47 +08:00
if (outType == 'P2SH') {
return crypto.hash160(this.buffer)
2014-03-31 11:47:47 +08:00
}
return crypto.hash160(this.buffer)
2014-03-01 19:12:50 +01:00
}
//TODO: support testnet
Script.prototype.getToAddress = function() {
2014-03-31 11:47:47 +08:00
var outType = this.getOutType()
2014-03-01 19:12:50 +01:00
2014-03-31 11:47:47 +08:00
if (outType == 'Pubkey') {
return new Address(this.chunks[2])
}
2014-03-01 19:12:50 +01:00
2014-03-31 11:47:47 +08:00
if (outType == 'P2SH') {
2014-03-01 19:12:50 +01:00
return new Address(this.chunks[1], 5)
2014-03-31 11:47:47 +08:00
}
return new Address(this.chunks[1], 5)
}
//TODO: support testnet
Script.prototype.getFromAddress = function(){
2014-03-31 11:47:47 +08:00
return new Address(this.simpleInHash())
}
/**
* Compare the script to known templates of scriptSig.
*
* This method will compare the script to a small number of standard script
* templates and return a string naming the detected type.
*
* WARNING: Use this method with caution. It merely represents a heuristic
* based on common transaction formats. A non-standard transaction could
* very easily match one of these templates by accident.
*
* Currently supported are:
* Address:
* Paying to a Bitcoin address which is the hash of a pubkey.
* [sig] [pubKey]
*
* Pubkey:
* Paying to a public key directly.
* [sig]
*
* Multisig:
* Paying to M-of-N public keys.
2014-03-01 19:12:50 +01:00
*
* Strange:
* Any other script (no template matched).
*/
2014-03-01 19:12:50 +01:00
Script.prototype.getInType = function() {
2014-03-31 11:47:47 +08:00
if (this.chunks.length == 1 &&
Array.isArray(this.chunks[0])) {
// 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.
return 'Pubkey'
} else if (this.chunks.length == 2 &&
Array.isArray(this.chunks[0]) &&
Array.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 && Array.isArray(chunk) && (chunk[0] == 48 || i == this.chunks.length - 1)
}, true)) {
return 'Multisig'
} else {
return 'Strange'
}
}
2012-01-11 02:40:45 +01:00
/**
* Returns the affected public key for this input.
*
* This currently only works with payToPubKeyHash transactions. It will also
* work in the future for standard payToScriptHash transactions that use a
* single public key.
*
* However for multi-key and other complex transactions, this will only return
* one of the keys or raise an error. Therefore, it is recommended for indexing
* purposes to use Script#simpleInHash or Script#simpleOutHash instead.
*
* @deprecated
*/
2014-03-01 19:12:50 +01:00
Script.prototype.simpleInPubKey = function() {
2014-03-31 11:47:47 +08:00
switch (this.getInType()) {
2014-03-01 19:12:50 +01:00
case 'Address':
2014-03-31 11:47:47 +08:00
return this.chunks[1]
2014-03-01 19:12:50 +01:00
case 'Pubkey':
2014-03-31 11:47:47 +08:00
// 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')
2014-03-01 19:12:50 +01:00
default:
2014-03-31 11:47:47 +08:00
throw new Error('Encountered non-standard scriptSig')
}
}
2012-01-11 02:40:45 +01:00
/**
* Returns the affected address hash for this input.
*
* For standard transactions, this will return the hash of the pubKey that
* can spend this output.
*
* In the future, for standard payToScriptHash inputs, this will return the
* scriptHash.
*
* Note: This function provided for convenience. If you have the corresponding
* scriptPubKey available, you are urged to use Script#simpleOutHash instead
* as it is more reliable for non-standard payToScriptHash transactions.
*
* This method is useful for indexing transactions.
*/
2014-03-01 19:12:50 +01:00
Script.prototype.simpleInHash = function() {
return crypto.hash160(this.simpleInPubKey())
2014-03-31 11:47:47 +08:00
}
2012-01-11 10:41:52 +01:00
/**
* Old name for Script#simpleInHash.
*
* @deprecated
*/
2014-03-31 11:47:47 +08:00
Script.prototype.simpleInPubKeyHash = Script.prototype.simpleInHash
2012-01-11 02:40:45 +01:00
/**
* Add an op code to the script.
*/
2014-03-01 19:12:50 +01:00
Script.prototype.writeOp = function(opcode) {
2014-03-31 11:47:47 +08:00
this.buffer.push(opcode)
this.chunks.push(opcode)
}
2012-01-11 02:40:45 +01:00
/**
* Add a data chunk to the script.
*/
2014-03-01 19:12:50 +01:00
Script.prototype.writeBytes = function(data) {
// FIXME: Script module doesn't support buffers yet
if (Buffer.isBuffer(data)) {
data = Array.prototype.map.bind(data, function(x) { return x })()
}
2014-03-31 11:47:47 +08:00
if (data.length < Opcode.map.OP_PUSHDATA1) {
this.buffer.push(data.length)
} else if (data.length <= 0xff) {
this.buffer.push(Opcode.map.OP_PUSHDATA1)
this.buffer.push(data.length)
} else if (data.length <= 0xffff) {
this.buffer.push(Opcode.map.OP_PUSHDATA2)
this.buffer.push(data.length & 0xff)
this.buffer.push((data.length >>> 8) & 0xff)
} else {
this.buffer.push(Opcode.map.OP_PUSHDATA4)
this.buffer.push(data.length & 0xff)
this.buffer.push((data.length >>> 8) & 0xff)
this.buffer.push((data.length >>> 16) & 0xff)
this.buffer.push((data.length >>> 24) & 0xff)
}
this.buffer = this.buffer.concat(data)
this.chunks.push(data)
}
2012-01-11 02:40:45 +01:00
/**
2013-10-08 03:26:34 -04:00
* Create an output for an address
*/
2014-03-01 19:12:50 +01:00
Script.createOutputScript = function(address) {
2014-03-31 11:47:47 +08:00
var script = new Script()
address = new Address(address)
if (address.version == network.mainnet.p2shVersion ||
address.version == network.testnet.p2shVersion) {
// Standard pay-to-script-hash
script.writeOp(Opcode.map.OP_HASH160)
script.writeBytes(address.hash)
script.writeOp(Opcode.map.OP_EQUAL)
}
else {
// Standard pay-to-pubkey-hash
script.writeOp(Opcode.map.OP_DUP)
script.writeOp(Opcode.map.OP_HASH160)
script.writeBytes(address.hash)
script.writeOp(Opcode.map.OP_EQUALVERIFY)
script.writeOp(Opcode.map.OP_CHECKSIG)
}
return script
}
2012-01-11 10:41:52 +01:00
/**
2013-10-08 03:26:34 -04:00
* Extract pubkeys from a multisig script
*/
2013-10-08 03:26:34 -04:00
Script.prototype.extractPubkeys = function() {
2014-03-31 11:47:47 +08:00
return this.chunks.filter(function(chunk) {
return(chunk[0] == 4 && chunk.length == 65 || chunk[0] < 4 && chunk.length == 33)
})
2013-10-08 03:26:34 -04:00
}
2012-01-11 02:40:45 +01:00
/**
* Create an m-of-n output script
*/
2014-03-01 19:12:50 +01:00
Script.createMultiSigOutputScript = function(m, pubkeys) {
2014-03-31 11:47:47 +08:00
var script = new Script()
pubkeys = pubkeys.sort()
2014-03-01 19:12:50 +01:00
2014-03-31 11:47:47 +08:00
script.writeOp(Opcode.map.OP_1 + m - 1)
for (var i = 0; i < pubkeys.length; ++i) {
script.writeBytes(pubkeys[i])
}
script.writeOp(Opcode.map.OP_1 + pubkeys.length - 1)
script.writeOp(Opcode.map.OP_CHECKMULTISIG)
2014-03-01 19:12:50 +01:00
2014-03-31 11:47:47 +08:00
return script
}
2012-02-05 16:04:45 +00:00
/**
* Create a standard payToPubKeyHash input.
*/
2014-03-01 19:12:50 +01:00
Script.createInputScript = function(signature, pubKey) {
2014-03-31 11:47:47 +08:00
var script = new Script()
script.writeBytes(signature)
script.writeBytes(pubKey)
return script
}
2012-01-11 02:40:45 +01:00
/**
* Create a multisig input
*/
2014-03-01 19:12:50 +01:00
Script.createMultiSigInputScript = function(signatures, script) {
2014-03-31 11:47:47 +08:00
script = new Script(script)
var k = script.chunks[0][0]
//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)
})
inScript.writeBytes(script.buffer)
return inScript
}
2014-03-01 19:12:50 +01:00
Script.prototype.clone = function() {
2014-03-31 11:47:47 +08:00
return new Script(this.buffer)
}
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
module.exports = Script