watchtower: introduce CommitmentType

In this commit a new enum, CommitmentType, is introduced and initially
there are 3 CommitmentTypes: Legacy, LegacyTweakless and Anchor.

Then, various methods are added to `CommitmentType`. This allows us to
remove a bunch of "if-else" chains from the `wtclient` and `lookout`
code. This will also make things easier to extend when a new commitment
type (like Taproot) is added.
This commit is contained in:
Elle Mouton
2023-05-30 17:13:11 +02:00
parent 4d8fa349ca
commit 204ca6cb0f
6 changed files with 305 additions and 161 deletions

View File

@ -3,6 +3,8 @@ package blob
import (
"fmt"
"strings"
"github.com/lightningnetwork/lnd/channeldb"
)
// Flag represents a specify option that can be present in a Type.
@ -81,6 +83,27 @@ func (t Type) Identifier() (string, error) {
}
}
// CommitmentType returns the appropriate CommitmentType for the given blob Type
// and channel type.
func (t Type) CommitmentType(chanType *channeldb.ChannelType) (CommitmentType,
error) {
switch {
case t.Has(FlagAnchorChannel):
return AnchorCommitment, nil
case t.Has(FlagCommitOutputs):
if chanType != nil && chanType.IsTweakless() {
return LegacyTweaklessCommitment, nil
}
return LegacyCommitment, nil
default:
return 0, ErrUnknownBlobType
}
}
// Has returns true if the Type has the passed flag enabled.
func (t Type) Has(flag Flag) bool {
return Flag(t)&flag == flag