rename scripts to script

This commit is contained in:
Daniel Cousens 2015-08-20 13:31:29 +10:00
parent 18e9cdcc02
commit 6ec687deba
13 changed files with 120 additions and 123 deletions

View file

@ -1,11 +1,11 @@
var base58check = require('bs58check') var bs58check = require('bs58check')
var script = require('./script')
var networks = require('./networks') var networks = require('./networks')
var scripts = require('./scripts')
var typeforce = require('typeforce') var typeforce = require('typeforce')
var types = require('./types') var types = require('./types')
function fromBase58Check (address) { function fromBase58Check (address) {
var payload = base58check.decode(address) var payload = bs58check.decode(address)
if (payload.length < 21) throw new TypeError(address + ' is too short') if (payload.length < 21) throw new TypeError(address + ' is too short')
if (payload.length > 21) throw new TypeError(address + ' is too long') if (payload.length > 21) throw new TypeError(address + ' is too long')
@ -15,14 +15,14 @@ function fromBase58Check (address) {
return { hash: hash, version: version } return { hash: hash, version: version }
} }
function fromOutputScript (script, network) { function fromOutputScript (scriptPubKey, network) {
network = network || networks.bitcoin network = network || networks.bitcoin
var chunks = scripts.decompile(script) var chunks = script.decompile(scriptPubKey)
if (scripts.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash) if (script.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash)
if (scripts.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash) if (script.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash)
throw new Error(scripts.toASM(chunks) + ' has no matching Address') throw new Error(script.toASM(chunks) + ' has no matching Address')
} }
function toBase58Check (hash, version) { function toBase58Check (hash, version) {
@ -32,21 +32,21 @@ function toBase58Check (hash, version) {
payload.writeUInt8(version, 0) payload.writeUInt8(version, 0)
hash.copy(payload, 1) hash.copy(payload, 1)
return base58check.encode(payload) return bs58check.encode(payload)
} }
function toOutputScript (address, network) { function toOutputScript (address, network) {
network = network || networks.bitcoin network = network || networks.bitcoin
var payload = base58check.decode(address) var payload = bs58check.decode(address)
if (payload.length < 21) throw new TypeError(address + ' is too short') if (payload.length < 21) throw new TypeError(address + ' is too short')
if (payload.length > 21) throw new TypeError(address + ' is too long') if (payload.length > 21) throw new TypeError(address + ' is too long')
var version = payload.readUInt8(0) var version = payload.readUInt8(0)
var hash = payload.slice(1) var hash = payload.slice(1)
if (version === network.pubKeyHash) return scripts.pubKeyHashOutput(hash) if (version === network.pubKeyHash) return script.pubKeyHashOutput(hash)
if (version === network.scriptHash) return scripts.scriptHashOutput(hash) if (version === network.scriptHash) return script.scriptHashOutput(hash)
throw new Error(address + ' has no matching Script') throw new Error(address + ' has no matching Script')
} }

View file

@ -12,5 +12,5 @@ module.exports = {
message: require('./message'), message: require('./message'),
networks: require('./networks'), networks: require('./networks'),
opcodes: require('./opcodes'), opcodes: require('./opcodes'),
scripts: require('./scripts') script: require('./script')
} }

View file

@ -1,7 +1,7 @@
var bufferutils = require('./bufferutils') var bufferutils = require('./bufferutils')
var crypto = require('./crypto') var crypto = require('./crypto')
var opcodes = require('./opcodes') var opcodes = require('./opcodes')
var scripts = require('./scripts') var script = require('./script')
var typeforce = require('typeforce') var typeforce = require('typeforce')
var types = require('./types') var types = require('./types')
@ -99,7 +99,7 @@ Transaction.isCoinbaseHash = function (buffer) {
var EMPTY_SCRIPT = new Buffer(0) var EMPTY_SCRIPT = new Buffer(0)
Transaction.prototype.addInput = function (hash, index, sequence, script) { Transaction.prototype.addInput = function (hash, index, sequence, scriptSig) {
typeforce(types.tuple( typeforce(types.tuple(
types.Hash256bit, types.Hash256bit,
types.UInt32, types.UInt32,
@ -111,13 +111,11 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
sequence = Transaction.DEFAULT_SEQUENCE sequence = Transaction.DEFAULT_SEQUENCE
} }
script = script || EMPTY_SCRIPT
// Add the input and return the input's index // Add the input and return the input's index
return (this.ins.push({ return (this.ins.push({
hash: hash, hash: hash,
index: index, index: index,
script: script, script: scriptSig || EMPTY_SCRIPT,
sequence: sequence sequence: sequence
}) - 1) }) - 1)
} }
@ -133,8 +131,8 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
} }
Transaction.prototype.byteLength = function () { Transaction.prototype.byteLength = function () {
function scriptSize (script) { function scriptSize (someScript) {
var length = script.length var length = someScript.length
return bufferutils.varIntSize(length) + length return bufferutils.varIntSize(length) + length
} }
@ -193,7 +191,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
// in case concatenating two scripts ends up with two codeseparators, // in case concatenating two scripts ends up with two codeseparators,
// or an extra one at the end, this prevents all those possible incompatibilities. // or an extra one at the end, this prevents all those possible incompatibilities.
var hashScript = scripts.compile(scripts.decompile(prevOutScript).filter(function (x) { var hashScript = script.compile(script.decompile(prevOutScript).filter(function (x) {
return x !== opcodes.OP_CODESEPARATOR return x !== opcodes.OP_CODESEPARATOR
})) }))
var i var i
@ -320,10 +318,10 @@ Transaction.prototype.toHex = function () {
return this.toBuffer().toString('hex') return this.toBuffer().toString('hex')
} }
Transaction.prototype.setInputScript = function (index, script) { Transaction.prototype.setInputScript = function (index, scriptSig) {
typeforce(types.tuple(types.Number, types.Buffer), arguments) typeforce(types.tuple(types.Number, types.Buffer), arguments)
this.ins[index].script = script this.ins[index].script = scriptSig
} }
module.exports = Transaction module.exports = Transaction

View file

@ -3,7 +3,7 @@ var bcrypto = require('./crypto')
var bufferutils = require('./bufferutils') var bufferutils = require('./bufferutils')
var networks = require('./networks') var networks = require('./networks')
var ops = require('./opcodes') var ops = require('./opcodes')
var scripts = require('./scripts') var script = require('./script')
var ECPair = require('./ecpair') var ECPair = require('./ecpair')
var ECSignature = require('./ecsignature') var ECSignature = require('./ecsignature')
@ -12,21 +12,21 @@ var Transaction = require('./transaction')
function extractInput (txIn) { function extractInput (txIn) {
var redeemScript var redeemScript
var scriptSig = txIn.script var scriptSig = txIn.script
var scriptSigChunks = scripts.decompile(scriptSig) var scriptSigChunks = script.decompile(scriptSig)
var prevOutScript var prevOutScript
var prevOutType = scripts.classifyInput(scriptSig, true) var prevOutType = script.classifyInput(scriptSig, true)
var scriptType var scriptType
// Re-classify if scriptHash // Re-classify if scriptHash
if (prevOutType === 'scripthash') { if (prevOutType === 'scripthash') {
redeemScript = scriptSigChunks.slice(-1)[0] redeemScript = scriptSigChunks.slice(-1)[0]
prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) prevOutScript = script.scriptHashOutput(bcrypto.hash160(redeemScript))
scriptSig = scripts.compile(scriptSigChunks.slice(0, -1)) scriptSig = script.compile(scriptSigChunks.slice(0, -1))
scriptSigChunks = scriptSigChunks.slice(0, -1) scriptSigChunks = scriptSigChunks.slice(0, -1)
scriptType = scripts.classifyInput(scriptSig, true) scriptType = script.classifyInput(scriptSig, true)
} else { } else {
scriptType = prevOutType scriptType = prevOutType
} }
@ -34,7 +34,7 @@ function extractInput (txIn) {
// pre-empt redeemScript decompilation // pre-empt redeemScript decompilation
var redeemScriptChunks var redeemScriptChunks
if (redeemScript) { if (redeemScript) {
redeemScriptChunks = scripts.decompile(redeemScript) redeemScriptChunks = script.decompile(redeemScript)
} }
// Extract hashType, pubKeys and signatures // Extract hashType, pubKeys and signatures
@ -46,7 +46,7 @@ function extractInput (txIn) {
hashType = parsed.hashType hashType = parsed.hashType
pubKeys = scriptSigChunks.slice(1) pubKeys = scriptSigChunks.slice(1)
signatures = [parsed.signature] signatures = [parsed.signature]
prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(pubKeys[0])) prevOutScript = script.pubKeyHashOutput(bcrypto.hash160(pubKeys[0]))
break break
@ -147,8 +147,8 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu
var input = {} var input = {}
if (prevOutScript) { if (prevOutScript) {
var prevOutScriptChunks = scripts.decompile(prevOutScript) var prevOutScriptChunks = script.decompile(prevOutScript)
var prevOutType = scripts.classifyOutput(prevOutScriptChunks) var prevOutType = script.classifyOutput(prevOutScriptChunks)
// if we can, extract pubKey information // if we can, extract pubKey information
switch (prevOutType) { switch (prevOutType) {
@ -240,7 +240,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
switch (scriptType) { switch (scriptType) {
case 'pubkeyhash': case 'pubkeyhash':
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) scriptSig = script.pubKeyHashInput(pkhSignature, input.pubKeys[0])
break break
case 'multisig': case 'multisig':
@ -262,12 +262,12 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
} }
var redeemScript = allowIncomplete ? undefined : input.redeemScript var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = scripts.multisigInput(msSignatures, redeemScript) scriptSig = script.multisigInput(msSignatures, redeemScript)
break break
case 'pubkey': case 'pubkey':
var pkSignature = input.signatures[0].toScriptSignature(input.hashType) var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyInput(pkSignature) scriptSig = script.pubKeyInput(pkSignature)
break break
} }
} }
@ -276,7 +276,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
if (scriptSig) { if (scriptSig) {
// wrap as scriptHash if necessary // wrap as scriptHash if necessary
if (input.prevOutType === 'scripthash') { if (input.prevOutType === 'scripthash') {
scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) scriptSig = script.scriptHashInput(scriptSig, input.redeemScript)
} }
tx.setInputScript(index, scriptSig) tx.setInputScript(index, scriptSig)
@ -318,14 +318,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
if (input.prevOutScript) { if (input.prevOutScript) {
if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH') if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH')
var scriptHash = scripts.decompile(input.prevOutScript)[1] var scriptHash = script.decompile(input.prevOutScript)[1]
if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex')) if (!bufferutils.equal(scriptHash, bcrypto.hash160(redeemScript))) throw new Error('RedeemScript does not match ' + scriptHash.toString('hex'))
} }
var scriptType = scripts.classifyOutput(redeemScript) var scriptType = script.classifyOutput(redeemScript)
if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')') if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')')
var redeemScriptChunks = scripts.decompile(redeemScript) var redeemScriptChunks = script.decompile(redeemScript)
var pubKeys = [] var pubKeys = []
switch (scriptType) { switch (scriptType) {
case 'multisig': case 'multisig':
@ -347,7 +347,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// if we don't have a prevOutScript, generate a P2SH script // if we don't have a prevOutScript, generate a P2SH script
if (!input.prevOutScript) { if (!input.prevOutScript) {
input.prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) input.prevOutScript = script.scriptHashOutput(bcrypto.hash160(redeemScript))
input.prevOutType = 'scripthash' input.prevOutType = 'scripthash'
} }
@ -365,7 +365,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash
// we know nothin' Jon Snow, assume pubKeyHash // we know nothin' Jon Snow, assume pubKeyHash
} else { } else {
input.prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer())) input.prevOutScript = script.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer()))
input.prevOutType = 'pubkeyhash' input.prevOutType = 'pubkeyhash'
input.pubKeys = [kpPubKey] input.pubKeys = [kpPubKey]
input.scriptType = input.prevOutType input.scriptType = input.prevOutType

