From 0bba21546fed6382de204952243093f14de8fce9 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 2 Mar 2015 13:31:03 +1100 Subject: [PATCH] various: more standard-format artifact fixes --- src/bufferutils.js | 4 ++-- src/ecsignature.js | 4 +++- src/hdnode.js | 2 +- src/scripts.js | 3 ++- src/transaction.js | 26 ++++++++++------------- src/transaction_builder.js | 40 +++++++++++++++++++++--------------- test/ecdsa.js | 10 ++------- test/ecsignature.js | 20 ++++-------------- test/integration/advanced.js | 2 +- test/integration/multisig.js | 6 +++--- test/transaction_builder.js | 7 +++++-- 11 files changed, 58 insertions(+), 66 deletions(-) diff --git a/src/bufferutils.js b/src/bufferutils.js index 99337a9..02c9594 100644 --- a/src/bufferutils.js +++ b/src/bufferutils.js @@ -20,7 +20,7 @@ function readPushDataInt (buffer, offset) { var opcode = buffer.readUInt8(offset) var number, size - // ~6 bit + // ~6 bit if (opcode < opcodes.OP_PUSHDATA1) { number = opcode size = 1 @@ -64,7 +64,7 @@ function readVarInt (buffer, offset) { var t = buffer.readUInt8(offset) var number, size - // 8 bit + // 8 bit if (t < 253) { number = t size = 1 diff --git a/src/ecsignature.js b/src/ecsignature.js index c2f5e53..abab161 100644 --- a/src/ecsignature.js +++ b/src/ecsignature.js @@ -82,8 +82,10 @@ ECSignature.parseScriptSignature = function (buffer) { } ECSignature.prototype.toCompact = function (i, compressed) { - if (compressed) + if (compressed) { i += 4 + } + i += 27 var buffer = new Buffer(65) diff --git a/src/hdnode.js b/src/hdnode.js index e3ca408..ea079d8 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -118,7 +118,7 @@ HDNode.fromBuffer = function (buffer, network, __ignoreDeprecation) { var chainCode = buffer.slice(13, 45) var data, hd - // 33 bytes: private key data (0x00 + k) + // 33 bytes: private key data (0x00 + k) if (version === network.bip32.private) { assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key') data = buffer.slice(46, 78) diff --git a/src/scripts.js b/src/scripts.js index 5beda58..79ef9eb 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -29,8 +29,9 @@ function isCanonicalSignature (buffer) { try { ECSignature.parseScriptSignature(buffer) } catch (e) { - if (!(e.message.match(/Not a DER sequence|Invalid sequence length|Expected a DER integer|R length is zero|S length is zero|R value excessively padded|S value excessively padded|R value is negative|S value is negative|Invalid hashType/))) + if (!(e.message.match(/Not a DER sequence|Invalid sequence length|Expected a DER integer|R length is zero|S length is zero|R value excessively padded|S value excessively padded|R value is negative|S value is negative|Invalid hashType/))) { throw e + } return false } diff --git a/src/transaction.js b/src/transaction.js index 9cdec45..c356bed 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -139,11 +139,11 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) { // Add the input and return the input's index return (this.ins.push({ - hash: hash, - index: index, - script: script, - sequence: sequence - }) - 1) + hash: hash, + index: index, + script: script, + sequence: sequence + }) - 1) } /** @@ -171,9 +171,9 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) { // Add the output and return the output's index return (this.outs.push({ - script: scriptPubKey, - value: value - }) - 1) + script: scriptPubKey, + value: value + }) - 1) } Transaction.prototype.clone = function () { @@ -271,15 +271,11 @@ Transaction.prototype.toBuffer = function () { } var buffer = new Buffer( - 8 + + 8 + bufferutils.varIntSize(this.ins.length) + bufferutils.varIntSize(this.outs.length) + - this.ins.reduce(function (sum, input) { - return sum + 40 + scriptSize(input.script) - }, 0) + - this.outs.reduce(function (sum, output) { - return sum + 8 + scriptSize(output.script) - }, 0) + this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) + + this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0) ) var offset = 0 diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 9d1fff6..8a58e9a 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -146,13 +146,15 @@ TransactionBuilder.prototype.addInput = function (prevTx, index, sequence, prevO // if we can, extract pubKey information switch (prevOutType) { - case 'multisig': + case 'multisig': { input.pubKeys = prevOutScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) - break + break + } - case 'pubkey': + case 'pubkey': { input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) - break + break + } } if (prevOutType !== 'scripthash') { @@ -223,12 +225,13 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) { if (input.signatures) { switch (scriptType) { - case 'pubkeyhash': + case 'pubkeyhash': { var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) - break + break + } - case 'multisig': + case 'multisig': { // Array.prototype.map is sparse-compatible var msSignatures = input.signatures.map(function (signature) { return signature.toScriptSignature(input.hashType) @@ -243,12 +246,14 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) { var redeemScript = allowIncomplete ? undefined : input.redeemScript scriptSig = scripts.multisigInput(msSignatures, redeemScript) - break + break + } - case 'pubkey': + case 'pubkey': { var pkSignature = input.signatures[0].toScriptSignature(input.hashType) scriptSig = scripts.pubKeyInput(pkSignature) - break + break + } } } @@ -304,21 +309,24 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash var pubKeys = [] switch (scriptType) { - case 'multisig': + case 'multisig': { pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) - break + break + } - case 'pubkeyhash': + case 'pubkeyhash': { var pkh1 = redeemScript.chunks[2] var pkh2 = privKey.pub.getAddress().hash assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input') pubKeys = [privKey.pub] - break + break + } - case 'pubkey': + case 'pubkey': { pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) - break + break + } } if (!input.prevOutScript) { diff --git a/test/ecdsa.js b/test/ecdsa.js index 173578f..3e2db40 100644 --- a/test/ecdsa.js +++ b/test/ecdsa.js @@ -175,10 +175,7 @@ describe('ecdsa', function () { var d = BigInteger.fromHex(f.d) var H = crypto.sha256(f.message) var e = BigInteger.fromBuffer(H) - var signature = new ECSignature( - new BigInteger(f.signature.r), - new BigInteger(f.signature.s) - ) + var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s)) var Q = curve.G.multiply(d) assert(ecdsa.verify(curve, H, signature, Q)) @@ -191,10 +188,7 @@ describe('ecdsa', function () { var H = crypto.sha256(f.message) var e = BigInteger.fromBuffer(H) var d = BigInteger.fromHex(f.d) - var signature = new ECSignature( - new BigInteger(f.signature.r), - new BigInteger(f.signature.s) - ) + var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s)) var Q = curve.G.multiply(d) assert.equal(ecdsa.verify(curve, H, signature, Q), false) diff --git a/test/ecsignature.js b/test/ecsignature.js index fa4b932..908876f 100644 --- a/test/ecsignature.js +++ b/test/ecsignature.js @@ -11,10 +11,7 @@ describe('ECSignature', function () { describe('toCompact', function () { fixtures.valid.forEach(function (f) { it('exports ' + f.compact.hex + ' correctly', function () { - var signature = new ECSignature( - new BigInteger(f.signature.r), - new BigInteger(f.signature.s) - ) + var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s)) var buffer = signature.toCompact(f.compact.i, f.compact.compressed) assert.equal(buffer.toString('hex'), f.compact.hex) @@ -49,10 +46,7 @@ describe('ECSignature', function () { describe('toDER', function () { fixtures.valid.forEach(function (f) { it('exports ' + f.DER + ' correctly', function () { - var signature = new ECSignature( - new BigInteger(f.signature.r), - new BigInteger(f.signature.s) - ) + var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s)) var DER = signature.toDER() assert.equal(DER.toString('hex'), f.DER) @@ -85,10 +79,7 @@ describe('ECSignature', function () { describe('toScriptSignature', function () { fixtures.valid.forEach(function (f) { it('exports ' + f.scriptSignature.hex + ' correctly', function () { - var signature = new ECSignature( - new BigInteger(f.signature.r), - new BigInteger(f.signature.s) - ) + var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s)) var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType) assert.equal(scriptSignature.toString('hex'), f.scriptSignature.hex) @@ -97,10 +88,7 @@ describe('ECSignature', function () { fixtures.invalid.scriptSignature.forEach(function (f) { it('throws ' + f.exception, function () { - var signature = new ECSignature( - new BigInteger(f.signature.r), - new BigInteger(f.signature.s) - ) + var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s)) assert.throws(function () { signature.toScriptSignature(f.hashType) diff --git a/test/integration/advanced.js b/test/integration/advanced.js index 594f8e8..07596b1 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -48,7 +48,7 @@ describe('bitcoinjs-lib (advanced)', function () { blockchain.transactions.propagate(txBuilt.toHex(), function (err) { if (err) return done(err) - // check that the message was propagated + // check that the message was propagated blockchain.transactions.get(txBuilt.getId(), function (err, transaction) { if (err) return done(err) diff --git a/test/integration/multisig.js b/test/integration/multisig.js index a4c79f9..fabaad9 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -38,11 +38,11 @@ describe('bitcoinjs-lib (multisig)', function () { blockchain.addresses.__faucetWithdraw(address, 2e4, function (err) { if (err) return done(err) - // get latest unspents from the address + // get latest unspents from the address blockchain.addresses.unspents(address, function (err, unspents) { if (err) return done(err) - // filter small unspents + // filter small unspents unspents = unspents.filter(function (unspent) { return unspent.value > 1e4 }) @@ -66,7 +66,7 @@ describe('bitcoinjs-lib (multisig)', function () { blockchain.transactions.propagate(txb.build().toHex(), function (err) { if (err) return done(err) - // check that the funds (1e4 Satoshis) indeed arrived at the intended address + // check that the funds (1e4 Satoshis) indeed arrived at the intended address blockchain.addresses.summary(targetAddress, function (err, result) { if (err) return done(err) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 0014829..11172e5 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -43,10 +43,13 @@ function construct (txb, f, sign) { } // FIXME: add support for locktime/version in TransactionBuilder API - if (f.version !== undefined) + if (f.version !== undefined) { txb.tx.version = f.version - if (f.locktime !== undefined) + } + + if (f.locktime !== undefined) { txb.tx.locktime = f.locktime + } } describe('TransactionBuilder', function () {