|
|
@ -1,12 +1,120 @@ |
|
|
|
var ops = require('./opcodes') |
|
|
|
var bufferutils = require('./bufferutils') |
|
|
|
var typeforce = require('typeforce') |
|
|
|
var types = require('./types') |
|
|
|
|
|
|
|
var ECSignature = require('./ecsignature') |
|
|
|
var ecurve = require('ecurve') |
|
|
|
var curve = ecurve.getCurveByName('secp256k1') |
|
|
|
|
|
|
|
var ECSignature = require('./ecsignature') |
|
|
|
var Script = require('./script') |
|
|
|
var OPS = require('./opcodes') |
|
|
|
var REVERSE_OPS = [] |
|
|
|
for (var op in OPS) { |
|
|
|
var code = OPS[op] |
|
|
|
REVERSE_OPS[code] = op |
|
|
|
} |
|
|
|
|
|
|
|
function toASM (chunks) { |
|
|
|
if (types.Buffer(chunks)) { |
|
|
|
chunks = decompile(chunks) |
|
|
|
} |
|
|
|
|
|
|
|
return chunks.map(function (chunk) { |
|
|
|
// data?
|
|
|
|
if (Buffer.isBuffer(chunk)) return chunk.toString('hex') |
|
|
|
|
|
|
|
// opcode!
|
|
|
|
return REVERSE_OPS[chunk] |
|
|
|
}).join(' ') |
|
|
|
} |
|
|
|
|
|
|
|
function fromASM (asm) { |
|
|
|
typeforce(types.String, asm) |
|
|
|
|
|
|
|
return compile(asm.split(' ').map(function (chunkStr) { |
|
|
|
// opcode?
|
|
|
|
if (OPS[chunkStr] !== undefined) return OPS[chunkStr] |
|
|
|
|
|
|
|
// data!
|
|
|
|
return new Buffer(chunkStr, 'hex') |
|
|
|
})) |
|
|
|
} |
|
|
|
|
|
|
|
function compile (chunks) { |
|
|
|
// TODO: remove me
|
|
|
|
if (types.Buffer(chunks)) return chunks |
|
|
|
|
|
|
|
typeforce(types.Array, chunks) |
|
|
|
|
|
|
|
var bufferSize = chunks.reduce(function (accum, chunk) { |
|
|
|
// data chunk
|
|
|
|
if (Buffer.isBuffer(chunk)) { |
|
|
|
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length |
|
|
|
} |
|
|
|
|
|
|
|
// opcode
|
|
|
|
return accum + 1 |
|
|
|
}, 0.0) |
|
|
|
|
|
|
|
var buffer = new Buffer(bufferSize) |
|
|
|
var offset = 0 |
|
|
|
|
|
|
|
chunks.forEach(function (chunk) { |
|
|
|
// data chunk
|
|
|
|
if (Buffer.isBuffer(chunk)) { |
|
|
|
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset) |
|
|
|
|
|
|
|
chunk.copy(buffer, offset) |
|
|
|
offset += chunk.length |
|
|
|
|
|
|
|
// opcode
|
|
|
|
} else { |
|
|
|
buffer.writeUInt8(chunk, offset) |
|
|
|
offset += 1 |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
if (offset !== buffer.length) throw new Error('Could not decode chunks') |
|
|
|
return buffer |
|
|
|
} |
|
|
|
|
|
|
|
function decompile (buffer) { |
|
|
|
// TODO: remove me
|
|
|
|
if (types.Array(buffer)) return buffer |
|
|
|
|
|
|
|
typeforce(types.Buffer, buffer) |
|
|
|
|
|
|
|
var chunks = [] |
|
|
|
var i = 0 |
|
|
|
|
|
|
|
while (i < buffer.length) { |
|
|
|
var opcode = buffer.readUInt8(i) |
|
|
|
|
|
|
|
// data chunk
|
|
|
|
if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) { |
|
|
|
var d = bufferutils.readPushDataInt(buffer, i) |
|
|
|
|
|
|
|
// did reading a pushDataInt fail? empty script
|
|
|
|
if (d === null) return [] |
|
|
|
i += d.size |
|
|
|
|
|
|
|
// attempt to read too much data? empty script
|
|
|
|
if (i + d.number > buffer.length) return [] |
|
|
|
|
|
|
|
var data = buffer.slice(i, i + d.number) |
|
|
|
i += d.number |
|
|
|
|
|
|
|
chunks.push(data) |
|
|
|
|
|
|
|
// opcode
|
|
|
|
} else { |
|
|
|
chunks.push(opcode) |
|
|
|
|
|
|
|
i += 1 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return chunks |
|
|
|
} |
|
|
|
|
|
|
|
function isCanonicalPubKey (buffer) { |
|
|
|
if (!Buffer.isBuffer(buffer)) return false |
|
|
@ -41,110 +149,124 @@ function isCanonicalSignature (buffer) { |
|
|
|
} |
|
|
|
|
|
|
|
function isPubKeyHashInput (script) { |
|
|
|
return script.chunks.length === 2 && |
|
|
|
isCanonicalSignature(script.chunks[0]) && |
|
|
|
isCanonicalPubKey(script.chunks[1]) |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
return chunks.length === 2 && |
|
|
|
isCanonicalSignature(chunks[0]) && |
|
|
|
isCanonicalPubKey(chunks[1]) |
|
|
|
} |
|
|
|
|
|
|
|
function isPubKeyHashOutput (script) { |
|
|
|
return script.chunks.length === 5 && |
|
|
|
script.chunks[0] === ops.OP_DUP && |
|
|
|
script.chunks[1] === ops.OP_HASH160 && |
|
|
|
Buffer.isBuffer(script.chunks[2]) && |
|
|
|
script.chunks[2].length === 20 && |
|
|
|
script.chunks[3] === ops.OP_EQUALVERIFY && |
|
|
|
script.chunks[4] === ops.OP_CHECKSIG |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
return chunks.length === 5 && |
|
|
|
chunks[0] === OPS.OP_DUP && |
|
|
|
chunks[1] === OPS.OP_HASH160 && |
|
|
|
Buffer.isBuffer(chunks[2]) && |
|
|
|
chunks[2].length === 20 && |
|
|
|
chunks[3] === OPS.OP_EQUALVERIFY && |
|
|
|
chunks[4] === OPS.OP_CHECKSIG |
|
|
|
} |
|
|
|
|
|
|
|
function isPubKeyInput (script) { |
|
|
|
return script.chunks.length === 1 && |
|
|
|
isCanonicalSignature(script.chunks[0]) |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
return chunks.length === 1 && |
|
|
|
isCanonicalSignature(chunks[0]) |
|
|
|
} |
|
|
|
|
|
|
|
function isPubKeyOutput (script) { |
|
|
|
return script.chunks.length === 2 && |
|
|
|
isCanonicalPubKey(script.chunks[0]) && |
|
|
|
script.chunks[1] === ops.OP_CHECKSIG |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
return chunks.length === 2 && |
|
|
|
isCanonicalPubKey(chunks[0]) && |
|
|
|
chunks[1] === OPS.OP_CHECKSIG |
|
|
|
} |
|
|
|
|
|
|
|
function isScriptHashInput (script, allowIncomplete) { |
|
|
|
if (script.chunks.length < 2) return false |
|
|
|
var chunks = decompile(script) |
|
|
|
if (chunks.length < 2) return false |
|
|
|
|
|
|
|
var lastChunk = script.chunks[script.chunks.length - 1] |
|
|
|
var lastChunk = chunks[chunks.length - 1] |
|
|
|
if (!Buffer.isBuffer(lastChunk)) return false |
|
|
|
|
|
|
|
var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)) |
|
|
|
var redeemScript = Script.fromBuffer(lastChunk) |
|
|
|
var scriptSigChunks = chunks.slice(0, -1) |
|
|
|
var redeemScriptChunks = decompile(lastChunk) |
|
|
|
|
|
|
|
// is redeemScript a valid script?
|
|
|
|
if (redeemScript.chunks.length === 0) return false |
|
|
|
if (redeemScriptChunks.length === 0) return false |
|
|
|
|
|
|
|
return classifyInput(scriptSig, allowIncomplete) === classifyOutput(redeemScript) |
|
|
|
return classifyInput(scriptSigChunks, allowIncomplete) === classifyOutput(redeemScriptChunks) |
|
|
|
} |
|
|
|
|
|
|
|
function isScriptHashOutput (script) { |
|
|
|
return script.chunks.length === 3 && |
|
|
|
script.chunks[0] === ops.OP_HASH160 && |
|
|
|
Buffer.isBuffer(script.chunks[1]) && |
|
|
|
script.chunks[1].length === 20 && |
|
|
|
script.chunks[2] === ops.OP_EQUAL |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
return chunks.length === 3 && |
|
|
|
chunks[0] === OPS.OP_HASH160 && |
|
|
|
Buffer.isBuffer(chunks[1]) && |
|
|
|
chunks[1].length === 20 && |
|
|
|
chunks[2] === OPS.OP_EQUAL |
|
|
|
} |
|
|
|
|
|
|
|
// allowIncomplete is to account for combining signatures
|
|
|
|
// See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197
|
|
|
|
function isMultisigInput (script, allowIncomplete) { |
|
|
|
if (script.chunks.length < 2) return false |
|
|
|
if (script.chunks[0] !== ops.OP_0) return false |
|
|
|
var chunks = decompile(script) |
|
|
|
if (chunks.length < 2) return false |
|
|
|
if (chunks[0] !== OPS.OP_0) return false |
|
|
|
|
|
|
|
if (allowIncomplete) { |
|
|
|
return script.chunks.slice(1).every(function (chunk) { |
|
|
|
return chunk === ops.OP_0 || isCanonicalSignature(chunk) |
|
|
|
return chunks.slice(1).every(function (chunk) { |
|
|
|
return chunk === OPS.OP_0 || isCanonicalSignature(chunk) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
return script.chunks.slice(1).every(isCanonicalSignature) |
|
|
|
return chunks.slice(1).every(isCanonicalSignature) |
|
|
|
} |
|
|
|
|
|
|
|
function isMultisigOutput (script) { |
|
|
|
if (script.chunks.length < 4) return false |
|
|
|
if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false |
|
|
|
|
|
|
|
var mOp = script.chunks[0] |
|
|
|
if (mOp === ops.OP_0) return false |
|
|
|
if (mOp < ops.OP_1) return false |
|
|
|
if (mOp > ops.OP_16) return false |
|
|
|
|
|
|
|
var nOp = script.chunks[script.chunks.length - 2] |
|
|
|
if (nOp === ops.OP_0) return false |
|
|
|
if (nOp < ops.OP_1) return false |
|
|
|
if (nOp > ops.OP_16) return false |
|
|
|
|
|
|
|
var m = mOp - (ops.OP_1 - 1) |
|
|
|
var n = nOp - (ops.OP_1 - 1) |
|
|
|
var chunks = decompile(script) |
|
|
|
if (chunks.length < 4) return false |
|
|
|
if (chunks[chunks.length - 1] !== OPS.OP_CHECKMULTISIG) return false |
|
|
|
|
|
|
|
var mOp = chunks[0] |
|
|
|
if (mOp === OPS.OP_0) return false |
|
|
|
if (mOp < OPS.OP_1) return false |
|
|
|
if (mOp > OPS.OP_16) return false |
|
|
|
|
|
|
|
var nOp = chunks[chunks.length - 2] |
|
|
|
if (nOp === OPS.OP_0) return false |
|
|
|
if (nOp < OPS.OP_1) return false |
|
|
|
if (nOp > OPS.OP_16) return false |
|
|
|
|
|
|
|
var m = mOp - (OPS.OP_1 - 1) |
|
|
|
var n = nOp - (OPS.OP_1 - 1) |
|
|
|
if (n < m) return false |
|
|
|
|
|
|
|
var pubKeys = script.chunks.slice(1, -2) |
|
|
|
var pubKeys = chunks.slice(1, -2) |
|
|
|
if (n < pubKeys.length) return false |
|
|
|
|
|
|
|
return pubKeys.every(isCanonicalPubKey) |
|
|
|
} |
|
|
|
|
|
|
|
function isNullDataOutput (script) { |
|
|
|
return script.chunks[0] === ops.OP_RETURN |
|
|
|
var chunks = decompile(script) |
|
|
|
return chunks[0] === OPS.OP_RETURN |
|
|
|
} |
|
|
|
|
|
|
|
function classifyOutput (script) { |
|
|
|
typeforce(types.Script, script) |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
if (isPubKeyHashOutput(script)) { |
|
|
|
if (isPubKeyHashOutput(chunks)) { |
|
|
|
return 'pubkeyhash' |
|
|
|
} else if (isScriptHashOutput(script)) { |
|
|
|
} else if (isScriptHashOutput(chunks)) { |
|
|
|
return 'scripthash' |
|
|
|
} else if (isMultisigOutput(script)) { |
|
|
|
} else if (isMultisigOutput(chunks)) { |
|
|
|
return 'multisig' |
|
|
|
} else if (isPubKeyOutput(script)) { |
|
|
|
} else if (isPubKeyOutput(chunks)) { |
|
|
|
return 'pubkey' |
|
|
|
} else if (isNullDataOutput(script)) { |
|
|
|
} else if (isNullDataOutput(chunks)) { |
|
|
|
return 'nulldata' |
|
|
|
} |
|
|
|
|
|
|
@ -152,15 +274,15 @@ function classifyOutput (script) { |
|
|
|
} |
|
|
|
|
|
|
|
function classifyInput (script, allowIncomplete) { |
|
|
|
typeforce(types.Script, script) |
|
|
|
var chunks = decompile(script) |
|
|
|
|
|
|
|
if (isPubKeyHashInput(script)) { |
|
|
|
if (isPubKeyHashInput(chunks)) { |
|
|
|
return 'pubkeyhash' |
|
|
|
} else if (isMultisigInput(script, allowIncomplete)) { |
|
|
|
} else if (isMultisigInput(chunks, allowIncomplete)) { |
|
|
|
return 'multisig' |
|
|
|
} else if (isScriptHashInput(script, allowIncomplete)) { |
|
|
|
} else if (isScriptHashInput(chunks, allowIncomplete)) { |
|
|
|
return 'scripthash' |
|
|
|
} else if (isPubKeyInput(script)) { |
|
|
|
} else if (isPubKeyInput(chunks)) { |
|
|
|
return 'pubkey' |
|
|
|
} |
|
|
|
|
|
|
@ -170,34 +292,21 @@ function classifyInput (script, allowIncomplete) { |
|
|
|
// Standard Script Templates
|
|
|
|
// {pubKey} OP_CHECKSIG
|
|
|
|
function pubKeyOutput (pubKey) { |
|
|
|
return Script.fromChunks([ |
|
|
|
pubKey, |
|
|
|
ops.OP_CHECKSIG |
|
|
|
]) |
|
|
|
return compile([pubKey, OPS.OP_CHECKSIG]) |
|
|
|
} |
|
|
|
|
|
|
|
// OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG
|
|
|
|
function pubKeyHashOutput (hash) { |
|
|
|
typeforce(types.Hash160bit, hash) |
|
|
|
|
|
|
|
return Script.fromChunks([ |
|
|
|
ops.OP_DUP, |
|
|
|
ops.OP_HASH160, |
|
|
|
hash, |
|
|
|
ops.OP_EQUALVERIFY, |
|
|
|
ops.OP_CHECKSIG |
|
|
|
]) |
|
|
|
function pubKeyHashOutput (pubKeyHash) { |
|
|
|
typeforce(types.Hash160bit, pubKeyHash) |
|
|
|
|
|
|
|
return compile([OPS.OP_DUP, OPS.OP_HASH160, pubKeyHash, OPS.OP_EQUALVERIFY, OPS.OP_CHECKSIG]) |
|
|
|
} |
|
|
|
|
|
|
|
// OP_HASH160 {scriptHash} OP_EQUAL
|
|
|
|
function scriptHashOutput (hash) { |
|
|
|
typeforce(types.Hash160bit, hash) |
|
|
|
|
|
|
|
return Script.fromChunks([ |
|
|
|
ops.OP_HASH160, |
|
|
|
hash, |
|
|
|
ops.OP_EQUAL |
|
|
|
]) |
|
|
|
function scriptHashOutput (scriptHash) { |
|
|
|
typeforce(types.Hash160bit, scriptHash) |
|
|
|
|
|
|
|
return compile([OPS.OP_HASH160, scriptHash, OPS.OP_EQUAL]) |
|
|
|
} |
|
|
|
|
|
|
|
// m [pubKeys ...] n OP_CHECKMULTISIG
|
|
|
@ -207,11 +316,11 @@ function multisigOutput (m, pubKeys) { |
|
|
|
var n = pubKeys.length |
|
|
|
if (n < m) throw new Error('Not enough pubKeys provided') |
|
|
|
|
|
|
|
return Script.fromChunks([].concat( |
|
|
|
(ops.OP_1 - 1) + m, |
|
|
|
return compile([].concat( |
|
|
|
(OPS.OP_1 - 1) + m, |
|
|
|
pubKeys, |
|
|
|
(ops.OP_1 - 1) + n, |
|
|
|
ops.OP_CHECKMULTISIG |
|
|
|
(OPS.OP_1 - 1) + n, |
|
|
|
OPS.OP_CHECKMULTISIG |
|
|
|
)) |
|
|
|
} |
|
|
|
|
|
|
@ -219,46 +328,55 @@ function multisigOutput (m, pubKeys) { |
|
|
|
function pubKeyInput (signature) { |
|
|
|
typeforce(types.Buffer, signature) |
|
|
|
|
|
|
|
return Script.fromChunks([signature]) |
|
|
|
return compile([signature]) |
|
|
|
} |
|
|
|
|
|
|
|
// {signature} {pubKey}
|
|
|
|
function pubKeyHashInput (signature, pubKey) { |
|
|
|
typeforce(types.tuple(types.Buffer, types.Buffer), arguments) |
|
|
|
|
|
|
|
return Script.fromChunks([signature, pubKey]) |
|
|
|
return compile([signature, pubKey]) |
|
|
|
} |
|
|
|
|
|
|
|
// <scriptSig> {serialized scriptPubKey script}
|
|
|
|
function scriptHashInput (scriptSig, scriptPubKey) { |
|
|
|
return Script.fromChunks([].concat( |
|
|
|
scriptSig.chunks, |
|
|
|
scriptPubKey.toBuffer() |
|
|
|
var scriptSigChunks = decompile(scriptSig) |
|
|
|
var serializedScriptPubKey = compile(scriptPubKey) |
|
|
|
|
|
|
|
return compile([].concat( |
|
|
|
scriptSigChunks, |
|
|
|
serializedScriptPubKey |
|
|
|
)) |
|
|
|
} |
|
|
|
|
|
|
|
// OP_0 [signatures ...]
|
|
|
|
function multisigInput (signatures, scriptPubKey) { |
|
|
|
if (scriptPubKey) { |
|
|
|
if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey') |
|
|
|
var chunks = decompile(scriptPubKey) |
|
|
|
if (!isMultisigOutput(chunks)) throw new Error('Expected multisig scriptPubKey') |
|
|
|
|
|
|
|
var mOp = scriptPubKey.chunks[0] |
|
|
|
var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2] |
|
|
|
var m = mOp - (ops.OP_1 - 1) |
|
|
|
var n = nOp - (ops.OP_1 - 1) |
|
|
|
var mOp = chunks[0] |
|
|
|
var nOp = chunks[chunks.length - 2] |
|
|
|
var m = mOp - (OPS.OP_1 - 1) |
|
|
|
var n = nOp - (OPS.OP_1 - 1) |
|
|
|
|
|
|
|
if (signatures.length < m) throw new Error('Not enough signatures provided') |
|
|
|
if (signatures.length > n) throw new Error('Too many signatures provided') |
|
|
|
} |
|
|
|
|
|
|
|
return Script.fromChunks([].concat(ops.OP_0, signatures)) |
|
|
|
return compile([].concat(OPS.OP_0, signatures)) |
|
|
|
} |
|
|
|
|
|
|
|
function nullDataOutput (data) { |
|
|
|
return Script.fromChunks([ops.OP_RETURN, data]) |
|
|
|
return compile([OPS.OP_RETURN, data]) |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
compile: compile, |
|
|
|
decompile: decompile, |
|
|
|
fromASM: fromASM, |
|
|
|
toASM: toASM, |
|
|
|
|
|
|
|
isCanonicalPubKey: isCanonicalPubKey, |
|
|
|
isCanonicalSignature: isCanonicalSignature, |
|
|
|
isPubKeyHashInput: isPubKeyHashInput, |
|
|
|