Merge pull request #438 from bitcoinjs/noscript

No Script
This commit is contained in:
Daniel Cousens 2015-08-19 15:03:40 +10:00
commit bfb7289cf2
19 changed files with 509 additions and 571 deletions

View file

@ -1,7 +1,7 @@
var base58check = require('bs58check')
var typeforce = require('typeforce')
var networks = require('./networks')
var scripts = require('./scripts')
var typeforce = require('typeforce')
var types = require('./types')
function fromBase58Check (address) {
@ -18,10 +18,11 @@ 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)
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() + ' has no matching Address')
throw new Error(scripts.toASM(chunks) + ' has no matching Address')
}
function toBase58Check (hash, version) {

View file

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

View file

@ -1,147 +0,0 @@
var bufferutils = require('./bufferutils')
var crypto = require('./crypto')
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
}
Script.fromASM = function (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 Script.fromChunks(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) {
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 new Script(buffer, chunks)
}
Script.fromHex = function (hex) {
return Script.fromBuffer(new Buffer(hex, 'hex'))
}
Script.EMPTY = Script.fromChunks([])
Script.prototype.equals = function (script) {
return bufferutils.equal(this.buffer, script.buffer)
}
Script.prototype.getHash = function () {
return crypto.hash160(this.buffer)
}
// FIXME: doesn't work for data chunks, maybe time to use buffertools.compare...
Script.prototype.without = function (needle) {
return Script.fromChunks(this.chunks.filter(function (op) {
return op !== needle
}))
}
var reverseOps = []
for (var op in opcodes) {
var code = opcodes[op]
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(' ')
}
Script.prototype.toBuffer = function () {
return this.buffer
}
Script.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
module.exports = Script

View file

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

View file

@ -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
@ -45,11 +44,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 +58,7 @@ Transaction.fromBuffer = function (buffer, __noStrict) {
tx.ins.push({
hash: hash,
index: readUInt32(),
script: readGenerationScript(),
script: readScript(),
sequence: readUInt32()
})
} else {
@ -102,19 +97,21 @@ Transaction.isCoinbaseHash = function (buffer) {
})
}
var EMPTY_SCRIPT = 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_SCRIPT
// Add the input and return the input's index
return (this.ins.push({
@ -126,7 +123,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 +134,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
}
@ -176,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.
@ -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,13 @@ 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 = scripts.compile(scripts.decompile(prevOutScript).filter(function (x) {
return x !== opcodes.OP_CODESEPARATOR
}))
var i
// blank out other inputs' signatures
txTmp.ins.forEach(function (input) { input.script = Script.EMPTY })
txTmp.ins.forEach(function (input) { input.script = EMPTY_SCRIPT })
txTmp.ins[inIndex].script = hashScript
// blank out some of the inputs
@ -225,8 +225,8 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
// blank all other outputs (clear scriptPubKey, value === -1)
var stubOut = {
script: Script.EMPTY,
valueBuffer: new Buffer('ffffffffffffffff', 'hex')
script: EMPTY_SCRIPT,
valueBuffer: VALUE_UINT64_MAX
}
for (i = 0; i < nOut; i++) {
@ -294,8 +294,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 +307,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 +321,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
}

View file

@ -7,53 +7,62 @@ 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 = scripts.decompile(scriptSig)
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 = scripts.compile(scriptSigChunks.slice(0, -1))
scriptSigChunks = scriptSigChunks.slice(0, -1)
scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1))
scriptType = scripts.classifyInput(scriptSig, true)
} else {
scriptType = prevOutType
}
// pre-empt redeemScript decompilation
var redeemScriptChunks
if (redeemScript) {
redeemScriptChunks = scripts.decompile(redeemScript)
}
// Extract hashType, pubKeys and signatures
var hashType, parsed, pubKeys, signatures
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 = redeemScriptChunks.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 +72,7 @@ function extractInput (txIn) {
})
if (redeemScript) {
pubKeys = redeemScript.chunks.slice(1, -2)
pubKeys = redeemScriptChunks.slice(1, -2)
}
break
@ -115,7 +124,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)
})
@ -138,16 +147,17 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
var input = {}
if (prevOutScript) {
var prevOutType = scripts.classifyOutput(prevOutScript)
var prevOutScriptChunks = scripts.decompile(prevOutScript)
var prevOutType = scripts.classifyOutput(prevOutScriptChunks)
// 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
}
@ -295,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')
@ -308,21 +318,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 = 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 = scripts.decompile(redeemScript)
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 +341,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'
}

