bitcoinjs-lib/src/script.js

135 lines
2.7 KiB
JavaScript
Raw Normal View History

var assert = require('assert')
2014-05-28 19:36:33 +10:00
var bufferutils = require('./bufferutils')
2014-04-08 22:00:28 +10:00
var crypto = require('./crypto')
2014-12-23 15:08:20 +11:00
var typeForce = require('typeforce')
2014-05-13 23:14:07 +10:00
var opcodes = require('./opcodes')
2012-01-11 02:40:45 +01:00
function Script(buffer, chunks) {
2014-12-23 15:08:20 +11:00
typeForce('Buffer', buffer)
typeForce('Array', chunks)
2014-05-05 13:07:19 +10:00
this.buffer = buffer
this.chunks = chunks
2014-03-31 11:47:47 +08:00
}
// Import operations
2014-06-25 15:44:15 +10:00
Script.fromASM = function(asm) {
var strChunks = asm.split(' ')
var chunks = strChunks.map(function(strChunk) {
if (strChunk in opcodes) {
return opcodes[strChunk]
} else {
return new Buffer(strChunk, 'hex')
}
})
return Script.fromChunks(chunks)
}
2014-05-02 06:25:57 +10:00
Script.fromBuffer = function(buffer) {
var chunks = []
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
var i = 0
2012-01-11 02:40:45 +01:00
while (i < buffer.length) {
var opcode = buffer.readUInt8(i)
if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) {
var d = bufferutils.readPushDataInt(buffer, i)
i += d.size
var data = buffer.slice(i, i + d.number)
i += d.number
chunks.push(data)
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
} else {
chunks.push(opcode)
i += 1
2012-01-11 02:40:45 +01:00
}
2014-03-31 11:47:47 +08:00
}
return new Script(buffer, chunks)
}
Script.fromChunks = function(chunks) {
2014-12-23 15:08:20 +11:00
typeForce('Array', chunks)
var bufferSize = chunks.reduce(function(accum, chunk) {
if (Buffer.isBuffer(chunk)) {
2014-06-13 09:36:11 +10:00
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
}
2014-06-13 09:36:11 +10:00
return accum + 1
}, 0.0)
var buffer = new Buffer(bufferSize)
var offset = 0
chunks.forEach(function(chunk) {
if (Buffer.isBuffer(chunk)) {
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)
chunk.copy(buffer, offset)
offset += chunk.length
} else {
buffer.writeUInt8(chunk, offset)
offset += 1
}
})
assert.equal(offset, buffer.length, 'Could not decode chunks')
return new Script(buffer, chunks)
}
Script.fromHex = function(hex) {
return Script.fromBuffer(new Buffer(hex, 'hex'))
}
// Constants
Script.EMPTY = Script.fromChunks([])
// Operations
Script.prototype.getHash = function() {
return crypto.hash160(this.buffer)
}
// FIXME: doesn't work for data chunks, maybe time to use buffertools.compare...
Script.prototype.without = function(needle) {
return Script.fromChunks(this.chunks.filter(function(op) {
return op !== needle
}))
}
// Export operations
2014-06-25 15:44:15 +10:00
var reverseOps = []
for (var op in opcodes) {
var code = opcodes[op]
reverseOps[code] = op
}
Script.prototype.toASM = function() {
return this.chunks.map(function(chunk) {
if (Buffer.isBuffer(chunk)) {
return chunk.toString('hex')
} else {
return reverseOps[chunk]
}
}).join(' ')
}
Script.prototype.toBuffer = function() {
return this.buffer
}
Script.prototype.toHex = function() {
return this.toBuffer().toString('hex')
2014-03-31 11:47:47 +08:00
}
2012-01-11 02:40:45 +01:00
2014-03-31 11:47:47 +08:00
module.exports = Script