remove usage of instanced Scripts

This commit is contained in:
Daniel Cousens 2015-08-07 16:30:24 +10:00
parent 5d2abb523e
commit e743c58ed6
11 changed files with 146 additions and 119 deletions

View file

@ -31,7 +31,7 @@ describe('Address', function () {
describe('fromOutputScript', function () {
fixtures.valid.forEach(function (f) {
it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = Script.fromASM(f.script)
var script = Script.fromASM(f.script).buffer
var address = Address.fromOutputScript(script, networks[f.network])
assert.strictEqual(address, f.base58check)
@ -40,7 +40,7 @@ describe('Address', function () {
fixtures.invalid.fromOutputScript.forEach(function (f) {
it('throws when ' + f.script.slice(0, 30) + '... ' + f.exception, function () {
var script = Script.fromASM(f.script)
var script = Script.fromASM(f.script).buffer
assert.throws(function () {
Address.fromOutputScript(script)
@ -66,7 +66,7 @@ describe('Address', function () {
it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () {
var script = Address.toOutputScript(f.base58check, network)
assert.strictEqual(script.toASM(), f.script)
assert.strictEqual(Script.fromBuffer(script).toASM(), f.script)
})
})

View file

@ -227,8 +227,8 @@ describe('Bitcoin-core', function () {
var transaction = Transaction.fromHex(txHex)
assert.strictEqual(transaction.toHex(), txHex)
var script = Script.fromHex(scriptHex)
assert.strictEqual(script.toHex(), scriptHex)
var script = Script.fromHex(scriptHex).buffer
assert.strictEqual(Script.fromBuffer(script).toHex(), scriptHex)
var hash = transaction.hashForSignature(inIndex, script, hashType)
assert.deepEqual(hash, expectedHash)

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

@ -34,7 +34,7 @@ describe('Script', function () {
var script = Script.fromASM(f.asm)
assert.strictEqual(script.toASM(), f.asm)
assert.strictEqual(script.toHex(), f.hex)
assert.strictEqual(script.buffer.toString('hex'), f.hex)
})
})
})
@ -45,17 +45,7 @@ describe('Script', function () {
var script = Script.fromHex(f.hex)
assert.strictEqual(script.toASM(), f.asm)
assert.strictEqual(script.toHex(), f.hex)
})
})
})
describe('getHash', function () {
fixtures.valid.forEach(function (f) {
it('produces a HASH160 of ' + f.description, function () {
var script = Script.fromHex(f.hex)
assert.strictEqual(script.getHash().toString('hex'), f.hash)
assert.strictEqual(script.buffer.toString('hex'), f.hex)
})
})
})
@ -71,7 +61,7 @@ describe('Script', function () {
opcodes.OP_EQUAL
])
assert.strictEqual(script.toHex(), 'a920000000000000000000000000000000000000000000000000000000000000000087')
assert.strictEqual(script.buffer.toString('hex'), 'a920000000000000000000000000000000000000000000000000000000000000000087')
})
})
@ -82,14 +72,14 @@ describe('Script', function () {
it('should return a script without the given value', function () {
var subScript = script.without(opcodes.OP_HASH160)
assert.strictEqual(subScript.toHex(), '14e8c300c87986efa94c37c0519929019ef86eb5b487')
assert.strictEqual(subScript.buffer.toString('hex'), '14e8c300c87986efa94c37c0519929019ef86eb5b487')
})
it('shouldnt mutate the original script', function () {
var subScript = script.without(opcodes.OP_EQUAL)
assert.notEqual(subScript.toHex(), hex)
assert.strictEqual(script.toHex(), hex)
assert.notEqual(subScript.buffer.toString('hex'), hex)
assert.strictEqual(script.buffer.toString('hex'), hex)
})
})
})

View file

@ -19,7 +19,7 @@ describe('Scripts', function () {
if (!f.scriptSig) return
it('classifies ' + f.scriptSig + ' as ' + f.type, function () {
var script = Script.fromASM(f.scriptSig)
var script = Script.fromASM(f.scriptSig).buffer
var type = scripts.classifyInput(script)
assert.strictEqual(type, f.type)
@ -31,7 +31,7 @@ describe('Scripts', function () {
if (!f.typeIncomplete) return
it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () {
var script = Script.fromASM(f.scriptSig)
var script = Script.fromASM(f.scriptSig).buffer
var type = scripts.classifyInput(script, true)
assert.strictEqual(type, f.typeIncomplete)
@ -44,7 +44,7 @@ describe('Scripts', function () {
if (!f.scriptPubKey) return
it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () {
var script = Script.fromASM(f.scriptPubKey)
var script = Script.fromASM(f.scriptPubKey).buffer
var type = scripts.classifyOutput(script)
assert.strictEqual(type, f.type)
@ -67,9 +67,9 @@ describe('Scripts', function () {
var script
if (f.scriptSig) {
script = Script.fromASM(f.scriptSig)
script = Script.fromASM(f.scriptSig).buffer
} else {
script = Script.fromHex(f.scriptSigHex)
script = Script.fromHex(f.scriptSigHex).buffer
}
it('returns ' + expected + ' for ' + f.scriptSig, function () {
@ -94,9 +94,9 @@ describe('Scripts', function () {
var script
if (f.scriptSig) {
script = Script.fromASM(f.scriptSig)
script = Script.fromASM(f.scriptSig).buffer
} else {
script = Script.fromHex(f.scriptSigHex)
script = Script.fromHex(f.scriptSigHex).buffer
}
assert.strictEqual(inputFn(script), false)
@ -111,7 +111,7 @@ describe('Scripts', function () {
if (outputFn && f.scriptPubKey) {
it('returns ' + expected + ' for ' + f.scriptPubKey, function () {
var script = Script.fromASM(f.scriptPubKey)
var script = Script.fromASM(f.scriptPubKey).buffer
assert.strictEqual(outputFn(script), expected)
})
@ -123,7 +123,7 @@ describe('Scripts', function () {
fixtures.invalid[outputFnName].forEach(function (f) {
if (outputFn && f.scriptPubKey) {
it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () {
var script = Script.fromASM(f.scriptPubKey)
var script = Script.fromASM(f.scriptPubKey).buffer
assert.strictEqual(outputFn(script), false)
})
@ -140,7 +140,7 @@ describe('Scripts', function () {
var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyInput(signature)
assert.strictEqual(scriptSig.toASM(), f.scriptSig)
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
})
})
})
@ -152,7 +152,7 @@ describe('Scripts', function () {
it('returns ' + f.scriptPubKey, function () {
var pubKey = new Buffer(f.pubKey, 'hex')
var scriptPubKey = scripts.pubKeyOutput(pubKey)
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
})
})
})
@ -167,7 +167,7 @@ describe('Scripts', function () {
var signature = new Buffer(f.signature, 'hex')
var scriptSig = scripts.pubKeyHashInput(signature, pubKey)
assert.strictEqual(scriptSig.toASM(), f.scriptSig)
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
})
})
})
@ -181,7 +181,7 @@ describe('Scripts', function () {
it('returns ' + f.scriptPubKey, function () {
var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash)
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
})
})
})
@ -196,12 +196,12 @@ describe('Scripts', function () {
})
var scriptSig = scripts.multisigInput(signatures)
assert.strictEqual(scriptSig.toASM(), f.scriptSig)
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
})
})
fixtures.invalid.multisigInput.forEach(function (f) {
var scriptPubKey = Script.fromASM(f.scriptPubKey)
var scriptPubKey = Script.fromASM(f.scriptPubKey).buffer
it('throws on ' + f.exception, function () {
var signatures = f.signatures.map(function (signature) {
@ -223,7 +223,7 @@ describe('Scripts', function () {
var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys)
it('returns ' + f.scriptPubKey, function () {
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
})
})
@ -244,16 +244,16 @@ describe('Scripts', function () {
fixtures.valid.forEach(function (f) {
if (f.type !== 'scripthash') return
var redeemScript = Script.fromASM(f.redeemScript)
var redeemScriptSig = Script.fromASM(f.redeemScriptSig)
var redeemScript = Script.fromASM(f.redeemScript).buffer
var redeemScriptSig = Script.fromASM(f.redeemScriptSig).buffer
it('returns ' + f.scriptSig, function () {
var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript)
if (f.scriptSig) {
assert.strictEqual(scriptSig.toASM(), f.scriptSig)
assert.strictEqual(Script.fromBuffer(scriptSig).toASM(), f.scriptSig)
} else {
assert.strictEqual(scriptSig.toHex(), f.scriptSigHex)
assert.strictEqual(Script.fromBuffer(scriptSig).toHex(), f.scriptSigHex)
}
})
})
@ -265,10 +265,10 @@ describe('Scripts', function () {
if (!f.scriptPubKey) return
it('returns ' + f.scriptPubKey, function () {
var redeemScript = Script.fromASM(f.redeemScript)
var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash())
var redeemScript = Script.fromASM(f.redeemScript).buffer
var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript))
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
})
})
})
@ -281,7 +281,7 @@ describe('Scripts', function () {
var scriptPubKey = scripts.nullDataOutput(data)
it('returns ' + f.scriptPubKey, function () {
assert.strictEqual(scriptPubKey.toASM(), f.scriptPubKey)
assert.strictEqual(Script.fromBuffer(scriptPubKey).toASM(), f.scriptPubKey)
})
})
})

View file

@ -19,9 +19,9 @@ describe('Transaction', function () {
if (txIn.data) {
var data = new Buffer(txIn.data, 'hex')
script = new Script(data, [])
script = data
} else if (txIn.script) {
script = Script.fromASM(txIn.script)
script = Script.fromASM(txIn.script).buffer
}
tx.addInput(txHash, txIn.index, txIn.sequence, script)
@ -32,9 +32,9 @@ describe('Transaction', function () {
if (txOut.data) {
var data = new Buffer(txOut.data, 'hex')
script = new Script(data, [])
script = data
} else if (txOut.script) {
script = Script.fromASM(txOut.script)
script = Script.fromASM(txOut.script).buffer
}
tx.addOutput(script, txOut.value)
@ -102,7 +102,7 @@ describe('Transaction', function () {
var tx = new Transaction()
tx.addInput(prevTxHash, 0)
assert.strictEqual(tx.ins[0].script, Script.EMPTY)
assert.strictEqual(tx.ins[0].script.length, 0)
})
fixtures.invalid.addInput.forEach(function (f) {
@ -120,8 +120,8 @@ describe('Transaction', function () {
describe('addOutput', function () {
it('returns an index', function () {
var tx = new Transaction()
assert.strictEqual(tx.addOutput(Script.EMPTY, 0), 0)
assert.strictEqual(tx.addOutput(Script.EMPTY, 0), 1)
assert.strictEqual(tx.addOutput(new Buffer(0), 0), 0)
assert.strictEqual(tx.addOutput(new Buffer(0), 0), 1)
})
})

View file

@ -22,14 +22,14 @@ function construct (f, sign) {
var prevTxScript
if (input.prevTxScript) {
prevTxScript = Script.fromASM(input.prevTxScript)
prevTxScript = Script.fromASM(input.prevTxScript).buffer
}
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
})
f.outputs.forEach(function (output) {
var script = Script.fromASM(output.script)
var script = Script.fromASM(output.script).buffer
txb.addOutput(script, output.value)
})
@ -41,7 +41,7 @@ function construct (f, sign) {
var redeemScript
if (sign.redeemScript) {
redeemScript = Script.fromASM(sign.redeemScript)
redeemScript = Script.fromASM(sign.redeemScript).buffer
}
txb.sign(index, keyPair, redeemScript, sign.hashType)
@ -200,7 +200,7 @@ describe('TransactionBuilder', function () {
var redeemScript
if (sign.redeemScript) {
redeemScript = Script.fromASM(sign.redeemScript)
redeemScript = Script.fromASM(sign.redeemScript).buffer
}
if (!sign.throws) {
@ -262,7 +262,7 @@ describe('TransactionBuilder', function () {
var network = NETWORKS[f.network]
f.inputs.forEach(function (input, i) {
var redeemScript = Script.fromASM(input.redeemScript)
var redeemScript = Script.fromASM(input.redeemScript).buffer
input.signs.forEach(function (sign) {
// rebuild the transaction each-time after the first
@ -272,11 +272,11 @@ describe('TransactionBuilder', function () {
var scriptSig = tx.ins[i].script
// ignore OP_0 on the front, ignore redeemScript
var signatures = scriptSig.chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
var signatures = Script.fromBuffer(scriptSig).chunks.slice(1, -1).filter(function (x) { return x !== ops.OP_0 })
// rebuild/replace the scriptSig without them
var replacement = scripts.scriptHashInput(scripts.multisigInput(signatures), redeemScript)
assert.strictEqual(replacement.toASM(), sign.scriptSigFiltered)
assert.strictEqual(Script.fromBuffer(replacement).toASM(), sign.scriptSigFiltered)
tx.ins[i].script = replacement
}
@ -292,7 +292,7 @@ describe('TransactionBuilder', function () {
tx = txb.buildIncomplete()
// now verify the serialized scriptSig is as expected
assert.strictEqual(tx.ins[i].script.toASM(), sign.scriptSig)
assert.strictEqual(Script.fromBuffer(tx.ins[i].script).toASM(), sign.scriptSig)
})
})
@ -309,7 +309,7 @@ describe('TransactionBuilder', function () {
txb = TransactionBuilder.fromTransaction(lameTx, network)
var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG')
var redeemScript = Script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG').buffer
var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network)
txb.sign(0, keyPair, redeemScript)