Merge pull request #907 from bitcoinjs/nocirc

rm bitcoin.script circular dependencies with templates
This commit is contained in:
Daniel Cousens 2017-09-26 20:02:38 +10:00 committed by GitHub
commit 4f041b256f
5 changed files with 68 additions and 61 deletions

View file

@ -1,4 +1,13 @@
var script = require('./script')
var templates = require('./templates')
for (var key in templates) {
script[key] = templates[key]
}
module.exports = {
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
Block: require('./block'),
ECPair: require('./ecpair'),
ECSignature: require('./ecsignature'),
@ -7,9 +16,8 @@ module.exports = {
TransactionBuilder: require('./transaction_builder'),
address: require('./address'),
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
crypto: require('./crypto'),
networks: require('./networks'),
opcodes: require('bitcoin-ops'),
script: require('./script')
script: script
}

View file

@ -212,8 +212,3 @@ module.exports = {
isPushOnly: isPushOnly,
isDefinedHashType: isDefinedHashType
}
var templates = require('./templates')
for (var key in templates) {
module.exports[key] = templates[key]
}

View file

@ -2,6 +2,7 @@
var Buffer = require('safe-buffer').Buffer
var bscript = require('../../script')
var p2mso = require('./output')
var typeforce = require('typeforce')
var OPS = require('bitcoin-ops')
@ -28,7 +29,7 @@ function encodeStack (signatures, scriptPubKey) {
typeforce([partialSignature], signatures)
if (scriptPubKey) {
var scriptData = bscript.multisig.output.decode(scriptPubKey)
var scriptData = p2mso.decode(scriptPubKey)
if (signatures.length < scriptData.m) {
throw new TypeError('Not enough signatures provided')

View file

@ -4,6 +4,12 @@ var Buffer = require('safe-buffer').Buffer
var bscript = require('../../script')
var typeforce = require('typeforce')
var p2ms = require('../multisig/')
var p2pk = require('../pubkey/')
var p2pkh = require('../pubkeyhash/')
var p2wpkho = require('../witnesspubkeyhash/output')
var p2wsho = require('../witnessscripthash/output')
function check (script, allowIncomplete) {
var chunks = bscript.decompile(script)
if (chunks.length < 1) return false
@ -22,19 +28,19 @@ function check (script, allowIncomplete) {
// is witness?
if (chunks.length === 1) {
return bscript.witnessScriptHash.output.check(redeemScriptChunks) ||
bscript.witnessPubKeyHash.output.check(redeemScriptChunks)
return p2wsho.check(redeemScriptChunks) ||
p2wpkho.check(redeemScriptChunks)
}
// match types
if (bscript.pubKeyHash.input.check(scriptSigChunks) &&
bscript.pubKeyHash.output.check(redeemScriptChunks)) return true
if (p2pkh.input.check(scriptSigChunks) &&
p2pkh.output.check(redeemScriptChunks)) return true
if (bscript.multisig.input.check(scriptSigChunks, allowIncomplete) &&
bscript.multisig.output.check(redeemScriptChunks)) return true
if (p2ms.input.check(scriptSigChunks, allowIncomplete) &&
p2ms.output.check(redeemScriptChunks)) return true
if (bscript.pubKey.input.check(scriptSigChunks) &&
bscript.pubKey.output.check(redeemScriptChunks)) return true
if (p2pk.input.check(scriptSigChunks) &&
p2pk.output.check(redeemScriptChunks)) return true
return false
}

View file

@ -3,6 +3,7 @@
var assert = require('assert')
var bcrypto = require('../src/crypto')
var bscript = require('../src/script')
var btemplates = require('../src/templates')
var ops = require('bitcoin-ops')
var fixtures = require('./fixtures/templates.json')
@ -14,7 +15,7 @@ describe('script-templates', function () {
it('classifies ' + f.input + ' as ' + f.type, function () {
var input = bscript.fromASM(f.input)
var type = bscript.classifyInput(input)
var type = btemplates.classifyInput(input)
assert.strictEqual(type, f.type)
})
@ -26,7 +27,7 @@ describe('script-templates', function () {
it('classifies incomplete ' + f.input + ' as ' + f.typeIncomplete, function () {
var input = bscript.fromASM(f.input)
var type = bscript.classifyInput(input, true)
var type = btemplates.classifyInput(input, true)
assert.strictEqual(type, f.typeIncomplete)
})
@ -39,7 +40,7 @@ describe('script-templates', function () {
it('classifies ' + f.output + ' as ' + f.type, function () {
var output = bscript.fromASM(f.output)
var type = bscript.classifyOutput(output)
var type = btemplates.classifyOutput(output)
assert.strictEqual(type, f.type)
})
@ -56,13 +57,13 @@ describe('script-templates', function () {
'nullData',
'witnessCommitment'
].forEach(function (name) {
var inputType = bscript[name].input
var outputType = bscript[name].output
var inputType = btemplates[name].input
var outputType = btemplates[name].output
describe(name + '.input.check', function () {
fixtures.valid.forEach(function (f) {
if (name.toLowerCase() === bscript.types.P2WPKH) return
if (name.toLowerCase() === bscript.types.P2WSH) return
if (name.toLowerCase() === btemplates.types.P2WPKH) return
if (name.toLowerCase() === btemplates.types.P2WSH) return
var expected = name.toLowerCase() === f.type.toLowerCase()
if (inputType && f.input) {
@ -109,12 +110,8 @@ describe('script-templates', function () {
it('returns ' + expected + ' for ' + f.output, function () {
var output = bscript.fromASM(f.output)
if (name.toLowerCase() === 'nulldata' && f.type === bscript.types.WITNESS_COMMITMENT) {
return
}
if (name.toLowerCase() === 'witnesscommitment' && f.type === bscript.types.NULLDATA) {
return
}
if (name.toLowerCase() === 'nulldata' && f.type === btemplates.types.WITNESS_COMMITMENT) return
if (name.toLowerCase() === 'witnesscommitment' && f.type === btemplates.types.NULLDATA) return
assert.strictEqual(outputType.check(output), expected)
})
}
@ -145,14 +142,14 @@ describe('script-templates', function () {
if (f.type !== 'pubkey') return
var signature = Buffer.from(f.signature, 'hex')
var input = bscript.pubKey.input.encode(signature)
var input = btemplates.pubKey.input.encode(signature)
it('encodes to ' + f.input, function () {
assert.strictEqual(bscript.toASM(input), f.input)
})
it('decodes to ' + f.signature, function () {
assert.deepEqual(bscript.pubKey.input.decode(input), signature)
assert.deepEqual(btemplates.pubKey.input.decode(input), signature)
})
})
})
@ -162,14 +159,14 @@ describe('script-templates', function () {
if (f.type !== 'pubkey') return
var pubKey = Buffer.from(f.pubKey, 'hex')
var output = bscript.pubKey.output.encode(pubKey)
var output = btemplates.pubKey.output.encode(pubKey)
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to ' + f.pubKey, function () {
assert.deepEqual(bscript.pubKey.output.decode(output), pubKey)
assert.deepEqual(btemplates.pubKey.output.decode(output), pubKey)
})
})
})
@ -180,14 +177,14 @@ describe('script-templates', function () {
var pubKey = Buffer.from(f.pubKey, 'hex')
var signature = Buffer.from(f.signature, 'hex')
var input = bscript.pubKeyHash.input.encode(signature, pubKey)
var input = btemplates.pubKeyHash.input.encode(signature, pubKey)
it('encodes to ' + f.input, function () {
assert.strictEqual(bscript.toASM(input), f.input)
})
it('decodes to original arguments', function () {
assert.deepEqual(bscript.pubKeyHash.input.decode(input), {
assert.deepEqual(btemplates.pubKeyHash.input.decode(input), {
signature: signature,
pubKey: pubKey
})
@ -201,14 +198,14 @@ describe('script-templates', function () {
var pubKey = Buffer.from(f.pubKey, 'hex')
var pubKeyHash = bcrypto.hash160(pubKey)
var output = bscript.pubKeyHash.output.encode(pubKeyHash)
var output = btemplates.pubKeyHash.output.encode(pubKeyHash)
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to ' + pubKeyHash.toString('hex'), function () {
assert.deepEqual(bscript.pubKeyHash.output.decode(output), pubKeyHash)
assert.deepEqual(btemplates.pubKeyHash.output.decode(output), pubKeyHash)
})
})
@ -218,7 +215,7 @@ describe('script-templates', function () {
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.pubKeyHash.output.encode(hash)
btemplates.pubKeyHash.output.encode(hash)
}, new RegExp(f.exception))
})
})
@ -233,14 +230,14 @@ describe('script-templates', function () {
return signature ? Buffer.from(signature, 'hex') : ops.OP_0
})
var input = bscript.multisig.input.encode(signatures)
var input = btemplates.multisig.input.encode(signatures)
it('encodes to ' + f.input, function () {
assert.strictEqual(bscript.toASM(input), f.input)
})
it('decodes to ' + signatures.map(function (x) { return x === ops.OP_0 ? 'OP_0' : x.toString('hex') }), function () {
assert.deepEqual(bscript.multisig.input.decode(input, allowIncomplete), signatures)
assert.deepEqual(btemplates.multisig.input.decode(input, allowIncomplete), signatures)
})
})
@ -254,7 +251,7 @@ describe('script-templates', function () {
})
assert.throws(function () {
bscript.multisig.input.encode(signatures, output)
btemplates.multisig.input.encode(signatures, output)
}, new RegExp(f.exception))
})
})
@ -267,14 +264,14 @@ describe('script-templates', function () {
var pubKeys = f.pubKeys.map(function (p) { return Buffer.from(p, 'hex') })
var m = pubKeys.length
var output = bscript.multisig.output.encode(m, pubKeys)
var output = btemplates.multisig.output.encode(m, pubKeys)
it('encodes ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to original arguments', function () {
assert.deepEqual(bscript.multisig.output.decode(output), {
assert.deepEqual(btemplates.multisig.output.decode(output), {
m: m,
pubKeys: pubKeys
})
@ -289,7 +286,7 @@ describe('script-templates', function () {
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.multisig.output.encode(f.m, pubKeys)
btemplates.multisig.output.encode(f.m, pubKeys)
}, new RegExp(f.exception))
})
})
@ -301,7 +298,7 @@ describe('script-templates', function () {
var redeemScript = bscript.fromASM(f.redeemScript)
var redeemScriptSig = bscript.fromASM(f.redeemScriptSig)
var input = bscript.scriptHash.input.encode(redeemScriptSig, redeemScript)
var input = btemplates.scriptHash.input.encode(redeemScriptSig, redeemScript)
it('encodes to ' + f.output, function () {
if (f.input) {
@ -312,7 +309,7 @@ describe('script-templates', function () {
})
it('decodes to original arguments', function () {
assert.deepEqual(bscript.scriptHash.input.decode(input), {
assert.deepEqual(btemplates.scriptHash.input.decode(input), {
redeemScriptSig: redeemScriptSig,
redeemScript: redeemScript
})
@ -327,14 +324,14 @@ describe('script-templates', function () {
var redeemScript = bscript.fromASM(f.redeemScript)
var scriptHash = bcrypto.hash160(redeemScript)
var output = bscript.scriptHash.output.encode(scriptHash)
var output = btemplates.scriptHash.output.encode(scriptHash)
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to ' + scriptHash.toString('hex'), function () {
assert.deepEqual(bscript.scriptHash.output.decode(output), scriptHash)
assert.deepEqual(btemplates.scriptHash.output.decode(output), scriptHash)
})
})
@ -344,7 +341,7 @@ describe('script-templates', function () {
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.scriptHash.output.encode(hash)
btemplates.scriptHash.output.encode(hash)
}, new RegExp(f.exception))
})
})
@ -357,14 +354,14 @@ describe('script-templates', function () {
var pubKey = Buffer.from(f.pubKey, 'hex')
var pubKeyHash = bcrypto.hash160(pubKey)
var output = bscript.witnessPubKeyHash.output.encode(pubKeyHash)
var output = btemplates.witnessPubKeyHash.output.encode(pubKeyHash)
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to ' + pubKeyHash.toString('hex'), function () {
assert.deepEqual(bscript.witnessPubKeyHash.output.decode(output), pubKeyHash)
assert.deepEqual(btemplates.witnessPubKeyHash.output.decode(output), pubKeyHash)
})
})
@ -374,7 +371,7 @@ describe('script-templates', function () {
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.witnessPubKeyHash.output.encode(hash)
btemplates.witnessPubKeyHash.output.encode(hash)
}, new RegExp(f.exception))
})
})
@ -387,14 +384,14 @@ describe('script-templates', function () {
var witnessScriptPubKey = bscript.fromASM(f.witnessScriptPubKey)
var scriptHash = bcrypto.hash256(witnessScriptPubKey)
var output = bscript.witnessScriptHash.output.encode(scriptHash)
var output = btemplates.witnessScriptHash.output.encode(scriptHash)
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to ' + scriptHash.toString('hex'), function () {
assert.deepEqual(bscript.witnessScriptHash.output.decode(output), scriptHash)
assert.deepEqual(btemplates.witnessScriptHash.output.decode(output), scriptHash)
})
})
@ -404,7 +401,7 @@ describe('script-templates', function () {
it('throws on ' + f.exception, function () {
assert.throws(function () {
bscript.witnessScriptHash.output.encode(hash)
btemplates.witnessScriptHash.output.encode(hash)
}, new RegExp(f.exception))
})
})
@ -416,14 +413,14 @@ describe('script-templates', function () {
if (!f.scriptPubKey) return
var commitment = Buffer.from(f.witnessCommitment, 'hex')
var scriptPubKey = bscript.witnessCommitment.output.encode(commitment)
var scriptPubKey = btemplates.witnessCommitment.output.encode(commitment)
it('encodes to ' + f.scriptPubKey, function () {
assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey)
})
it('decodes to ' + commitment.toString('hex'), function () {
assert.deepEqual(bscript.witnessCommitment.output.decode(scriptPubKey), commitment)
assert.deepEqual(btemplates.witnessCommitment.output.decode(scriptPubKey), commitment)
})
})
@ -432,7 +429,7 @@ describe('script-templates', function () {
var hash = Buffer.from(f.commitment, 'hex')
it('throws on bad encode data', function () {
assert.throws(function () {
bscript.witnessCommitment.output.encode(hash)
btemplates.witnessCommitment.output.encode(hash)
}, new RegExp(f.exception))
})
}
@ -440,7 +437,7 @@ describe('script-templates', function () {
if (f.scriptPubKeyHex) {
it('.decode throws on ' + f.description, function () {
assert.throws(function () {
bscript.witnessCommitment.output.decode(Buffer.from(f.scriptPubKeyHex, 'hex'))
btemplates.witnessCommitment.output.decode(Buffer.from(f.scriptPubKeyHex, 'hex'))
}, new RegExp(f.exception))
})
}
@ -452,14 +449,14 @@ describe('script-templates', function () {
if (f.type !== 'nulldata') return
var data = Buffer.from(f.data, 'hex')
var output = bscript.nullData.output.encode(data)
var output = btemplates.nullData.output.encode(data)
it('encodes to ' + f.output, function () {
assert.strictEqual(bscript.toASM(output), f.output)
})
it('decodes to ' + f.data, function () {
assert.deepEqual(bscript.nullData.output.decode(output), data)
assert.deepEqual(btemplates.nullData.output.decode(output), data)
})
})
})