From 99cda74f6a4aada0b79950466b30a21077461100 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 27 Apr 2022 21:05:39 +0200 Subject: [PATCH] input: define new signing method constants --- input/signdescriptor.go | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/input/signdescriptor.go b/input/signdescriptor.go index e3ffc5e24..039523108 100644 --- a/input/signdescriptor.go +++ b/input/signdescriptor.go @@ -3,6 +3,7 @@ package input import ( "encoding/binary" "errors" + "fmt" "io" "github.com/btcsuite/btcd/btcec/v2" @@ -89,6 +90,67 @@ type SignDescriptor struct { InputIndex int } +// SignMethod defines the different ways a signer can sign, given a specific +// input. +type SignMethod uint8 + +const ( + // WitnessV0SignMethod denotes that a SegWit v0 (p2wkh, np2wkh, p2wsh) + // input script should be signed. + WitnessV0SignMethod SignMethod = 0 + + // TaprootKeySpendBIP0086SignMethod denotes that a SegWit v1 (p2tr) + // input should be signed by using the BIP0086 method (commit to + // internal key only). + TaprootKeySpendBIP0086SignMethod SignMethod = 1 + + // TaprootKeySpendSignMethod denotes that a SegWit v1 (p2tr) + // input should be signed by using a given taproot hash to commit to in + // addition to the internal key. + TaprootKeySpendSignMethod SignMethod = 2 + + // TaprootScriptSpendSignMethod denotes that a SegWit v1 (p2tr) input + // should be spent using the script path and that a specific leaf script + // should be signed for. + TaprootScriptSpendSignMethod SignMethod = 3 +) + +// String returns a human-readable representation of the signing method. +func (s SignMethod) String() string { + switch s { + case WitnessV0SignMethod: + return "witness_v0" + case TaprootKeySpendBIP0086SignMethod: + return "taproot_key_spend_bip86" + case TaprootKeySpendSignMethod: + return "taproot_key_spend" + case TaprootScriptSpendSignMethod: + return "taproot_script_spend" + default: + return fmt.Sprintf("unknown<%d>", s) + } +} + +// PkScriptCompatible returns true if the given public key script is compatible +// with the sign method. +func (s SignMethod) PkScriptCompatible(pkScript []byte) bool { + switch s { + // SegWit v0 can be p2wkh, np2wkh, p2wsh. + case WitnessV0SignMethod: + return txscript.IsPayToWitnessPubKeyHash(pkScript) || + txscript.IsPayToWitnessScriptHash(pkScript) || + txscript.IsPayToScriptHash(pkScript) + + case TaprootKeySpendBIP0086SignMethod, TaprootKeySpendSignMethod, + TaprootScriptSpendSignMethod: + + return txscript.IsPayToTaproot(pkScript) + + default: + return false + } +} + // WriteSignDescriptor serializes a SignDescriptor struct into the passed // io.Writer stream. //