Skip to content

Commit efbaf90

Browse files
committed
lnwallet: add Tree() method, fix formatting
1 parent f0648e2 commit efbaf90

File tree

3 files changed

+66
-48
lines changed

3 files changed

+66
-48
lines changed

input/script_desc.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ const (
3333
ScriptPathDelay
3434
)
3535

36-
// ScriptDesciptor is an interface that abstracts over the various ways a
36+
// ScriptDescriptor is an interface that abstracts over the various ways a
3737
// pkScript can be spent from an output. This supports both normal p2wsh
38-
// (witness script, etc), and also tapscript paths which have distinct
38+
// (witness script, etc.), and also tapscript paths which have distinct
3939
// tapscript leaves.
4040
type ScriptDescriptor interface {
4141
// PkScript is the public key script that commits to the final
4242
// contract.
4343
PkScript() []byte
4444

45-
// WitnessScript returns the witness script that we'll use when signing
46-
// for the remote party, and also verifying signatures on our
45+
// WitnessScriptToSign returns the witness script that we'll use when
46+
// signing for the remote party, and also verifying signatures on our
4747
// transactions. As an example, when we create an outgoing HTLC for the
4848
// remote party, we want to sign their success path.
4949
//
@@ -73,6 +73,9 @@ type TapscriptDescriptor interface {
7373

7474
// TapScriptTree returns the underlying tapscript tree.
7575
TapScriptTree() *txscript.IndexedTapScriptTree
76+
77+
// Tree returns the underlying ScriptTree.
78+
Tree() ScriptTree
7679
}
7780

7881
// ScriptTree holds the contents needed to spend a script within a tapscript

input/script_utils.go

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,8 @@ func (h *HtlcScriptTree) WitnessScriptForPath(path ScriptPath) ([]byte, error) {
689689

690690
// CtrlBlockForPath returns the control block for the given spending path. For
691691
// script types that don't have a control block, nil is returned.
692-
func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
693-
) (*txscript.ControlBlock, error) {
692+
func (h *HtlcScriptTree) CtrlBlockForPath(
693+
path ScriptPath) (*txscript.ControlBlock, error) {
694694

695695
switch path {
696696
case ScriptPathSuccess:
@@ -708,6 +708,11 @@ func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
708708
}
709709
}
710710

711+
// Tree returns the underlying ScriptTree of the HtlcScriptTree.
712+
func (h *HtlcScriptTree) Tree() ScriptTree {
713+
return h.ScriptTree
714+
}
715+
711716
// A compile time check to ensure HtlcScriptTree implements the
712717
// TapscriptMultiplexer interface.
713718
var _ TapscriptDescriptor = (*HtlcScriptTree)(nil)
@@ -1690,18 +1695,18 @@ func TaprootSecondLevelScriptTree(revokeKey, delayKey *btcec.PublicKey,
16901695
}, nil
16911696
}
16921697

1693-
// WitnessScript returns the witness script that we'll use when signing for the
1694-
// remote party, and also verifying signatures on our transactions. As an
1695-
// example, when we create an outgoing HTLC for the remote party, we want to
1698+
// WitnessScriptToSign returns the witness script that we'll use when signing
1699+
// for the remote party, and also verifying signatures on our transactions. As
1700+
// an example, when we create an outgoing HTLC for the remote party, we want to
16961701
// sign their success path.
16971702
func (s *SecondLevelScriptTree) WitnessScriptToSign() []byte {
16981703
return s.SuccessTapLeaf.Script
16991704
}
17001705

17011706
// WitnessScriptForPath returns the witness script for the given spending path.
17021707
// An error is returned if the path is unknown.
1703-
func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
1704-
) ([]byte, error) {
1708+
func (s *SecondLevelScriptTree) WitnessScriptForPath(
1709+
path ScriptPath) ([]byte, error) {
17051710

17061711
switch path {
17071712
case ScriptPathDelay:
@@ -1716,8 +1721,8 @@ func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
17161721

17171722
// CtrlBlockForPath returns the control block for the given spending path. For
17181723
// script types that don't have a control block, nil is returned.
1719-
func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
1720-
) (*txscript.ControlBlock, error) {
1724+
func (s *SecondLevelScriptTree) CtrlBlockForPath(
1725+
path ScriptPath) (*txscript.ControlBlock, error) {
17211726

17221727
switch path {
17231728
case ScriptPathDelay:
@@ -1733,6 +1738,11 @@ func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
17331738
}
17341739
}
17351740

1741+
// Tree returns the underlying ScriptTree of the SecondLevelScriptTree.
1742+
func (s *SecondLevelScriptTree) Tree() ScriptTree {
1743+
return s.ScriptTree
1744+
}
1745+
17361746
// A compile time check to ensure SecondLevelScriptTree implements the
17371747
// TapscriptDescriptor interface.
17381748
var _ TapscriptDescriptor = (*SecondLevelScriptTree)(nil)
@@ -2069,9 +2079,9 @@ type CommitScriptTree struct {
20692079
// TapscriptDescriptor interface.
20702080
var _ TapscriptDescriptor = (*CommitScriptTree)(nil)
20712081

2072-
// WitnessScript returns the witness script that we'll use when signing for the
2073-
// remote party, and also verifying signatures on our transactions. As an
2074-
// example, when we create an outgoing HTLC for the remote party, we want to
2082+
// WitnessScriptToSign returns the witness script that we'll use when signing
2083+
// for the remote party, and also verifying signatures on our transactions. As
2084+
// an example, when we create an outgoing HTLC for the remote party, we want to
20752085
// sign their success path.
20762086
func (c *CommitScriptTree) WitnessScriptToSign() []byte {
20772087
// TODO(roasbeef): abstraction leak here? always dependent
@@ -2080,8 +2090,8 @@ func (c *CommitScriptTree) WitnessScriptToSign() []byte {
20802090

20812091
// WitnessScriptForPath returns the witness script for the given spending path.
20822092
// An error is returned if the path is unknown.
2083-
func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
2084-
) ([]byte, error) {
2093+
func (c *CommitScriptTree) WitnessScriptForPath(
2094+
path ScriptPath) ([]byte, error) {
20852095

20862096
switch path {
20872097
// For the commitment output, the delay and success path are the same,
@@ -2099,8 +2109,8 @@ func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
20992109

21002110
// CtrlBlockForPath returns the control block for the given spending path. For
21012111
// script types that don't have a control block, nil is returned.
2102-
func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
2103-
) (*txscript.ControlBlock, error) {
2112+
func (c *CommitScriptTree) CtrlBlockForPath(
2113+
path ScriptPath) (*txscript.ControlBlock, error) {
21042114

21052115
switch path {
21062116
case ScriptPathDelay:
@@ -2120,6 +2130,11 @@ func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
21202130
}
21212131
}
21222132

2133+
// Tree returns the underlying ScriptTree of the CommitScriptTree.
2134+
func (c *CommitScriptTree) Tree() ScriptTree {
2135+
return c.ScriptTree
2136+
}
2137+
21232138
// NewLocalCommitScriptTree returns a new CommitScript tree that can be used to
21242139
// create and spend the commitment output for the local party.
21252140
func NewLocalCommitScriptTree(csvTimeout uint32,
@@ -2241,7 +2256,7 @@ func TaprootCommitScriptToSelf(csvTimeout uint32,
22412256
return commitScriptTree.TaprootKey, nil
22422257
}
22432258

2244-
// MakeTaprootSCtrlBlock takes a leaf script, the internal key (usually the
2259+
// MakeTaprootCtrlBlock takes a leaf script, the internal key (usually the
22452260
// revoke key), and a script tree and creates a valid control block for a spend
22462261
// of the leaf.
22472262
func MakeTaprootCtrlBlock(leafScript []byte, internalKey *btcec.PublicKey,
@@ -2296,9 +2311,6 @@ func TaprootCommitSpendSuccess(signer Signer, signDesc *SignDescriptor,
22962311
witnessStack[0] = maybeAppendSighash(sweepSig, signDesc.HashType)
22972312
witnessStack[1] = signDesc.WitnessScript
22982313
witnessStack[2] = ctrlBlockBytes
2299-
if err != nil {
2300-
return nil, err
2301-
}
23022314

23032315
return witnessStack, nil
23042316
}
@@ -2773,8 +2785,8 @@ type AnchorScriptTree struct {
27732785

27742786
// NewAnchorScriptTree makes a new script tree for an anchor output with the
27752787
// passed anchor key.
2776-
func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
2777-
) (*AnchorScriptTree, error) {
2788+
func NewAnchorScriptTree(
2789+
anchorKey *btcec.PublicKey) (*AnchorScriptTree, error) {
27782790

27792791
// The main script used is just a OP_16 CSV (anyone can sweep after 16
27802792
// blocks).
@@ -2810,18 +2822,18 @@ func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
28102822
}, nil
28112823
}
28122824

2813-
// WitnessScript returns the witness script that we'll use when signing for the
2814-
// remote party, and also verifying signatures on our transactions. As an
2815-
// example, when we create an outgoing HTLC for the remote party, we want to
2825+
// WitnessScriptToSign returns the witness script that we'll use when signing
2826+
// for the remote party, and also verifying signatures on our transactions. As
2827+
// an example, when we create an outgoing HTLC for the remote party, we want to
28162828
// sign their success path.
28172829
func (a *AnchorScriptTree) WitnessScriptToSign() []byte {
28182830
return a.SweepLeaf.Script
28192831
}
28202832

28212833
// WitnessScriptForPath returns the witness script for the given spending path.
28222834
// An error is returned if the path is unknown.
2823-
func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
2824-
) ([]byte, error) {
2835+
func (a *AnchorScriptTree) WitnessScriptForPath(
2836+
path ScriptPath) ([]byte, error) {
28252837

28262838
switch path {
28272839
case ScriptPathDelay:
@@ -2836,8 +2848,8 @@ func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
28362848

28372849
// CtrlBlockForPath returns the control block for the given spending path. For
28382850
// script types that don't have a control block, nil is returned.
2839-
func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
2840-
) (*txscript.ControlBlock, error) {
2851+
func (a *AnchorScriptTree) CtrlBlockForPath(
2852+
path ScriptPath) (*txscript.ControlBlock, error) {
28412853

28422854
switch path {
28432855
case ScriptPathDelay:
@@ -2853,6 +2865,11 @@ func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
28532865
}
28542866
}
28552867

2868+
// Tree returns the underlying ScriptTree of the AnchorScriptTree.
2869+
func (a *AnchorScriptTree) Tree() ScriptTree {
2870+
return a.ScriptTree
2871+
}
2872+
28562873
// A compile time check to ensure AnchorScriptTree implements the
28572874
// TapscriptDescriptor interface.
28582875
var _ TapscriptDescriptor = (*AnchorScriptTree)(nil)

lnwallet/commitment.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,20 @@ func (w *WitnessScriptDesc) PkScript() []byte {
200200
return w.OutputScript
201201
}
202202

203-
// WitnessScript returns the witness script that we'll use when signing for the
204-
// remote party, and also verifying signatures on our transactions. As an
205-
// example, when we create an outgoing HTLC for the remote party, we want to
203+
// WitnessScriptToSign returns the witness script that we'll use when signing
204+
// for the remote party, and also verifying signatures on our transactions. As
205+
// an example, when we create an outgoing HTLC for the remote party, we want to
206206
// sign their success path.
207207
func (w *WitnessScriptDesc) WitnessScriptToSign() []byte {
208208
return w.WitnessScript
209209
}
210210

211211
// WitnessScriptForPath returns the witness script for the given spending path.
212212
// An error is returned if the path is unknown. This is useful as when
213-
// constructing a contrl block for a given path, one also needs witness script
213+
// constructing a control block for a given path, one also needs witness script
214214
// being signed.
215-
func (w *WitnessScriptDesc) WitnessScriptForPath(_ input.ScriptPath,
216-
) ([]byte, error) {
215+
func (w *WitnessScriptDesc) WitnessScriptForPath(
216+
_ input.ScriptPath) ([]byte, error) {
217217

218218
return w.WitnessScript, nil
219219
}
@@ -532,8 +532,8 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
532532
input.ScriptDescriptor, input.ScriptDescriptor, error) {
533533

534534
var (
535-
anchorScript func(key *btcec.PublicKey) (
536-
input.ScriptDescriptor, error)
535+
anchorScript func(
536+
key *btcec.PublicKey) (input.ScriptDescriptor, error)
537537

538538
keySelector func(*channeldb.ChannelConfig,
539539
bool) *btcec.PublicKey
@@ -544,12 +544,10 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
544544
// level key is now the (relative) local delay and remote public key,
545545
// since these are fully revealed once the commitment hits the chain.
546546
case chanType.IsTaproot():
547-
anchorScript = func(key *btcec.PublicKey,
548-
) (input.ScriptDescriptor, error) {
547+
anchorScript = func(
548+
key *btcec.PublicKey) (input.ScriptDescriptor, error) {
549549

550-
return input.NewAnchorScriptTree(
551-
key,
552-
)
550+
return input.NewAnchorScriptTree(key)
553551
}
554552

555553
keySelector = func(cfg *channeldb.ChannelConfig,
@@ -567,8 +565,8 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
567565
default:
568566
// For normal channels, we'll create a p2wsh script based on
569567
// the target key.
570-
anchorScript = func(key *btcec.PublicKey,
571-
) (input.ScriptDescriptor, error) {
568+
anchorScript = func(
569+
key *btcec.PublicKey) (input.ScriptDescriptor, error) {
572570

573571
script, err := input.CommitScriptAnchor(key)
574572
if err != nil {

0 commit comments

Comments
 (0)