lnwallet: add Tree() method, fix formatting

This commit is contained in:
Oliver Gugger 2024-04-11 14:26:15 +02:00
parent f0648e24a5
commit efbaf90caf
No known key found for this signature in database
GPG key ID: 8E4256593F177720
3 changed files with 66 additions and 48 deletions

View file

@ -33,17 +33,17 @@ const (
ScriptPathDelay ScriptPathDelay
) )
// ScriptDesciptor is an interface that abstracts over the various ways a // ScriptDescriptor is an interface that abstracts over the various ways a
// pkScript can be spent from an output. This supports both normal p2wsh // pkScript can be spent from an output. This supports both normal p2wsh
// (witness script, etc), and also tapscript paths which have distinct // (witness script, etc.), and also tapscript paths which have distinct
// tapscript leaves. // tapscript leaves.
type ScriptDescriptor interface { type ScriptDescriptor interface {
// PkScript is the public key script that commits to the final // PkScript is the public key script that commits to the final
// contract. // contract.
PkScript() []byte PkScript() []byte
// WitnessScript returns the witness script that we'll use when signing // WitnessScriptToSign returns the witness script that we'll use when
// for the remote party, and also verifying signatures on our // signing for the remote party, and also verifying signatures on our
// transactions. As an example, when we create an outgoing HTLC for the // transactions. As an example, when we create an outgoing HTLC for the
// remote party, we want to sign their success path. // remote party, we want to sign their success path.
// //
@ -73,6 +73,9 @@ type TapscriptDescriptor interface {
// TapScriptTree returns the underlying tapscript tree. // TapScriptTree returns the underlying tapscript tree.
TapScriptTree() *txscript.IndexedTapScriptTree TapScriptTree() *txscript.IndexedTapScriptTree
// Tree returns the underlying ScriptTree.
Tree() ScriptTree
} }
// ScriptTree holds the contents needed to spend a script within a tapscript // ScriptTree holds the contents needed to spend a script within a tapscript

View file

@ -689,8 +689,8 @@ func (h *HtlcScriptTree) WitnessScriptForPath(path ScriptPath) ([]byte, error) {
// CtrlBlockForPath returns the control block for the given spending path. For // CtrlBlockForPath returns the control block for the given spending path. For
// script types that don't have a control block, nil is returned. // script types that don't have a control block, nil is returned.
func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath, func (h *HtlcScriptTree) CtrlBlockForPath(
) (*txscript.ControlBlock, error) { path ScriptPath) (*txscript.ControlBlock, error) {
switch path { switch path {
case ScriptPathSuccess: case ScriptPathSuccess:
@ -708,6 +708,11 @@ func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
} }
} }
// Tree returns the underlying ScriptTree of the HtlcScriptTree.
func (h *HtlcScriptTree) Tree() ScriptTree {
return h.ScriptTree
}
// A compile time check to ensure HtlcScriptTree implements the // A compile time check to ensure HtlcScriptTree implements the
// TapscriptMultiplexer interface. // TapscriptMultiplexer interface.
var _ TapscriptDescriptor = (*HtlcScriptTree)(nil) var _ TapscriptDescriptor = (*HtlcScriptTree)(nil)
@ -1690,9 +1695,9 @@ func TaprootSecondLevelScriptTree(revokeKey, delayKey *btcec.PublicKey,
}, nil }, nil
} }
// WitnessScript returns the witness script that we'll use when signing for the // WitnessScriptToSign returns the witness script that we'll use when signing
// remote party, and also verifying signatures on our transactions. As an // for the remote party, and also verifying signatures on our transactions. As
// example, when we create an outgoing HTLC for the remote party, we want to // an example, when we create an outgoing HTLC for the remote party, we want to
// sign their success path. // sign their success path.
func (s *SecondLevelScriptTree) WitnessScriptToSign() []byte { func (s *SecondLevelScriptTree) WitnessScriptToSign() []byte {
return s.SuccessTapLeaf.Script return s.SuccessTapLeaf.Script
@ -1700,8 +1705,8 @@ func (s *SecondLevelScriptTree) WitnessScriptToSign() []byte {
// WitnessScriptForPath returns the witness script for the given spending path. // WitnessScriptForPath returns the witness script for the given spending path.
// An error is returned if the path is unknown. // An error is returned if the path is unknown.
func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath, func (s *SecondLevelScriptTree) WitnessScriptForPath(
) ([]byte, error) { path ScriptPath) ([]byte, error) {
switch path { switch path {
case ScriptPathDelay: case ScriptPathDelay:
@ -1716,8 +1721,8 @@ func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
// CtrlBlockForPath returns the control block for the given spending path. For // CtrlBlockForPath returns the control block for the given spending path. For
// script types that don't have a control block, nil is returned. // script types that don't have a control block, nil is returned.
func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath, func (s *SecondLevelScriptTree) CtrlBlockForPath(
) (*txscript.ControlBlock, error) { path ScriptPath) (*txscript.ControlBlock, error) {
switch path { switch path {
case ScriptPathDelay: case ScriptPathDelay:
@ -1733,6 +1738,11 @@ func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
} }
} }
// Tree returns the underlying ScriptTree of the SecondLevelScriptTree.
func (s *SecondLevelScriptTree) Tree() ScriptTree {
return s.ScriptTree
}
// A compile time check to ensure SecondLevelScriptTree implements the // A compile time check to ensure SecondLevelScriptTree implements the
// TapscriptDescriptor interface. // TapscriptDescriptor interface.
var _ TapscriptDescriptor = (*SecondLevelScriptTree)(nil) var _ TapscriptDescriptor = (*SecondLevelScriptTree)(nil)
@ -2069,9 +2079,9 @@ type CommitScriptTree struct {
// TapscriptDescriptor interface. // TapscriptDescriptor interface.
var _ TapscriptDescriptor = (*CommitScriptTree)(nil) var _ TapscriptDescriptor = (*CommitScriptTree)(nil)
// WitnessScript returns the witness script that we'll use when signing for the // WitnessScriptToSign returns the witness script that we'll use when signing
// remote party, and also verifying signatures on our transactions. As an // for the remote party, and also verifying signatures on our transactions. As
// example, when we create an outgoing HTLC for the remote party, we want to // an example, when we create an outgoing HTLC for the remote party, we want to
// sign their success path. // sign their success path.
func (c *CommitScriptTree) WitnessScriptToSign() []byte { func (c *CommitScriptTree) WitnessScriptToSign() []byte {
// TODO(roasbeef): abstraction leak here? always dependent // TODO(roasbeef): abstraction leak here? always dependent
@ -2080,8 +2090,8 @@ func (c *CommitScriptTree) WitnessScriptToSign() []byte {
// WitnessScriptForPath returns the witness script for the given spending path. // WitnessScriptForPath returns the witness script for the given spending path.
// An error is returned if the path is unknown. // An error is returned if the path is unknown.
func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath, func (c *CommitScriptTree) WitnessScriptForPath(
) ([]byte, error) { path ScriptPath) ([]byte, error) {
switch path { switch path {
// For the commitment output, the delay and success path are the same, // For the commitment output, the delay and success path are the same,
@ -2099,8 +2109,8 @@ func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
// CtrlBlockForPath returns the control block for the given spending path. For // CtrlBlockForPath returns the control block for the given spending path. For
// script types that don't have a control block, nil is returned. // script types that don't have a control block, nil is returned.
func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath, func (c *CommitScriptTree) CtrlBlockForPath(
) (*txscript.ControlBlock, error) { path ScriptPath) (*txscript.ControlBlock, error) {
switch path { switch path {
case ScriptPathDelay: case ScriptPathDelay:
@ -2120,6 +2130,11 @@ func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
} }
} }
// Tree returns the underlying ScriptTree of the CommitScriptTree.
func (c *CommitScriptTree) Tree() ScriptTree {
return c.ScriptTree
}
// NewLocalCommitScriptTree returns a new CommitScript tree that can be used to // NewLocalCommitScriptTree returns a new CommitScript tree that can be used to
// create and spend the commitment output for the local party. // create and spend the commitment output for the local party.
func NewLocalCommitScriptTree(csvTimeout uint32, func NewLocalCommitScriptTree(csvTimeout uint32,
@ -2241,7 +2256,7 @@ func TaprootCommitScriptToSelf(csvTimeout uint32,
return commitScriptTree.TaprootKey, nil return commitScriptTree.TaprootKey, nil
} }
// MakeTaprootSCtrlBlock takes a leaf script, the internal key (usually the // MakeTaprootCtrlBlock takes a leaf script, the internal key (usually the
// revoke key), and a script tree and creates a valid control block for a spend // revoke key), and a script tree and creates a valid control block for a spend
// of the leaf. // of the leaf.
func MakeTaprootCtrlBlock(leafScript []byte, internalKey *btcec.PublicKey, func MakeTaprootCtrlBlock(leafScript []byte, internalKey *btcec.PublicKey,
@ -2296,9 +2311,6 @@ func TaprootCommitSpendSuccess(signer Signer, signDesc *SignDescriptor,
witnessStack[0] = maybeAppendSighash(sweepSig, signDesc.HashType) witnessStack[0] = maybeAppendSighash(sweepSig, signDesc.HashType)
witnessStack[1] = signDesc.WitnessScript witnessStack[1] = signDesc.WitnessScript
witnessStack[2] = ctrlBlockBytes witnessStack[2] = ctrlBlockBytes
if err != nil {
return nil, err
}
return witnessStack, nil return witnessStack, nil
} }
@ -2773,8 +2785,8 @@ type AnchorScriptTree struct {
// NewAnchorScriptTree makes a new script tree for an anchor output with the // NewAnchorScriptTree makes a new script tree for an anchor output with the
// passed anchor key. // passed anchor key.
func NewAnchorScriptTree(anchorKey *btcec.PublicKey, func NewAnchorScriptTree(
) (*AnchorScriptTree, error) { anchorKey *btcec.PublicKey) (*AnchorScriptTree, error) {
// The main script used is just a OP_16 CSV (anyone can sweep after 16 // The main script used is just a OP_16 CSV (anyone can sweep after 16
// blocks). // blocks).
@ -2810,9 +2822,9 @@ func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
}, nil }, nil
} }
// WitnessScript returns the witness script that we'll use when signing for the // WitnessScriptToSign returns the witness script that we'll use when signing
// remote party, and also verifying signatures on our transactions. As an // for the remote party, and also verifying signatures on our transactions. As
// example, when we create an outgoing HTLC for the remote party, we want to // an example, when we create an outgoing HTLC for the remote party, we want to
// sign their success path. // sign their success path.
func (a *AnchorScriptTree) WitnessScriptToSign() []byte { func (a *AnchorScriptTree) WitnessScriptToSign() []byte {
return a.SweepLeaf.Script return a.SweepLeaf.Script
@ -2820,8 +2832,8 @@ func (a *AnchorScriptTree) WitnessScriptToSign() []byte {
// WitnessScriptForPath returns the witness script for the given spending path. // WitnessScriptForPath returns the witness script for the given spending path.
// An error is returned if the path is unknown. // An error is returned if the path is unknown.
func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath, func (a *AnchorScriptTree) WitnessScriptForPath(
) ([]byte, error) { path ScriptPath) ([]byte, error) {
switch path { switch path {
case ScriptPathDelay: case ScriptPathDelay:
@ -2836,8 +2848,8 @@ func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
// CtrlBlockForPath returns the control block for the given spending path. For // CtrlBlockForPath returns the control block for the given spending path. For
// script types that don't have a control block, nil is returned. // script types that don't have a control block, nil is returned.
func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath, func (a *AnchorScriptTree) CtrlBlockForPath(
) (*txscript.ControlBlock, error) { path ScriptPath) (*txscript.ControlBlock, error) {
switch path { switch path {
case ScriptPathDelay: case ScriptPathDelay:
@ -2853,6 +2865,11 @@ func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
} }
} }
// Tree returns the underlying ScriptTree of the AnchorScriptTree.
func (a *AnchorScriptTree) Tree() ScriptTree {
return a.ScriptTree
}
// A compile time check to ensure AnchorScriptTree implements the // A compile time check to ensure AnchorScriptTree implements the
// TapscriptDescriptor interface. // TapscriptDescriptor interface.
var _ TapscriptDescriptor = (*AnchorScriptTree)(nil) var _ TapscriptDescriptor = (*AnchorScriptTree)(nil)

View file

@ -200,9 +200,9 @@ func (w *WitnessScriptDesc) PkScript() []byte {
return w.OutputScript return w.OutputScript
} }
// WitnessScript returns the witness script that we'll use when signing for the // WitnessScriptToSign returns the witness script that we'll use when signing
// remote party, and also verifying signatures on our transactions. As an // for the remote party, and also verifying signatures on our transactions. As
// example, when we create an outgoing HTLC for the remote party, we want to // an example, when we create an outgoing HTLC for the remote party, we want to
// sign their success path. // sign their success path.
func (w *WitnessScriptDesc) WitnessScriptToSign() []byte { func (w *WitnessScriptDesc) WitnessScriptToSign() []byte {
return w.WitnessScript return w.WitnessScript
@ -210,10 +210,10 @@ func (w *WitnessScriptDesc) WitnessScriptToSign() []byte {
// WitnessScriptForPath returns the witness script for the given spending path. // WitnessScriptForPath returns the witness script for the given spending path.
// An error is returned if the path is unknown. This is useful as when // An error is returned if the path is unknown. This is useful as when
// constructing a contrl block for a given path, one also needs witness script // constructing a control block for a given path, one also needs witness script
// being signed. // being signed.
func (w *WitnessScriptDesc) WitnessScriptForPath(_ input.ScriptPath, func (w *WitnessScriptDesc) WitnessScriptForPath(
) ([]byte, error) { _ input.ScriptPath) ([]byte, error) {
return w.WitnessScript, nil return w.WitnessScript, nil
} }
@ -532,8 +532,8 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
input.ScriptDescriptor, input.ScriptDescriptor, error) { input.ScriptDescriptor, input.ScriptDescriptor, error) {
var ( var (
anchorScript func(key *btcec.PublicKey) ( anchorScript func(
input.ScriptDescriptor, error) key *btcec.PublicKey) (input.ScriptDescriptor, error)
keySelector func(*channeldb.ChannelConfig, keySelector func(*channeldb.ChannelConfig,
bool) *btcec.PublicKey bool) *btcec.PublicKey
@ -544,12 +544,10 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
// level key is now the (relative) local delay and remote public key, // level key is now the (relative) local delay and remote public key,
// since these are fully revealed once the commitment hits the chain. // since these are fully revealed once the commitment hits the chain.
case chanType.IsTaproot(): case chanType.IsTaproot():
anchorScript = func(key *btcec.PublicKey, anchorScript = func(
) (input.ScriptDescriptor, error) { key *btcec.PublicKey) (input.ScriptDescriptor, error) {
return input.NewAnchorScriptTree( return input.NewAnchorScriptTree(key)
key,
)
} }
keySelector = func(cfg *channeldb.ChannelConfig, keySelector = func(cfg *channeldb.ChannelConfig,
@ -567,8 +565,8 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
default: default:
// For normal channels, we'll create a p2wsh script based on // For normal channels, we'll create a p2wsh script based on
// the target key. // the target key.
anchorScript = func(key *btcec.PublicKey, anchorScript = func(
) (input.ScriptDescriptor, error) { key *btcec.PublicKey) (input.ScriptDescriptor, error) {
script, err := input.CommitScriptAnchor(key) script, err := input.CommitScriptAnchor(key)
if err != nil { if err != nil {