Browse Source

merge Script/scripts

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
e2abe2b3af
  1. 8
      src/address.js
  2. 14
      src/index.js
  3. 129
      src/script.js
  4. 224
      src/scripts.js
  5. 7
      src/transaction.js
  6. 13
      src/transaction_builder.js
  7. 8
      test/address.js
  8. 10
      test/bitcoin.core.js
  9. 96
      test/fixtures/script.json
  10. 71
      test/fixtures/scripts.json
  11. 43
      test/script.js
  12. 120
      test/scripts.js
  13. 6
      test/transaction.js
  14. 19
      test/transaction_builder.js

8
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) {

14
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')
}

129
src/script.js

@ -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
}

224
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])
}
// <scriptSig> {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,

7
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

13
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':

8
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)
})
})

10
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)

96
test/fixtures/script.json

@ -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"
}
]
}

71
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",

43
test/script.js

@ -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)
})
})
})
})

120
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)
})
})
})

6
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)

19
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)

Loading…
Cancel
Save