@exodus/bitcoinjs-lib-forks 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/crypto.js +1 -1
- package/src/transaction_builder.js +29 -12
- package/CHANGELOG.md +0 -7
package/package.json
CHANGED
package/src/crypto.js
CHANGED
|
@@ -708,22 +708,22 @@ function canSign (input) {
|
|
|
708
708
|
)
|
|
709
709
|
}
|
|
710
710
|
|
|
711
|
-
TransactionBuilder.prototype.
|
|
711
|
+
TransactionBuilder.prototype._prepareForSigning = function (vin, keyPair, redeemScript, hashType, witnessValue, witnessScript) {
|
|
712
712
|
// TODO: remove keyPair.network matching in 4.0.0
|
|
713
713
|
if (keyPair.network && keyPair.network !== this.network) throw new TypeError('Inconsistent network')
|
|
714
714
|
if (!this.inputs[vin]) throw new Error('No input at index: ' + vin)
|
|
715
715
|
hashType = hashType || Transaction.SIGHASH_ALL
|
|
716
716
|
|
|
717
|
-
|
|
717
|
+
let input = this.inputs[vin]
|
|
718
718
|
|
|
719
719
|
// if redeemScript was previously provided, enforce consistency
|
|
720
720
|
if (input.redeemScript !== undefined &&
|
|
721
|
-
|
|
722
|
-
|
|
721
|
+
redeemScript &&
|
|
722
|
+
!input.redeemScript.equals(redeemScript)) {
|
|
723
723
|
throw new Error('Inconsistent redeemScript')
|
|
724
724
|
}
|
|
725
725
|
|
|
726
|
-
|
|
726
|
+
let kpPubKey = keyPair.publicKey || keyPair.getPublicKeyBuffer()
|
|
727
727
|
if (!canSign(input)) {
|
|
728
728
|
if (witnessValue !== undefined) {
|
|
729
729
|
if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue')
|
|
@@ -736,7 +736,7 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|
|
736
736
|
}
|
|
737
737
|
|
|
738
738
|
// ready to sign
|
|
739
|
-
|
|
739
|
+
let signatureHash
|
|
740
740
|
if (this.bitcoinGold) {
|
|
741
741
|
signatureHash = this.tx.hashForGoldSignature(vin, input.signScript, witnessValue, hashType, input.witness)
|
|
742
742
|
} else if (this.bitcoinCash) {
|
|
@@ -750,20 +750,37 @@ TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashTy
|
|
|
750
750
|
}
|
|
751
751
|
|
|
752
752
|
// enforce in order signing of public keys
|
|
753
|
-
|
|
753
|
+
const index = input.pubKeys.findIndex(function (pubKey, i) {
|
|
754
754
|
if (!kpPubKey.equals(pubKey)) return false
|
|
755
755
|
if (input.signatures[i]) throw new Error('Signature already exists')
|
|
756
756
|
if (kpPubKey.length !== 33 &&
|
|
757
757
|
input.signType === scriptTypes.P2WPKH) throw new Error('BIP143 rejects uncompressed public keys in P2WPKH or P2WSH')
|
|
758
758
|
|
|
759
|
-
var signature = keyPair.sign(signatureHash)
|
|
760
|
-
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)
|
|
761
|
-
|
|
762
|
-
input.signatures[i] = signature.toScriptSignature(hashType)
|
|
763
759
|
return true
|
|
764
760
|
})
|
|
765
761
|
|
|
766
|
-
if (
|
|
762
|
+
if (index === -1) throw new Error('Key pair cannot sign for this input')
|
|
763
|
+
|
|
764
|
+
return { input, index, signatureHash, hashType }
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
TransactionBuilder.prototype.signAsync = async function (vin, signer, redeemScript, suppliedHashType, witnessValue, witnessScript) {
|
|
768
|
+
if (!signer || !signer.publicKey || !signer.sign) throw new TypeError('signer must have sign and publicKey props')
|
|
769
|
+
const { input, index, signatureHash, hashType } = this._prepareForSigning(vin, signer, redeemScript, suppliedHashType, witnessValue, witnessScript)
|
|
770
|
+
|
|
771
|
+
let signature = await signer.sign(signatureHash)
|
|
772
|
+
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)
|
|
773
|
+
|
|
774
|
+
input.signatures[index] = signature.toScriptSignature(hashType)
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, suppliedHashType, witnessValue, witnessScript) {
|
|
778
|
+
const { input, index, signatureHash, hashType } = this._prepareForSigning(vin, keyPair, redeemScript, suppliedHashType, witnessValue, witnessScript)
|
|
779
|
+
|
|
780
|
+
let signature = keyPair.sign(signatureHash)
|
|
781
|
+
if (Buffer.isBuffer(signature)) signature = ECSignature.fromRSBuffer(signature)
|
|
782
|
+
|
|
783
|
+
input.signatures[index] = signature.toScriptSignature(hashType)
|
|
767
784
|
}
|
|
768
785
|
|
|
769
786
|
function signatureHashType (buffer) {
|