Skip to content

Commit a868d27

Browse files
committed
TxBuilder: add support for RSBuffer type keyPairs and .publicKey
1 parent b94da10 commit a868d27

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/transaction_builder.js

Lines changed: 9 additions & 4 deletions
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/transaction_builder.js

Lines changed: 13 additions & 0 deletions
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)