various: more standard-format artifact fixes

This commit is contained in:
Daniel Cousens 2015-03-02 13:31:03 +11:00
parent 8aa4f9ecc9
commit 0bba21546f
11 changed files with 58 additions and 66 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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
}

View file

@ -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

View file

@ -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) {

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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 () {