Browse Source

various: more standard-format artifact fixes

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
0bba21546f
  1. 4
      src/bufferutils.js
  2. 4
      src/ecsignature.js
  3. 2
      src/hdnode.js
  4. 3
      src/scripts.js
  5. 26
      src/transaction.js
  6. 40
      src/transaction_builder.js
  7. 10
      test/ecdsa.js
  8. 20
      test/ecsignature.js
  9. 2
      test/integration/advanced.js
  10. 6
      test/integration/multisig.js
  11. 7
      test/transaction_builder.js

4
src/bufferutils.js

@ -20,7 +20,7 @@ function readPushDataInt (buffer, offset) {
var opcode = buffer.readUInt8(offset) var opcode = buffer.readUInt8(offset)
var number, size var number, size
// ~6 bit // ~6 bit
if (opcode < opcodes.OP_PUSHDATA1) { if (opcode < opcodes.OP_PUSHDATA1) {
number = opcode number = opcode
size = 1 size = 1
@ -64,7 +64,7 @@ function readVarInt (buffer, offset) {
var t = buffer.readUInt8(offset) var t = buffer.readUInt8(offset)
var number, size var number, size
// 8 bit // 8 bit
if (t < 253) { if (t < 253) {
number = t number = t
size = 1 size = 1

4
src/ecsignature.js

@ -82,8 +82,10 @@ ECSignature.parseScriptSignature = function (buffer) {
} }
ECSignature.prototype.toCompact = function (i, compressed) { ECSignature.prototype.toCompact = function (i, compressed) {
if (compressed) if (compressed) {
i += 4 i += 4
}
i += 27 i += 27
var buffer = new Buffer(65) var buffer = new Buffer(65)

2
src/hdnode.js

@ -118,7 +118,7 @@ HDNode.fromBuffer = function (buffer, network, __ignoreDeprecation) {
var chainCode = buffer.slice(13, 45) var chainCode = buffer.slice(13, 45)
var data, hd var data, hd
// 33 bytes: private key data (0x00 + k) // 33 bytes: private key data (0x00 + k)
if (version === network.bip32.private) { if (version === network.bip32.private) {
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key') assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
data = buffer.slice(46, 78) data = buffer.slice(46, 78)

3
src/scripts.js

@ -29,8 +29,9 @@ function isCanonicalSignature (buffer) {
try { try {
ECSignature.parseScriptSignature(buffer) ECSignature.parseScriptSignature(buffer)
} catch (e) { } 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 throw e
}
return false return false
} }

26
src/transaction.js

@ -139,11 +139,11 @@ Transaction.prototype.addInput = function (hash, index, sequence, script) {
// Add the input and return the input's index // Add the input and return the input's index
return (this.ins.push({ return (this.ins.push({
hash: hash, hash: hash,
index: index, index: index,
script: script, script: script,
sequence: sequence sequence: sequence
}) - 1) }) - 1)
} }
/** /**
@ -171,9 +171,9 @@ Transaction.prototype.addOutput = function (scriptPubKey, value) {
// Add the output and return the output's index // Add the output and return the output's index
return (this.outs.push({ return (this.outs.push({
script: scriptPubKey, script: scriptPubKey,
value: value value: value
}) - 1) }) - 1)
} }
Transaction.prototype.clone = function () { Transaction.prototype.clone = function () {
@ -271,15 +271,11 @@ Transaction.prototype.toBuffer = function () {
} }
var buffer = new Buffer( var buffer = new Buffer(
8 + 8 +
bufferutils.varIntSize(this.ins.length) + bufferutils.varIntSize(this.ins.length) +
bufferutils.varIntSize(this.outs.length) + bufferutils.varIntSize(this.outs.length) +
this.ins.reduce(function (sum, input) { this.ins.reduce(function (sum, input) { return sum + 40 + scriptSize(input.script) }, 0) +
return sum + 40 + scriptSize(input.script) this.outs.reduce(function (sum, output) { return sum + 8 + scriptSize(output.script) }, 0)
}, 0) +
this.outs.reduce(function (sum, output) {
return sum + 8 + scriptSize(output.script)
}, 0)
) )
var offset = 0 var offset = 0

40
src/transaction_builder.js

@ -146,13 +146,15 @@ TransactionBuilder.prototype.addInput = function (prevTx, index, sequence, prevO
// if we can, extract pubKey information // if we can, extract pubKey information
switch (prevOutType) { switch (prevOutType) {
case 'multisig': case 'multisig': {
input.pubKeys = prevOutScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) 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) input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
break break
}
} }
if (prevOutType !== 'scripthash') { if (prevOutType !== 'scripthash') {
@ -223,12 +225,13 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
if (input.signatures) { if (input.signatures) {
switch (scriptType) { switch (scriptType) {
case 'pubkeyhash': case 'pubkeyhash': {
var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) var pkhSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0])
break break
}
case 'multisig': case 'multisig': {
// Array.prototype.map is sparse-compatible // Array.prototype.map is sparse-compatible
var msSignatures = input.signatures.map(function (signature) { var msSignatures = input.signatures.map(function (signature) {
return signature.toScriptSignature(input.hashType) return signature.toScriptSignature(input.hashType)
@ -243,12 +246,14 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
var redeemScript = allowIncomplete ? undefined : input.redeemScript var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = scripts.multisigInput(msSignatures, redeemScript) scriptSig = scripts.multisigInput(msSignatures, redeemScript)
break break
}
case 'pubkey': case 'pubkey': {
var pkSignature = input.signatures[0].toScriptSignature(input.hashType) var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyInput(pkSignature) scriptSig = scripts.pubKeyInput(pkSignature)
break break
}
} }
} }
@ -304,21 +309,24 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
var pubKeys = [] var pubKeys = []
switch (scriptType) { switch (scriptType) {
case 'multisig': case 'multisig': {
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
break break
}
case 'pubkeyhash': case 'pubkeyhash': {
var pkh1 = redeemScript.chunks[2] var pkh1 = redeemScript.chunks[2]
var pkh2 = privKey.pub.getAddress().hash var pkh2 = privKey.pub.getAddress().hash
assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input') assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input')
pubKeys = [privKey.pub] pubKeys = [privKey.pub]
break break
}
case 'pubkey': case 'pubkey': {
pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
break break
}
} }
if (!input.prevOutScript) { if (!input.prevOutScript) {

10
test/ecdsa.js

@ -175,10 +175,7 @@ describe('ecdsa', function () {
var d = BigInteger.fromHex(f.d) var d = BigInteger.fromHex(f.d)
var H = crypto.sha256(f.message) var H = crypto.sha256(f.message)
var e = BigInteger.fromBuffer(H) var e = BigInteger.fromBuffer(H)
var signature = new ECSignature( var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
new BigInteger(f.signature.r),
new BigInteger(f.signature.s)
)
var Q = curve.G.multiply(d) var Q = curve.G.multiply(d)
assert(ecdsa.verify(curve, H, signature, Q)) assert(ecdsa.verify(curve, H, signature, Q))
@ -191,10 +188,7 @@ describe('ecdsa', function () {
var H = crypto.sha256(f.message) var H = crypto.sha256(f.message)
var e = BigInteger.fromBuffer(H) var e = BigInteger.fromBuffer(H)
var d = BigInteger.fromHex(f.d) var d = BigInteger.fromHex(f.d)
var signature = new ECSignature( var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
new BigInteger(f.signature.r),
new BigInteger(f.signature.s)
)
var Q = curve.G.multiply(d) var Q = curve.G.multiply(d)
assert.equal(ecdsa.verify(curve, H, signature, Q), false) assert.equal(ecdsa.verify(curve, H, signature, Q), false)

20
test/ecsignature.js

@ -11,10 +11,7 @@ describe('ECSignature', function () {
describe('toCompact', function () { describe('toCompact', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('exports ' + f.compact.hex + ' correctly', function () { it('exports ' + f.compact.hex + ' correctly', function () {
var signature = new ECSignature( var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
new BigInteger(f.signature.r),
new BigInteger(f.signature.s)
)
var buffer = signature.toCompact(f.compact.i, f.compact.compressed) var buffer = signature.toCompact(f.compact.i, f.compact.compressed)
assert.equal(buffer.toString('hex'), f.compact.hex) assert.equal(buffer.toString('hex'), f.compact.hex)
@ -49,10 +46,7 @@ describe('ECSignature', function () {
describe('toDER', function () { describe('toDER', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('exports ' + f.DER + ' correctly', function () { it('exports ' + f.DER + ' correctly', function () {
var signature = new ECSignature( var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
new BigInteger(f.signature.r),
new BigInteger(f.signature.s)
)
var DER = signature.toDER() var DER = signature.toDER()
assert.equal(DER.toString('hex'), f.DER) assert.equal(DER.toString('hex'), f.DER)
@ -85,10 +79,7 @@ describe('ECSignature', function () {
describe('toScriptSignature', function () { describe('toScriptSignature', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('exports ' + f.scriptSignature.hex + ' correctly', function () { it('exports ' + f.scriptSignature.hex + ' correctly', function () {
var signature = new ECSignature( var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
new BigInteger(f.signature.r),
new BigInteger(f.signature.s)
)
var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType) var scriptSignature = signature.toScriptSignature(f.scriptSignature.hashType)
assert.equal(scriptSignature.toString('hex'), f.scriptSignature.hex) assert.equal(scriptSignature.toString('hex'), f.scriptSignature.hex)
@ -97,10 +88,7 @@ describe('ECSignature', function () {
fixtures.invalid.scriptSignature.forEach(function (f) { fixtures.invalid.scriptSignature.forEach(function (f) {
it('throws ' + f.exception, function () { it('throws ' + f.exception, function () {
var signature = new ECSignature( var signature = new ECSignature(new BigInteger(f.signature.r), new BigInteger(f.signature.s))
new BigInteger(f.signature.r),
new BigInteger(f.signature.s)
)
assert.throws(function () { assert.throws(function () {
signature.toScriptSignature(f.hashType) signature.toScriptSignature(f.hashType)

2
test/integration/advanced.js

@ -48,7 +48,7 @@ describe('bitcoinjs-lib (advanced)', function () {
blockchain.transactions.propagate(txBuilt.toHex(), function (err) { blockchain.transactions.propagate(txBuilt.toHex(), function (err) {
if (err) return done(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) { blockchain.transactions.get(txBuilt.getId(), function (err, transaction) {
if (err) return done(err) if (err) return done(err)

6
test/integration/multisig.js

@ -38,11 +38,11 @@ describe('bitcoinjs-lib (multisig)', function () {
blockchain.addresses.__faucetWithdraw(address, 2e4, function (err) { blockchain.addresses.__faucetWithdraw(address, 2e4, function (err) {
if (err) return done(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) { blockchain.addresses.unspents(address, function (err, unspents) {
if (err) return done(err) if (err) return done(err)
// filter small unspents // filter small unspents
unspents = unspents.filter(function (unspent) { unspents = unspents.filter(function (unspent) {
return unspent.value > 1e4 return unspent.value > 1e4
}) })
@ -66,7 +66,7 @@ describe('bitcoinjs-lib (multisig)', function () {
blockchain.transactions.propagate(txb.build().toHex(), function (err) { blockchain.transactions.propagate(txb.build().toHex(), function (err) {
if (err) return done(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) { blockchain.addresses.summary(targetAddress, function (err, result) {
if (err) return done(err) if (err) return done(err)

7
test/transaction_builder.js

@ -43,10 +43,13 @@ function construct (txb, f, sign) {
} }
// FIXME: add support for locktime/version in TransactionBuilder API // FIXME: add support for locktime/version in TransactionBuilder API
if (f.version !== undefined) if (f.version !== undefined) {
txb.tx.version = f.version txb.tx.version = f.version
if (f.locktime !== undefined) }
if (f.locktime !== undefined) {
txb.tx.locktime = f.locktime txb.tx.locktime = f.locktime
}
} }
describe('TransactionBuilder', function () { describe('TransactionBuilder', function () {

Loading…
Cancel
Save