diff --git a/src/address.js b/src/address.js index f5fb21534..669b6d57a 100644 --- a/src/address.js +++ b/src/address.js @@ -1,11 +1,11 @@ -var base58check = require('bs58check') +var bs58check = require('bs58check') +var bscript = require('./script') var networks = require('./networks') -var scripts = require('./scripts') var typeforce = require('typeforce') var types = require('./types') 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 long') @@ -15,14 +15,14 @@ function fromBase58Check (address) { return { hash: hash, version: version } } -function fromOutputScript (script, network) { +function fromOutputScript (scriptPubKey, network) { network = network || networks.bitcoin - 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) + var chunks = bscript.decompile(scriptPubKey) + if (bscript.isPubKeyHashOutput(chunks)) return toBase58Check(chunks[2], network.pubKeyHash) + if (bscript.isScriptHashOutput(chunks)) return toBase58Check(chunks[1], network.scriptHash) - throw new Error(scripts.toASM(chunks) + ' has no matching Address') + throw new Error(bscript.toASM(chunks) + ' has no matching Address') } function toBase58Check (hash, version) { @@ -32,21 +32,21 @@ function toBase58Check (hash, version) { payload.writeUInt8(version, 0) hash.copy(payload, 1) - return base58check.encode(payload) + return bs58check.encode(payload) } function toOutputScript (address, network) { 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 long') var version = payload.readUInt8(0) var hash = payload.slice(1) - if (version === network.pubKeyHash) return scripts.pubKeyHashOutput(hash) - if (version === network.scriptHash) return scripts.scriptHashOutput(hash) + if (version === network.pubKeyHash) return bscript.pubKeyHashOutput(hash) + if (version === network.scriptHash) return bscript.scriptHashOutput(hash) throw new Error(address + ' has no matching Script') } diff --git a/src/block.js b/src/block.js index 8a36f6517..a79178464 100644 --- a/src/block.js +++ b/src/block.js @@ -1,5 +1,5 @@ var bufferutils = require('./bufferutils') -var crypto = require('./crypto') +var bcrypto = require('./crypto') var Transaction = require('./transaction') @@ -66,7 +66,7 @@ Block.fromHex = function (hex) { } Block.prototype.getHash = function () { - return crypto.hash256(this.toBuffer(true)) + return bcrypto.hash256(this.toBuffer(true)) } Block.prototype.getId = function () { diff --git a/src/ecpair.js b/src/ecpair.js index bafc311bd..45990393b 100644 --- a/src/ecpair.js +++ b/src/ecpair.js @@ -1,25 +1,29 @@ -var bs58check = require('bs58check') var bcrypto = require('./crypto') +var bs58check = require('bs58check') var ecdsa = require('./ecdsa') var ecurve = require('ecurve') -var NETWORKS = require('./networks') var randomBytes = require('randombytes') var typeforce = require('typeforce') var types = require('./types') +var NETWORKS = require('./networks') var BigInteger = require('bigi') +var secp256k1 = ecurve.getCurveByName('secp256k1') + function ECPair (d, Q, options) { - options = options || {} + if (options) { + typeforce({ + compressed: types.maybe(types.Boolean), + network: types.maybe(types.Network) + }, options) + } - typeforce({ - compressed: types.maybe(types.Boolean), - network: types.maybe(types.Network) - }, options) + options = options || {} if (d) { if (d.signum() <= 0) throw new Error('Private key must be greater than 0') - if (d.compareTo(ECPair.curve.n) >= 0) throw new Error('Private key must be less than the curve order') + if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order') if (Q) throw new TypeError('Unexpected publicKey parameter') this.d = d @@ -37,18 +41,15 @@ function ECPair (d, Q, options) { Object.defineProperty(ECPair.prototype, 'Q', { get: function () { if (!this.__Q && this.d) { - this.__Q = ECPair.curve.G.multiply(this.d) + this.__Q = secp256k1.G.multiply(this.d) } return this.__Q } }) -// Public access to secp256k1 curve -ECPair.curve = ecurve.getCurveByName('secp256k1') - ECPair.fromPublicKeyBuffer = function (buffer, network) { - var Q = ecurve.Point.decodeFrom(ECPair.curve, buffer) + var Q = ecurve.Point.decodeFrom(secp256k1, buffer) return new ECPair(null, Q, { compressed: Q.compressed, @@ -108,7 +109,7 @@ ECPair.makeRandom = function (options) { typeforce(types.Buffer256bit, buffer) var d = BigInteger.fromBuffer(buffer) - d = d.mod(ECPair.curve.n) + d = d.mod(secp256k1.n) return new ECPair(d, null, options) } @@ -147,11 +148,11 @@ ECPair.prototype.getPublicKeyBuffer = function () { ECPair.prototype.sign = function (hash) { if (!this.d) throw new Error('Missing private key') - return ecdsa.sign(ECPair.curve, hash, this.d) + return ecdsa.sign(secp256k1, hash, this.d) } ECPair.prototype.verify = function (hash, signature) { - return ecdsa.verify(ECPair.curve, hash, signature, this.Q) + return ecdsa.verify(secp256k1, hash, signature, this.Q) } module.exports = ECPair diff --git a/src/index.js b/src/index.js index 80d8f4963..a191f1705 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,4 @@ module.exports = { - Address: require('./address'), Block: require('./block'), ECPair: require('./ecpair'), ECSignature: require('./ecsignature'), @@ -7,10 +6,11 @@ module.exports = { Transaction: require('./transaction'), TransactionBuilder: require('./transaction_builder'), + address: require('./address'), bufferutils: require('./bufferutils'), crypto: require('./crypto'), message: require('./message'), networks: require('./networks'), opcodes: require('./opcodes'), - scripts: require('./scripts') + script: require('./script') } diff --git a/src/message.js b/src/message.js index bced6dc05..16fc1fee9 100644 --- a/src/message.js +++ b/src/message.js @@ -1,5 +1,5 @@ var bufferutils = require('./bufferutils') -var crypto = require('./crypto') +var bcrypto = require('./crypto') var ecdsa = require('./ecdsa') var networks = require('./networks') @@ -16,7 +16,7 @@ function magicHash (message, network) { var lengthBuffer = bufferutils.varIntBuffer(messageBuffer.length) var buffer = Buffer.concat([messagePrefix, lengthBuffer, messageBuffer]) - return crypto.hash256(buffer) + return bcrypto.hash256(buffer) } function sign (keyPair, message, network) { diff --git a/src/scripts.js b/src/script.js similarity index 100% rename from src/scripts.js rename to src/script.js diff --git a/src/transaction.js b/src/transaction.js index 4a5bc0f4e..f80997d48 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -1,7 +1,7 @@ var bufferutils = require('./bufferutils') -var crypto = require('./crypto') +var bcrypto = require('./crypto') var opcodes = require('./opcodes') -var scripts = require('./scripts') +var bscript = require('./script') var typeforce = require('typeforce') var types = require('./types') @@ -99,7 +99,7 @@ Transaction.isCoinbaseHash = function (buffer) { 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( types.Hash256bit, types.UInt32, @@ -111,13 +111,11 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) { sequence = Transaction.DEFAULT_SEQUENCE } - script = script || EMPTY_SCRIPT - // Add the input and return the input's index return (this.ins.push({ hash: hash, index: index, - script: script, + script: scriptSig || EMPTY_SCRIPT, sequence: sequence }) - 1) } @@ -133,8 +131,8 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) { } Transaction.prototype.byteLength = function () { - function scriptSize (script) { - var length = script.length + function scriptSize (someScript) { + var length = someScript.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, // 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 = bscript.compile(bscript.decompile(prevOutScript).filter(function (x) { return x !== opcodes.OP_CODESEPARATOR })) var i @@ -252,11 +250,11 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT buffer.writeInt32LE(hashType, buffer.length - 4) txTmp.toBuffer().copy(buffer, 0) - return crypto.hash256(buffer) + return bcrypto.hash256(buffer) } Transaction.prototype.getHash = function () { - return crypto.hash256(this.toBuffer()) + return bcrypto.hash256(this.toBuffer()) } Transaction.prototype.getId = function () { @@ -320,10 +318,10 @@ Transaction.prototype.toHex = function () { 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) - this.ins[index].script = script + this.ins[index].script = scriptSig } module.exports = Transaction diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 465c66101..6e1865a4a 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -1,10 +1,10 @@ +var baddress = require('./address') var bcrypto = require('./crypto') +var bscript = require('./script') var bufferutils = require('./bufferutils') var networks = require('./networks') var ops = require('./opcodes') -var scripts = require('./scripts') -var Address = require('./address') var ECPair = require('./ecpair') var ECSignature = require('./ecsignature') var Transaction = require('./transaction') @@ -12,21 +12,21 @@ var Transaction = require('./transaction') function extractInput (txIn) { var redeemScript var scriptSig = txIn.script - var scriptSigChunks = scripts.decompile(scriptSig) + var scriptSigChunks = bscript.decompile(scriptSig) var prevOutScript - var prevOutType = scripts.classifyInput(scriptSig, true) + var prevOutType = bscript.classifyInput(scriptSig, true) var scriptType // Re-classify if scriptHash if (prevOutType === 'scripthash') { redeemScript = scriptSigChunks.slice(-1)[0] - prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) + prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript)) - scriptSig = scripts.compile(scriptSigChunks.slice(0, -1)) + scriptSig = bscript.compile(scriptSigChunks.slice(0, -1)) scriptSigChunks = scriptSigChunks.slice(0, -1) - scriptType = scripts.classifyInput(scriptSig, true) + scriptType = bscript.classifyInput(scriptSig, true) } else { scriptType = prevOutType } @@ -34,7 +34,7 @@ function extractInput (txIn) { // pre-empt redeemScript decompilation var redeemScriptChunks if (redeemScript) { - redeemScriptChunks = scripts.decompile(redeemScript) + redeemScriptChunks = bscript.decompile(redeemScript) } // Extract hashType, pubKeys and signatures @@ -46,7 +46,7 @@ function extractInput (txIn) { hashType = parsed.hashType pubKeys = scriptSigChunks.slice(1) signatures = [parsed.signature] - prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(pubKeys[0])) + prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(pubKeys[0])) break @@ -147,8 +147,8 @@ TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOu var input = {} if (prevOutScript) { - var prevOutScriptChunks = scripts.decompile(prevOutScript) - var prevOutType = scripts.classifyOutput(prevOutScriptChunks) + var prevOutScriptChunks = bscript.decompile(prevOutScript) + var prevOutType = bscript.classifyOutput(prevOutScriptChunks) // if we can, extract pubKey information switch (prevOutType) { @@ -198,7 +198,7 @@ TransactionBuilder.prototype.addOutput = function (scriptPubKey, value) { // Attempt to get a script if it's a base58 address string if (typeof scriptPubKey === 'string') { - scriptPubKey = Address.toOutputScript(scriptPubKey, this.network) + scriptPubKey = baddress.toOutputScript(scriptPubKey, this.network) } return this.tx.addOutput(scriptPubKey, value) @@ -240,7 +240,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) { switch (scriptType) { case 'pubkeyhash': var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) - scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) + scriptSig = bscript.pubKeyHashInput(pkhSignature, input.pubKeys[0]) break case 'multisig': @@ -262,12 +262,12 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) { } var redeemScript = allowIncomplete ? undefined : input.redeemScript - scriptSig = scripts.multisigInput(msSignatures, redeemScript) + scriptSig = bscript.multisigInput(msSignatures, redeemScript) break case 'pubkey': var pkSignature = input.signatures[0].toScriptSignature(input.hashType) - scriptSig = scripts.pubKeyInput(pkSignature) + scriptSig = bscript.pubKeyInput(pkSignature) break } } @@ -276,7 +276,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) { if (scriptSig) { // wrap as scriptHash if necessary if (input.prevOutType === 'scripthash') { - scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) + scriptSig = bscript.scriptHashInput(scriptSig, input.redeemScript) } tx.setInputScript(index, scriptSig) @@ -318,14 +318,14 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash if (input.prevOutScript) { if (input.prevOutType !== 'scripthash') throw new Error('PrevOutScript must be P2SH') - var scriptHash = scripts.decompile(input.prevOutScript)[1] + var scriptHash = bscript.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) + var scriptType = bscript.classifyOutput(redeemScript) if (!canSignTypes[scriptType]) throw new Error('RedeemScript not supported (' + scriptType + ')') - var redeemScriptChunks = scripts.decompile(redeemScript) + var redeemScriptChunks = bscript.decompile(redeemScript) var pubKeys = [] switch (scriptType) { 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 (!input.prevOutScript) { - input.prevOutScript = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) + input.prevOutScript = bscript.scriptHashOutput(bcrypto.hash160(redeemScript)) input.prevOutType = 'scripthash' } @@ -365,7 +365,7 @@ TransactionBuilder.prototype.sign = function (index, keyPair, redeemScript, hash // we know nothin' Jon Snow, assume pubKeyHash } else { - input.prevOutScript = scripts.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer())) + input.prevOutScript = bscript.pubKeyHashOutput(bcrypto.hash160(keyPair.getPublicKeyBuffer())) input.prevOutType = 'pubkeyhash' input.pubKeys = [kpPubKey] input.scriptType = input.prevOutType diff --git a/test/address.js b/test/address.js index 077196ac2..e765eb022 100644 --- a/test/address.js +++ b/test/address.js @@ -1,18 +1,16 @@ /* global describe, it */ var assert = require('assert') +var baddress = require('../src/address') var networks = require('../src/networks') -var scripts = require('../src/scripts') - -var Address = require('../src/address') - +var bscript = require('../src/script') var fixtures = require('./fixtures/address.json') -describe('Address', function () { +describe('address', function () { describe('fromBase58Check', function () { fixtures.valid.forEach(function (f) { it('decodes ' + f.base58check, function () { - var decode = Address.fromBase58Check(f.base58check) + var decode = baddress.fromBase58Check(f.base58check) assert.strictEqual(decode.version, f.version) assert.strictEqual(decode.hash.toString('hex'), f.hash) @@ -22,7 +20,7 @@ describe('Address', function () { fixtures.invalid.fromBase58Check.forEach(function (f) { it('throws on ' + f.exception, function () { assert.throws(function () { - Address.fromBase58Check(f.address) + baddress.fromBase58Check(f.address) }, new RegExp(f.address + ' ' + f.exception)) }) }) @@ -31,8 +29,8 @@ describe('Address', function () { describe('fromOutputScript', function () { fixtures.valid.forEach(function (f) { it('parses ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { - var script = scripts.fromASM(f.script) - var address = Address.fromOutputScript(script, networks[f.network]) + var script = bscript.fromASM(f.script) + var address = baddress.fromOutputScript(script, networks[f.network]) assert.strictEqual(address, f.base58check) }) @@ -40,10 +38,10 @@ describe('Address', function () { fixtures.invalid.fromOutputScript.forEach(function (f) { 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 () { - Address.fromOutputScript(script) + baddress.fromOutputScript(script) }, new RegExp(f.script + ' ' + f.exception)) }) }) @@ -52,7 +50,7 @@ describe('Address', function () { describe('toBase58Check', function () { fixtures.valid.forEach(function (f) { it('formats ' + f.hash + ' (' + f.network + ')', function () { - var address = Address.toBase58Check(new Buffer(f.hash, 'hex'), f.version) + var address = baddress.toBase58Check(new Buffer(f.hash, 'hex'), f.version) assert.strictEqual(address, f.base58check) }) @@ -64,16 +62,16 @@ describe('Address', function () { var network = networks[f.network] it('exports ' + f.script.slice(0, 30) + '... (' + f.network + ')', function () { - var script = Address.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) }) }) fixtures.invalid.toOutputScript.forEach(function (f) { it('throws when ' + f.exception, function () { assert.throws(function () { - Address.toOutputScript(f.address) + baddress.toOutputScript(f.address) }, new RegExp(f.address + ' ' + f.exception)) }) }) diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 5aa3a6b43..cdc3b8e00 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -37,7 +37,7 @@ describe('Bitcoin-core', function () { }) // base58_keys_valid - describe('Address.toBase58Check', function () { + describe('address.toBase58Check', function () { var typeMap = { 'pubkey': 'pubKeyHash', 'script': 'scriptHash' @@ -60,7 +60,7 @@ describe('Bitcoin-core', function () { }) // base58_keys_invalid - describe('Address.fromBase58Check', function () { + describe('address.fromBase58Check', function () { var allowedNetworks = [ bitcoin.networks.bitcoin.pubkeyhash, bitcoin.networks.bitcoin.scripthash, @@ -73,7 +73,7 @@ describe('Bitcoin-core', function () { it('throws on ' + string, function () { assert.throws(function () { - var address = bitcoin.Address.fromBase58Check(string) + var address = bitcoin.address.fromBase58Check(string) assert.notEqual(allowedNetworks.indexOf(address.version), -1, 'Invalid network') }, /(Invalid (checksum|network))|(too (short|long))/) @@ -162,7 +162,7 @@ describe('Bitcoin-core', function () { }) }) - describe('scripts.fromASM', function () { + describe('script.fromASM', function () { tx_valid.forEach(function (f) { // Objects that are only a single string are ignored if (f.length === 1) return @@ -185,7 +185,7 @@ describe('Bitcoin-core', function () { it('can parse ' + prevOutScriptPubKey, function () { // 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) var script = new Buffer(scriptHex, 'hex') - var scriptChunks = bitcoin.scripts.decompile(script) - assert.strictEqual(bitcoin.scripts.compile(scriptChunks).toString('hex'), scriptHex) + var scriptChunks = bitcoin.script.decompile(script) + assert.strictEqual(bitcoin.script.compile(scriptChunks).toString('hex'), scriptHex) var hash = transaction.hashForSignature(inIndex, script, hashType) assert.deepEqual(hash, expectedHash) diff --git a/test/crypto.js b/test/crypto.js index c61f7cb77..57b5bffee 100644 --- a/test/crypto.js +++ b/test/crypto.js @@ -1,7 +1,7 @@ /* global describe, it */ var assert = require('assert') -var crypto = require('../src/crypto') +var bcrypto = require('../src/crypto') var fixtures = require('./fixtures/crypto') @@ -9,7 +9,7 @@ describe('Crypto', function () { ['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(function (algorithm) { describe(algorithm, function () { fixtures.valid.forEach(function (f) { - var fn = crypto[algorithm] + var fn = bcrypto[algorithm] var expected = f[algorithm] it('returns ' + expected + ' for ' + f.hex, function () { diff --git a/test/ecdsa.js b/test/ecdsa.js index 26159cd86..c3bcf148f 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -1,7 +1,7 @@ /* global describe, it */ var assert = require('assert') -var crypto = require('../src/crypto') +var bcrypto = require('../src/crypto') var ecdsa = require('../src/ecdsa') var message = require('../src/message') var networks = require('../src/networks') @@ -24,7 +24,7 @@ describe('ecdsa', function () { fixtures.valid.ecdsa.forEach(function (f) { it('for "' + f.message + '"', function () { var d = BigInteger.fromHex(f.d) - var h1 = crypto.sha256(f.message) + var h1 = bcrypto.sha256(f.message) var k = ecdsa.deterministicGenerateK(curve, h1, d, checkSig) assert.strictEqual(k.toHex(), f.k) @@ -68,7 +68,7 @@ describe('ecdsa', function () { fixtures.valid.rfc6979.forEach(function (f) { it('produces the expected k values for ' + f.message + " if k wasn't suitable", function () { var d = BigInteger.fromHex(f.d) - var h1 = crypto.sha256(f.message) + var h1 = bcrypto.sha256(f.message) var results = [] ecdsa.deterministicGenerateK(curve, h1, d, function (k) { @@ -90,7 +90,7 @@ describe('ecdsa', function () { var d = BigInteger.fromHex(f.d) var Q = curve.G.multiply(d) var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex')) - var h1 = crypto.sha256(f.message) + var h1 = bcrypto.sha256(f.message) var e = BigInteger.fromBuffer(h1) var Qprime = ecdsa.recoverPubKey(curve, e, signature, f.i) @@ -137,7 +137,7 @@ describe('ecdsa', function () { fixtures.valid.ecdsa.forEach(function (f) { it('produces a deterministic signature for "' + f.message + '"', function () { var d = BigInteger.fromHex(f.d) - var hash = crypto.sha256(f.message) + var hash = bcrypto.sha256(f.message) var signature = ecdsa.sign(curve, hash, d).toDER() assert.strictEqual(signature.toString('hex'), f.signature) @@ -145,7 +145,7 @@ describe('ecdsa', function () { }) it('should sign with low S value', function () { - var hash = crypto.sha256('Vires in numeris') + var hash = bcrypto.sha256('Vires in numeris') var sig = ecdsa.sign(curve, hash, BigInteger.ONE) // See BIP62 for more information @@ -158,7 +158,7 @@ describe('ecdsa', function () { fixtures.valid.ecdsa.forEach(function (f) { it('verifies a valid signature for "' + f.message + '"', function () { var d = BigInteger.fromHex(f.d) - var H = crypto.sha256(f.message) + var H = bcrypto.sha256(f.message) var signature = ECSignature.fromDER(new Buffer(f.signature, 'hex')) var Q = curve.G.multiply(d) @@ -168,7 +168,7 @@ describe('ecdsa', function () { fixtures.invalid.verify.forEach(function (f) { it('fails to verify with ' + f.description, function () { - var H = crypto.sha256(f.message) + var H = bcrypto.sha256(f.message) var d = BigInteger.fromHex(f.d) var signature diff --git a/test/ecpair.js b/test/ecpair.js index 8c09a9cb8..b64f1d22c 100644 --- a/test/ecpair.js +++ b/test/ecpair.js @@ -11,6 +11,7 @@ var BigInteger = require('bigi') var ECPair = require('../src/ecpair') var fixtures = require('./fixtures/ecpair.json') +var secp256k1 = ecurve.getCurveByName('secp256k1') var NETWORKS = require('../src/networks') var NETWORKS_LIST = [] // Object.values(NETWORKS) @@ -53,7 +54,7 @@ describe('ECPair', function () { it('throws if public and private key given', function () { var qBuffer = new Buffer('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 'hex') - var Q = ecurve.Point.decodeFrom(ECPair.curve, qBuffer) + var Q = ecurve.Point.decodeFrom(secp256k1, qBuffer) assert.throws(function () { new ECPair(BigInteger.ONE, Q) @@ -200,25 +201,10 @@ describe('ECPair', function () { hash = new Buffer(32) }) - it('uses the secp256k1 curve by default', function () { - var secp256k1 = ecurve.getCurveByName('secp256k1') - - for (var property in secp256k1) { - // FIXME: circular structures in ecurve - if (property === 'G') continue - if (property === 'infinity') continue - - var actual = ECPair.curve[property] - var expected = secp256k1[property] - - assert.deepEqual(actual, expected) - } - }) - describe('signing', function () { it('wraps ecdsa.sign', sinon.test(function () { this.mock(ecdsa).expects('sign') - .once().calledWith(ECPair.curve, hash, keyPair.d) + .once().calledWith(secp256k1, hash, keyPair.d) keyPair.sign(hash) })) @@ -241,7 +227,7 @@ describe('ECPair', function () { it('wraps ecdsa.verify', sinon.test(function () { this.mock(ecdsa).expects('verify') - .once().calledWith(ECPair.curve, hash, signature, keyPair.Q) + .once().calledWith(secp256k1, hash, signature, keyPair.Q) keyPair.verify(hash, signature) })) diff --git a/test/fixtures/scripts.json b/test/fixtures/script.json similarity index 100% rename from test/fixtures/scripts.json rename to test/fixtures/script.json diff --git a/test/integration/advanced.js b/test/integration/advanced.js index 030bd3869..c8f1f7a2b 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -36,7 +36,7 @@ describe('bitcoinjs-lib (advanced)', function () { var tx = new bitcoin.TransactionBuilder(network) var data = new Buffer('bitcoinjs-lib') - var dataScript = bitcoin.scripts.nullDataOutput(data) + var dataScript = bitcoin.script.nullDataOutput(data) var unspent = unspents.pop() @@ -55,7 +55,7 @@ describe('bitcoinjs-lib (advanced)', function () { var actual = bitcoin.Transaction.fromHex(transaction.txHex) 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(data, data2) diff --git a/test/integration/crypto.js b/test/integration/crypto.js index 96c66ae90..12ed70cdb 100644 --- a/test/integration/crypto.js +++ b/test/integration/crypto.js @@ -7,10 +7,13 @@ var bitcoin = require('../../') var blockchain = require('./_blockchain') var crypto = require('crypto') +var ecurve = require('ecurve') +var secp256k1 = ecurve.getCurveByName('secp256k1') + describe('bitcoinjs-lib (crypto)', function () { it('can generate a single-key stealth address', function () { - var G = bitcoin.ECPair.curve.G - var n = bitcoin.ECPair.curve.n + var G = secp256k1.G + var n = secp256k1.n function stealthSend (Q) { var noncePair = bitcoin.ECPair.makeRandom() @@ -56,7 +59,7 @@ describe('bitcoinjs-lib (crypto)', function () { assert(!master.keyPair.d, 'You already have the parent private key') assert(child.keyPair.d, 'Missing child private key') - var curve = bitcoin.ECPair.curve + var curve = secp256k1 var QP = master.keyPair.Q var serQP = master.keyPair.getPublicKeyBuffer() @@ -129,9 +132,9 @@ describe('bitcoinjs-lib (crypto)', function () { inputs.forEach(function (input) { var transaction = transactions[input.txId] 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 prevVout = transaction.ins[input.vout].index @@ -162,7 +165,7 @@ describe('bitcoinjs-lib (crypto)', function () { async.parallel(tasks, function (err) { if (err) throw err - var n = bitcoin.ECPair.curve.n + var n = secp256k1.n for (var i = 0; i < inputs.length; ++i) { for (var j = i + 1; j < inputs.length; ++j) { diff --git a/test/integration/multisig.js b/test/integration/multisig.js index cae7a478f..df1f8688c 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -14,9 +14,9 @@ describe('bitcoinjs-lib (multisig)', function () { return new Buffer(hex, 'hex') }) - var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 3 - var scriptPubKey = bitcoin.scripts.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) - var address = bitcoin.Address.fromOutputScript(scriptPubKey) + var redeemScript = bitcoin.script.multisigOutput(2, pubKeys) // 2 of 3 + var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) + var address = bitcoin.address.fromOutputScript(scriptPubKey) assert.strictEqual(address, '36NUkt6FWUi3LAWBqWRdDmdTWbt91Yvfu7') }) @@ -32,9 +32,9 @@ describe('bitcoinjs-lib (multisig)', function () { ].map(function (wif) { return bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet) }) var pubKeys = keyPairs.map(function (x) { return x.getPublicKeyBuffer() }) - var redeemScript = bitcoin.scripts.multisigOutput(2, pubKeys) // 2 of 4 - var scriptPubKey = bitcoin.scripts.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) - var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet) + var redeemScript = bitcoin.script.multisigOutput(2, pubKeys) // 2 of 4 + var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) + var address = bitcoin.address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet) // attempt to send funds to the source address blockchain.t.faucet(address, 2e4, function (err) { diff --git a/test/scripts.js b/test/script.js similarity index 67% rename from test/scripts.js rename to test/script.js index 73b9f0dc6..85e50bacc 100644 --- a/test/scripts.js +++ b/test/script.js @@ -2,12 +2,12 @@ var assert = require('assert') var bcrypto = require('../src/crypto') +var bscript = require('../src/script') var ops = require('../src/opcodes') -var scripts = require('../src/scripts') -var fixtures = require('./fixtures/scripts.json') +var fixtures = require('./fixtures/script.json') -describe('scripts', function () { +describe('script', function () { // TODO describe.skip('isCanonicalPubKey', function () {}) describe.skip('isCanonicalSignature', function () {}) @@ -16,17 +16,17 @@ describe('scripts', function () { fixtures.valid.forEach(function (f) { if (f.scriptSig) { it('encodes/decodes ' + f.scriptSig, function () { - var script = scripts.fromASM(f.scriptSig) + var scriptSig = bscript.fromASM(f.scriptSig) - assert.strictEqual(scripts.toASM(script), f.scriptSig) + assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) }) } if (f.scriptPubKey) { it('encodes/decodes ' + f.scriptPubKey, function () { - var script = scripts.fromASM(f.scriptPubKey) + var scriptPubKey = bscript.fromASM(f.scriptPubKey) - assert.strictEqual(scripts.toASM(script), f.scriptPubKey) + assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) }) } }) @@ -36,17 +36,17 @@ describe('scripts', function () { fixtures.valid.forEach(function (f) { if (f.scriptSig) { it('compiles ' + f.scriptSig, function () { - var script = scripts.fromASM(f.scriptSig) + var scriptSig = bscript.fromASM(f.scriptSig) - assert.strictEqual(scripts.compile(script).toString('hex'), f.scriptSigHex) + assert.strictEqual(bscript.compile(scriptSig).toString('hex'), f.scriptSigHex) }) } if (f.scriptPubKey) { it('compiles ' + f.scriptPubKey, function () { - var script = scripts.fromASM(f.scriptPubKey) + var scriptPubKey = bscript.fromASM(f.scriptPubKey) - assert.strictEqual(scripts.compile(script).toString('hex'), f.scriptPubKeyHex) + assert.strictEqual(bscript.compile(scriptPubKey).toString('hex'), f.scriptPubKeyHex) }) } }) @@ -56,24 +56,24 @@ describe('scripts', function () { fixtures.valid.forEach(function (f) { if (f.scriptSigHex) { it('decompiles ' + f.scriptSig, function () { - var chunks = scripts.decompile(new Buffer(f.scriptSigHex, 'hex')) + var chunks = bscript.decompile(new Buffer(f.scriptSigHex, 'hex')) - assert.strictEqual(scripts.toASM(chunks), f.scriptSig) + assert.strictEqual(bscript.toASM(chunks), f.scriptSig) }) } if (f.scriptPubKeyHex) { it('decompiles ' + f.scriptPubKey, function () { - var chunks = scripts.decompile(new Buffer(f.scriptPubKeyHex, 'hex')) + var chunks = bscript.decompile(new Buffer(f.scriptPubKeyHex, 'hex')) - assert.strictEqual(scripts.toASM(chunks), f.scriptPubKey) + assert.strictEqual(bscript.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')) + var chunks = bscript.decompile(new Buffer(f.hex, 'hex')) assert.strictEqual(chunks.length, 0) }) @@ -85,8 +85,8 @@ describe('scripts', function () { if (!f.scriptSig) return it('classifies ' + f.scriptSig + ' as ' + f.type, function () { - var script = scripts.fromASM(f.scriptSig) - var type = scripts.classifyInput(script) + var scriptSig = bscript.fromASM(f.scriptSig) + var type = bscript.classifyInput(scriptSig) assert.strictEqual(type, f.type) }) @@ -97,8 +97,8 @@ describe('scripts', function () { if (!f.typeIncomplete) return it('classifies incomplete ' + f.scriptSig + ' as ' + f.typeIncomplete, function () { - var script = scripts.fromASM(f.scriptSig) - var type = scripts.classifyInput(script, true) + var scriptSig = bscript.fromASM(f.scriptSig) + var type = bscript.classifyInput(scriptSig, true) assert.strictEqual(type, f.typeIncomplete) }) @@ -110,8 +110,8 @@ describe('scripts', function () { if (!f.scriptPubKey) return it('classifies ' + f.scriptPubKey + ' as ' + f.type, function () { - var script = scripts.fromASM(f.scriptPubKey) - var type = scripts.classifyOutput(script) + var scriptPubKey = bscript.fromASM(f.scriptPubKey) + var type = bscript.classifyOutput(scriptPubKey) assert.strictEqual(type, f.type) }) @@ -122,25 +122,25 @@ describe('scripts', function () { var inputFnName = 'is' + type + 'Input' var outputFnName = 'is' + type + 'Output' - var inputFn = scripts[inputFnName] - var outputFn = scripts[outputFnName] + var inputFn = bscript[inputFnName] + var outputFn = bscript[outputFnName] describe('is' + type + 'Input', function () { fixtures.valid.forEach(function (f) { var expected = type.toLowerCase() === f.type if (inputFn && f.scriptSig) { - var script = scripts.fromASM(f.scriptSig) + var scriptSig = bscript.fromASM(f.scriptSig) it('returns ' + expected + ' for ' + f.scriptSig, function () { - assert.strictEqual(inputFn(script), expected) + assert.strictEqual(inputFn(scriptSig), expected) }) if (f.typeIncomplete) { var expectedIncomplete = type.toLowerCase() === f.typeIncomplete 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) { if (inputFn && (f.scriptSig || f.scriptSigHex)) { it('returns false for ' + f.description + ' (' + (f.scriptSig || f.scriptSigHex) + ')', function () { - var script + var scriptSig if (f.scriptSig) { - script = scripts.fromASM(f.scriptSig) + scriptSig = bscript.fromASM(f.scriptSig) } else { - script = scripts.fromHex(f.scriptSigHex) + scriptSig = bscript.fromHex(f.scriptSigHex) } - assert.strictEqual(inputFn(script), false) + assert.strictEqual(inputFn(scriptSig), false) }) } }) @@ -171,9 +171,9 @@ describe('scripts', function () { if (outputFn && f.scriptPubKey) { it('returns ' + expected + ' for ' + f.scriptPubKey, function () { - var script = scripts.fromASM(f.scriptPubKey) + var scriptPubKey = bscript.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) { if (outputFn && f.scriptPubKey) { it('returns false for ' + f.description + ' (' + f.scriptPubKey + ')', function () { - var script = scripts.fromASM(f.scriptPubKey) + var scriptPubKey = bscript.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 () { var signature = new Buffer(f.signature, 'hex') - var scriptSig = scripts.pubKeyInput(signature) - assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) + var scriptSig = bscript.pubKeyInput(signature) + assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) }) }) }) @@ -211,9 +211,9 @@ describe('scripts', function () { it('returns ' + f.scriptPubKey, function () { var pubKey = new Buffer(f.pubKey, 'hex') - var scriptPubKey = scripts.pubKeyOutput(pubKey) + var scriptPubKey = bscript.pubKeyOutput(pubKey) - assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) }) }) }) @@ -227,8 +227,8 @@ describe('scripts', function () { it('returns ' + f.scriptSig, function () { var signature = new Buffer(f.signature, 'hex') - var scriptSig = scripts.pubKeyHashInput(signature, pubKey) - assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) + var scriptSig = bscript.pubKeyHashInput(signature, pubKey) + assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) }) }) }) @@ -241,8 +241,8 @@ describe('scripts', function () { var pubKeyHash = bcrypto.hash160(pubKey) it('returns ' + f.scriptPubKey, function () { - var scriptPubKey = scripts.pubKeyHashOutput(pubKeyHash) - assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) + var scriptPubKey = bscript.pubKeyHashOutput(pubKeyHash) + assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) }) }) @@ -251,7 +251,7 @@ describe('scripts', function () { it('throws on ' + f.exception, function () { assert.throws(function () { - scripts.pubKeyHashOutput(hash) + bscript.pubKeyHashOutput(hash) }, new RegExp(f.exception)) }) }) @@ -266,13 +266,13 @@ describe('scripts', function () { return signature ? new Buffer(signature, 'hex') : ops.OP_0 }) - var scriptSig = scripts.multisigInput(signatures) - assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) + var scriptSig = bscript.multisigInput(signatures) + assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) }) }) fixtures.invalid.multisigInput.forEach(function (f) { - var scriptPubKey = scripts.fromASM(f.scriptPubKey) + var scriptPubKey = bscript.fromASM(f.scriptPubKey) it('throws on ' + f.exception, function () { var signatures = f.signatures.map(function (signature) { @@ -280,7 +280,7 @@ describe('scripts', function () { }) assert.throws(function () { - scripts.multisigInput(signatures, scriptPubKey) + bscript.multisigInput(signatures, scriptPubKey) }, new RegExp(f.exception)) }) }) @@ -291,10 +291,10 @@ describe('scripts', function () { if (f.type !== 'multisig') return var pubKeys = f.pubKeys.map(function (p) { return new Buffer(p, 'hex') }) - var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) + var scriptPubKey = bscript.multisigOutput(pubKeys.length, pubKeys) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) }) }) @@ -305,7 +305,7 @@ describe('scripts', function () { it('throws on ' + f.exception, function () { assert.throws(function () { - scripts.multisigOutput(f.m, pubKeys) + bscript.multisigOutput(f.m, pubKeys) }, new RegExp(f.exception)) }) }) @@ -315,14 +315,14 @@ describe('scripts', function () { fixtures.valid.forEach(function (f) { if (f.type !== 'scripthash') return - var redeemScript = scripts.fromASM(f.redeemScript) - var redeemScriptSig = scripts.fromASM(f.redeemScriptSig) + var redeemScript = bscript.fromASM(f.redeemScript) + var redeemScriptSig = bscript.fromASM(f.redeemScriptSig) it('returns ' + f.scriptSig, function () { - var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) + var scriptSig = bscript.scriptHashInput(redeemScriptSig, redeemScript) if (f.scriptSig) { - assert.strictEqual(scripts.toASM(scriptSig), f.scriptSig) + assert.strictEqual(bscript.toASM(scriptSig), f.scriptSig) } else { assert.strictEqual(scriptSig.toString('hex'), f.scriptSigHex) @@ -337,10 +337,10 @@ describe('scripts', function () { if (!f.scriptPubKey) return it('returns ' + f.scriptPubKey, function () { - var redeemScript = scripts.fromASM(f.redeemScript) - var scriptPubKey = scripts.scriptHashOutput(bcrypto.hash160(redeemScript)) + var redeemScript = bscript.fromASM(f.redeemScript) + var scriptPubKey = bscript.scriptHashOutput(bcrypto.hash160(redeemScript)) - assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) }) }) @@ -349,7 +349,7 @@ describe('scripts', function () { it('throws on ' + f.exception, function () { assert.throws(function () { - scripts.scriptHashOutput(hash) + bscript.scriptHashOutput(hash) }, new RegExp(f.exception)) }) }) @@ -360,10 +360,10 @@ describe('scripts', function () { if (f.type !== 'nulldata') return var data = new Buffer(f.data, 'hex') - var scriptPubKey = scripts.nullDataOutput(data) + var scriptPubKey = bscript.nullDataOutput(data) it('returns ' + f.scriptPubKey, function () { - assert.strictEqual(scripts.toASM(scriptPubKey), f.scriptPubKey) + assert.strictEqual(bscript.toASM(scriptPubKey), f.scriptPubKey) }) }) }) diff --git a/test/transaction.js b/test/transaction.js index f5effe6bb..6433887f1 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -1,7 +1,7 @@ /* global describe, it, beforeEach */ var assert = require('assert') -var scripts = require('../src/scripts') +var bscript = require('../src/script') var Transaction = require('../src/transaction') @@ -15,17 +15,16 @@ describe('Transaction', function () { raw.ins.forEach(function (txIn) { var txHash = new Buffer(txIn.hash, 'hex') - var script + var scriptSig if (txIn.data) { - var data = new Buffer(txIn.data, 'hex') - script = data + scriptSig = new Buffer(txIn.data, 'hex') } 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) { @@ -36,7 +35,7 @@ describe('Transaction', function () { script = data } else if (txOut.script) { - script = scripts.fromASM(txOut.script) + script = bscript.fromASM(txOut.script) } tx.addOutput(script, txOut.value) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index f6a6a1d21..4fff2eb6d 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -1,10 +1,10 @@ /* global describe, it, beforeEach */ var assert = require('assert') +var baddress = require('../src/address') +var bscript = require('../src/script') var ops = require('../src/opcodes') -var script = require('../src/scripts') -var Address = require('../src/address') var BigInteger = require('bigi') var ECPair = require('../src/ecpair') var Transaction = require('../src/transaction') @@ -21,14 +21,14 @@ function construct (f, sign) { var prevTxScript if (input.prevTxScript) { - prevTxScript = script.fromASM(input.prevTxScript) + prevTxScript = bscript.fromASM(input.prevTxScript) } txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function (output) { - txb.addOutput(script.fromASM(output.script), output.value) + txb.addOutput(bscript.fromASM(output.script), output.value) }) if (sign === undefined || sign) { @@ -38,7 +38,7 @@ function construct (f, sign) { var redeemScript if (sign.redeemScript) { - redeemScript = script.fromASM(sign.redeemScript) + redeemScript = bscript.fromASM(sign.redeemScript) } txb.sign(index, keyPair, redeemScript, sign.hashType) @@ -58,14 +58,14 @@ function construct (f, sign) { return txb } -describe.only('TransactionBuilder', function () { +describe('TransactionBuilder', function () { // constants var keyPair = new ECPair(BigInteger.ONE) var scripts = [ '1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', '1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP' ].map(function (x) { - return Address.toOutputScript(x) + return baddress.toOutputScript(x) }) var txHash = new Buffer('0e7cea811c0be9f73c0aca591034396e7264473fc25c1ca45195d7417b36cbe2', 'hex') var txb @@ -198,7 +198,7 @@ describe.only('TransactionBuilder', function () { var redeemScript if (sign.redeemScript) { - redeemScript = script.fromASM(sign.redeemScript) + redeemScript = bscript.fromASM(sign.redeemScript) } if (!sign.throws) { @@ -260,7 +260,7 @@ describe.only('TransactionBuilder', function () { var network = NETWORKS[f.network] f.inputs.forEach(function (input, i) { - var redeemScript = script.fromASM(input.redeemScript) + var redeemScript = bscript.fromASM(input.redeemScript) input.signs.forEach(function (sign) { // rebuild the transaction each-time after the first @@ -270,11 +270,11 @@ describe.only('TransactionBuilder', function () { var scriptSig = tx.ins[i].script // ignore OP_0 on the front, ignore redeemScript - var signatures = script.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) + var signatures = bscript.decompile(scriptSig).slice(1, -1).filter(function (x) { return x !== ops.OP_0 }) // rebuild/replace the scriptSig without them - var replacement = script.scriptHashInput(script.multisigInput(signatures), redeemScript) - assert.strictEqual(script.toASM(replacement), sign.scriptSigFiltered) + var replacement = bscript.scriptHashInput(bscript.multisigInput(signatures), redeemScript) + assert.strictEqual(bscript.toASM(replacement), sign.scriptSigFiltered) tx.ins[i].script = replacement } @@ -290,7 +290,7 @@ describe.only('TransactionBuilder', function () { tx = txb.buildIncomplete() // now verify the serialized scriptSig is as expected - assert.strictEqual(script.toASM(tx.ins[i].script), sign.scriptSig) + assert.strictEqual(bscript.toASM(tx.ins[i].script), sign.scriptSig) }) }) @@ -307,7 +307,7 @@ describe.only('TransactionBuilder', function () { txb = TransactionBuilder.fromTransaction(lameTx, network) - var redeemScript = script.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG') + var redeemScript = bscript.fromASM('OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a 04f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672 OP_3 OP_CHECKMULTISIG') var keyPair = ECPair.fromWIF('91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgx3cTMqe', network) txb.sign(0, keyPair, redeemScript)