@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bitcoinjs-lib-forks",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Client-side Bitcoin Forks JavaScript library",
5
5
  "main": "./src/index.js",
6
6
  "engines": {
package/src/crypto.js CHANGED
@@ -1,7 +1,7 @@
1
1
  var createHash = require('create-hash')
2
2
 
3
3
  function ripemd160 (buffer) {
4
- return createHash('rmd160').update(buffer).digest()
4
+ return createHash('ripemd160').update(buffer).digest()
5
5
  }
6
6
 
7
7
  function sha1 (buffer) {
@@ -708,22 +708,22 @@ function canSign (input) {
708
708
  )
709
709
  }
710
710
 
711
- TransactionBuilder.prototype.sign = function (vin, keyPair, redeemScript, hashType, witnessValue, witnessScript) {
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
- var input = this.inputs[vin]
717
+ let input = this.inputs[vin]
718
718
 
719
719
  // if redeemScript was previously provided, enforce consistency
720
720
  if (input.redeemScript !== undefined &&
721
- redeemScript &&
722
- !input.redeemScript.equals(redeemScript)) {
721
+ redeemScript &&
722
+ !input.redeemScript.equals(redeemScript)) {
723
723
  throw new Error('Inconsistent redeemScript')
724
724
  }
725
725
 
726
- var kpPubKey = keyPair.publicKey || keyPair.getPublicKeyBuffer()
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
- var signatureHash
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
- var signed = input.pubKeys.some(function (pubKey, i) {
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 (!signed) throw new Error('Key pair cannot sign for this input')
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) {
package/CHANGELOG.md DELETED
@@ -1,7 +0,0 @@
1
- # 0.0.2 / 2018-12-12
2
-
3
- - Remove unneeded BCH fork changes
4
-
5
- # 0.0.1 / 2018-11-14
6
-
7
- Initial release, with 2018-11-15 BCH (BitcoinABC) HF support.