Skip to content

Commit

Permalink
fix signature verification
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jun 22, 2024
1 parent 0f3e6e6 commit edf298b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
11 changes: 7 additions & 4 deletions x/btcbridge/types/bitcoin_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const (

// default minimum relay fee
MinRelayFee = 1000

// default hash type for signature
SigHashType = txscript.SigHashAll
)

// BuildPsbt builds a bitcoin psbt from the given params.
Expand Down Expand Up @@ -161,17 +164,17 @@ func GetTxVirtualSize(tx *wire.MsgTx, utxos []*UTXO) int64 {

switch txscript.GetScriptClass(utxos[i].PubKeyScript) {
case txscript.WitnessV1TaprootTy:
dummyWitness = make([]byte, 64)
dummyWitness = make([]byte, 65)

case txscript.WitnessV0PubKeyHashTy:
dummyWitness = make([]byte, 72+33)
dummyWitness = make([]byte, 73+33)

case txscript.ScriptHashTy:
dummySigScript = make([]byte, 1+1+1+20)
dummyWitness = make([]byte, 72+33)
dummyWitness = make([]byte, 73+33)

case txscript.PubKeyHashTy:
dummySigScript = make([]byte, 1+72+1+33)
dummySigScript = make([]byte, 1+73+1+33)

default:
}
Expand Down
27 changes: 16 additions & 11 deletions x/btcbridge/types/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import (
)

// VerifyPsbtSignatures verifies the signatures of the given psbt
// Note: assume that the psbt is valid and all inputs are native segwit
// Note: assume that the psbt is finalized and all inputs are native segwit
func VerifyPsbtSignatures(p *psbt.Packet) bool {
// extract signed tx
signedTx, err := psbt.Extract(p)
if err != nil {
return false
}

// build previous output fetcher
prevOutputFetcher := txscript.NewMultiPrevOutFetcher(nil)

Expand All @@ -25,32 +31,31 @@ func VerifyPsbtSignatures(p *psbt.Packet) bool {
// verify signatures
for i := range p.Inputs {
output := p.Inputs[i].WitnessUtxo
hashType := p.Inputs[i].SighashType

witness := p.Inputs[i].FinalScriptWitness
if len(witness) < 72+33 {
witness := signedTx.TxIn[i].Witness
if len(witness) != 2 {
return false
}

sigBytes := witness[0 : len(witness)-33]
pkBytes := witness[len(witness)-33:]
sigBytes := witness[0]
pkBytes := witness[1]

if sigBytes[len(sigBytes)-1] != byte(hashType) {
sig, err := ecdsa.ParseDERSignature(sigBytes)
if err != nil {
return false
}

sig, err := ecdsa.ParseDERSignature(sigBytes[0 : len(sigBytes)-1])
pk, err := secp256k1.ParsePubKey(pkBytes)
if err != nil {
return false
}

pk, err := secp256k1.ParsePubKey(pkBytes)
if err != nil {
if sigBytes[len(sigBytes)-1] != byte(SigHashType) {
return false
}

sigHash, err := txscript.CalcWitnessSigHash(output.PkScript, txscript.NewTxSigHashes(p.UnsignedTx, prevOutputFetcher),
hashType, p.UnsignedTx, i, output.Value)
SigHashType, p.UnsignedTx, i, output.Value)
if err != nil {
return false
}
Expand Down

0 comments on commit edf298b

Please sign in to comment.