@@ -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
@@ -944,15 +960,13 @@ object Transactions {
944960 sig64
945961 }
946962
947- def sign (txinfo : TransactionWithInputInfo , key : PrivateKey , sighashType : Int ): ByteVector64 = {
963+ private def sign (txinfo : TransactionWithInputInfo , key : PrivateKey , sighashType : Int ): ByteVector64 = {
948964 // NB: the tx may have multiple inputs, we will only sign the one provided in txinfo.input. Bear in mind that the
949965 // signature will be invalidated if other inputs are added *afterwards* and sighashType was SIGHASH_ALL.
950966 val inputIndex = txinfo.tx.txIn.zipWithIndex.find(_._1.outPoint == txinfo.input.outPoint).get._2
951967 sign(txinfo.tx, txinfo.input.redeemScript, txinfo.input.txOut.amount, key, sighashType, inputIndex)
952968 }
953969
954- def sign (txinfo : TransactionWithInputInfo , key : PrivateKey , txOwner : TxOwner , commitmentFormat : CommitmentFormat ): ByteVector64 = sign(txinfo, key, txinfo.sighash(txOwner, commitmentFormat))
955-
956970 def addSigs (commitTx : CommitTx , localFundingPubkey : PublicKey , remoteFundingPubkey : PublicKey , localSig : ByteVector64 , remoteSig : ByteVector64 ): CommitTx = {
957971 val witness = Scripts .witness2of2(localSig, remoteSig, localFundingPubkey, remoteFundingPubkey)
958972 commitTx.copy(tx = commitTx.tx.updateWitness(0 , witness))
@@ -1027,11 +1041,4 @@ object Transactions {
10271041 // NB: we don't verify the other inputs as they should only be wallet inputs used to RBF the transaction
10281042 Try (Transaction .correctlySpends(txinfo.tx, Map (txinfo.input.outPoint -> txinfo.input.txOut), ScriptFlags .STANDARD_SCRIPT_VERIFY_FLAGS ))
10291043 }
1030-
1031- def checkSig (txinfo : TransactionWithInputInfo , sig : ByteVector64 , pubKey : PublicKey , txOwner : TxOwner , commitmentFormat : CommitmentFormat ): Boolean = {
1032- val sighash = txinfo.sighash(txOwner, commitmentFormat)
1033- val data = Transaction .hashForSigning(txinfo.tx, inputIndex = 0 , txinfo.input.redeemScript, sighash, txinfo.input.txOut.amount, SIGVERSION_WITNESS_V0 )
1034- Crypto .verifySignature(data, sig, pubKey)
1035- }
1036-
10371044}
0 commit comments