chanfunding: fix PSBT funding for taproot chans

This commit is contained in:
Oliver Gugger 2023-08-28 11:13:25 +02:00
parent c6fcceb35b
commit 646444cdfe
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

View File

@ -1,7 +1,6 @@
package chanfunding package chanfunding
import ( import (
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"sync" "sync"
@ -178,23 +177,26 @@ func (i *PsbtIntent) FundingParams() (btcutil.Address, int64, *psbt.Packet,
// The funding output needs to be known already at this point, which // The funding output needs to be known already at this point, which
// means we need to have the local and remote multisig keys bound // means we need to have the local and remote multisig keys bound
// already. // already.
witnessScript, out, err := i.FundingOutput() _, out, err := i.FundingOutput()
if err != nil { if err != nil {
return nil, 0, nil, fmt.Errorf("unable to create funding "+ return nil, 0, nil, fmt.Errorf("unable to create funding "+
"output: %v", err) "output: %v", err)
} }
witnessScriptHash := sha256.Sum256(witnessScript)
// Encode the address in the human readable bech32 format. script, err := txscript.ParsePkScript(out.PkScript)
addr, err := btcutil.NewAddressWitnessScriptHash( if err != nil {
witnessScriptHash[:], i.netParams, return nil, 0, nil, fmt.Errorf("unable to parse funding "+
) "output script: %w", err)
}
// Encode the address in the human-readable bech32 format.
addr, err := script.Address(i.netParams)
if err != nil { if err != nil {
return nil, 0, nil, fmt.Errorf("unable to encode address: %v", return nil, 0, nil, fmt.Errorf("unable to encode address: %v",
err) err)
} }
// We'll also encode the address/amount in a machine readable raw PSBT // We'll also encode the address/amount in a machine-readable raw PSBT
// format. If the user supplied a base PSBT, we'll add the output to // format. If the user supplied a base PSBT, we'll add the output to
// that one, otherwise we'll create a new one. // that one, otherwise we'll create a new one.
packet := i.BasePsbt packet := i.BasePsbt
@ -597,8 +599,8 @@ func verifyAllInputsSegWit(txIns []*wire.TxIn, ins []psbt.PInput) error {
txIn := txIns[idx] txIn := txIns[idx]
txOut := utxo.TxOut[txIn.PreviousOutPoint.Index] txOut := utxo.TxOut[txIn.PreviousOutPoint.Index]
if !isSegWitScript(txOut.PkScript) && if !txscript.IsWitnessProgram(txOut.PkScript) &&
!isSegWitScript(in.RedeemScript) { !txscript.IsWitnessProgram(in.RedeemScript) {
return fmt.Errorf("input %d is non-SegWit "+ return fmt.Errorf("input %d is non-SegWit "+
"spend or missing redeem script", idx) "spend or missing redeem script", idx)
@ -614,19 +616,3 @@ func verifyAllInputsSegWit(txIns []*wire.TxIn, ins []psbt.PInput) error {
return nil return nil
} }
// isSegWitScript returns true if the given pkScript can be parsed successfully
// as a SegWit v0 spend.
func isSegWitScript(pkScript []byte) bool {
if len(pkScript) == 0 {
return false
}
parsed, err := txscript.ParsePkScript(pkScript)
if err != nil {
return false
}
return parsed.Class() == txscript.WitnessV0PubKeyHashTy ||
parsed.Class() == txscript.WitnessV0ScriptHashTy
}