View file

@ -3,7 +3,7 @@
var assert = require('assert') var assert = require('assert')
var baddress = require('../src/address') var baddress = require('../src/address')
var networks = require('../src/networks') var networks = require('../src/networks')
var scripts = require('../src/scripts') var bscript = require('../src/script')
var fixtures = require('./fixtures/address.json') var fixtures = require('./fixtures/address.json')
describe('address', function () { describe('address', function () {
@ -29,7 +29,7 @@ describe('address', function () {
describe('fromOutputScript', function () { describe('fromOutputScript', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = scripts.fromASM(f.script) var script = bscript.fromASM(f.script)
var address = baddress.fromOutputScript(script, networks[f.network]) var address = baddress.fromOutputScript(script, networks[f.network])
assert.strictEqual(address, f.base58check) assert.strictEqual(address, f.base58check)
@ -38,7 +38,7 @@ describe('address', function () {
fixtures.invalid.fromOutputScript.forEach(function (f) { fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () { it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
var script = scripts.fromASM(f.script) var script = bscript.fromASM(f.script)
assert.throws(function () { assert.throws(function () {
baddress.fromOutputScript(script) baddress.fromOutputScript(script)
@ -64,7 +64,7 @@ describe('address', function () {
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = baddress.toOutputScript(f.base58check, network) var script = baddress.toOutputScript(f.base58check, network)
assert.strictEqual(scripts.toASM(script), f.script) assert.strictEqual(bscript.toASM(script), f.script)
}) })
}) })

View file

@ -162,7 +162,7 @@ describe('Bitcoin-core', function () {
}) })
}) })
describe('scripts.fromASM', function () { describe('script.fromASM', function () {
tx_valid.forEach(function (f) { tx_valid.forEach(function (f) {
// Objects that are only a single string are ignored // Objects that are only a single string are ignored
if (f.length === 1) return if (f.length === 1) return
@ -185,7 +185,7 @@ describe('Bitcoin-core', function () {
it('can parse ' + prevOutScriptPubKey, function () { it('can parse ' + prevOutScriptPubKey, function () {
// TODO: we can probably do better validation than this // TODO: we can probably do better validation than this
bitcoin.scripts.fromASM(prevOutScriptPubKey) bitcoin.script.fromASM(prevOutScriptPubKey)
}) })
}) })
}) })
@ -218,8 +218,8 @@ describe('Bitcoin-core', function () {
assert.strictEqual(transaction.toHex(), txHex) assert.strictEqual(transaction.toHex(), txHex)
var script = new Buffer(scriptHex, 'hex') var script = new Buffer(scriptHex, 'hex')
var scriptChunks = bitcoin.scripts.decompile(script) var scriptChunks = bitcoin.script.decompile(script)
assert.strictEqual(bitcoin.scripts.compile(scriptChunks).toString('hex'), scriptHex) assert.strictEqual(bitcoin.script.compile(scriptChunks).toString('hex'), scriptHex)
var hash = transaction.hashForSignature(inIndex, script, hashType) var hash = transaction.hashForSignature(inIndex, script, hashType)
assert.deepEqual(hash, expectedHash) assert.deepEqual(hash, expectedHash)

View file

@ -36,7 +36,7 @@ describe('bitcoinjs-lib (advanced)', function () {
var tx = new bitcoin.TransactionBuilder(network) var tx = new bitcoin.TransactionBuilder(network)
var data = new Buffer('bitcoinjs-lib') var data = new Buffer('bitcoinjs-lib')
var dataScript = bitcoin.scripts.nullDataOutput(data) var dataScript = bitcoin.script.nullDataOutput(data)
var unspent = unspents.pop() var unspent = unspents.pop()
@ -55,7 +55,7 @@ describe('bitcoinjs-lib (advanced)', function () {
var actual = bitcoin.Transaction.fromHex(transaction.txHex) var actual = bitcoin.Transaction.fromHex(transaction.txHex)
var dataScript2 = actual.outs[0].script var dataScript2 = actual.outs[0].script
var data2 = bitcoin.scripts.decompile(dataScript2)[1] var data2 = bitcoin.script.decompile(dataScript2)[1]
assert.deepEqual(dataScript, dataScript2) assert.deepEqual(dataScript, dataScript2)
assert.deepEqual(data, data2) assert.deepEqual(data, data2)

View file

@ -132,9 +132,9 @@ describe('bitcoinjs-lib (crypto)', function () {
inputs.forEach(function (input) { inputs.forEach(function (input) {
var transaction = transactions[input.txId] var transaction = transactions[input.txId]
var script = transaction.ins[input.vout].script var script = transaction.ins[input.vout].script
var scriptChunks = bitcoin.scripts.decompile(script) var scriptChunks = bitcoin.script.decompile(script)
assert(bitcoin.scripts.isPubKeyHashInput(scriptChunks), 'Expected pubKeyHash script') assert(bitcoin.script.isPubKeyHashInput(scriptChunks), 'Expected pubKeyHash script')
var prevOutTxId = bitcoin.bufferutils.reverse(transaction.ins[input.vout].hash).toString('hex') var prevOutTxId = bitcoin.bufferutils.reverse(transaction.ins[input.vout].hash).toString('hex')
var prevVout = transaction.ins[input.vout].index var prevVout = transaction.ins[input.vout].index

View file

@ -3,11 +3,11 @@
var assert = require('assert') var assert = require('assert')
var bcrypto = require('../src/crypto') var bcrypto = require('../src/crypto')
var ops = require('../src/opcodes') var ops = require('../src/opcodes')
var scripts = require('../src/scripts') var script = require('../src/script')
var fixtures = require('./fixtures/scripts.json') var fixtures = require('./fixtures/script.json')
describe('scripts', function () { describe('script', function () {
// TODO // TODO
describe.skip('isCanonicalPubKey', function () {}) describe.skip('isCanonicalPubKey', function () {})
describe.skip('isCanonicalSignature', function () {}) describe.skip('isCanonicalSignature', function () {})
@ -16,17 +16,17 @@ describe('scripts', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.scriptSig) { if (f.scriptSig) {
it('encodes/decodes ' + f.scriptSig, function () { it('encodes/decodes ' + f.scriptSig, function () {
var script = scripts.fromASM(f.scriptSig) var scriptSig = script.fromASM(f.scriptSig)
assert.strictEqual(scripts.toASM(script), f.scriptSig) assert.strictEqual(script.toASM(scriptSig), f.scriptSig)
}) })
} }
if (f.scriptPubKey) { if (f.scriptPubKey) {
it('encodes/decodes ' + f.scriptPubKey, function () { it('encodes/decodes ' + f.scriptPubKey, function () {
var script = scripts.fromASM(f.scriptPubKey) var scriptPubKey = script.fromASM(f.scriptPubKey)
assert.strictEqual(scripts.toASM(script), f.scriptPubKey) assert.strictEqual(script.toASM(scriptPubKey), f.scriptPubKey)
}) })
} }
}) })
@ -36,17 +36,17 @@ describe('scripts', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.scriptSig) { if (f.scriptSig) {
it('compiles ' + f.scriptSig, function () { it('compiles ' + f.scriptSig, function () {
var script = scripts.fromASM(f.scriptSig) var scriptSig = script.fromASM(f.scriptSig)
assert.strictEqual(scripts.compile(script).toString('hex'), f.scriptSigHex) assert.strictEqual(script.compile(scriptSig).toString('hex'), f.scriptSigHex)
}) })
} }
if (f.scriptPubKey) { if (f.scriptPubKey) {
it('compiles ' + f.scriptPubKey, function () { it('compiles ' + f.scriptPubKey, function () {
var script = scripts.fromASM(f.scriptPubKey) var scriptPubKey = script.fromASM(f.scriptPubKey)
assert.strictEqual(scripts.compile(script).toString('hex'), f.scriptPubKeyHex) assert.strictEqual(script.compile(scriptPubKey).toString('hex'), f.scriptPubKeyHex)
}) })
} }
}) })
@ -56,24 +56,24 @@ describe('scripts', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.scriptSigHex) { if (f.scriptSigHex) {
it('decompiles ' + f.scriptSig, function () { it('decompiles ' + f.scriptSig, function () {
var chunks = scripts.decompile(new Buffer(f.scriptSigHex, 'hex')) var chunks = script.decompile(new Buffer(f.scriptSigHex, 'hex'))
assert.strictEqual(scripts.toASM(chunks), f.scriptSig) assert.strictEqual(script.toASM(chunks), f.scriptSig)
}) })
} }
if (f.scriptPubKeyHex) { if (f.scriptPubKeyHex) {
it('decompiles ' + f.scriptPubKey, function () { it('decompiles ' + f.scriptPubKey, function () {
var chunks = scripts.decompile(new Buffer(f.scriptPubKeyHex, 'hex')) var chunks = script.decompile(new Buffer(f.scriptPubKeyHex, 'hex'))
assert.strictEqual(scripts.toASM(chunks), f.scriptPubKey) assert.strictEqual(script.toASM(chunks), f.scriptPubKey)
}) })
} }
}) })
fixtures.invalid.decompile.forEach(function (f) { fixtures.invalid.decompile.forEach(function (f) {
it('decompiles ' + f.hex + ' to [] because of "' + f.description + '"', function () { it('decompiles ' + f.hex + ' to [] because of "' + f.description + '"', function () {
var chunks = scripts.decompile(new Buffer(f.hex, 'hex')) var chunks = script.decompile(new Buffer(f.hex, 'hex'))
assert.strictEqual(chunks.length, 0) assert.strictEqual(chunks.length, 0)
}) })
@ -85,8 +85,8 @@ describe('scripts', function () {
if (!f.scriptSig) return if (!f.scriptSig) return
it('classifies ' + f.scriptSig + ' as ' + f.type, function () { it('classifies ' + f.scriptSig + ' as ' + f.type, function () {
var script = scripts.fromASM(f.scriptSig) var scriptSig = script.fromASM(f.scriptSig)
var type = scripts.classifyInput(script) var type = script.classifyInput(scriptSig)
assert.strictEqual(type, f.type) assert.strictEqual(type, f.type)
}) })
@ -97,8 +97,8 @@ describe('scripts', function () {
if (!f.typeIncomplete) return if (!f.typeIncomplete) return
it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () { it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () {
var script = scripts.fromASM(f.scriptSig) var scriptSig = script.fromASM(f.scriptSig)
var type = scripts.classifyInput(script, true) var type = script.classifyInput(scriptSig, true)
assert.strictEqual(type, f.typeIncomplete) assert.strictEqual(type, f.typeIncomplete)
}) })
@ -110,8 +110,8 @@ describe('scripts', function () {
if (!f.scriptPubKey) return if (!f.scriptPubKey) return
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () { it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () {
var script = scripts.fromASM(f.scriptPubKey) var scriptPubKey = script.fromASM(f.scriptPubKey)
var type = scripts.classifyOutput(script) var type = script.classifyOutput(scriptPubKey)
assert.strictEqual(type, f.type) assert.strictEqual(type, f.type)
}) })
@ -122,25 +122,25 @@ describe('scripts', function () {
var inputFnName = 'is' + type + 'Input' var inputFnName = 'is' + type + 'Input'
var outputFnName = 'is' + type + 'Output' var outputFnName = 'is' + type + 'Output'
var inputFn = scripts[inputFnName] var inputFn = script[inputFnName]
var outputFn = scripts[outputFnName] var outputFn = script[outputFnName]
describe('is' + type + 'Input', function () { describe('is' + type + 'Input', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
var expected = type.toLowerCase() === f.type var expected = type.toLowerCase() === f.type
if (inputFn && f.scriptSig) { if (inputFn && f.scriptSig) {
var script = scripts.fromASM(f.scriptSig) var scriptSig = script.fromASM(f.scriptSig)
it('returns ' + expected + ' for ' + f.scriptSig, function () { it('returns ' + expected + ' for ' + f.scriptSig, function () {
assert.strictEqual(inputFn(script), expected) assert.strictEqual(inputFn(scriptSig), expected)
}) })
if (f.typeIncomplete) { if (f.typeIncomplete) {
var expectedIncomplete = type.toLowerCase() === f.typeIncomplete var expectedIncomplete = type.toLowerCase() === f.typeIncomplete
it('returns ' + expected + ' for ' + f.scriptSig, function () { it('returns ' + expected + ' for ' + f.scriptSig, function () {
assert.strictEqual(inputFn(script, true), expectedIncomplete) assert.strictEqual(inputFn(scriptSig, true), expectedIncomplete)
}) })
} }
} }
@ -151,15 +151,15 @@ describe('scripts', function () {
fixtures.invalid[inputFnName].forEach(function (f) { fixtures.invalid[inputFnName].forEach(function (f) {
if (inputFn && (f.scriptSig || f.scriptSigHex)) { if (inputFn && (f.scriptSig || f.scriptSigHex)) {
it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () { it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () {
var script var scriptSig
if (f.scriptSig) { if (f.scriptSig) {
script = scripts.fromASM(f.scriptSig) scriptSig = script.fromASM(f.scriptSig)
} else { } else {
script = scripts.fromHex(f.scriptSigHex) scriptSig = script.fromHex(f.scriptSigHex)
} }
assert.strictEqual(inputFn(script), false) assert.strictEqual(inputFn(scriptSig), false)
}) })
} }
}) })
@ -171,9 +171,9 @@ describe('scripts', function () {
if (outputFn && f.scriptPubKey) { if (outputFn && f.scriptPubKey) {
it('returns ' + expected + ' for ' + f.scriptPubKey, function () { it('returns ' + expected + ' for ' + f.scriptPubKey, function () {
var script = scripts.fromASM(f.scriptPubKey) var scriptPubKey = script.fromASM(f.scriptPubKey)
assert.strictEqual(outputFn(script), expected) assert.strictEqual(outputFn(scriptPubKey), expected)
}) })
} }
}) })
@ -183,9 +183,9 @@ describe('scripts', function () {
fixtures.invalid[outputFnName].forEach(function (f) { fixtures.invalid[outputFnName].forEach(function (f) {
if (outputFn && f.scriptPubKey) { if (outputFn && f.scriptPubKey) {
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () { it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
var script = scripts.fromASM(f.scriptPubKey) var scriptPubKey = script.fromASM(f.scriptPubKey)
assert.strictEqual(outputFn(script), false) assert.strictEqual(outputFn(scriptPubKey), false)
}) })
} }
}) })
@ -199,8 +199,8 @@ describe('scripts', function () {
it('returns ' + f.scriptSig, function () { it('returns ' + f.scriptSig, function () {
var signature = new Buffer(f.signature, 'hex') var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyInput(signature) var scriptSig = script.pubKeyInput(signature)
assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) assert.strictEqual(script.toASM(scriptSig), f.scriptSig)
}) })
}) })
}) })
@ -211,9 +211,9 @@ describe('scripts', function () {
it('returns ' + f.scriptPubKey, function () { it('returns ' + f.scriptPubKey, function () {
var pubKey = new Buffer(f.pubKey, 'hex') var pubKey = new Buffer(f.pubKey, 'hex')
var scriptPubKey = scripts.pubKeyOutput(pubKey) var scriptPubKey = script.pubKeyOutput(pubKey)
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(script.toASM(scriptPubKey), f.scriptPubKey)
}) })
}) })
}) })
@ -227,8 +227,8 @@ describe('scripts', function () {
it('returns ' + f.scriptSig, function () { it('returns ' + f.scriptSig, function () {
var signature = new Buffer(f.signature, 'hex') var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyHashInput(signature, pubKey) var scriptSig = script.pubKeyHashInput(signature, pubKey)
assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) assert.strictEqual(script.toASM(scriptSig), f.scriptSig)
}) })
}) })
}) })
@ -241,8 +241,8 @@ describe('scripts', function () {
var pubKeyHash = bcrypto.hash160(pubKey) var pubKeyHash = bcrypto.hash160(pubKey)
it('returns ' + f.scriptPubKey, function () { it('returns ' + f.scriptPubKey, function () {
var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash) var scriptPubKey = script.pubKeyHashOutput(pubKeyHash)
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(script.toASM(scriptPubKey), f.scriptPubKey)
}) })
}) })
@ -251,7 +251,7 @@ describe('scripts', function () {
it('throws on ' + f.exception, function () { it('throws on ' + f.exception, function () {
assert.throws(function () { assert.throws(function () {
scripts.pubKeyHashOutput(hash) script.pubKeyHashOutput(hash)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })
@ -266,13 +266,13 @@ describe('scripts', function () {
return signature ? new Buffer(signature, 'hex') : ops.OP_0 return signature ? new Buffer(signature, 'hex') : ops.OP_0
}) })
var scriptSig = scripts.multisigInput(signatures) var scriptSig = script.multisigInput(signatures)
assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) assert.strictEqual(script.toASM(scriptSig), f.scriptSig)
}) })
}) })
fixtures.invalid.multisigInput.forEach(function (f) { fixtures.invalid.multisigInput.forEach(function (f) {
var scriptPubKey = scripts.fromASM(f.scriptPubKey) var scriptPubKey = script.fromASM(f.scriptPubKey)
it('throws on ' + f.exception, function () { it('throws on ' + f.exception, function () {
var signatures = f.signatures.map(function (signature) { var signatures = f.signatures.map(function (signature) {
@ -280,7 +280,7 @@ describe('scripts', function () {
}) })
assert.throws(function () { assert.throws(function () {
scripts.multisigInput(signatures, scriptPubKey) script.multisigInput(signatures, scriptPubKey)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })
@ -291,10 +291,10 @@ describe('scripts', function () {
if (f.type !== 'multisig') return if (f.type !== 'multisig') return
var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') }) var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') })
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) var scriptPubKey = script.multisigOutput(pubKeys.length, pubKeys)
it('returns ' + f.scriptPubKey, function () { it('returns ' + f.scriptPubKey, function () {
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(script.toASM(scriptPubKey), f.scriptPubKey)
}) })
}) })
@ -305,7 +305,7 @@ describe('scripts', function () {
it('throws on ' + f.exception, function () { it('throws on ' + f.exception, function () {
assert.throws(function () { assert.throws(function () {
scripts.multisigOutput(f.m, pubKeys) script.multisigOutput(f.m, pubKeys)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })
@ -315,14 +315,14 @@ describe('scripts', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
if (f.type !== 'scripthash') return if (f.type !== 'scripthash') return
var redeemScript = scripts.fromASM(f.redeemScript) var redeemScript = script.fromASM(f.redeemScript)
var redeemScriptSig = scripts.fromASM(f.redeemScriptSig) var redeemScriptSig = script.fromASM(f.redeemScriptSig)
it('returns ' + f.scriptSig, function () { it('returns ' + f.scriptSig, function () {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) var scriptSig = script.scriptHashInput(redeemScriptSig, redeemScript)
if (f.scriptSig) { if (f.scriptSig) {
assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) assert.strictEqual(script.toASM(scriptSig), f.scriptSig)
} else { } else {
assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex) assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex)
@ -337,10 +337,10 @@ describe('scripts', function () {
if (!f.scriptPubKey) return if (!f.scriptPubKey) return
it('returns ' + f.scriptPubKey, function () { it('returns ' + f.scriptPubKey, function () {
var redeemScript = scripts.fromASM(f.redeemScript) var redeemScript = script.fromASM(f.redeemScript)
var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) var scriptPubKey = script.scriptHashOutput(bcrypto.hash160(redeemScript))
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(script.toASM(scriptPubKey), f.scriptPubKey)
}) })
}) })
@ -349,7 +349,7 @@ describe('scripts', function () {
it('throws on ' + f.exception, function () { it('throws on ' + f.exception, function () {
assert.throws(function () { assert.throws(function () {
scripts.scriptHashOutput(hash) script.scriptHashOutput(hash)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })
@ -360,10 +360,10 @@ describe('scripts', function () {
if (f.type !== 'nulldata') return if (f.type !== 'nulldata') return
var data = new Buffer(f.data, 'hex') var data = new Buffer(f.data, 'hex')
var scriptPubKey = scripts.nullDataOutput(data) var scriptPubKey = script.nullDataOutput(data)
it('returns ' + f.scriptPubKey, function () { it('returns ' + f.scriptPubKey, function () {
assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) assert.strictEqual(script.toASM(scriptPubKey), f.scriptPubKey)
}) })
}) })
}) })

View file

@ -1,7 +1,7 @@
/* global describe, it, beforeEach */ /* global describe, it, beforeEach */
var assert = require('assert') var assert = require('assert')
var scripts = require('../src/scripts') var bscript = require('../src/script')
var Transaction = require('../src/transaction') var Transaction = require('../src/transaction')
@ -15,17 +15,16 @@ describe('Transaction', function () {
raw.ins.forEach(function (txIn) { raw.ins.forEach(function (txIn) {
var txHash = new Buffer(txIn.hash, 'hex') var txHash = new Buffer(txIn.hash, 'hex')
var script var scriptSig
if (txIn.data) { if (txIn.data) {
var data = new Buffer(txIn.data, 'hex') scriptSig = new Buffer(txIn.data, 'hex')
script = data
} else if (txIn.script) { } else if (txIn.script) {
script = scripts.fromASM(txIn.script) scriptSig = bscript.fromASM(txIn.script)
} }
tx.addInput(txHash, txIn.index, txIn.sequence, script) tx.addInput(txHash, txIn.index, txIn.sequence, scriptSig)
}) })
raw.outs.forEach(function (txOut) { raw.outs.forEach(function (txOut) {
@ -36,7 +35,7 @@ describe('Transaction', function () {
script = data script = data
} else if (txOut.script) { } else if (txOut.script) {
script = scripts.fromASM(txOut.script) script = bscript.fromASM(txOut.script)
} }
tx.addOutput(script, txOut.value) tx.addOutput(script, txOut.value)

View file

@ -3,7 +3,7 @@
var address = require('../src/address') var address = require('../src/address')
var assert = require('assert') var assert = require('assert')
var ops = require('../src/opcodes') var ops = require('../src/opcodes')
var script = require('../src/scripts') var script = require('../src/script')
var BigInteger = require('bigi') var BigInteger = require('bigi')
var ECPair = require('../src/ecpair') var ECPair = require('../src/ecpair')