Browse Source

various: more standard-format artifact fixes

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
0bba21546f
  1. 4
      src/ecsignature.js
  2. 3
      src/scripts.js
  3. 8
      src/transaction.js
  4. 24
      src/transaction_builder.js
  5. 10
      test/ecdsa.js
  6. 20
      test/ecsignature.js
  7. 7
      test/transaction_builder.js

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)

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

8
src/transaction.js

@ -274,12 +274,8 @@ Transaction.prototype.toBuffer = function () {
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

24
src/transaction_builder.js

@ -146,14 +146,16 @@ 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') {
input.scriptType = prevOutType input.scriptType = prevOutType
@ -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)
@ -244,13 +247,15 @@ 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
} }
} }
}
// did we build a scriptSig? // did we build a scriptSig?
if (scriptSig) { if (scriptSig) {
@ -304,22 +309,25 @@ 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) {
input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())

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)

7
test/transaction_builder.js

@ -43,11 +43,14 @@ 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 () {
var privAddress, privScript var privAddress, privScript

Loading…
Cancel
Save