@@ -118,7 +118,23 @@ object Transactions {
118118 }
119119 /** Sighash flags to use when signing the transaction. */
120120 def sighash (txOwner : TxOwner , commitmentFormat : CommitmentFormat ): Int = SIGHASH_ALL
121+
122+ def sign (key : PrivateKey , txOwner : TxOwner , commitmentFormat : CommitmentFormat ): ByteVector64 = Transactions .sign(this , key, sighash(txOwner, commitmentFormat))
123+
124+ def sign (key : PrivateKey , sighashType : Int ): ByteVector64 = {
125+ // NB: the tx may have multiple inputs, we will only sign the one provided in txinfo.input. Bear in mind that the
126+ // signature will be invalidated if other inputs are added *afterwards* and sighashType was SIGHASH_ALL.
127+ val inputIndex = tx.txIn.zipWithIndex.find(_._1.outPoint == input.outPoint).get._2
128+ Transactions .sign(tx, input.redeemScript, input.txOut.amount, key, sighashType, inputIndex)
129+ }
130+
131+ def checkSig (sig : ByteVector64 , pubKey : PublicKey , txOwner : TxOwner , commitmentFormat : CommitmentFormat ): Boolean = {
132+ val sighash = this .sighash(txOwner, commitmentFormat)
133+ val data = Transaction .hashForSigning(tx, inputIndex = 0 , input.redeemScript, sighash, input.txOut.amount, SIGVERSION_WITNESS_V0 )
134+ Crypto .verifySignature(data, sig, pubKey)
135+ }
121136 }
137+
122138 sealed trait ReplaceableTransactionWithInputInfo extends TransactionWithInputInfo {
123139 /** Block before which the transaction must be confirmed. */
124140 def confirmationTarget : ConfirmationTarget
@@ -852,15 +868,13 @@ object Transactions {
852868 sig64
853869 }
854870
855- def sign (txinfo : TransactionWithInputInfo , key : PrivateKey , sighashType : Int ): ByteVector64 = {
871+ private def sign (txinfo : TransactionWithInputInfo , key : PrivateKey , sighashType : Int ): ByteVector64 = {
856872 // NB: the tx may have multiple inputs, we will only sign the one provided in txinfo.input. Bear in mind that the
857873 // signature will be invalidated if other inputs are added *afterwards* and sighashType was SIGHASH_ALL.
858874 val inputIndex = txinfo.tx.txIn.zipWithIndex.find(_._1.outPoint == txinfo.input.outPoint).get._2
859875 sign(txinfo.tx, txinfo.input.redeemScript, txinfo.input.txOut.amount, key, sighashType, inputIndex)
860876 }
861877
862- def sign (txinfo : TransactionWithInputInfo , key : PrivateKey , txOwner : TxOwner , commitmentFormat : CommitmentFormat ): ByteVector64 = sign(txinfo, key, txinfo.sighash(txOwner, commitmentFormat))
863-
864878 def addSigs (commitTx : CommitTx , localFundingPubkey : PublicKey , remoteFundingPubkey : PublicKey , localSig : ByteVector64 , remoteSig : ByteVector64 ): CommitTx = {
865879 val witness = Scripts .witness2of2(localSig, remoteSig, localFundingPubkey, remoteFundingPubkey)
866880 commitTx.copy(tx = commitTx.tx.updateWitness(0 , witness))
@@ -935,11 +949,4 @@ object Transactions {
935949 // NB: we don't verify the other inputs as they should only be wallet inputs used to RBF the transaction
936950 Try (Transaction .correctlySpends(txinfo.tx, Map (txinfo.input.outPoint -> txinfo.input.txOut), ScriptFlags .STANDARD_SCRIPT_VERIFY_FLAGS ))
937951 }
938-
939- def checkSig (txinfo : TransactionWithInputInfo , sig : ByteVector64 , pubKey : PublicKey , txOwner : TxOwner , commitmentFormat : CommitmentFormat ): Boolean = {
940- val sighash = txinfo.sighash(txOwner, commitmentFormat)
941- val data = Transaction .hashForSigning(txinfo.tx, inputIndex = 0 , txinfo.input.redeemScript, sighash, txinfo.input.txOut.amount, SIGVERSION_WITNESS_V0 )
942- Crypto .verifySignature(data, sig, pubKey)
943- }
944-
945952}
0 commit comments