Skip to content

Commit 337e6a7

Browse files
authoredOct 30, 2017
Merge pull request bitcoinjs#915 from bitcoinjs/bip32400
BIP32/secp256k1/keyPair preparation
2 parents a301aa8 + a868d27 commit 337e6a7

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed
 

‎src/ecsignature.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,30 @@ function ECSignature (r, s) {
1212
}
1313

1414
ECSignature.parseCompact = function (buffer) {
15-
if (buffer.length !== 65) throw new Error('Invalid signature length')
15+
typeforce(types.BufferN(65), buffer)
1616

1717
var flagByte = buffer.readUInt8(0) - 27
1818
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
1919

2020
var compressed = !!(flagByte & 4)
2121
var recoveryParam = flagByte & 3
22-
23-
var r = BigInteger.fromBuffer(buffer.slice(1, 33))
24-
var s = BigInteger.fromBuffer(buffer.slice(33))
22+
var signature = ECSignature.fromRSBuffer(buffer.slice(1))
2523

2624
return {
2725
compressed: compressed,
2826
i: recoveryParam,
29-
signature: new ECSignature(r, s)
27+
signature: signature
3028
}
3129
}
3230

31+
ECSignature.fromRSBuffer = function (buffer) {
32+
typeforce(types.BufferN(64), buffer)
33+
34+
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
35+
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
36+
return new ECSignature(r, s)
37+
}
38+
3339
ECSignature.fromDER = function (buffer) {
3440
var decode = bip66.decode(buffer)
3541
var r = BigInteger.fromDERInteger(decode.r)
@@ -60,9 +66,7 @@ ECSignature.prototype.toCompact = function (i, compressed) {
6066

6167
var buffer = Buffer.alloc(65)
6268
buffer.writeUInt8(i, 0)
63-
this.r.toBuffer(32).copy(buffer, 1)
64-
this.s.toBuffer(32).copy(buffer, 33)
65-
69+
this.toRSBuffer(buffer, 1)
6670
return buffer
6771
}
6872

@@ -73,6 +77,13 @@ ECSignature.prototype.toDER = function () {
7377
return bip66.encode(r, s)
7478
}
7579

80+
ECSignature.prototype.toRSBuffer = function (buffer, offset) {
81+
buffer = buffer || Buffer.alloc(64)
82+
this.r.toBuffer(32).copy(buffer, offset)
83+
this.s.toBuffer(32).copy(buffer, offset + 32)
84+
return buffer
85+
}
86+
7687
ECSignature.prototype.toScriptSignature = function (hashType) {
7788
var hashTypeMod = hashType & ~0x80
7889
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)

‎src/transaction_builder.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,8 @@ function canSign (input) {
667667
}
668668

669669
TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashType, witnessValue, witnessScript) {
670-
if (keyPair.network !== this.network) throw new Error('Inconsistent network')
670+
// TODO: remove keyPair.network matching in 4.0.0
671+
if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network')
671672
if (!this.inputs[vin]) throw new Error('No input at index: ' + vin)
672673
hashType = hashType || Transaction.SIGHASH_ALL
673674

@@ -680,7 +681,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
680681
throw new Error('Inconsistent redeemScript')
681682
}
682683

683-
var kpPubKey = keyPair.getPublicKeyBuffer()
684+
var kpPubKey = keyPair.publicKey || keyPair.getPublicKeyBuffer()
684685
if (!canSign(input)) {
685686
if (witnessValue !== undefined) {
686687
if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue')
@@ -699,14 +700,18 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
699700
} else {
700701
signatureHash = this.tx.hashForSignature(vin, input.signScript, hashType)
701702
}
703+
702704
// enforce in order signing of public keys
703705
var signed = input.pubKeys.some(function (pubKey, i) {
704706
if (!kpPubKey.equals(pubKey)) return false
705707
if (input.signatures[i]) throw new Error('Signature already exists')
706-
if (!keyPair.compressed &&
708+
if (kpPubKey.length !== 33 &&
707709
input.signType === scriptTypes.P2WPKH) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
708710

709-
input.signatures[i] = keyPair.sign(signatureHash).toScriptSignature(hashType)
711+
var signature = keyPair.sign(signatureHash)
712+
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)
713+
714+
input.signatures[i] = signature.toScriptSignature(hashType)
710715
return true
711716
})
712717

‎test/fixtures/ecsignature.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@
120120
"hex": "23987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62"
121121
},
122122
{
123-
"exception": "Invalid signature length",
123+
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 68\\)",
124124
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e93791d7629795b62000000"
125125
},
126126
{
127-
"exception": "Invalid signature length",
127+
"exception": "Expected Buffer\\(Length: 65\\), got Buffer\\(Length: 59\\)",
128128
"hex": "1c987ceade6a304fc5823ab38f99fc3c5f772a2d3e89ea05931e2726105fc53b9e601fc3231f35962c714fcbce5c95b427496edc7ae8b3d12e9379"
129129
}
130130
],

‎test/transaction_builder.js

+13
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,19 @@ describe('TransactionBuilder', function () {
294294
})
295295

296296
describe('sign', function () {
297+
it('supports the alternative abstract interface { publicKey, sign }', function () {
298+
var keyPair = {
299+
publicKey: Buffer.alloc(33, 0x03),
300+
sign: function (hash) { return Buffer.alloc(64) }
301+
}
302+
303+
var txb = new TransactionBuilder()
304+
txb.addInput('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 1)
305+
txb.addOutput('1111111111111111111114oLvT2', 100000)
306+
txb.sign(0, keyPair)
307+
assert.equal(txb.build().toHex(), '0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000002c0930060201000201000121030303030303030303030303030303030303030303030303030303030303030303ffffffff01a0860100000000001976a914000000000000000000000000000000000000000088ac00000000')
308+
})
309+
297310
fixtures.invalid.sign.forEach(function (f) {
298311
it('throws on ' + f.exception + (f.description ? ' (' + f.description + ')' : ''), function () {
299312
var txb = construct(f, true)

0 commit comments

Comments
 (0)
Please sign in to comment.