View file

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

View file

@ -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(), f.script)
assert.strictEqual(scripts.toASM(script), f.script)
})
})

View file

@ -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)
})
})
})
@ -227,8 +227,9 @@ 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 = new Buffer(scriptHex, 'hex')
var scriptChunks = scripts.decompile(script)
assert.strictEqual(scripts.compile(scriptChunks).toString('hex'), scriptHex)
var hash = transaction.hashForSignature(inIndex, script, hashType)
assert.deepEqual(hash, expectedHash)

View file

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

View file

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

View file

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

View file

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

View file

@ -129,7 +129,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
@ -141,8 +143,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')

View file

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

View file

@ -1,95 +0,0 @@
/* global describe, it */
/* eslint-disable no-new */
var assert = require('assert')
var opcodes = require('../src/opcodes')
var Script = require('../src/script')
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
it('decodes/encodes ' + f.description, function () {
var script = Script.fromASM(f.asm)
assert.strictEqual(script.toASM(), f.asm)
assert.strictEqual(script.toHex(), f.hex)
})
})
})
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.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)
})
})
})
describe('fromChunks', function () {
it('should match expected behaviour', function () {
var hash = new Buffer(32)
hash.fill(0)
var script = Script.fromChunks([
opcodes.OP_HASH160,
hash,
opcodes.OP_EQUAL
])
assert.strictEqual(script.toHex(), '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.toHex(), '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)
})
})
})

View file

@ -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(scriptSig.toASM(), f.scriptSig)
assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig)
})
})
})
@ -152,7 +212,8 @@ 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(scripts.toASM(scriptPubKey), f.scriptPubKey)
})
})
})
@ -167,7 +228,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(scripts.toASM(scriptSig), f.scriptSig)
})
})
})
@ -181,7 +242,17 @@ describe('Scripts', function () {
it('returns ' + f.scriptPubKey, function () {
var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash)
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
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))
})
})
})
@ -196,12 +267,12 @@ describe('Scripts', function () {
})
var scriptSig = scripts.multisigInput(signatures)
assert.strictEqual(scriptSig.toASM(), 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) {
@ -223,7 +294,7 @@ describe('Scripts', function () {
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
it('returns ' + f.scriptPubKey, function () {
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey)
})
})
@ -244,16 +315,17 @@ 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(scriptSig.toASM(), f.scriptSig)
assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig)
} else {
assert.strictEqual(scriptSig.toHex(), f.scriptSigHex)
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
}
})
})
@ -265,10 +337,20 @@ 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 = scripts.fromASM(f.redeemScript)
var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
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))
})
})
})
@ -281,7 +363,7 @@ describe('Scripts', function () {
var scriptPubKey = scripts.nullDataOutput(data)
it('returns ' + f.scriptPubKey, function () {
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey)
})
})
})

View file

@ -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')
@ -19,9 +19,10 @@ 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 = scripts.fromASM(txIn.script)
}
tx.addInput(txHash, txIn.index, txIn.sequence, script)
@ -32,9 +33,10 @@ 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 = scripts.fromASM(txOut.script)
}
tx.addOutput(script, txOut.value)
@ -102,7 +104,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 +122,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)
})
})

View file

@ -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 = scriptSig.chunks.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(replacement.toASM(), 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(tx.ins[i].script.toASM(), 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)