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) {
if (compressed)
if (compressed) {
i += 4
}
i += 27
var buffer = new Buffer(65)

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

8
src/transaction.js

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

24
src/transaction_builder.js

@ -146,14 +146,16 @@ 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
}
case 'pubkey':
case 'pubkey': {
input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
break
}
}
if (prevOutType !== 'scripthash') {
input.scriptType = prevOutType
@ -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
}
case 'multisig':
case 'multisig': {
// Array.prototype.map is sparse-compatible
var msSignatures = input.signatures.map(function (signature) {
return signature.toScriptSignature(input.hashType)
@ -244,13 +247,15 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = scripts.multisigInput(msSignatures, redeemScript)
break
}
case 'pubkey':
case 'pubkey': {
var pkSignature = input.signatures[0].toScriptSignature(input.hashType)
scriptSig = scripts.pubKeyInput(pkSignature)
break
}
}
}
// did we build a scriptSig?
if (scriptSig) {
@ -304,22 +309,25 @@ 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
}
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
}
case 'pubkey':
case 'pubkey': {
pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer)
break
}
}
if (!input.prevOutScript) {
input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash())

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

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

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

Loading…
Cancel
Save