|
1 | 1 | package types |
2 | 2 |
|
3 | | -func (m *BitcoinSigningRequest) ValidateSignatures(sigs []string) bool { |
| 3 | +import ( |
| 4 | + secp256k1 "github.com/btcsuite/btcd/btcec/v2" |
| 5 | + "github.com/btcsuite/btcd/btcec/v2/ecdsa" |
| 6 | + "github.com/btcsuite/btcd/btcutil/psbt" |
| 7 | + "github.com/btcsuite/btcd/txscript" |
| 8 | +) |
4 | 9 |
|
5 | | - return false |
| 10 | +// VerifyPsbtSignatures verifies the signatures of the given psbt |
| 11 | +// Note: assume that the psbt is valid and all inputs are native segwit |
| 12 | +func VerifyPsbtSignatures(p *psbt.Packet) bool { |
| 13 | + // build previous output fetcher |
| 14 | + prevOutputFetcher := txscript.NewMultiPrevOutFetcher(nil) |
| 15 | + |
| 16 | + for i, txIn := range p.UnsignedTx.TxIn { |
| 17 | + prevOutput := p.Inputs[i].WitnessUtxo |
| 18 | + if prevOutput == nil { |
| 19 | + return false |
| 20 | + } |
| 21 | + |
| 22 | + prevOutputFetcher.AddPrevOut(txIn.PreviousOutPoint, prevOutput) |
| 23 | + } |
| 24 | + |
| 25 | + // verify signatures |
| 26 | + for i := range p.Inputs { |
| 27 | + output := p.Inputs[i].WitnessUtxo |
| 28 | + hashType := p.Inputs[i].SighashType |
| 29 | + |
| 30 | + witness := p.Inputs[i].FinalScriptWitness |
| 31 | + if len(witness) < 72+33 { |
| 32 | + return false |
| 33 | + } |
| 34 | + |
| 35 | + sigBytes := witness[0 : len(witness)-33] |
| 36 | + pkBytes := witness[len(witness)-33:] |
| 37 | + |
| 38 | + if sigBytes[len(sigBytes)-1] != byte(hashType) { |
| 39 | + return false |
| 40 | + } |
| 41 | + |
| 42 | + sig, err := ecdsa.ParseDERSignature(sigBytes[0 : len(sigBytes)-1]) |
| 43 | + if err != nil { |
| 44 | + return false |
| 45 | + } |
| 46 | + |
| 47 | + pk, err := secp256k1.ParsePubKey(pkBytes) |
| 48 | + if err != nil { |
| 49 | + return false |
| 50 | + } |
| 51 | + |
| 52 | + sigHash, err := txscript.CalcWitnessSigHash(output.PkScript, txscript.NewTxSigHashes(p.UnsignedTx, prevOutputFetcher), |
| 53 | + hashType, p.UnsignedTx, i, output.Value) |
| 54 | + if err != nil { |
| 55 | + return false |
| 56 | + } |
| 57 | + |
| 58 | + if !sig.Verify(sigHash, pk) { |
| 59 | + return false |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return true |
6 | 64 | } |
0 commit comments