|
|
@ -667,7 +667,8 @@ function canSign (input) { |
|
|
|
} |
|
|
|
|
|
|
|
TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashType, witnessValue, witnessScript) { |
|
|
|
if (keyPair.network !== this.network) throw new Error('Inconsistent network') |
|
|
|
// TODO: remove keyPair.network matching in 4.0.0
|
|
|
|
if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network') |
|
|
|
if (!this.inputs[vin]) throw new Error('No input at index: ' + vin) |
|
|
|
hashType = hashType || Transaction.SIGHASH_ALL |
|
|
|
|
|
|
@ -680,7 +681,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy |
|
|
|
throw new Error('Inconsistent redeemScript') |
|
|
|
} |
|
|
|
|
|
|
|
var kpPubKey = keyPair.getPublicKeyBuffer() |
|
|
|
var kpPubKey = keyPair.publicKey || keyPair.getPublicKeyBuffer() |
|
|
|
if (!canSign(input)) { |
|
|
|
if (witnessValue !== undefined) { |
|
|
|
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 |
|
|
|
} else { |
|
|
|
signatureHash = this.tx.hashForSignature(vin, input.signScript, hashType) |
|
|
|
} |
|
|
|
|
|
|
|
// enforce in order signing of public keys
|
|
|
|
var signed = input.pubKeys.some(function (pubKey, i) { |
|
|
|
if (!kpPubKey.equals(pubKey)) return false |
|
|
|
if (input.signatures[i]) throw new Error('Signature already exists') |
|
|
|
if (!keyPair.compressed && |
|
|
|
if (kpPubKey.length !== 33 && |
|
|
|
input.signType === scriptTypes.P2WPKH) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH') |
|
|
|
|
|
|
|
input.signatures[i] = keyPair.sign(signatureHash).toScriptSignature(hashType) |
|
|
|
var signature = keyPair.sign(signatureHash) |
|
|
|
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature) |
|
|
|
|
|
|
|
input.signatures[i] = signature.toScriptSignature(hashType) |
|
|
|
return true |
|
|
|
}) |
|
|
|
|
|
|
|