From e743c58ed66a3e720a28c3151d9e64ca62fe42a6 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 7 Aug 2015 16:30:24 +1000 Subject: [PATCH 01/13] remove usage of instanced Scripts --- src/address.js | 8 +++-- src/scripts.js | 62 ++++++++++++++++++++++++++---------- src/transaction.js | 36 ++++++++++----------- src/transaction_builder.js | 43 ++++++++++++++----------- test/address.js | 6 ++-- test/bitcoin.core.js | 4 +-- test/integration/multisig.js | 4 +-- test/script.js | 22 ++++--------- test/scripts.js | 48 ++++++++++++++-------------- test/transaction.js | 14 ++++---- test/transaction_builder.js | 18 +++++------ 11 files changed, 146 insertions(+), 119 deletions(-) diff --git a/src/address.js b/src/address.js index 879b089..98eb888 100644 --- a/src/address.js +++ b/src/address.js @@ -1,6 +1,8 @@ var base58check = require('bs58check') var typeforce = require('typeforce') var networks = require('./networks') + +var Script = require('./script') var scripts = require('./scripts') var types = require('./types') @@ -18,10 +20,10 @@ function fromBase58Check (address) { function fromOutputScript (script, network) { network = network || networks.bitcoin - if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script.chunks[2], network.pubKeyHash) - if (scripts.isScriptHashOutput(script)) return toBase58Check(script.chunks[1], network.scriptHash) + if (scripts.isPubKeyHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[2], network.pubKeyHash) + if (scripts.isScriptHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[1], network.scriptHash) - throw new Error(script.toASM() + ' has no matching Address') + throw new Error(Script.fromBuffer(script).toASM() + ' has no matching Address') } function toBase58Check (hash, version) { diff --git a/src/scripts.js b/src/scripts.js index d6add2f..5437cdb 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -41,12 +41,18 @@ function isCanonicalSignature (buffer) { } function isPubKeyHashInput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + return script.chunks.length === 2 && isCanonicalSignature(script.chunks[0]) && isCanonicalPubKey(script.chunks[1]) } function isPubKeyHashOutput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + return script.chunks.length === 5 && script.chunks[0] === ops.OP_DUP && script.chunks[1] === ops.OP_HASH160 && @@ -57,32 +63,44 @@ function isPubKeyHashOutput (script) { } function isPubKeyInput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + return script.chunks.length === 1 && isCanonicalSignature(script.chunks[0]) } function isPubKeyOutput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + return script.chunks.length === 2 && isCanonicalPubKey(script.chunks[0]) && script.chunks[1] === ops.OP_CHECKSIG } function isScriptHashInput (script, allowIncomplete) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + if (script.chunks.length < 2) return false var lastChunk = script.chunks[script.chunks.length - 1] if (!Buffer.isBuffer(lastChunk)) return false - var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)) - var redeemScript = Script.fromBuffer(lastChunk) + var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)).buffer + var redeemScript = lastChunk // is redeemScript a valid script? - if (redeemScript.chunks.length === 0) return false + if (Script.fromBuffer(redeemScript).chunks.length === 0) return false return classifyInput(scriptSig, allowIncomplete) === classifyOutput(redeemScript) } function isScriptHashOutput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + return script.chunks.length === 3 && script.chunks[0] === ops.OP_HASH160 && Buffer.isBuffer(script.chunks[1]) && @@ -93,6 +111,9 @@ function isScriptHashOutput (script) { // 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) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + if (script.chunks.length < 2) return false if (script.chunks[0] !== ops.OP_0) return false @@ -106,6 +127,9 @@ function isMultisigInput (script, allowIncomplete) { } function isMultisigOutput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + if (script.chunks.length < 4) return false if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false @@ -130,11 +154,14 @@ function isMultisigOutput (script) { } function isNullDataOutput (script) { + typeforce(types.Buffer, script) + script = Script.fromBuffer(script) + return script.chunks[0] === ops.OP_RETURN } function classifyOutput (script) { - typeforce(types.Script, script) + typeforce(types.Buffer, script) if (isPubKeyHashOutput(script)) { return 'pubkeyhash' @@ -152,7 +179,7 @@ function classifyOutput (script) { } function classifyInput (script, allowIncomplete) { - typeforce(types.Script, script) + typeforce(types.Buffer, script) if (isPubKeyHashInput(script)) { return 'pubkeyhash' @@ -173,7 +200,7 @@ function pubKeyOutput (pubKey) { return Script.fromChunks([ pubKey, ops.OP_CHECKSIG - ]) + ]).buffer } // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG @@ -186,7 +213,7 @@ function pubKeyHashOutput (hash) { hash, ops.OP_EQUALVERIFY, ops.OP_CHECKSIG - ]) + ]).buffer } // OP_HASH160 {scriptHash} OP_EQUAL @@ -197,7 +224,7 @@ function scriptHashOutput (hash) { ops.OP_HASH160, hash, ops.OP_EQUAL - ]) + ]).buffer } // m [pubKeys ...] n OP_CHECKMULTISIG @@ -212,35 +239,38 @@ function multisigOutput (m, pubKeys) { pubKeys, (ops.OP_1 - 1) + n, ops.OP_CHECKMULTISIG - )) + )).buffer } // {signature} function pubKeyInput (signature) { typeforce(types.Buffer, signature) - return Script.fromChunks([signature]) + return Script.fromChunks([signature]).buffer } // {signature} {pubKey} function pubKeyHashInput (signature, pubKey) { typeforce(types.tuple(types.Buffer, types.Buffer), arguments) - return Script.fromChunks([signature, pubKey]) + return Script.fromChunks([signature, pubKey]).buffer } // {serialized scriptPubKey script} function scriptHashInput (scriptSig, scriptPubKey) { + var scriptSigChunks = Script.fromBuffer(scriptSig).chunks + return Script.fromChunks([].concat( - scriptSig.chunks, - scriptPubKey.toBuffer() - )) + scriptSigChunks, + scriptPubKey + )).buffer } // OP_0 [signatures ...] function multisigInput (signatures, scriptPubKey) { if (scriptPubKey) { if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey') + scriptPubKey = Script.fromBuffer(scriptPubKey) var mOp = scriptPubKey.chunks[0] var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2] @@ -251,11 +281,11 @@ function multisigInput (signatures, scriptPubKey) { if (signatures.length > n) throw new Error('Too many signatures provided') } - return Script.fromChunks([].concat(ops.OP_0, signatures)) + return Script.fromChunks([].concat(ops.OP_0, signatures)).buffer } function nullDataOutput (data) { - return Script.fromChunks([ops.OP_RETURN, data]) + return Script.fromChunks([ops.OP_RETURN, data]).buffer } module.exports = { diff --git a/src/transaction.js b/src/transaction.js index 1267b5e..0933589 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -45,11 +45,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) { } function readScript () { - return Script.fromBuffer(readSlice(readVarInt())) - } - - function readGenerationScript () { - return new Script(readSlice(readVarInt()), []) + return readSlice(readVarInt()) } var tx = new Transaction() @@ -63,7 +59,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) { tx.ins.push({ hash: hash, index: readUInt32(), - script: readGenerationScript(), + script: readScript(), sequence: readUInt32() }) } else { @@ -102,19 +98,21 @@ Transaction.isCoinbaseHash = function (buffer) { }) } +var EMPTY = new Buffer(0) + Transaction.prototype.addInput = function (hash, index, sequence, script) { typeforce(types.tuple( types.Hash256bit, types.UInt32, types.maybe(types.UInt32), - types.maybe(types.Script) + types.maybe(types.Buffer) ), arguments) if (types.Null(sequence)) { sequence = Transaction.DEFAULT_SEQUENCE } - script = script || Script.EMPTY + script = script || EMPTY // Add the input and return the input's index return (this.ins.push({ @@ -126,7 +124,7 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) { } Transaction.prototype.addOutput = function (scriptPubKey, value) { - typeforce(types.tuple(types.Script, types.UInt53), arguments) + typeforce(types.tuple(types.Buffer, types.UInt53), arguments) // Add the output and return the output's index return (this.outs.push({ @@ -137,7 +135,7 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) { Transaction.prototype.byteLength = function () { function scriptSize (script) { - var length = script.buffer.length + var length = script.length return bufferutils.varIntSize(length) + length } @@ -186,7 +184,7 @@ var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000 * This hash can then be used to sign the provided transaction input. */ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashType) { - typeforce(types.tuple(types.UInt32, types.Script, /* types.UInt8 */ types.Number), arguments) + typeforce(types.tuple(types.UInt32, types.Buffer, /* types.UInt8 */ types.Number), arguments) // https://github.com/bitcoin/bitcoin/blob/master/src/test/sighash_tests.cpp#L29 if (inIndex >= this.ins.length) return ONE @@ -195,11 +193,11 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT // in case concatenating two scripts ends up with two codeseparators, // or an extra one at the end, this prevents all those possible incompatibilities. - var hashScript = prevOutScript.without(opcodes.OP_CODESEPARATOR) + var hashScript = Script.fromBuffer(prevOutScript).without(opcodes.OP_CODESEPARATOR).buffer var i // blank out other inputs' signatures - txTmp.ins.forEach(function (input) { input.script = Script.EMPTY }) + txTmp.ins.forEach(function (input) { input.script = EMPTY }) txTmp.ins[inIndex].script = hashScript // blank out some of the inputs @@ -225,7 +223,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT // blank all other outputs (clear scriptPubKey, value === -1) var stubOut = { - script: Script.EMPTY, + script: EMPTY, valueBuffer: new Buffer('ffffffffffffffff', 'hex') } @@ -294,8 +292,8 @@ Transaction.prototype.toBuffer = function () { this.ins.forEach(function (txIn) { writeSlice(txIn.hash) writeUInt32(txIn.index) - writeVarInt(txIn.script.buffer.length) - writeSlice(txIn.script.buffer) + writeVarInt(txIn.script.length) + writeSlice(txIn.script) writeUInt32(txIn.sequence) }) @@ -307,8 +305,8 @@ Transaction.prototype.toBuffer = function () { writeSlice(txOut.valueBuffer) } - writeVarInt(txOut.script.buffer.length) - writeSlice(txOut.script.buffer) + writeVarInt(txOut.script.length) + writeSlice(txOut.script) }) writeUInt32(this.locktime) @@ -321,7 +319,7 @@ Transaction.prototype.toHex = function () { } Transaction.prototype.setInputScript = function (index, script) { - typeforce(types.tuple(types.Number, types.Script), arguments) + typeforce(types.tuple(types.Number, types.Buffer), arguments) this.ins[index].script = script } diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 421bb9b..fd6ab38 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -13,16 +13,20 @@ var Transaction = require('./transaction') function extractInput (txIn) { var redeemScript var scriptSig = txIn.script + var scriptSigChunks = Script.fromBuffer(scriptSig).chunks + var prevOutScript var prevOutType = scripts.classifyInput(scriptSig, true) var scriptType // Re-classify if scriptHash if (prevOutType === 'scripthash') { - redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0]) - prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) + redeemScript = scriptSigChunks.slice(-1)[0] + prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) + + scriptSig = Script.fromChunks(scriptSigChunks.slice(0, -1)).buffer + scriptSigChunks = scriptSigChunks.slice(0, -1) - scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1)) scriptType = scripts.classifyInput(scriptSig, true) } else { scriptType = prevOutType @@ -33,27 +37,27 @@ function extractInput (txIn) { switch (scriptType) { case 'pubkeyhash': - parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + parsed = ECSignature.parseScriptSignature(scriptSigChunks[0]) hashType = parsed.hashType - pubKeys = scriptSig.chunks.slice(1) + pubKeys = scriptSigChunks.slice(1) signatures = [parsed.signature] prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(pubKeys[0])) break case 'pubkey': - parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + parsed = ECSignature.parseScriptSignature(scriptSigChunks[0]) hashType = parsed.hashType signatures = [parsed.signature] if (redeemScript) { - pubKeys = redeemScript.chunks.slice(0, 1) + pubKeys = Script.fromBuffer(redeemScript).chunks.slice(0, 1) } break case 'multisig': - signatures = scriptSig.chunks.slice(1).map(function (chunk) { + signatures = scriptSigChunks.slice(1).map(function (chunk) { if (chunk === ops.OP_0) return chunk var parsed = ECSignature.parseScriptSignature(chunk) @@ -63,7 +67,7 @@ function extractInput (txIn) { }) if (redeemScript) { - pubKeys = redeemScript.chunks.slice(1, -2) + pubKeys = Script.fromBuffer(redeemScript).chunks.slice(1, -2) } break @@ -115,7 +119,7 @@ TransactionBuilder.fromTransaction = function (transaction, network) { } // Ignore empty scripts - if (txIn.script.buffer.length === 0) return {} + if (txIn.script.length === 0) return {} return extractInput(txIn) }) @@ -139,15 +143,16 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu var input = {} if (prevOutScript) { var prevOutType = scripts.classifyOutput(prevOutScript) + var prevOutScriptChunks = Script.fromBuffer(prevOutScript).chunks // if we can, extract pubKey information switch (prevOutType) { case 'multisig': - input.pubKeys = prevOutScript.chunks.slice(1, -2) + input.pubKeys = prevOutScriptChunks.slice(1, -2) break case 'pubkey': - input.pubKeys = prevOutScript.chunks.slice(0, 1) + input.pubKeys = prevOutScriptChunks.slice(0, 1) break } @@ -308,21 +313,22 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash if (input.prevOutScript) { if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH') - var scriptHash = input.prevOutScript.chunks[1] - if (!bufferutils.equal(scriptHash, redeemScript.getHash())) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex')) + var scriptHash = Script.fromBuffer(input.prevOutScript).chunks[1] + if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex')) } var scriptType = scripts.classifyOutput(redeemScript) if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')') + var redeemScriptChunks = Script.fromBuffer(redeemScript).chunks var pubKeys = [] switch (scriptType) { case 'multisig': - pubKeys = redeemScript.chunks.slice(1, -2) + pubKeys = redeemScriptChunks.slice(1, -2) break case 'pubkeyhash': - var pkh1 = redeemScript.chunks[2] + var pkh1 = redeemScriptChunks[2] var pkh2 = bcrypto.hash160(keyPair.getPublicKeyBuffer()) if (!bufferutils.equal(pkh1, pkh2)) throw new Error('privateKey cannot sign for this input') @@ -330,12 +336,13 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash break case 'pubkey': - pubKeys = redeemScript.chunks.slice(0, 1) + pubKeys = redeemScriptChunks.slice(0, 1) break } + // if we don't have a prevOutScript, generate a P2SH script if (!input.prevOutScript) { - input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) + input.prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) input.prevOutType = 'scripthash' } diff --git a/test/address.js b/test/address.js index 541fb88..ab37ed8 100644 --- a/test/address.js +++ b/test/address.js @@ -31,7 +31,7 @@ describe('Address', function () { describe('fromOutputScript', function () { fixtures.valid.forEach(function (f) { it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { - var script = Script.fromASM(f.script) + var script = Script.fromASM(f.script).buffer var address = Address.fromOutputScript(script, networks[f.network]) assert.strictEqual(address, f.base58check) @@ -40,7 +40,7 @@ describe('Address', function () { fixtures.invalid.fromOutputScript.forEach(function (f) { it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () { - var script = Script.fromASM(f.script) + var script = Script.fromASM(f.script).buffer assert.throws(function () { Address.fromOutputScript(script) @@ -66,7 +66,7 @@ describe('Address', function () { it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { var script = Address.toOutputScript(f.base58check, network) - assert.strictEqual(script.toASM(), f.script) + assert.strictEqual(Script.fromBuffer(script).toASM(), f.script) }) }) diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index ff6cbff..769a612 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -227,8 +227,8 @@ describe('Bitcoin-core', function () { var transaction = Transaction.fromHex(txHex) assert.strictEqual(transaction.toHex(), txHex) - var script = Script.fromHex(scriptHex) - assert.strictEqual(script.toHex(), scriptHex) + var script = Script.fromHex(scriptHex).buffer + assert.strictEqual(Script.fromBuffer(script).toHex(), scriptHex) var hash = transaction.hashForSignature(inIndex, script, hashType) assert.deepEqual(hash, expectedHash) diff --git a/test/integration/multisig.js b/test/integration/multisig.js index 28a2d93..cae7a47 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -15,7 +15,7 @@ describe('bitcoinjs-lib (multisig)', function () { }) var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 3 - var scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash()) + var scriptPubKey = bitcoin.scripts.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) var address = bitcoin.Address.fromOutputScript(scriptPubKey) assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7') @@ -33,7 +33,7 @@ describe('bitcoinjs-lib (multisig)', function () { var pubKeys = keyPairs.map(function (x) { return x.getPublicKeyBuffer() }) var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 4 - var scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash()) + var scriptPubKey = bitcoin.scripts.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet) // attempt to send funds to the source address diff --git a/test/script.js b/test/script.js index 4ce400e..8c20403 100644 --- a/test/script.js +++ b/test/script.js @@ -34,7 +34,7 @@ describe('Script', function () { var script = Script.fromASM(f.asm) assert.strictEqual(script.toASM(), f.asm) - assert.strictEqual(script.toHex(), f.hex) + assert.strictEqual(script.buffer.toString('hex'), f.hex) }) }) }) @@ -45,17 +45,7 @@ describe('Script', function () { var script = Script.fromHex(f.hex) assert.strictEqual(script.toASM(), f.asm) - assert.strictEqual(script.toHex(), f.hex) - }) - }) - }) - - describe('getHash', function () { - fixtures.valid.forEach(function (f) { - it('produces a HASH160 of ' + f.description, function () { - var script = Script.fromHex(f.hex) - - assert.strictEqual(script.getHash().toString('hex'), f.hash) + assert.strictEqual(script.buffer.toString('hex'), f.hex) }) }) }) @@ -71,7 +61,7 @@ describe('Script', function () { opcodes.OP_EQUAL ]) - assert.strictEqual(script.toHex(), 'a920000000000000000000000000000000000000000000000000000000000000000087') + assert.strictEqual(script.buffer.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087') }) }) @@ -82,14 +72,14 @@ describe('Script', function () { it('should return a script without the given value', function () { var subScript = script.without(opcodes.OP_HASH160) - assert.strictEqual(subScript.toHex(), '14e8c300c87986efa94c37c0519929019ef86eb5b487') + assert.strictEqual(subScript.buffer.toString('hex'), '14e8c300c87986efa94c37c0519929019ef86eb5b487') }) it('shouldnt mutate the original script', function () { var subScript = script.without(opcodes.OP_EQUAL) - assert.notEqual(subScript.toHex(), hex) - assert.strictEqual(script.toHex(), hex) + assert.notEqual(subScript.buffer.toString('hex'), hex) + assert.strictEqual(script.buffer.toString('hex'), hex) }) }) }) diff --git a/test/scripts.js b/test/scripts.js index f89c44a..64635ce 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -19,7 +19,7 @@ describe('Scripts', function () { if (!f.scriptSig) return it('classifies ' + f.scriptSig + ' as ' + f.type, function () { - var script = Script.fromASM(f.scriptSig) + var script = Script.fromASM(f.scriptSig).buffer var type = scripts.classifyInput(script) assert.strictEqual(type, f.type) @@ -31,7 +31,7 @@ describe('Scripts', function () { if (!f.typeIncomplete) return it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () { - var script = Script.fromASM(f.scriptSig) + var script = Script.fromASM(f.scriptSig).buffer var type = scripts.classifyInput(script, true) assert.strictEqual(type, f.typeIncomplete) @@ -44,7 +44,7 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () { - var script = Script.fromASM(f.scriptPubKey) + var script = Script.fromASM(f.scriptPubKey).buffer var type = scripts.classifyOutput(script) assert.strictEqual(type, f.type) @@ -67,9 +67,9 @@ describe('Scripts', function () { var script if (f.scriptSig) { - script = Script.fromASM(f.scriptSig) + script = Script.fromASM(f.scriptSig).buffer } else { - script = Script.fromHex(f.scriptSigHex) + script = Script.fromHex(f.scriptSigHex).buffer } it('returns ' + expected + ' for ' + f.scriptSig, function () { @@ -94,9 +94,9 @@ describe('Scripts', function () { var script if (f.scriptSig) { - script = Script.fromASM(f.scriptSig) + script = Script.fromASM(f.scriptSig).buffer } else { - script = Script.fromHex(f.scriptSigHex) + script = Script.fromHex(f.scriptSigHex).buffer } assert.strictEqual(inputFn(script), false) @@ -111,7 +111,7 @@ describe('Scripts', function () { if (outputFn && f.scriptPubKey) { it('returns ' + expected + ' for ' + f.scriptPubKey, function () { - var script = Script.fromASM(f.scriptPubKey) + var script = Script.fromASM(f.scriptPubKey).buffer assert.strictEqual(outputFn(script), expected) }) @@ -123,7 +123,7 @@ describe('Scripts', function () { fixtures.invalid[outputFnName].forEach(function (f) { if (outputFn && f.scriptPubKey) { it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () { - var script = Script.fromASM(f.scriptPubKey) + var script = Script.fromASM(f.scriptPubKey).buffer assert.strictEqual(outputFn(script), false) }) @@ -140,7 +140,7 @@ describe('Scripts', function () { var signature = new Buffer(f.signature, 'hex') var scriptSig = scripts.pubKeyInput(signature) - assert.strictEqual(scriptSig.toASM(), f.scriptSig) + assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) }) }) }) @@ -152,7 +152,7 @@ describe('Scripts', function () { it('returns ' + f.scriptPubKey, function () { var pubKey = new Buffer(f.pubKey, 'hex') var scriptPubKey = scripts.pubKeyOutput(pubKey) - assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey) + assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) }) }) }) @@ -167,7 +167,7 @@ describe('Scripts', function () { var signature = new Buffer(f.signature, 'hex') var scriptSig = scripts.pubKeyHashInput(signature, pubKey) - assert.strictEqual(scriptSig.toASM(), f.scriptSig) + assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) }) }) }) @@ -181,7 +181,7 @@ describe('Scripts', function () { it('returns ' + f.scriptPubKey, function () { var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash) - assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey) + assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) }) }) }) @@ -196,12 +196,12 @@ describe('Scripts', function () { }) var scriptSig = scripts.multisigInput(signatures) - assert.strictEqual(scriptSig.toASM(), f.scriptSig) + assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) }) }) fixtures.invalid.multisigInput.forEach(function (f) { - var scriptPubKey = Script.fromASM(f.scriptPubKey) + var scriptPubKey = Script.fromASM(f.scriptPubKey).buffer it('throws on ' + f.exception, function () { var signatures = f.signatures.map(function (signature) { @@ -223,7 +223,7 @@ describe('Scripts', function () { var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey) + assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) }) }) @@ -244,16 +244,16 @@ describe('Scripts', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'scripthash') return - var redeemScript = Script.fromASM(f.redeemScript) - var redeemScriptSig = Script.fromASM(f.redeemScriptSig) + var redeemScript = Script.fromASM(f.redeemScript).buffer + var redeemScriptSig = Script.fromASM(f.redeemScriptSig).buffer it('returns ' + f.scriptSig, function () { var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) if (f.scriptSig) { - assert.strictEqual(scriptSig.toASM(), f.scriptSig) + assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) } else { - assert.strictEqual(scriptSig.toHex(), f.scriptSigHex) + assert.strictEqual(Script.fromBuffer(scriptSig).toHex(), f.scriptSigHex) } }) }) @@ -265,10 +265,10 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('returns ' + f.scriptPubKey, function () { - var redeemScript = Script.fromASM(f.redeemScript) - var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash()) + var redeemScript = Script.fromASM(f.redeemScript).buffer + var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) - assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey) + assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) }) }) }) @@ -281,7 +281,7 @@ describe('Scripts', function () { var scriptPubKey = scripts.nullDataOutput(data) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey) + assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) }) }) }) diff --git a/test/transaction.js b/test/transaction.js index 0cd376b..6ac9bb8 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -19,9 +19,9 @@ describe('Transaction', function () { if (txIn.data) { var data = new Buffer(txIn.data, 'hex') - script = new Script(data, []) + script = data } else if (txIn.script) { - script = Script.fromASM(txIn.script) + script = Script.fromASM(txIn.script).buffer } tx.addInput(txHash, txIn.index, txIn.sequence, script) @@ -32,9 +32,9 @@ describe('Transaction', function () { if (txOut.data) { var data = new Buffer(txOut.data, 'hex') - script = new Script(data, []) + script = data } else if (txOut.script) { - script = Script.fromASM(txOut.script) + script = Script.fromASM(txOut.script).buffer } tx.addOutput(script, txOut.value) @@ -102,7 +102,7 @@ describe('Transaction', function () { var tx = new Transaction() tx.addInput(prevTxHash, 0) - assert.strictEqual(tx.ins[0].script, Script.EMPTY) + assert.strictEqual(tx.ins[0].script.length, 0) }) fixtures.invalid.addInput.forEach(function (f) { @@ -120,8 +120,8 @@ describe('Transaction', function () { describe('addOutput', function () { it('returns an index', function () { var tx = new Transaction() - assert.strictEqual(tx.addOutput(Script.EMPTY, 0), 0) - assert.strictEqual(tx.addOutput(Script.EMPTY, 0), 1) + assert.strictEqual(tx.addOutput(new Buffer(0), 0), 0) + assert.strictEqual(tx.addOutput(new Buffer(0), 0), 1) }) }) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 3bfacd5..0367aac 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -22,14 +22,14 @@ function construct (f, sign) { var prevTxScript if (input.prevTxScript) { - prevTxScript = Script.fromASM(input.prevTxScript) + prevTxScript = Script.fromASM(input.prevTxScript).buffer } txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function (output) { - var script = Script.fromASM(output.script) + var script = Script.fromASM(output.script).buffer txb.addOutput(script, output.value) }) @@ -41,7 +41,7 @@ function construct (f, sign) { var redeemScript if (sign.redeemScript) { - redeemScript = Script.fromASM(sign.redeemScript) + redeemScript = Script.fromASM(sign.redeemScript).buffer } txb.sign(index, keyPair, redeemScript, sign.hashType) @@ -200,7 +200,7 @@ describe('TransactionBuilder', function () { var redeemScript if (sign.redeemScript) { - redeemScript = Script.fromASM(sign.redeemScript) + redeemScript = Script.fromASM(sign.redeemScript).buffer } if (!sign.throws) { @@ -262,7 +262,7 @@ describe('TransactionBuilder', function () { var network = NETWORKS[f.network] f.inputs.forEach(function (input, i) { - var redeemScript = Script.fromASM(input.redeemScript) + var redeemScript = Script.fromASM(input.redeemScript).buffer input.signs.forEach(function (sign) { // rebuild the transaction each-time after the first @@ -272,11 +272,11 @@ describe('TransactionBuilder', function () { var scriptSig = tx.ins[i].script // ignore OP_0 on the front, ignore redeemScript - var signatures = scriptSig.chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) + var signatures = Script.fromBuffer(scriptSig).chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) // rebuild/replace the scriptSig without them var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript) - assert.strictEqual(replacement.toASM(), sign.scriptSigFiltered) + assert.strictEqual(Script.fromBuffer(replacement).toASM(), sign.scriptSigFiltered) tx.ins[i].script = replacement } @@ -292,7 +292,7 @@ describe('TransactionBuilder', function () { tx = txb.buildIncomplete() // now verify the serialized scriptSig is as expected - assert.strictEqual(tx.ins[i].script.toASM(), sign.scriptSig) + assert.strictEqual(Script.fromBuffer(tx.ins[i].script).toASM(), sign.scriptSig) }) }) @@ -309,7 +309,7 @@ describe('TransactionBuilder', function () { txb = TransactionBuilder.fromTransaction(lameTx, network) - var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG') + var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG').buffer var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network) txb.sign(0, keyPair, redeemScript) From e05f8a542d380f2e2552c5d6b646959a805dcddb Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 10:17:04 +1000 Subject: [PATCH 02/13] remove Script object --- src/address.js | 6 +- src/script.js | 140 ++++++++++------------- src/scripts.js | 217 ++++++++++++++++-------------------- src/transaction.js | 4 +- src/transaction_builder.js | 16 +-- test/address.js | 6 +- test/bitcoin.core.js | 5 +- test/script.js | 62 ++--------- test/scripts.js | 48 ++++---- test/transaction.js | 6 +- test/transaction_builder.js | 18 +-- 11 files changed, 223 insertions(+), 305 deletions(-) diff --git a/src/address.js b/src/address.js index 98eb888..9b9da05 100644 --- a/src/address.js +++ b/src/address.js @@ -20,10 +20,10 @@ function fromBase58Check (address) { function fromOutputScript (script, network) { network = network || networks.bitcoin - if (scripts.isPubKeyHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[2], network.pubKeyHash) - if (scripts.isScriptHashOutput(script)) return toBase58Check(Script.fromBuffer(script).chunks[1], network.scriptHash) + if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script[2], network.pubKeyHash) + if (scripts.isScriptHashOutput(script)) return toBase58Check(script[1], network.scriptHash) - throw new Error(Script.fromBuffer(script).toASM() + ' has no matching Address') + throw new Error(Script.toASM(script) + ' has no matching Address') } function toBase58Check (hash, version) { diff --git a/src/script.js b/src/script.js index 211c74d..0e29f73 100644 --- a/src/script.js +++ b/src/script.js @@ -1,17 +1,30 @@ var bufferutils = require('./bufferutils') -var crypto = require('./crypto') +var opcodes = require('./opcodes') var typeforce = require('typeforce') var types = require('./types') -var opcodes = require('./opcodes') -function Script (buffer, chunks) { - typeforce(types.tuple(types.Buffer, types.Array), arguments) - - this.buffer = buffer - this.chunks = chunks +function coerceChunks (chunks) { + return types.Array(chunks) ? chunks : decompile(chunks) } -Script.fromASM = function (asm) { +function toASM (chunks) { + chunks = coerceChunks(chunks) + + return chunks.map(function (chunk) { + // data chunk + if (Buffer.isBuffer(chunk)) { + return chunk.toString('hex') + + // opcode + } else { + return reverseOps[chunk] + } + }).join(' ') +} + +function fromASM (asm) { + typeforce(types.String, asm) + var strChunks = asm.split(' ') var chunks = strChunks.map(function (strChunk) { // opcode @@ -24,44 +37,10 @@ Script.fromASM = function (asm) { } }) - return Script.fromChunks(chunks) + return chunks } -Script.fromBuffer = function (buffer) { - var chunks = [] - var i = 0 - - while (i < buffer.length) { - var opcode = buffer.readUInt8(i) - - // data chunk - if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) { - var d = bufferutils.readPushDataInt(buffer, i) - - // did reading a pushDataInt fail? return non-chunked script - if (d === null) return new Script(buffer, []) - i += d.size - - // attempt to read too much data? - if (i + d.number > buffer.length) return new Script(buffer, []) - - var data = buffer.slice(i, i + d.number) - i += d.number - - chunks.push(data) - - // opcode - } else { - chunks.push(opcode) - - i += 1 - } - } - - return new Script(buffer, chunks) -} - -Script.fromChunks = function (chunks) { +function compile (chunks) { typeforce(types.Array, chunks) var bufferSize = chunks.reduce(function (accum, chunk) { @@ -93,28 +72,43 @@ Script.fromChunks = function (chunks) { }) if (offset !== buffer.length) throw new Error('Could not decode chunks') - return new Script(buffer, chunks) + return buffer } -Script.fromHex = function (hex) { - return Script.fromBuffer(new Buffer(hex, 'hex')) -} +function decompile (buffer) { + typeforce(types.Buffer, buffer) -Script.EMPTY = Script.fromChunks([]) + var chunks = [] + var i = 0 -Script.prototype.equals = function (script) { - return bufferutils.equal(this.buffer, script.buffer) -} + while (i < buffer.length) { + var opcode = buffer.readUInt8(i) -Script.prototype.getHash = function () { - return crypto.hash160(this.buffer) -} + // data chunk + if ((opcode > opcodes.OP_0) && (opcode <= opcodes.OP_PUSHDATA4)) { + var d = bufferutils.readPushDataInt(buffer, i) -// 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 - })) + // 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 } var reverseOps = [] @@ -123,25 +117,9 @@ for (var op in opcodes) { reverseOps[code] = op } -Script.prototype.toASM = function () { - return this.chunks.map(function (chunk) { - // data chunk - if (Buffer.isBuffer(chunk)) { - return chunk.toString('hex') - - // opcode - } else { - return reverseOps[chunk] - } - }).join(' ') +module.exports = { + compile: compile, + decompile: decompile, + toASM: toASM, + fromASM: fromASM } - -Script.prototype.toBuffer = function () { - return this.buffer -} - -Script.prototype.toHex = function () { - return this.toBuffer().toString('hex') -} - -module.exports = Script diff --git a/src/scripts.js b/src/scripts.js index 5437cdb..ec836fb 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -40,105 +40,102 @@ function isCanonicalSignature (buffer) { return true } -function isPubKeyHashInput (script) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) - - return script.chunks.length === 2 && - isCanonicalSignature(script.chunks[0]) && - isCanonicalPubKey(script.chunks[1]) +function coerceBuffer (buffer) { + return types.Buffer(buffer) ? buffer : Script.compile(buffer) } -function isPubKeyHashOutput (script) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(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 +function coerceChunks (chunks) { + return types.Array(chunks) ? chunks : Script.decompile(chunks) } -function isPubKeyInput (script) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) +function isPubKeyHashInput (chunks) { + chunks = coerceChunks(chunks) - return script.chunks.length === 1 && - isCanonicalSignature(script.chunks[0]) + return chunks.length === 2 && + isCanonicalSignature(chunks[0]) && + isCanonicalPubKey(chunks[1]) } -function isPubKeyOutput (script) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) +function isPubKeyHashOutput (chunks) { + chunks = coerceChunks(chunks) - return script.chunks.length === 2 && - isCanonicalPubKey(script.chunks[0]) && - script.chunks[1] === ops.OP_CHECKSIG + 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 isScriptHashInput (script, allowIncomplete) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) +function isPubKeyInput (chunks) { + chunks = coerceChunks(chunks) - if (script.chunks.length < 2) return false + return chunks.length === 1 && + isCanonicalSignature(chunks[0]) +} - var lastChunk = script.chunks[script.chunks.length - 1] +function isPubKeyOutput (chunks) { + chunks = coerceChunks(chunks) + + return chunks.length === 2 && + isCanonicalPubKey(chunks[0]) && + chunks[1] === ops.OP_CHECKSIG +} + +function isScriptHashInput (chunks, allowIncomplete) { + chunks = coerceChunks(chunks) + if (chunks.length < 2) return false + + var lastChunk = chunks[chunks.length - 1] if (!Buffer.isBuffer(lastChunk)) return false - var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)).buffer - var redeemScript = lastChunk + var scriptSigChunks = chunks.slice(0, -1) + var redeemScriptChunks = Script.decompile(lastChunk) // is redeemScript a valid script? - if (Script.fromBuffer(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) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) +function isScriptHashOutput (chunks) { + chunks = coerceChunks(chunks) - 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 + 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) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) - - if (script.chunks.length < 2) return false - if (script.chunks[0] !== ops.OP_0) return false +function isMultisigInput (chunks, allowIncomplete) { + chunks = coerceChunks(chunks) + 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 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) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) +function isMultisigOutput (chunks) { + chunks = coerceChunks(chunks) + if (chunks.length < 4) return false + if (chunks[chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false - if (script.chunks.length < 4) return false - if (script.chunks[script.chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false - - var mOp = script.chunks[0] + 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 = script.chunks[script.chunks.length - 2] + 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 @@ -147,47 +144,43 @@ function isMultisigOutput (script) { 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) { - typeforce(types.Buffer, script) - script = Script.fromBuffer(script) - - return script.chunks[0] === ops.OP_RETURN +function isNullDataOutput (chunks) { + chunks = coerceChunks(chunks) + return chunks[0] === ops.OP_RETURN } -function classifyOutput (script) { - typeforce(types.Buffer, script) - - if (isPubKeyHashOutput(script)) { +function classifyOutput (chunks) { + chunks = coerceChunks(chunks) + 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' } return 'nonstandard' } -function classifyInput (script, allowIncomplete) { - typeforce(types.Buffer, script) - - if (isPubKeyHashInput(script)) { +function classifyInput (chunks, allowIncomplete) { + chunks = coerceChunks(chunks) + 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' } @@ -197,34 +190,21 @@ function classifyInput (script, allowIncomplete) { // Standard Script Templates // {pubKey} OP_CHECKSIG function pubKeyOutput (pubKey) { - return Script.fromChunks([ - pubKey, - ops.OP_CHECKSIG - ]).buffer + return Script.compile([pubKey, ops.OP_CHECKSIG]) } // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG -function pubKeyHashOutput (hash) { - typeforce(types.Hash160bit, hash) +function pubKeyHashOutput (pubKeyHash) { + typeforce(typeforce.Buffer, pubKeyHash) - return Script.fromChunks([ - ops.OP_DUP, - ops.OP_HASH160, - hash, - ops.OP_EQUALVERIFY, - ops.OP_CHECKSIG - ]).buffer + return Script.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) +function scriptHashOutput (scriptHash) { + typeforce(typeforce.Buffer, scriptHash) - return Script.fromChunks([ - ops.OP_HASH160, - hash, - ops.OP_EQUAL - ]).buffer + return Script.compile([ops.OP_HASH160, scriptHash, ops.OP_EQUAL]) } // m [pubKeys ...] n OP_CHECKMULTISIG @@ -234,46 +214,47 @@ function multisigOutput (m, pubKeys) { var n = pubKeys.length if (n < m) throw new Error('Not enough pubKeys provided') - return Script.fromChunks([].concat( + return Script.compile([].concat( (ops.OP_1 - 1) + m, pubKeys, (ops.OP_1 - 1) + n, ops.OP_CHECKMULTISIG - )).buffer + )) } // {signature} function pubKeyInput (signature) { typeforce(types.Buffer, signature) - return Script.fromChunks([signature]).buffer + return Script.compile([signature]) } // {signature} {pubKey} function pubKeyHashInput (signature, pubKey) { typeforce(types.tuple(types.Buffer, types.Buffer), arguments) - return Script.fromChunks([signature, pubKey]).buffer + return Script.compile([signature, pubKey]) } // {serialized scriptPubKey script} -function scriptHashInput (scriptSig, scriptPubKey) { - var scriptSigChunks = Script.fromBuffer(scriptSig).chunks +function scriptHashInput (scriptSig, scriptPubKeyBuffer) { + scriptSig = coerceChunks(scriptSig) + scriptPubKeyBuffer = coerceBuffer(scriptPubKeyBuffer) - return Script.fromChunks([].concat( - scriptSigChunks, - scriptPubKey - )).buffer + return Script.compile([].concat( + scriptSig, + scriptPubKeyBuffer + )) } // OP_0 [signatures ...] function multisigInput (signatures, scriptPubKey) { if (scriptPubKey) { if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey') - scriptPubKey = Script.fromBuffer(scriptPubKey) + scriptPubKey = coerceChunks(scriptPubKey) - var mOp = scriptPubKey.chunks[0] - var nOp = scriptPubKey.chunks[scriptPubKey.chunks.length - 2] + var mOp = scriptPubKey[0] + var nOp = scriptPubKey[scriptPubKey.length - 2] var m = mOp - (ops.OP_1 - 1) var n = nOp - (ops.OP_1 - 1) @@ -281,11 +262,11 @@ function multisigInput (signatures, scriptPubKey) { if (signatures.length > n) throw new Error('Too many signatures provided') } - return Script.fromChunks([].concat(ops.OP_0, signatures)).buffer + return Script.compile([].concat(ops.OP_0, signatures)) } function nullDataOutput (data) { - return Script.fromChunks([ops.OP_RETURN, data]).buffer + return Script.compile([ops.OP_RETURN, data]) } module.exports = { diff --git a/src/transaction.js b/src/transaction.js index 0933589..b5a289e 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -193,7 +193,9 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT // in case concatenating two scripts ends up with two codeseparators, // or an extra one at the end, this prevents all those possible incompatibilities. - var hashScript = Script.fromBuffer(prevOutScript).without(opcodes.OP_CODESEPARATOR).buffer + var hashScript = Script.compile(Script.decompile(prevOutScript).filter(function (x) { + return x !== opcodes.OP_CODESEPARATOR + })) var i // blank out other inputs' signatures diff --git a/src/transaction_builder.js b/src/transaction_builder.js index fd6ab38..8683699 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -13,7 +13,7 @@ var Transaction = require('./transaction') function extractInput (txIn) { var redeemScript var scriptSig = txIn.script - var scriptSigChunks = Script.fromBuffer(scriptSig).chunks + var scriptSigChunks = Script.decompile(scriptSig) var prevOutScript var prevOutType = scripts.classifyInput(scriptSig, true) @@ -24,7 +24,7 @@ function extractInput (txIn) { redeemScript = scriptSigChunks.slice(-1)[0] prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) - scriptSig = Script.fromChunks(scriptSigChunks.slice(0, -1)).buffer + scriptSig = Script.compile(scriptSigChunks.slice(0, -1)) scriptSigChunks = scriptSigChunks.slice(0, -1) scriptType = scripts.classifyInput(scriptSig, true) @@ -51,7 +51,7 @@ function extractInput (txIn) { signatures = [parsed.signature] if (redeemScript) { - pubKeys = Script.fromBuffer(redeemScript).chunks.slice(0, 1) + pubKeys = Script.decompile(redeemScript).slice(0, 1) } break @@ -67,7 +67,7 @@ function extractInput (txIn) { }) if (redeemScript) { - pubKeys = Script.fromBuffer(redeemScript).chunks.slice(1, -2) + pubKeys = Script.decompile(redeemScript).slice(1, -2) } break @@ -142,8 +142,8 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu var input = {} if (prevOutScript) { - var prevOutType = scripts.classifyOutput(prevOutScript) - var prevOutScriptChunks = Script.fromBuffer(prevOutScript).chunks + var prevOutScriptChunks = Script.decompile(prevOutScript) + var prevOutType = scripts.classifyOutput(prevOutScriptChunks) // if we can, extract pubKey information switch (prevOutType) { @@ -313,14 +313,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash if (input.prevOutScript) { if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH') - var scriptHash = Script.fromBuffer(input.prevOutScript).chunks[1] + var scriptHash = Script.decompile(input.prevOutScript)[1] if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex')) } var scriptType = scripts.classifyOutput(redeemScript) if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')') - var redeemScriptChunks = Script.fromBuffer(redeemScript).chunks + var redeemScriptChunks = Script.decompile(redeemScript) var pubKeys = [] switch (scriptType) { case 'multisig': diff --git a/test/address.js b/test/address.js index ab37ed8..bdf52a6 100644 --- a/test/address.js +++ b/test/address.js @@ -31,7 +31,7 @@ describe('Address', function () { describe('fromOutputScript', function () { fixtures.valid.forEach(function (f) { it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { - var script = Script.fromASM(f.script).buffer + var script = Script.fromASM(f.script) var address = Address.fromOutputScript(script, networks[f.network]) assert.strictEqual(address, f.base58check) @@ -40,7 +40,7 @@ describe('Address', function () { fixtures.invalid.fromOutputScript.forEach(function (f) { it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () { - var script = Script.fromASM(f.script).buffer + var script = Script.fromASM(f.script) assert.throws(function () { Address.fromOutputScript(script) @@ -66,7 +66,7 @@ describe('Address', function () { it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { var script = Address.toOutputScript(f.base58check, network) - assert.strictEqual(Script.fromBuffer(script).toASM(), f.script) + assert.strictEqual(Script.toASM(script), f.script) }) }) diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 769a612..49d160a 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -227,8 +227,9 @@ describe('Bitcoin-core', function () { var transaction = Transaction.fromHex(txHex) assert.strictEqual(transaction.toHex(), txHex) - var script = Script.fromHex(scriptHex).buffer - assert.strictEqual(Script.fromBuffer(script).toHex(), scriptHex) + var script = new Buffer(scriptHex, 'hex') + var scriptChunks = Script.decompile(script) + assert.strictEqual(Script.compile(scriptChunks).toString('hex'), scriptHex) var hash = transaction.hashForSignature(inIndex, script, hashType) assert.deepEqual(hash, expectedHash) diff --git a/test/script.js b/test/script.js index 8c20403..0a885b5 100644 --- a/test/script.js +++ b/test/script.js @@ -2,30 +2,12 @@ /* eslint-disable no-new */ var assert = require('assert') -var opcodes = require('../src/opcodes') - var Script = require('../src/script') +var OPS = require('../src/opcodes') var fixtures = require('./fixtures/script.json') describe('Script', function () { - describe('constructor', function () { - it('accepts valid parameters', function () { - var buffer = new Buffer([1]) - var chunks = [1] - var script = new Script(buffer, chunks) - - assert.strictEqual(script.buffer, buffer) - assert.strictEqual(script.chunks, chunks) - }) - - it('throws an error when input is not an array', function () { - assert.throws(function () { - new Script({}) - }, /Expected Buffer, got/) - }) - }) - describe('fromASM/toASM', function () { fixtures.valid.forEach(function (f) { if (!f.asm) return @@ -33,53 +15,23 @@ describe('Script', function () { it('decodes/encodes ' + f.description, function () { var script = Script.fromASM(f.asm) - assert.strictEqual(script.toASM(), f.asm) - assert.strictEqual(script.buffer.toString('hex'), f.hex) + assert.strictEqual(Script.toASM(script), f.asm) }) }) }) - describe('fromHex/toHex', function () { - fixtures.valid.forEach(function (f) { - it('decodes/encodes ' + f.description, function () { - var script = Script.fromHex(f.hex) - - assert.strictEqual(script.toASM(), f.asm) - assert.strictEqual(script.buffer.toString('hex'), f.hex) - }) - }) - }) - - describe('fromChunks', function () { + describe('compile', function () { it('should match expected behaviour', function () { var hash = new Buffer(32) hash.fill(0) - var script = Script.fromChunks([ - opcodes.OP_HASH160, + var script = Script.compile([ + OPS.OP_HASH160, hash, - opcodes.OP_EQUAL + OPS.OP_EQUAL ]) - assert.strictEqual(script.buffer.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087') - }) - }) - - describe('without', function () { - var hex = 'a914e8c300c87986efa94c37c0519929019ef86eb5b487' - var script = Script.fromHex(hex) - - it('should return a script without the given value', function () { - var subScript = script.without(opcodes.OP_HASH160) - - assert.strictEqual(subScript.buffer.toString('hex'), '14e8c300c87986efa94c37c0519929019ef86eb5b487') - }) - - it('shouldnt mutate the original script', function () { - var subScript = script.without(opcodes.OP_EQUAL) - - assert.notEqual(subScript.buffer.toString('hex'), hex) - assert.strictEqual(script.buffer.toString('hex'), hex) + assert.strictEqual(script.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087') }) }) }) diff --git a/test/scripts.js b/test/scripts.js index 64635ce..bdb9264 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -19,7 +19,7 @@ describe('Scripts', function () { if (!f.scriptSig) return it('classifies ' + f.scriptSig + ' as ' + f.type, function () { - var script = Script.fromASM(f.scriptSig).buffer + var script = Script.fromASM(f.scriptSig) var type = scripts.classifyInput(script) assert.strictEqual(type, f.type) @@ -31,7 +31,7 @@ describe('Scripts', function () { if (!f.typeIncomplete) return it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () { - var script = Script.fromASM(f.scriptSig).buffer + var script = Script.fromASM(f.scriptSig) var type = scripts.classifyInput(script, true) assert.strictEqual(type, f.typeIncomplete) @@ -44,7 +44,7 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () { - var script = Script.fromASM(f.scriptPubKey).buffer + var script = Script.fromASM(f.scriptPubKey) var type = scripts.classifyOutput(script) assert.strictEqual(type, f.type) @@ -67,9 +67,9 @@ describe('Scripts', function () { var script if (f.scriptSig) { - script = Script.fromASM(f.scriptSig).buffer + script = Script.fromASM(f.scriptSig) } else { - script = Script.fromHex(f.scriptSigHex).buffer + script = Script.fromHex(f.scriptSigHex) } it('returns ' + expected + ' for ' + f.scriptSig, function () { @@ -94,9 +94,9 @@ describe('Scripts', function () { var script if (f.scriptSig) { - script = Script.fromASM(f.scriptSig).buffer + script = Script.fromASM(f.scriptSig) } else { - script = Script.fromHex(f.scriptSigHex).buffer + script = Script.fromHex(f.scriptSigHex) } assert.strictEqual(inputFn(script), false) @@ -111,7 +111,7 @@ describe('Scripts', function () { if (outputFn && f.scriptPubKey) { it('returns ' + expected + ' for ' + f.scriptPubKey, function () { - var script = Script.fromASM(f.scriptPubKey).buffer + var script = Script.fromASM(f.scriptPubKey) assert.strictEqual(outputFn(script), expected) }) @@ -123,7 +123,7 @@ describe('Scripts', function () { fixtures.invalid[outputFnName].forEach(function (f) { if (outputFn && f.scriptPubKey) { it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () { - var script = Script.fromASM(f.scriptPubKey).buffer + var script = Script.fromASM(f.scriptPubKey) assert.strictEqual(outputFn(script), false) }) @@ -140,7 +140,7 @@ describe('Scripts', function () { var signature = new Buffer(f.signature, 'hex') var scriptSig = scripts.pubKeyInput(signature) - assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) + assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) }) }) }) @@ -152,7 +152,8 @@ describe('Scripts', function () { it('returns ' + f.scriptPubKey, function () { var pubKey = new Buffer(f.pubKey, 'hex') var scriptPubKey = scripts.pubKeyOutput(pubKey) - assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) + + assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -167,7 +168,7 @@ describe('Scripts', function () { var signature = new Buffer(f.signature, 'hex') var scriptSig = scripts.pubKeyHashInput(signature, pubKey) - assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) + assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) }) }) }) @@ -181,7 +182,7 @@ describe('Scripts', function () { it('returns ' + f.scriptPubKey, function () { var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash) - assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) + assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -196,12 +197,12 @@ describe('Scripts', function () { }) var scriptSig = scripts.multisigInput(signatures) - assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) + assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) }) }) fixtures.invalid.multisigInput.forEach(function (f) { - var scriptPubKey = Script.fromASM(f.scriptPubKey).buffer + var scriptPubKey = Script.fromASM(f.scriptPubKey) it('throws on ' + f.exception, function () { var signatures = f.signatures.map(function (signature) { @@ -223,7 +224,7 @@ describe('Scripts', function () { var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) + assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) }) }) @@ -244,16 +245,17 @@ describe('Scripts', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'scripthash') return - var redeemScript = Script.fromASM(f.redeemScript).buffer - var redeemScriptSig = Script.fromASM(f.redeemScriptSig).buffer + var redeemScript = Script.fromASM(f.redeemScript) + var redeemScriptSig = Script.fromASM(f.redeemScriptSig) it('returns ' + f.scriptSig, function () { var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) if (f.scriptSig) { - assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig) + assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) + } else { - assert.strictEqual(Script.fromBuffer(scriptSig).toHex(), f.scriptSigHex) + assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex) } }) }) @@ -265,10 +267,10 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('returns ' + f.scriptPubKey, function () { - var redeemScript = Script.fromASM(f.redeemScript).buffer + var redeemScript = Script.compile(Script.fromASM(f.redeemScript)) var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) - assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) + assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -281,7 +283,7 @@ describe('Scripts', function () { var scriptPubKey = scripts.nullDataOutput(data) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey) + assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) }) }) }) diff --git a/test/transaction.js b/test/transaction.js index 6ac9bb8..af8639d 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -20,8 +20,9 @@ describe('Transaction', function () { if (txIn.data) { var data = new Buffer(txIn.data, 'hex') script = data + } else if (txIn.script) { - script = Script.fromASM(txIn.script).buffer + script = Script.compile(Script.fromASM(txIn.script)) } tx.addInput(txHash, txIn.index, txIn.sequence, script) @@ -33,8 +34,9 @@ describe('Transaction', function () { if (txOut.data) { var data = new Buffer(txOut.data, 'hex') script = data + } else if (txOut.script) { - script = Script.fromASM(txOut.script).buffer + script = Script.compile(Script.fromASM(txOut.script)) } tx.addOutput(script, txOut.value) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 0367aac..8737fdf 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -22,14 +22,14 @@ function construct (f, sign) { var prevTxScript if (input.prevTxScript) { - prevTxScript = Script.fromASM(input.prevTxScript).buffer + prevTxScript = Script.compile(Script.fromASM(input.prevTxScript)) } txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function (output) { - var script = Script.fromASM(output.script).buffer + var script = Script.compile(Script.fromASM(output.script)) txb.addOutput(script, output.value) }) @@ -41,7 +41,7 @@ function construct (f, sign) { var redeemScript if (sign.redeemScript) { - redeemScript = Script.fromASM(sign.redeemScript).buffer + redeemScript = Script.compile(Script.fromASM(sign.redeemScript)) } txb.sign(index, keyPair, redeemScript, sign.hashType) @@ -200,7 +200,7 @@ describe('TransactionBuilder', function () { var redeemScript if (sign.redeemScript) { - redeemScript = Script.fromASM(sign.redeemScript).buffer + redeemScript = Script.compile(Script.fromASM(sign.redeemScript)) } if (!sign.throws) { @@ -262,7 +262,7 @@ describe('TransactionBuilder', function () { var network = NETWORKS[f.network] f.inputs.forEach(function (input, i) { - var redeemScript = Script.fromASM(input.redeemScript).buffer + var redeemScript = Script.compile(Script.fromASM(input.redeemScript)) input.signs.forEach(function (sign) { // rebuild the transaction each-time after the first @@ -272,11 +272,11 @@ describe('TransactionBuilder', function () { var scriptSig = tx.ins[i].script // ignore OP_0 on the front, ignore redeemScript - var signatures = Script.fromBuffer(scriptSig).chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) + var signatures = Script.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) // rebuild/replace the scriptSig without them var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript) - assert.strictEqual(Script.fromBuffer(replacement).toASM(), sign.scriptSigFiltered) + assert.strictEqual(Script.toASM(replacement), sign.scriptSigFiltered) tx.ins[i].script = replacement } @@ -292,7 +292,7 @@ describe('TransactionBuilder', function () { tx = txb.buildIncomplete() // now verify the serialized scriptSig is as expected - assert.strictEqual(Script.fromBuffer(tx.ins[i].script).toASM(), sign.scriptSig) + assert.strictEqual(Script.toASM(tx.ins[i].script), sign.scriptSig) }) }) @@ -309,7 +309,7 @@ describe('TransactionBuilder', function () { txb = TransactionBuilder.fromTransaction(lameTx, network) - var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG').buffer + var redeemScript = Script.compile(Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG')) var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network) txb.sign(0, keyPair, redeemScript) From 2aff7bd899e0fedc22dca021cebaaf034d400c8d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 13 Aug 2015 19:00:47 +1000 Subject: [PATCH 03/13] tests: add more tests for Script compile/decompile --- test/script.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/test/script.js b/test/script.js index 0a885b5..b3e2258 100644 --- a/test/script.js +++ b/test/script.js @@ -3,7 +3,6 @@ var assert = require('assert') var Script = require('../src/script') -var OPS = require('../src/opcodes') var fixtures = require('./fixtures/script.json') @@ -21,17 +20,24 @@ describe('Script', function () { }) describe('compile', function () { - it('should match expected behaviour', function () { - var hash = new Buffer(32) - hash.fill(0) + fixtures.valid.forEach(function (f) { + if (!f.asm) return - var script = Script.compile([ - OPS.OP_HASH160, - hash, - OPS.OP_EQUAL - ]) + it('decodes/encodes ' + f.description, function () { + var script = Script.fromASM(f.asm) - assert.strictEqual(script.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087') + assert.strictEqual(Script.compile(script).toString('hex'), f.hex) + }) + }) + }) + + describe('decompile', function () { + fixtures.valid.forEach(function (f) { + it('decodes/encodes ' + f.description, function () { + var script = Script.decompile(new Buffer(f.hex, 'hex')) + + assert.strictEqual(Script.toASM(script), f.asm) + }) }) }) }) From 812d74aa0ad178c6ac926d5aa2880aaea8c842b9 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 14 Aug 2015 10:26:24 +1000 Subject: [PATCH 04/13] coerce wherever necessary for now --- src/address.js | 5 +++-- src/script.js | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/address.js b/src/address.js index 9b9da05..0bf33e6 100644 --- a/src/address.js +++ b/src/address.js @@ -20,8 +20,9 @@ function fromBase58Check (address) { function fromOutputScript (script, network) { network = network || networks.bitcoin - if (scripts.isPubKeyHashOutput(script)) return toBase58Check(script[2], network.pubKeyHash) - if (scripts.isScriptHashOutput(script)) return toBase58Check(script[1], network.scriptHash) + var chunks = Script.decompile(script) + if (scripts.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash) + if (scripts.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash) throw new Error(Script.toASM(script) + ' has no matching Address') } diff --git a/src/script.js b/src/script.js index 0e29f73..6c75f11 100644 --- a/src/script.js +++ b/src/script.js @@ -4,7 +4,15 @@ var typeforce = require('typeforce') var types = require('./types') function coerceChunks (chunks) { - return types.Array(chunks) ? chunks : decompile(chunks) + if (types.Array(chunks)) return chunks + + return decompile(chunks) +} + +function coerceBuffer (buffer) { + if (types.Buffer(buffer)) return buffer + + return compile(buffer) } function toASM (chunks) { @@ -41,6 +49,8 @@ function fromASM (asm) { } function compile (chunks) { + chunks = coerceChunks(chunks) + typeforce(types.Array, chunks) var bufferSize = chunks.reduce(function (accum, chunk) { @@ -76,6 +86,8 @@ function compile (chunks) { } function decompile (buffer) { + buffer = coerceBuffer(buffer) + typeforce(types.Buffer, buffer) var chunks = [] From 151386c46d30d04a57240e044442196a8b53ef28 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 14 Aug 2015 10:31:48 +1000 Subject: [PATCH 05/13] scripts: remove coercion, fromASM returns Buffer --- src/script.js | 28 ++++++--------- src/scripts.js | 72 +++++++++++++++++-------------------- src/transaction_builder.js | 10 ++++-- test/scripts.js | 2 +- test/transaction.js | 4 +-- test/transaction_builder.js | 12 +++---- 6 files changed, 60 insertions(+), 68 deletions(-) diff --git a/src/script.js b/src/script.js index 6c75f11..564e5a7 100644 --- a/src/script.js +++ b/src/script.js @@ -3,20 +3,10 @@ var opcodes = require('./opcodes') var typeforce = require('typeforce') var types = require('./types') -function coerceChunks (chunks) { - if (types.Array(chunks)) return chunks - - return decompile(chunks) -} - -function coerceBuffer (buffer) { - if (types.Buffer(buffer)) return buffer - - return compile(buffer) -} - function toASM (chunks) { - chunks = coerceChunks(chunks) + if (types.Buffer(chunks)) { + chunks = decompile(chunks) + } return chunks.map(function (chunk) { // data chunk @@ -45,11 +35,12 @@ function fromASM (asm) { } }) - return chunks + return compile(chunks) } function compile (chunks) { - chunks = coerceChunks(chunks) + // TODO: remove me + if (types.Buffer(chunks)) return chunks typeforce(types.Array, chunks) @@ -86,7 +77,8 @@ function compile (chunks) { } function decompile (buffer) { - buffer = coerceBuffer(buffer) + // TODO: remove me + if (types.Array(buffer)) return buffer typeforce(types.Buffer, buffer) @@ -132,6 +124,6 @@ for (var op in opcodes) { module.exports = { compile: compile, decompile: decompile, - toASM: toASM, - fromASM: fromASM + fromASM: fromASM, + toASM: toASM } diff --git a/src/scripts.js b/src/scripts.js index ec836fb..52c2b6c 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -40,24 +40,16 @@ function isCanonicalSignature (buffer) { return true } -function coerceBuffer (buffer) { - return types.Buffer(buffer) ? buffer : Script.compile(buffer) -} - -function coerceChunks (chunks) { - return types.Array(chunks) ? chunks : Script.decompile(chunks) -} - -function isPubKeyHashInput (chunks) { - chunks = coerceChunks(chunks) +function isPubKeyHashInput (script) { + var chunks = Script.decompile(script) return chunks.length === 2 && isCanonicalSignature(chunks[0]) && isCanonicalPubKey(chunks[1]) } -function isPubKeyHashOutput (chunks) { - chunks = coerceChunks(chunks) +function isPubKeyHashOutput (script) { + var chunks = Script.decompile(script) return chunks.length === 5 && chunks[0] === ops.OP_DUP && @@ -68,23 +60,23 @@ function isPubKeyHashOutput (chunks) { chunks[4] === ops.OP_CHECKSIG } -function isPubKeyInput (chunks) { - chunks = coerceChunks(chunks) +function isPubKeyInput (script) { + var chunks = Script.decompile(script) return chunks.length === 1 && isCanonicalSignature(chunks[0]) } -function isPubKeyOutput (chunks) { - chunks = coerceChunks(chunks) +function isPubKeyOutput (script) { + var chunks = Script.decompile(script) return chunks.length === 2 && isCanonicalPubKey(chunks[0]) && chunks[1] === ops.OP_CHECKSIG } -function isScriptHashInput (chunks, allowIncomplete) { - chunks = coerceChunks(chunks) +function isScriptHashInput (script, allowIncomplete) { + var chunks = Script.decompile(script) if (chunks.length < 2) return false var lastChunk = chunks[chunks.length - 1] @@ -99,8 +91,8 @@ function isScriptHashInput (chunks, allowIncomplete) { return classifyInput(scriptSigChunks, allowIncomplete) === classifyOutput(redeemScriptChunks) } -function isScriptHashOutput (chunks) { - chunks = coerceChunks(chunks) +function isScriptHashOutput (script) { + var chunks = Script.decompile(script) return chunks.length === 3 && chunks[0] === ops.OP_HASH160 && @@ -111,8 +103,8 @@ function isScriptHashOutput (chunks) { // allowIncomplete is to account for combining signatures // See https://github.com/bitcoin/bitcoin/blob/f425050546644a36b0b8e0eb2f6934a3e0f6f80f/src/script/sign.cpp#L195-L197 -function isMultisigInput (chunks, allowIncomplete) { - chunks = coerceChunks(chunks) +function isMultisigInput (script, allowIncomplete) { + var chunks = Script.decompile(script) if (chunks.length < 2) return false if (chunks[0] !== ops.OP_0) return false @@ -125,8 +117,8 @@ function isMultisigInput (chunks, allowIncomplete) { return chunks.slice(1).every(isCanonicalSignature) } -function isMultisigOutput (chunks) { - chunks = coerceChunks(chunks) +function isMultisigOutput (script) { + var chunks = Script.decompile(script) if (chunks.length < 4) return false if (chunks[chunks.length - 1] !== ops.OP_CHECKMULTISIG) return false @@ -150,13 +142,14 @@ function isMultisigOutput (chunks) { return pubKeys.every(isCanonicalPubKey) } -function isNullDataOutput (chunks) { - chunks = coerceChunks(chunks) +function isNullDataOutput (script) { + var chunks = Script.decompile(script) return chunks[0] === ops.OP_RETURN } -function classifyOutput (chunks) { - chunks = coerceChunks(chunks) +function classifyOutput (script) { + var chunks = Script.decompile(script) + if (isPubKeyHashOutput(chunks)) { return 'pubkeyhash' } else if (isScriptHashOutput(chunks)) { @@ -172,8 +165,9 @@ function classifyOutput (chunks) { return 'nonstandard' } -function classifyInput (chunks, allowIncomplete) { - chunks = coerceChunks(chunks) +function classifyInput (script, allowIncomplete) { + var chunks = Script.decompile(script) + if (isPubKeyHashInput(chunks)) { return 'pubkeyhash' } else if (isMultisigInput(chunks, allowIncomplete)) { @@ -237,24 +231,24 @@ function pubKeyHashInput (signature, pubKey) { } // {serialized scriptPubKey script} -function scriptHashInput (scriptSig, scriptPubKeyBuffer) { - scriptSig = coerceChunks(scriptSig) - scriptPubKeyBuffer = coerceBuffer(scriptPubKeyBuffer) +function scriptHashInput (scriptSig, scriptPubKey) { + var scriptSigChunks = Script.decompile(scriptSig) + var serializedScriptPubKey = Script.compile(scriptPubKey) return Script.compile([].concat( - scriptSig, - scriptPubKeyBuffer + scriptSigChunks, + serializedScriptPubKey )) } // OP_0 [signatures ...] function multisigInput (signatures, scriptPubKey) { if (scriptPubKey) { - if (!isMultisigOutput(scriptPubKey)) throw new Error('Expected multisig scriptPubKey') - scriptPubKey = coerceChunks(scriptPubKey) + var chunks = Script.decompile(scriptPubKey) + if (!isMultisigOutput(chunks)) throw new Error('Expected multisig scriptPubKey') - var mOp = scriptPubKey[0] - var nOp = scriptPubKey[scriptPubKey.length - 2] + var mOp = chunks[0] + var nOp = chunks[chunks.length - 2] var m = mOp - (ops.OP_1 - 1) var n = nOp - (ops.OP_1 - 1) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 8683699..cca8b35 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -32,6 +32,12 @@ function extractInput (txIn) { scriptType = prevOutType } + // pre-empt redeemScript decompilation + var redeemScriptChunks + if (redeemScript) { + redeemScriptChunks = Script.decompile(redeemScript) + } + // Extract hashType, pubKeys and signatures var hashType, parsed, pubKeys, signatures @@ -51,7 +57,7 @@ function extractInput (txIn) { signatures = [parsed.signature] if (redeemScript) { - pubKeys = Script.decompile(redeemScript).slice(0, 1) + pubKeys = redeemScriptChunks.slice(0, 1) } break @@ -67,7 +73,7 @@ function extractInput (txIn) { }) if (redeemScript) { - pubKeys = Script.decompile(redeemScript).slice(1, -2) + pubKeys = redeemScriptChunks.slice(1, -2) } break diff --git a/test/scripts.js b/test/scripts.js index bdb9264..f3a8b57 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -267,7 +267,7 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('returns ' + f.scriptPubKey, function () { - var redeemScript = Script.compile(Script.fromASM(f.redeemScript)) + var redeemScript = Script.fromASM(f.redeemScript) var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) diff --git a/test/transaction.js b/test/transaction.js index af8639d..cd7f9be 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -22,7 +22,7 @@ describe('Transaction', function () { script = data } else if (txIn.script) { - script = Script.compile(Script.fromASM(txIn.script)) + script = Script.fromASM(txIn.script) } tx.addInput(txHash, txIn.index, txIn.sequence, script) @@ -36,7 +36,7 @@ describe('Transaction', function () { script = data } else if (txOut.script) { - script = Script.compile(Script.fromASM(txOut.script)) + script = Script.fromASM(txOut.script) } tx.addOutput(script, txOut.value) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 8737fdf..57094f4 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -22,14 +22,14 @@ function construct (f, sign) { var prevTxScript if (input.prevTxScript) { - prevTxScript = Script.compile(Script.fromASM(input.prevTxScript)) + prevTxScript = Script.fromASM(input.prevTxScript) } txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function (output) { - var script = Script.compile(Script.fromASM(output.script)) + var script = Script.fromASM(output.script) txb.addOutput(script, output.value) }) @@ -41,7 +41,7 @@ function construct (f, sign) { var redeemScript if (sign.redeemScript) { - redeemScript = Script.compile(Script.fromASM(sign.redeemScript)) + redeemScript = Script.fromASM(sign.redeemScript) } txb.sign(index, keyPair, redeemScript, sign.hashType) @@ -200,7 +200,7 @@ describe('TransactionBuilder', function () { var redeemScript if (sign.redeemScript) { - redeemScript = Script.compile(Script.fromASM(sign.redeemScript)) + redeemScript = Script.fromASM(sign.redeemScript) } if (!sign.throws) { @@ -262,7 +262,7 @@ describe('TransactionBuilder', function () { var network = NETWORKS[f.network] f.inputs.forEach(function (input, i) { - var redeemScript = Script.compile(Script.fromASM(input.redeemScript)) + var redeemScript = Script.fromASM(input.redeemScript) input.signs.forEach(function (sign) { // rebuild the transaction each-time after the first @@ -309,7 +309,7 @@ describe('TransactionBuilder', function () { txb = TransactionBuilder.fromTransaction(lameTx, network) - var redeemScript = Script.compile(Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG')) + var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG') var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network) txb.sign(0, keyPair, redeemScript) From e2abe2b3af5a0ae58ea133c921bcd8b843f7f2ef Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 14 Aug 2015 11:16:17 +1000 Subject: [PATCH 06/13] merge Script/scripts --- src/address.js | 8 +- src/index.js | 14 +-- src/script.js | 129 --------------------- src/scripts.js | 224 ++++++++++++++++++++++++++++-------- src/transaction.js | 7 +- src/transaction_builder.js | 13 +-- test/address.js | 8 +- test/bitcoin.core.js | 10 +- test/fixtures/script.json | 96 ---------------- test/fixtures/scripts.json | 71 ++++++++++-- test/script.js | 43 ------- test/scripts.js | 120 ++++++++++++++----- test/transaction.js | 6 +- test/transaction_builder.js | 19 ++- 14 files changed, 365 insertions(+), 403 deletions(-) delete mode 100644 src/script.js delete mode 100644 test/fixtures/script.json delete mode 100644 test/script.js diff --git a/src/address.js b/src/address.js index 0bf33e6..f5fb215 100644 --- a/src/address.js +++ b/src/address.js @@ -1,9 +1,7 @@ var base58check = require('bs58check') -var typeforce = require('typeforce') var networks = require('./networks') - -var Script = require('./script') var scripts = require('./scripts') +var typeforce = require('typeforce') var types = require('./types') function fromBase58Check (address) { @@ -20,11 +18,11 @@ function fromBase58Check (address) { function fromOutputScript (script, network) { network = network || networks.bitcoin - var chunks = Script.decompile(script) + var chunks = scripts.decompile(script) if (scripts.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash) if (scripts.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash) - throw new Error(Script.toASM(script) + ' has no matching Address') + throw new Error(scripts.toASM(chunks) + ' has no matching Address') } function toBase58Check (hash, version) { diff --git a/src/index.js b/src/index.js index 8f4a8d7..80d8f49 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,16 @@ module.exports = { Address: require('./address'), Block: require('./block'), - bufferutils: require('./bufferutils'), - crypto: require('./crypto'), ECPair: require('./ecpair'), ECSignature: require('./ecsignature'), - message: require('./message'), - opcodes: require('./opcodes'), HDNode: require('./hdnode'), - Script: require('./script'), - scripts: require('./scripts'), Transaction: require('./transaction'), TransactionBuilder: require('./transaction_builder'), - networks: require('./networks') + + bufferutils: require('./bufferutils'), + crypto: require('./crypto'), + message: require('./message'), + networks: require('./networks'), + opcodes: require('./opcodes'), + scripts: require('./scripts') } diff --git a/src/script.js b/src/script.js deleted file mode 100644 index 564e5a7..0000000 --- a/src/script.js +++ /dev/null @@ -1,129 +0,0 @@ -var bufferutils = require('./bufferutils') -var opcodes = require('./opcodes') -var typeforce = require('typeforce') -var types = require('./types') - -function toASM (chunks) { - if (types.Buffer(chunks)) { - chunks = decompile(chunks) - } - - return chunks.map(function (chunk) { - // data chunk - if (Buffer.isBuffer(chunk)) { - return chunk.toString('hex') - - // opcode - } else { - return reverseOps[chunk] - } - }).join(' ') -} - -function fromASM (asm) { - typeforce(types.String, asm) - - var strChunks = asm.split(' ') - var chunks = strChunks.map(function (strChunk) { - // opcode - if (strChunk in opcodes) { - return opcodes[strChunk] - - // data chunk - } else { - return new Buffer(strChunk, 'hex') - } - }) - - return compile(chunks) -} - -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 > opcodes.OP_0) && (opcode <= opcodes.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 -} - -var reverseOps = [] -for (var op in opcodes) { - var code = opcodes[op] - reverseOps[code] = op -} - -module.exports = { - compile: compile, - decompile: decompile, - fromASM: fromASM, - toASM: toASM -} diff --git a/src/scripts.js b/src/scripts.js index 52c2b6c..89a19d0 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -1,12 +1,129 @@ -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 chunk + if (Buffer.isBuffer(chunk)) { + return chunk.toString('hex') + + // opcode + } else { + return REVERSE_OPS[chunk] + } + }).join(' ') +} + +function fromASM (asm) { + typeforce(types.String, asm) + + var strChunks = asm.split(' ') + var chunks = strChunks.map(function (strChunk) { + // opcode + if (strChunk in OPS) { + return OPS[strChunk] + + // data chunk + } else { + return new Buffer(strChunk, 'hex') + } + }) + + return compile(chunks) +} + +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,7 +158,7 @@ function isCanonicalSignature (buffer) { } function isPubKeyHashInput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) return chunks.length === 2 && isCanonicalSignature(chunks[0]) && @@ -49,41 +166,41 @@ function isPubKeyHashInput (script) { } function isPubKeyHashOutput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) return chunks.length === 5 && - chunks[0] === ops.OP_DUP && - chunks[1] === ops.OP_HASH160 && + 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 + chunks[3] === OPS.OP_EQUALVERIFY && + chunks[4] === OPS.OP_CHECKSIG } function isPubKeyInput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) return chunks.length === 1 && isCanonicalSignature(chunks[0]) } function isPubKeyOutput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) return chunks.length === 2 && isCanonicalPubKey(chunks[0]) && - chunks[1] === ops.OP_CHECKSIG + chunks[1] === OPS.OP_CHECKSIG } function isScriptHashInput (script, allowIncomplete) { - var chunks = Script.decompile(script) + var chunks = decompile(script) if (chunks.length < 2) return false var lastChunk = chunks[chunks.length - 1] if (!Buffer.isBuffer(lastChunk)) return false var scriptSigChunks = chunks.slice(0, -1) - var redeemScriptChunks = Script.decompile(lastChunk) + var redeemScriptChunks = decompile(lastChunk) // is redeemScript a valid script? if (redeemScriptChunks.length === 0) return false @@ -92,25 +209,25 @@ function isScriptHashInput (script, allowIncomplete) { } function isScriptHashOutput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) return chunks.length === 3 && - chunks[0] === ops.OP_HASH160 && + chunks[0] === OPS.OP_HASH160 && Buffer.isBuffer(chunks[1]) && chunks[1].length === 20 && - chunks[2] === ops.OP_EQUAL + 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) { - var chunks = Script.decompile(script) + var chunks = decompile(script) if (chunks.length < 2) return false - if (chunks[0] !== ops.OP_0) return false + if (chunks[0] !== OPS.OP_0) return false if (allowIncomplete) { return chunks.slice(1).every(function (chunk) { - return chunk === ops.OP_0 || isCanonicalSignature(chunk) + return chunk === OPS.OP_0 || isCanonicalSignature(chunk) }) } @@ -118,22 +235,22 @@ function isMultisigInput (script, allowIncomplete) { } function isMultisigOutput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) if (chunks.length < 4) return false - if (chunks[chunks.length - 1] !== ops.OP_CHECKMULTISIG) 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 + 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 + 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 m = mOp - (OPS.OP_1 - 1) + var n = nOp - (OPS.OP_1 - 1) if (n < m) return false var pubKeys = chunks.slice(1, -2) @@ -143,12 +260,12 @@ function isMultisigOutput (script) { } function isNullDataOutput (script) { - var chunks = Script.decompile(script) - return chunks[0] === ops.OP_RETURN + var chunks = decompile(script) + return chunks[0] === OPS.OP_RETURN } function classifyOutput (script) { - var chunks = Script.decompile(script) + var chunks = decompile(script) if (isPubKeyHashOutput(chunks)) { return 'pubkeyhash' @@ -166,7 +283,7 @@ function classifyOutput (script) { } function classifyInput (script, allowIncomplete) { - var chunks = Script.decompile(script) + var chunks = decompile(script) if (isPubKeyHashInput(chunks)) { return 'pubkeyhash' @@ -184,21 +301,21 @@ function classifyInput (script, allowIncomplete) { // Standard Script Templates // {pubKey} OP_CHECKSIG function pubKeyOutput (pubKey) { - return Script.compile([pubKey, ops.OP_CHECKSIG]) + return compile([pubKey, OPS.OP_CHECKSIG]) } // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG function pubKeyHashOutput (pubKeyHash) { typeforce(typeforce.Buffer, pubKeyHash) - return Script.compile([ops.OP_DUP, ops.OP_HASH160, pubKeyHash, ops.OP_EQUALVERIFY, ops.OP_CHECKSIG]) + return compile([OPS.OP_DUP, OPS.OP_HASH160, pubKeyHash, OPS.OP_EQUALVERIFY, OPS.OP_CHECKSIG]) } // OP_HASH160 {scriptHash} OP_EQUAL function scriptHashOutput (scriptHash) { typeforce(typeforce.Buffer, scriptHash) - return Script.compile([ops.OP_HASH160, scriptHash, ops.OP_EQUAL]) + return compile([OPS.OP_HASH160, scriptHash, OPS.OP_EQUAL]) } // m [pubKeys ...] n OP_CHECKMULTISIG @@ -208,11 +325,11 @@ function multisigOutput (m, pubKeys) { var n = pubKeys.length if (n < m) throw new Error('Not enough pubKeys provided') - return Script.compile([].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 )) } @@ -220,22 +337,22 @@ function multisigOutput (m, pubKeys) { function pubKeyInput (signature) { typeforce(types.Buffer, signature) - return Script.compile([signature]) + return compile([signature]) } // {signature} {pubKey} function pubKeyHashInput (signature, pubKey) { typeforce(types.tuple(types.Buffer, types.Buffer), arguments) - return Script.compile([signature, pubKey]) + return compile([signature, pubKey]) } // {serialized scriptPubKey script} function scriptHashInput (scriptSig, scriptPubKey) { - var scriptSigChunks = Script.decompile(scriptSig) - var serializedScriptPubKey = Script.compile(scriptPubKey) + var scriptSigChunks = decompile(scriptSig) + var serializedScriptPubKey = compile(scriptPubKey) - return Script.compile([].concat( + return compile([].concat( scriptSigChunks, serializedScriptPubKey )) @@ -244,26 +361,31 @@ function scriptHashInput (scriptSig, scriptPubKey) { // OP_0 [signatures ...] function multisigInput (signatures, scriptPubKey) { if (scriptPubKey) { - var chunks = Script.decompile(scriptPubKey) + var chunks = decompile(scriptPubKey) if (!isMultisigOutput(chunks)) throw new Error('Expected multisig scriptPubKey') var mOp = chunks[0] var nOp = chunks[chunks.length - 2] - var m = mOp - (ops.OP_1 - 1) - var n = nOp - (ops.OP_1 - 1) + 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.compile([].concat(ops.OP_0, signatures)) + return compile([].concat(OPS.OP_0, signatures)) } function nullDataOutput (data) { - return Script.compile([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, diff --git a/src/transaction.js b/src/transaction.js index b5a289e..ca17573 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -1,10 +1,9 @@ var bufferutils = require('./bufferutils') var crypto = require('./crypto') +var opcodes = require('./opcodes') +var scripts = require('./scripts') var typeforce = require('typeforce') var types = require('./types') -var opcodes = require('./opcodes') - -var Script = require('./script') function Transaction () { this.version = 1 @@ -193,7 +192,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT // in case concatenating two scripts ends up with two codeseparators, // or an extra one at the end, this prevents all those possible incompatibilities. - var hashScript = Script.compile(Script.decompile(prevOutScript).filter(function (x) { + var hashScript = scripts.compile(scripts.decompile(prevOutScript).filter(function (x) { return x !== opcodes.OP_CODESEPARATOR })) var i diff --git a/src/transaction_builder.js b/src/transaction_builder.js index cca8b35..080363e 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -7,13 +7,12 @@ var scripts = require('./scripts') var Address = require('./address') var ECPair = require('./ecpair') var ECSignature = require('./ecsignature') -var Script = require('./script') var Transaction = require('./transaction') function extractInput (txIn) { var redeemScript var scriptSig = txIn.script - var scriptSigChunks = Script.decompile(scriptSig) + var scriptSigChunks = scripts.decompile(scriptSig) var prevOutScript var prevOutType = scripts.classifyInput(scriptSig, true) @@ -24,7 +23,7 @@ function extractInput (txIn) { redeemScript = scriptSigChunks.slice(-1)[0] prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) - scriptSig = Script.compile(scriptSigChunks.slice(0, -1)) + scriptSig = scripts.compile(scriptSigChunks.slice(0, -1)) scriptSigChunks = scriptSigChunks.slice(0, -1) scriptType = scripts.classifyInput(scriptSig, true) @@ -35,7 +34,7 @@ function extractInput (txIn) { // pre-empt redeemScript decompilation var redeemScriptChunks if (redeemScript) { - redeemScriptChunks = Script.decompile(redeemScript) + redeemScriptChunks = scripts.decompile(redeemScript) } // Extract hashType, pubKeys and signatures @@ -148,7 +147,7 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu var input = {} if (prevOutScript) { - var prevOutScriptChunks = Script.decompile(prevOutScript) + var prevOutScriptChunks = scripts.decompile(prevOutScript) var prevOutType = scripts.classifyOutput(prevOutScriptChunks) // if we can, extract pubKey information @@ -319,14 +318,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash if (input.prevOutScript) { if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH') - var scriptHash = Script.decompile(input.prevOutScript)[1] + var scriptHash = scripts.decompile(input.prevOutScript)[1] if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex')) } var scriptType = scripts.classifyOutput(redeemScript) if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')') - var redeemScriptChunks = Script.decompile(redeemScript) + var redeemScriptChunks = scripts.decompile(redeemScript) var pubKeys = [] switch (scriptType) { case 'multisig': diff --git a/test/address.js b/test/address.js index bdf52a6..077196a 100644 --- a/test/address.js +++ b/test/address.js @@ -2,9 +2,9 @@ var assert = require('assert') var networks = require('../src/networks') +var scripts = require('../src/scripts') var Address = require('../src/address') -var Script = require('../src/script') var fixtures = require('./fixtures/address.json') @@ -31,7 +31,7 @@ describe('Address', function () { describe('fromOutputScript', function () { fixtures.valid.forEach(function (f) { it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { - var script = Script.fromASM(f.script) + var script = scripts.fromASM(f.script) var address = Address.fromOutputScript(script, networks[f.network]) assert.strictEqual(address, f.base58check) @@ -40,7 +40,7 @@ describe('Address', function () { fixtures.invalid.fromOutputScript.forEach(function (f) { it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () { - var script = Script.fromASM(f.script) + var script = scripts.fromASM(f.script) assert.throws(function () { Address.fromOutputScript(script) @@ -66,7 +66,7 @@ describe('Address', function () { it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { var script = Address.toOutputScript(f.base58check, network) - assert.strictEqual(Script.toASM(script), f.script) + assert.strictEqual(scripts.toASM(script), f.script) }) }) diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 49d160a..f846677 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -9,10 +9,10 @@ var Block = Bitcoin.Block var ECPair = Bitcoin.ECPair var ECSignature = Bitcoin.ECSignature var Transaction = Bitcoin.Transaction -var Script = Bitcoin.Script var bufferutils = Bitcoin.bufferutils var networks = Bitcoin.networks +var scripts = Bitcoin.scripts var base58_encode_decode = require('./fixtures/core/base58_encode_decode.json') var base58_keys_invalid = require('./fixtures/core/base58_keys_invalid.json') @@ -172,7 +172,7 @@ describe('Bitcoin-core', function () { }) }) - describe('Script.fromASM', function () { + describe('scripts.fromASM', function () { tx_valid.forEach(function (f) { // Objects that are only a single string are ignored if (f.length === 1) return @@ -195,7 +195,7 @@ describe('Bitcoin-core', function () { it('can parse ' + prevOutScriptPubKey, function () { // TODO: we can probably do better validation than this - Script.fromASM(prevOutScriptPubKey) + scripts.fromASM(prevOutScriptPubKey) }) }) }) @@ -228,8 +228,8 @@ describe('Bitcoin-core', function () { assert.strictEqual(transaction.toHex(), txHex) var script = new Buffer(scriptHex, 'hex') - var scriptChunks = Script.decompile(script) - assert.strictEqual(Script.compile(scriptChunks).toString('hex'), scriptHex) + var scriptChunks = scripts.decompile(script) + assert.strictEqual(scripts.compile(scriptChunks).toString('hex'), scriptHex) var hash = transaction.hashForSignature(inIndex, script, hashType) assert.deepEqual(hash, expectedHash) diff --git a/test/fixtures/script.json b/test/fixtures/script.json deleted file mode 100644 index 714976c..0000000 --- a/test/fixtures/script.json +++ /dev/null @@ -1,96 +0,0 @@ -{ - "valid": [ - { - "asm": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95 OP_CHECKSIG", - "description": "pay-to-PubKey", - "hash": "26e645ab170255f2a0a82d29e48f35b14ae7c826", - "hex": "21031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95ac", - "pubKey": "031f1e68f82112b373f0fe980b3a89d212d2b5c01fb51eb25acb8b4c4b4299ce95" - }, - { - "asm": "OP_HASH160 e8c300c87986efa84c37c0519929019ef86eb5b4 OP_EQUAL", - "description": "P2SH ScriptPubKey", - "hash": "0ba47b56a573bab4b430ad6ed3ec79270e04b066", - "hex": "a914e8c300c87986efa84c37c0519929019ef86eb5b487" - }, - { - "asm": "OP_DUP OP_HASH160 5a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a8 OP_EQUALVERIFY OP_CHECKSIG", - "description": "PubKeyHash ScriptPubKey", - "hash": "a5313f33d5c7b81674b35f7f3febc3522ef234db", - "hex": "76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac" - }, - { - "asm": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301 040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8", - "description": "pubKeyHash scriptSig", - "hash": "b9bac2a5c5c29bb27c382d41fa3d179c646c78fd", - "hex": "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8" - }, - { - "asm": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301", - "description": "pubKey scriptSig", - "hash": "44d9982c3e79452e02ef5816976a0e20a0ec1cba", - "hex": "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301", - "signature": "304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f301" - }, - { - "asm": "OP_TRUE 032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca33016 02308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a OP_2 OP_CHECKMULTISIG", - "description": "Valid multisig script", - "hash": "f1c98f0b74ecabcf78ae20dfa224bb6666051fbe", - "hex": "5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae" - }, - { - "asm": "OP_0 304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101 3045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601", - "description": "mutisig scriptSig", - "hash": "b1ef3ae2c77b356eff81049aad7dfd2eeb34c6f5", - "hex": "0047304402202b29881db1b4cc128442d955e906d41c77365ed9a8392b584be12d980b236459022009da4bc60d09280aa26f4f981bfbed94eb7263d92920961e48a7f3f0991895b101483045022100871708a7597c1dbebff2a5527a56a1f2b49d73e35cd825a07285f5f29f5766d8022003bd7ac25334e9a6d6020cc8ba1be67a8c70dca8e7063ea0547d79c45b9bc12601" - }, - { - "asm": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", - "description": "OP_RETURN script", - "hash": "ec88f016655477663455fe6a8e83508c348ea145", - "hex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474" - }, - { - "asm": "OP_HASH256 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 OP_EQUAL", - "description": "Non standard script", - "hash": "3823382e70d1930989813d3459988e0d7c2861d8", - "hex": "aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087" - }, - { - "asm": "OP_0 OP_0 OP_0 OP_CHECKMULTISIG", - "description": "Invalid multisig script", - "hash": "62ede8963f9387544935f168745262f703dab1fb", - "hex": "000000ae" - }, - { - "asm": "", - "description": "Not enough data: OP_1", - "hash": "c51b66bced5e4491001bd702669770dccf440982", - "hex": "01" - }, - { - "asm": "", - "description": "Not enough data: OP_2", - "hash": "d48ce86c698f246829921ba9fb2a844ae2adba67", - "hex": "0201" - }, - { - "asm": "", - "description": "Not enough data: OP_PUSHDATA1 0x02", - "hash": "b663ef01a96ff65bec84a3fb14688d6ff7fc617c", - "hex": "4c0201" - }, - { - "asm": "", - "description": "Not enough data: OP_PUSHDATA2 0xffff", - "hash": "b4d2fac2836232e59d7b1628f64f24bce3cb4478", - "hex": "4dffff01" - }, - { - "asm": "", - "description": "Not enough data: OP_PUSHDATA4 0xffffffff", - "hash": "941db1ca32faf29e1338fb966bb56d98fbce4823", - "hex": "4effffffff01" - } - ] -} diff --git a/test/fixtures/scripts.json b/test/fixtures/scripts.json index 2fcd423..2442d52 100644 --- a/test/fixtures/scripts.json +++ b/test/fixtures/scripts.json @@ -5,14 +5,18 @@ "pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", "signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", "scriptPubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 OP_CHECKSIG", - "scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801" + "scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "scriptSigHex": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "scriptPubKeyHex": "2102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ac" }, { "type": "pubkeyhash", "pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", "signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", "scriptPubKey": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", - "scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1" + "scriptSig": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "scriptSigHex": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca28012102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "scriptPubKeyHex": "76a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac" }, { "type": "multisig", @@ -25,7 +29,9 @@ "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501" ], "scriptPubKey": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG", - "scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501" + "scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", + "scriptSigHex": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", + "scriptPubKeyHex": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae" }, { "type": "multisig", @@ -40,19 +46,30 @@ "3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01" ], "scriptPubKey": "OP_3 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 02b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340 024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a34 OP_3 OP_CHECKMULTISIG", - "scriptSig": "OP_0 3045022100fe324541215798b2df68cbd44039615e23c506d4ec1a05572064392a98196b82022068c849fa6699206da2fc6d7848efc1d3804a5816d6293615fe34c1a7f34e1c2f01 3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901 3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01" + "scriptSig": "OP_0 3045022100fe324541215798b2df68cbd44039615e23c506d4ec1a05572064392a98196b82022068c849fa6699206da2fc6d7848efc1d3804a5816d6293615fe34c1a7f34e1c2f01 3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901 3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01", + "scriptSigHex": "00483045022100fe324541215798b2df68cbd44039615e23c506d4ec1a05572064392a98196b82022068c849fa6699206da2fc6d7848efc1d3804a5816d6293615fe34c1a7f34e1c2f01473044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901483045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01", + "scriptPubKeyHex": "53210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f817982102b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f84834021024289801366bcee6172b771cf5a7f13aaecd237a0b9a1ff9d769cabc2e6b70a3453ae" }, { "type": "scripthash", "redeemScript": "OP_2 02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1 0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a OP_2 OP_CHECKMULTISIG", "redeemScriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", "scriptSig": "OP_0 304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801 3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501 522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae", - "scriptPubKey": "OP_HASH160 722ff0bc2c3f47b35c20df646c395594da24e90e OP_EQUAL" + "scriptPubKey": "OP_HASH160 722ff0bc2c3f47b35c20df646c395594da24e90e OP_EQUAL", + "scriptSigHex": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae", + "scriptPubKeyHex": "a914722ff0bc2c3f47b35c20df646c395594da24e90e87" + }, + { + "type": "nulldata", + "data": "06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", + "scriptPubKey": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", + "scriptPubKeyHex": "6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474" }, { "type": "nulldata", "data": "deadffffffffffffffffffffffffffffffffbeef", - "scriptPubKey": "OP_RETURN deadffffffffffffffffffffffffffffffffbeef" + "scriptPubKey": "OP_RETURN deadffffffffffffffffffffffffffffffffbeef", + "scriptPubKeyHex": "6a14deadffffffffffffffffffffffffffffffffbeef" }, { "type": "nonstandard", @@ -67,7 +84,8 @@ "3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901", "3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01" ], - "scriptSig": "OP_0 OP_0 3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901 3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01" + "scriptSig": "OP_0 OP_0 3044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901 3045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01", + "scriptSigHex": "0000473044022001ab168e80b863fdec694350b587339bb72a37108ac3c989849251444d13ebba02201811272023e3c1038478eb972a82d3ad431bfc2408e88e4da990f1a7ecbb263901483045022100aaeb7204c17eee2f2c4ff1c9f8b39b79e75e7fbf33e92cc67ac51be8f15b75f90220659eee314a4943a6384d2b154fa5821ef7a084814d7ee2c6f9f7f0ffb53be34b01" }, { "type": "nonstandard", @@ -82,7 +100,8 @@ null, null ], - "scriptSig": "OP_0 OP_0 OP_0 OP_0" + "scriptSig": "OP_0 OP_0 OP_0 OP_0", + "scriptSigHex": "00000000" }, { "type": "nonstandard", @@ -97,16 +116,50 @@ ], "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", "redeemScriptSig": "OP_0 OP_0 30450221009c92c1ae1767ac04e424da7f6db045d979b08cde86b1ddba48621d59a109d818022004f5bb21ad72255177270abaeb2d7940ac18f1e5ca1f53db4f3fd1045647a8a801", - "scriptSig": "OP_0 OP_0 30450221009c92c1ae1767ac04e424da7f6db045d979b08cde86b1ddba48621d59a109d818022004f5bb21ad72255177270abaeb2d7940ac18f1e5ca1f53db4f3fd1045647a8a801 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae" + "scriptSig": "OP_0 OP_0 30450221009c92c1ae1767ac04e424da7f6db045d979b08cde86b1ddba48621d59a109d818022004f5bb21ad72255177270abaeb2d7940ac18f1e5ca1f53db4f3fd1045647a8a801 52410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae", + "scriptSigHex": "00004830450221009c92c1ae1767ac04e424da7f6db045d979b08cde86b1ddba48621d59a109d818022004f5bb21ad72255177270abaeb2d7940ac18f1e5ca1f53db4f3fd1045647a8a8014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52ae" + }, + { + "type": "nonstandard", + "scriptPubKey": "OP_HASH256 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000 OP_EQUAL", + "scriptPubKeyHex": "aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087" + }, + { + "type": "nonstandard", + "scriptPubKey": "OP_0 OP_0 OP_0 OP_CHECKMULTISIG", + "scriptPubKeyHex": "000000ae" }, { "type": "scripthash", "redeemScript": "OP_0", "redeemScriptSig": "OP_0", + "scriptSig": "OP_0 00", "scriptSigHex": "000100" } ], "invalid": { + "decompile": [ + { + "description": "Not enough data: OP_1", + "hex": "01" + }, + { + "description": "Not enough data: OP_2", + "hex": "0201" + }, + { + "description": "Not enough data: OP_PUSHDATA1 0x02", + "hex": "4c0201" + }, + { + "description": "Not enough data: OP_PUSHDATA2 0xffff", + "hex": "4dffff01" + }, + { + "description": "Not enough data: OP_PUSHDATA4 0xffffffff", + "hex": "4effffffff01" + } + ], "isPubKeyHashInput": [ { "description": "pubKeyHash input : extraneous data", diff --git a/test/script.js b/test/script.js deleted file mode 100644 index b3e2258..0000000 --- a/test/script.js +++ /dev/null @@ -1,43 +0,0 @@ -/* global describe, it */ -/* eslint-disable no-new */ - -var assert = require('assert') -var Script = require('../src/script') - -var fixtures = require('./fixtures/script.json') - -describe('Script', function () { - describe('fromASM/toASM', function () { - fixtures.valid.forEach(function (f) { - if (!f.asm) return - - it('decodes/encodes ' + f.description, function () { - var script = Script.fromASM(f.asm) - - assert.strictEqual(Script.toASM(script), f.asm) - }) - }) - }) - - describe('compile', function () { - fixtures.valid.forEach(function (f) { - if (!f.asm) return - - it('decodes/encodes ' + f.description, function () { - var script = Script.fromASM(f.asm) - - assert.strictEqual(Script.compile(script).toString('hex'), f.hex) - }) - }) - }) - - describe('decompile', function () { - fixtures.valid.forEach(function (f) { - it('decodes/encodes ' + f.description, function () { - var script = Script.decompile(new Buffer(f.hex, 'hex')) - - assert.strictEqual(Script.toASM(script), f.asm) - }) - }) - }) -}) diff --git a/test/scripts.js b/test/scripts.js index f3a8b57..daf2ff8 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -5,21 +5,87 @@ var bcrypto = require('../src/crypto') var ops = require('../src/opcodes') var scripts = require('../src/scripts') -var Script = require('../src/script') - var fixtures = require('./fixtures/scripts.json') -describe('Scripts', function () { +describe('scripts', function () { // TODO describe.skip('isCanonicalPubKey', function () {}) describe.skip('isCanonicalSignature', function () {}) + describe('fromASM/toASM', function () { + fixtures.valid.forEach(function (f) { + if (f.scriptSig) { + it('encodes/decodes ' + f.scriptSig, function () { + var script = scripts.fromASM(f.scriptSig) + + assert.strictEqual(scripts.toASM(script), f.scriptSig) + }) + } + + if (f.scriptPubKey) { + it('encodes/decodes ' + f.scriptPubKey, function () { + var script = scripts.fromASM(f.scriptPubKey) + + assert.strictEqual(scripts.toASM(script), f.scriptPubKey) + }) + } + }) + }) + + describe('compile', function () { + fixtures.valid.forEach(function (f) { + if (f.scriptSig) { + it('compiles ' + f.scriptSig, function () { + var script = scripts.fromASM(f.scriptSig) + + assert.strictEqual(scripts.compile(script).toString('hex'), f.scriptSigHex) + }) + } + + if (f.scriptPubKey) { + it('compiles ' + f.scriptPubKey, function () { + var script = scripts.fromASM(f.scriptPubKey) + + assert.strictEqual(scripts.compile(script).toString('hex'), f.scriptPubKeyHex) + }) + } + }) + }) + + describe('decompile', function () { + fixtures.valid.forEach(function (f) { + if (f.scriptSigHex) { + it('decompiles ' + f.scriptSig, function () { + var chunks = scripts.decompile(new Buffer(f.scriptSigHex, 'hex')) + + assert.strictEqual(scripts.toASM(chunks), f.scriptSig) + }) + } + + if (f.scriptPubKeyHex) { + it('decompiles ' + f.scriptPubKey, function () { + var chunks = scripts.decompile(new Buffer(f.scriptPubKeyHex, 'hex')) + + assert.strictEqual(scripts.toASM(chunks), f.scriptPubKey) + }) + } + }) + + fixtures.invalid.decompile.forEach(function (f) { + it('decompiles ' + f.hex + ' to [] because of "' + f.description + '"', function () { + var chunks = scripts.decompile(new Buffer(f.hex, 'hex')) + + assert.strictEqual(chunks.length, 0) + }) + }) + }) + describe('classifyInput', function () { fixtures.valid.forEach(function (f) { if (!f.scriptSig) return it('classifies ' + f.scriptSig + ' as ' + f.type, function () { - var script = Script.fromASM(f.scriptSig) + var script = scripts.fromASM(f.scriptSig) var type = scripts.classifyInput(script) assert.strictEqual(type, f.type) @@ -31,7 +97,7 @@ describe('Scripts', function () { if (!f.typeIncomplete) return it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () { - var script = Script.fromASM(f.scriptSig) + var script = scripts.fromASM(f.scriptSig) var type = scripts.classifyInput(script, true) assert.strictEqual(type, f.typeIncomplete) @@ -44,7 +110,7 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () { - var script = Script.fromASM(f.scriptPubKey) + var script = scripts.fromASM(f.scriptPubKey) var type = scripts.classifyOutput(script) assert.strictEqual(type, f.type) @@ -64,13 +130,7 @@ describe('Scripts', function () { var expected = type.toLowerCase() === f.type if (inputFn && f.scriptSig) { - var script - - if (f.scriptSig) { - script = Script.fromASM(f.scriptSig) - } else { - script = Script.fromHex(f.scriptSigHex) - } + var script = scripts.fromASM(f.scriptSig) it('returns ' + expected + ' for ' + f.scriptSig, function () { assert.strictEqual(inputFn(script), expected) @@ -94,9 +154,9 @@ describe('Scripts', function () { var script if (f.scriptSig) { - script = Script.fromASM(f.scriptSig) + script = scripts.fromASM(f.scriptSig) } else { - script = Script.fromHex(f.scriptSigHex) + script = scripts.fromHex(f.scriptSigHex) } assert.strictEqual(inputFn(script), false) @@ -111,7 +171,7 @@ describe('Scripts', function () { if (outputFn && f.scriptPubKey) { it('returns ' + expected + ' for ' + f.scriptPubKey, function () { - var script = Script.fromASM(f.scriptPubKey) + var script = scripts.fromASM(f.scriptPubKey) assert.strictEqual(outputFn(script), expected) }) @@ -123,7 +183,7 @@ describe('Scripts', function () { fixtures.invalid[outputFnName].forEach(function (f) { if (outputFn && f.scriptPubKey) { it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () { - var script = Script.fromASM(f.scriptPubKey) + var script = scripts.fromASM(f.scriptPubKey) assert.strictEqual(outputFn(script), false) }) @@ -140,7 +200,7 @@ describe('Scripts', function () { var signature = new Buffer(f.signature, 'hex') var scriptSig = scripts.pubKeyInput(signature) - assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) + assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) }) }) }) @@ -153,7 +213,7 @@ describe('Scripts', function () { var pubKey = new Buffer(f.pubKey, 'hex') var scriptPubKey = scripts.pubKeyOutput(pubKey) - assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -168,7 +228,7 @@ describe('Scripts', function () { var signature = new Buffer(f.signature, 'hex') var scriptSig = scripts.pubKeyHashInput(signature, pubKey) - assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) + assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) }) }) }) @@ -182,7 +242,7 @@ describe('Scripts', function () { it('returns ' + f.scriptPubKey, function () { var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash) - assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -197,12 +257,12 @@ describe('Scripts', function () { }) var scriptSig = scripts.multisigInput(signatures) - assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) + assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) }) }) fixtures.invalid.multisigInput.forEach(function (f) { - var scriptPubKey = Script.fromASM(f.scriptPubKey) + var scriptPubKey = scripts.fromASM(f.scriptPubKey) it('throws on ' + f.exception, function () { var signatures = f.signatures.map(function (signature) { @@ -224,7 +284,7 @@ describe('Scripts', function () { var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) @@ -245,14 +305,14 @@ describe('Scripts', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'scripthash') return - var redeemScript = Script.fromASM(f.redeemScript) - var redeemScriptSig = Script.fromASM(f.redeemScriptSig) + var redeemScript = scripts.fromASM(f.redeemScript) + var redeemScriptSig = scripts.fromASM(f.redeemScriptSig) it('returns ' + f.scriptSig, function () { var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) if (f.scriptSig) { - assert.strictEqual(Script.toASM(scriptSig), f.scriptSig) + assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) } else { assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex) @@ -267,10 +327,10 @@ describe('Scripts', function () { if (!f.scriptPubKey) return it('returns ' + f.scriptPubKey, function () { - var redeemScript = Script.fromASM(f.redeemScript) + var redeemScript = scripts.fromASM(f.redeemScript) var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) - assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -283,7 +343,7 @@ describe('Scripts', function () { var scriptPubKey = scripts.nullDataOutput(data) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(Script.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) }) diff --git a/test/transaction.js b/test/transaction.js index cd7f9be..f5effe6 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -1,9 +1,9 @@ /* global describe, it, beforeEach */ var assert = require('assert') +var scripts = require('../src/scripts') var Transaction = require('../src/transaction') -var Script = require('../src/script') var fixtures = require('./fixtures/transaction') @@ -22,7 +22,7 @@ describe('Transaction', function () { script = data } else if (txIn.script) { - script = Script.fromASM(txIn.script) + script = scripts.fromASM(txIn.script) } tx.addInput(txHash, txIn.index, txIn.sequence, script) @@ -36,7 +36,7 @@ describe('Transaction', function () { script = data } else if (txOut.script) { - script = Script.fromASM(txOut.script) + script = scripts.fromASM(txOut.script) } tx.addOutput(script, txOut.value) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 57094f4..2cdbd51 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -7,7 +7,6 @@ var scripts = require('../src/scripts') var Address = require('../src/address') var BigInteger = require('bigi') var ECPair = require('../src/ecpair') -var Script = require('../src/script') var Transaction = require('../src/transaction') var TransactionBuilder = require('../src/transaction_builder') var NETWORKS = require('../src/networks') @@ -22,14 +21,14 @@ function construct (f, sign) { var prevTxScript if (input.prevTxScript) { - prevTxScript = Script.fromASM(input.prevTxScript) + prevTxScript = scripts.fromASM(input.prevTxScript) } txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function (output) { - var script = Script.fromASM(output.script) + var script = scripts.fromASM(output.script) txb.addOutput(script, output.value) }) @@ -41,7 +40,7 @@ function construct (f, sign) { var redeemScript if (sign.redeemScript) { - redeemScript = Script.fromASM(sign.redeemScript) + redeemScript = scripts.fromASM(sign.redeemScript) } txb.sign(index, keyPair, redeemScript, sign.hashType) @@ -200,7 +199,7 @@ describe('TransactionBuilder', function () { var redeemScript if (sign.redeemScript) { - redeemScript = Script.fromASM(sign.redeemScript) + redeemScript = scripts.fromASM(sign.redeemScript) } if (!sign.throws) { @@ -262,7 +261,7 @@ describe('TransactionBuilder', function () { var network = NETWORKS[f.network] f.inputs.forEach(function (input, i) { - var redeemScript = Script.fromASM(input.redeemScript) + var redeemScript = scripts.fromASM(input.redeemScript) input.signs.forEach(function (sign) { // rebuild the transaction each-time after the first @@ -272,11 +271,11 @@ describe('TransactionBuilder', function () { var scriptSig = tx.ins[i].script // ignore OP_0 on the front, ignore redeemScript - var signatures = Script.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) + var signatures = scripts.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) // rebuild/replace the scriptSig without them var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript) - assert.strictEqual(Script.toASM(replacement), sign.scriptSigFiltered) + assert.strictEqual(scripts.toASM(replacement), sign.scriptSigFiltered) tx.ins[i].script = replacement } @@ -292,7 +291,7 @@ describe('TransactionBuilder', function () { tx = txb.buildIncomplete() // now verify the serialized scriptSig is as expected - assert.strictEqual(Script.toASM(tx.ins[i].script), sign.scriptSig) + assert.strictEqual(scripts.toASM(tx.ins[i].script), sign.scriptSig) }) }) @@ -309,7 +308,7 @@ describe('TransactionBuilder', function () { txb = TransactionBuilder.fromTransaction(lameTx, network) - var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG') + var redeemScript = scripts.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG') var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network) txb.sign(0, keyPair, redeemScript) From 1c92cfb373d7bd5aeb8f88380f975a96a8f4b8cd Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 14 Aug 2015 11:32:35 +1000 Subject: [PATCH 07/13] integration: fix script usage in integration tests --- test/integration/advanced.js | 2 +- test/integration/crypto.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/integration/advanced.js b/test/integration/advanced.js index 366c4f3..030bd38 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -55,7 +55,7 @@ describe('bitcoinjs-lib (advanced)', function () { var actual = bitcoin.Transaction.fromHex(transaction.txHex) var dataScript2 = actual.outs[0].script - var data2 = dataScript2.chunks[1] + var data2 = bitcoin.scripts.decompile(dataScript2)[1] assert.deepEqual(dataScript, dataScript2) assert.deepEqual(data, data2) diff --git a/test/integration/crypto.js b/test/integration/crypto.js index 7c97cd0..f4b4c88 100644 --- a/test/integration/crypto.js +++ b/test/integration/crypto.js @@ -121,7 +121,9 @@ describe('bitcoinjs-lib (crypto)', function () { inputs.forEach(function (input) { var transaction = transactions[input.txId] var script = transaction.ins[input.vout].script - assert(bitcoin.scripts.isPubKeyHashInput(script), 'Expected pubKeyHash script') + var scriptChunks = bitcoin.scripts.decompile(script) + + assert(bitcoin.scripts.isPubKeyHashInput(scriptChunks), 'Expected pubKeyHash script') var prevOutTxId = bitcoin.bufferutils.reverse(transaction.ins[input.vout].hash).toString('hex') var prevVout = transaction.ins[input.vout].index @@ -133,8 +135,8 @@ describe('bitcoinjs-lib (crypto)', function () { var prevOut = bitcoin.Transaction.fromHex(result.txHex) var prevOutScript = prevOut.outs[prevVout].script - var scriptSignature = bitcoin.ECSignature.parseScriptSignature(script.chunks[0]) - var publicKey = bitcoin.ECPair.fromPublicKeyBuffer(script.chunks[1]) + var scriptSignature = bitcoin.ECSignature.parseScriptSignature(scriptChunks[0]) + var publicKey = bitcoin.ECPair.fromPublicKeyBuffer(scriptChunks[1]) var m = transaction.hashForSignature(input.vout, prevOutScript, scriptSignature.hashType) assert(publicKey.verify(m, scriptSignature.signature), 'Invalid m') From 20fd3cee8cba2c8f973658f306b8fa4de6fe0d01 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 09:04:07 +1000 Subject: [PATCH 08/13] rename EMPTY to EMPTY_SCRIPT, extract VALUE_UINT64_MAX constant --- src/transaction.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index ca17573..4a5bc0f 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -97,7 +97,7 @@ Transaction.isCoinbaseHash = function (buffer) { }) } -var EMPTY = new Buffer(0) +var EMPTY_SCRIPT = new Buffer(0) Transaction.prototype.addInput = function (hash, index, sequence, script) { typeforce(types.tuple( @@ -111,7 +111,7 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) { sequence = Transaction.DEFAULT_SEQUENCE } - script = script || EMPTY + script = script || EMPTY_SCRIPT // Add the input and return the input's index return (this.ins.push({ @@ -173,6 +173,7 @@ Transaction.prototype.clone = function () { } var ONE = new Buffer('0000000000000000000000000000000000000000000000000000000000000001', 'hex') +var VALUE_UINT64_MAX = new Buffer('ffffffffffffffff', 'hex') /** * Hash transaction for signing a specific input. @@ -198,7 +199,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT var i // blank out other inputs' signatures - txTmp.ins.forEach(function (input) { input.script = EMPTY }) + txTmp.ins.forEach(function (input) { input.script = EMPTY_SCRIPT }) txTmp.ins[inIndex].script = hashScript // blank out some of the inputs @@ -224,8 +225,8 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT // blank all other outputs (clear scriptPubKey, value === -1) var stubOut = { - script: EMPTY, - valueBuffer: new Buffer('ffffffffffffffff', 'hex') + script: EMPTY_SCRIPT, + valueBuffer: VALUE_UINT64_MAX } for (i = 0; i < nOut; i++) { From b933c6da44c9d8faed816d03a45df4904205d584 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 09:05:23 +1000 Subject: [PATCH 09/13] scripts: re-add constrained hash types to *HashOutput --- src/scripts.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts.js b/src/scripts.js index 89a19d0..ff9d961 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -306,14 +306,14 @@ function pubKeyOutput (pubKey) { // OP_DUP OP_HASH160 {pubKeyHash} OP_EQUALVERIFY OP_CHECKSIG function pubKeyHashOutput (pubKeyHash) { - typeforce(typeforce.Buffer, 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 (scriptHash) { - typeforce(typeforce.Buffer, scriptHash) + typeforce(types.Hash160bit, scriptHash) return compile([OPS.OP_HASH160, scriptHash, OPS.OP_EQUAL]) } From ab1215adc18fe0a1d0f43a0ddb9706c4f7495fed Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 09:12:02 +1000 Subject: [PATCH 10/13] scripts: refactor to/from ASM --- src/scripts.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/src/scripts.js b/src/scripts.js index ff9d961..ffd4f14 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -19,33 +19,24 @@ function toASM (chunks) { } return chunks.map(function (chunk) { - // data chunk - if (Buffer.isBuffer(chunk)) { - return chunk.toString('hex') + // data? + if (Buffer.isBuffer(chunk)) return chunk.toString('hex') - // opcode - } else { - return REVERSE_OPS[chunk] - } + // opcode! + return REVERSE_OPS[chunk] }).join(' ') } function fromASM (asm) { typeforce(types.String, asm) - var strChunks = asm.split(' ') - var chunks = strChunks.map(function (strChunk) { - // opcode - if (strChunk in OPS) { - return OPS[strChunk] + return compile(asm.split(' ').map(function (chunkStr) { + // opcode? + if (OPS[chunkStr] !== undefined) return OPS[chunkStr] - // data chunk - } else { - return new Buffer(strChunk, 'hex') - } - }) - - return compile(chunks) + // data! + return new Buffer(chunkStr, 'hex') + })) } function compile (chunks) { From 706f5d7a9cdd244adf4b2c85a0b2ccca11115b8f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 09:18:23 +1000 Subject: [PATCH 11/13] scripts: adds tests for *HashOutput --- src/types.js | 2 +- test/fixtures/scripts.json | 12 ++++++++++++ test/fixtures/transaction.json | 4 ++-- test/scripts.js | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/types.js b/src/types.js index bd7c149..2944995 100644 --- a/src/types.js +++ b/src/types.js @@ -4,7 +4,7 @@ var typeforce = require('typeforce') function nBuffer (value, n) { if (!Buffer.isBuffer(value)) return false - if (value.length !== n) throw new Error('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit') + if (value.length !== n) throw new Error('Expected ' + (n * 8) + '-bit Buffer, got ' + (value.length * 8) + '-bit Buffer') return true } diff --git a/test/fixtures/scripts.json b/test/fixtures/scripts.json index 2442d52..1a17622 100644 --- a/test/fixtures/scripts.json +++ b/test/fixtures/scripts.json @@ -265,6 +265,18 @@ "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801" ] } + ], + "pubKeyHashOutput": [ + { + "exception": "Expected 160-bit Buffer, got 16-bit Buffer", + "hash": "ffff" + } + ], + "scriptHashOutput": [ + { + "exception": "Expected 160-bit Buffer, got 24-bit Buffer", + "hash": "ffffff" + } ] } } diff --git a/test/fixtures/transaction.json b/test/fixtures/transaction.json index a47cf3a..8fb1a94 100644 --- a/test/fixtures/transaction.json +++ b/test/fixtures/transaction.json @@ -229,12 +229,12 @@ "invalid": { "addInput": [ { - "exception": "Expected 256-bit Buffer, got 240-bit", + "exception": "Expected 256-bit Buffer, got 240-bit Buffer", "hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816d", "index": 0 }, { - "exception": "Expected 256-bit Buffer, got 272-bit", + "exception": "Expected 256-bit Buffer, got 272-bit Buffer", "hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816da9b0ffff", "index": 0 } diff --git a/test/scripts.js b/test/scripts.js index daf2ff8..73b9f0d 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -245,6 +245,16 @@ describe('scripts', function () { assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) + + fixtures.invalid.pubKeyHashOutput.forEach(function (f) { + var hash = new Buffer(f.hash, 'hex') + + it('throws on ' + f.exception, function () { + assert.throws(function () { + scripts.pubKeyHashOutput(hash) + }, new RegExp(f.exception)) + }) + }) }) describe('multisigInput', function () { @@ -333,6 +343,16 @@ describe('scripts', function () { assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) }) }) + + fixtures.invalid.scriptHashOutput.forEach(function (f) { + var hash = new Buffer(f.hash, 'hex') + + it('throws on ' + f.exception, function () { + assert.throws(function () { + scripts.scriptHashOutput(hash) + }, new RegExp(f.exception)) + }) + }) }) describe('nullDataOutput', function () { From 97a0008445a873c46f2d4e322314054c293a3f2a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 14:45:00 +1000 Subject: [PATCH 12/13] types: rm Script type --- src/types.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/types.js b/src/types.js index 2944995..a730b2b 100644 --- a/src/types.js +++ b/src/types.js @@ -42,11 +42,6 @@ var Network = typeforce.compile({ dustThreshold: UInt53 }) -var Script = typeforce.compile({ - buffer: typeforce.Buffer, - chunks: typeforce.arrayOf(typeforce.oneOf(typeforce.Number, typeforce.Buffer)) -}) - // extend typeforce types with ours var types = { BigInt: BigInt, @@ -57,7 +52,6 @@ var types = { Hash160bit: Hash160bit, Hash256bit: Hash256bit, Network: Network, - Script: Script, UInt2: UInt2, UInt8: UInt8, UInt32: UInt32, From c06df2df8af985e8f5c1ac410821bc6c7491a176 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 18 Aug 2015 14:55:01 +1000 Subject: [PATCH 13/13] TxBuilder: fix equals is undefined in Node 0.10 --- src/transaction_builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 080363e..465c661 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -305,7 +305,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash if (canSign) { // if redeemScript was provided, enforce consistency if (redeemScript) { - if (!input.redeemScript.equals(redeemScript)) throw new Error('Inconsistent redeemScript') + if (!bufferutils.equal(input.redeemScript, redeemScript)) throw new Error('Inconsistent redeemScript') } if (input.hashType !== hashType) throw new Error('Inconsistent hashType')