refactor: Use ECDigitalSignature.appendHashType() throughout codebase (#5761)

This commit is contained in:
Chris Stewart 2024-11-09 09:15:37 -06:00 committed by GitHub
parent 39e23c2a09
commit d255914765
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 18 deletions

View File

@ -80,8 +80,7 @@ sealed abstract class TransactionSignatureCreator {
taprootOptions = TaprootSerializationOptions.empty) taprootOptions = TaprootSerializationOptions.empty)
val signature = sign(hash.bytes) val signature = sign(hash.bytes)
// append 1 byte hash type onto the end // append 1 byte hash type onto the end
val sig = signature.map(s => val sig = signature.map(_.appendHashType(hashType))
ECDigitalSignature(s.bytes ++ ByteVector.fromByte(hashType.byte)))
sig.map { s => sig.map { s =>
require( require(
s.isStrictEncoded, s.isStrictEncoded,

View File

@ -134,15 +134,14 @@ trait Sign extends AsyncSign {
bytes: ByteVector, bytes: ByteVector,
hashType: HashType): ECDigitalSignature = { hashType: HashType): ECDigitalSignature = {
val sigNoHashType = sign(bytes) val sigNoHashType = sign(bytes)
sigNoHashType.copy(bytes = sigNoHashType.appendHashType(hashType)
sigNoHashType.bytes ++ ByteVector.fromByte(hashType.byte))
} }
def signLowRWithHashType( def signLowRWithHashType(
bytes: ByteVector, bytes: ByteVector,
hashType: HashType): ECDigitalSignature = { hashType: HashType): ECDigitalSignature = {
val lowR = signLowR(bytes) val lowR = signLowR(bytes)
lowR.copy(lowR.bytes ++ ByteVector.fromByte(hashType.byte)) lowR.appendHashType(hashType)
} }
override def asyncSign(bytes: ByteVector): Future[ECDigitalSignature] = { override def asyncSign(bytes: ByteVector): Future[ECDigitalSignature] = {
Future.successful(sign(bytes)) Future.successful(sign(bytes))

View File

@ -25,7 +25,6 @@ import org.bitcoins.crypto.{
HashType HashType
} }
import org.scalacheck.Gen import org.scalacheck.Gen
import scodec.bits.ByteVector
import scala.annotation.tailrec import scala.annotation.tailrec
import scala.concurrent.duration.DurationInt import scala.concurrent.duration.DurationInt
@ -50,9 +49,7 @@ sealed abstract class ScriptGenerators {
for { for {
sig <- CryptoGenerators.digitalSignature sig <- CryptoGenerators.digitalSignature
hashType <- CryptoGenerators.hashType hashType <- CryptoGenerators.hashType
digitalSignature = ECDigitalSignature( digitalSignature = sig.appendHashType(hashType)
sig.bytes ++ ByteVector.fromByte(hashType.byte)
)
} yield P2PKScriptSignature(digitalSignature) } yield P2PKScriptSignature(digitalSignature)
def p2pkhScriptSignature: Gen[P2PKHScriptSignature] = def p2pkhScriptSignature: Gen[P2PKHScriptSignature] =
@ -60,9 +57,8 @@ sealed abstract class ScriptGenerators {
privKey <- CryptoGenerators.privateKey privKey <- CryptoGenerators.privateKey
hash <- CryptoGenerators.doubleSha256Digest hash <- CryptoGenerators.doubleSha256Digest
hashType <- CryptoGenerators.hashType hashType <- CryptoGenerators.hashType
signature = ECDigitalSignature.fromBytes( signature = privKey.sign(hash).appendHashType(hashType)
privKey.sign(hash).bytes ++ ByteVector.fromByte(hashType.byte)
)
} yield P2PKHScriptSignature(signature, privKey.publicKey) } yield P2PKHScriptSignature(signature, privKey.publicKey)
def p2pkWithTimeoutScriptSignature: Gen[ConditionalScriptSignature] = def p2pkWithTimeoutScriptSignature: Gen[ConditionalScriptSignature] =
@ -70,9 +66,7 @@ sealed abstract class ScriptGenerators {
privKey <- CryptoGenerators.privateKey privKey <- CryptoGenerators.privateKey
hash <- CryptoGenerators.doubleSha256Digest hash <- CryptoGenerators.doubleSha256Digest
hashType <- CryptoGenerators.hashType hashType <- CryptoGenerators.hashType
signature = ECDigitalSignature.fromBytes( signature = privKey.sign(hash).appendHashType(hashType)
privKey.sign(hash).bytes ++ ByteVector.fromByte(hashType.byte)
)
beforeTimeout <- NumberGenerator.bool beforeTimeout <- NumberGenerator.bool
} yield P2PKWithTimeoutScriptSignature(beforeTimeout, signature) } yield P2PKWithTimeoutScriptSignature(beforeTimeout, signature)
@ -84,9 +78,7 @@ sealed abstract class ScriptGenerators {
privKeys <- CryptoGenerators.privateKeySeq(numKeys) privKeys <- CryptoGenerators.privateKeySeq(numKeys)
} yield for { } yield for {
privKey <- privKeys privKey <- privKeys
} yield ECDigitalSignature.fromBytes( } yield privKey.sign(hash).appendHashType(hashType)
privKey.sign(hash).bytes ++ ByteVector.fromByte(hashType.byte)
)
signatures.map(sigs => MultiSignatureScriptSignature(sigs)) signatures.map(sigs => MultiSignatureScriptSignature(sigs))
} }