Skip to content

Commit 15abb14

Browse files
committed
lnwallet: add Tree() method, fix formatting
1 parent 4bda93a commit 15abb14

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
@@ -733,8 +733,8 @@ func (h *HtlcScriptTree) WitnessScriptForPath(path ScriptPath) ([]byte, error) {
733733

734734
// CtrlBlockForPath returns the control block for the given spending path. For
735735
// script types that don't have a control block, nil is returned.
736-
func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
737-
) (*txscript.ControlBlock, error) {
736+
func (h *HtlcScriptTree) CtrlBlockForPath(
737+
path ScriptPath) (*txscript.ControlBlock, error) {
738738

739739
switch path {
740740
case ScriptPathSuccess:
@@ -752,6 +752,11 @@ func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
752752
}
753753
}
754754

755+
// Tree returns the underlying ScriptTree of the HtlcScriptTree.
756+
func (h *HtlcScriptTree) Tree() ScriptTree {
757+
return h.ScriptTree
758+
}
759+
755760
// A compile time check to ensure HtlcScriptTree implements the
756761
// TapscriptMultiplexer interface.
757762
var _ TapscriptDescriptor = (*HtlcScriptTree)(nil)
@@ -1752,18 +1757,18 @@ func TaprootSecondLevelScriptTree(revokeKey, delayKey *btcec.PublicKey,
17521757
}, nil
17531758
}
17541759

1755-
// WitnessScript returns the witness script that we'll use when signing for the
1756-
// remote party, and also verifying signatures on our transactions. As an
1757-
// example, when we create an outgoing HTLC for the remote party, we want to
1760+
// WitnessScriptToSign returns the witness script that we'll use when signing
1761+
// for the remote party, and also verifying signatures on our transactions. As
1762+
// an example, when we create an outgoing HTLC for the remote party, we want to
17581763
// sign their success path.
17591764
func (s *SecondLevelScriptTree) WitnessScriptToSign() []byte {
17601765
return s.SuccessTapLeaf.Script
17611766
}
17621767

17631768
// WitnessScriptForPath returns the witness script for the given spending path.
17641769
// An error is returned if the path is unknown.
1765-
func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
1766-
) ([]byte, error) {
1770+
func (s *SecondLevelScriptTree) WitnessScriptForPath(
1771+
path ScriptPath) ([]byte, error) {
17671772

17681773
switch path {
17691774
case ScriptPathDelay:
@@ -1778,8 +1783,8 @@ func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
17781783

17791784
// CtrlBlockForPath returns the control block for the given spending path. For
17801785
// script types that don't have a control block, nil is returned.
1781-
func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
1782-
) (*txscript.ControlBlock, error) {
1786+
func (s *SecondLevelScriptTree) CtrlBlockForPath(
1787+
path ScriptPath) (*txscript.ControlBlock, error) {
17831788

17841789
switch path {
17851790
case ScriptPathDelay:
@@ -1795,6 +1800,11 @@ func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
17951800
}
17961801
}
17971802

1803+
// Tree returns the underlying ScriptTree of the SecondLevelScriptTree.
1804+
func (s *SecondLevelScriptTree) Tree() ScriptTree {
1805+
return s.ScriptTree
1806+
}
1807+
17981808
// A compile time check to ensure SecondLevelScriptTree implements the
17991809
// TapscriptDescriptor interface.
18001810
var _ TapscriptDescriptor = (*SecondLevelScriptTree)(nil)
@@ -2137,9 +2147,9 @@ type CommitScriptTree struct {
21372147
// TapscriptDescriptor interface.
21382148
var _ TapscriptDescriptor = (*CommitScriptTree)(nil)
21392149

2140-
// WitnessScript returns the witness script that we'll use when signing for the
2141-
// remote party, and also verifying signatures on our transactions. As an
2142-
// example, when we create an outgoing HTLC for the remote party, we want to
2150+
// WitnessScriptToSign returns the witness script that we'll use when signing
2151+
// for the remote party, and also verifying signatures on our transactions. As
2152+
// an example, when we create an outgoing HTLC for the remote party, we want to
21432153
// sign their success path.
21442154
func (c *CommitScriptTree) WitnessScriptToSign() []byte {
21452155
// TODO(roasbeef): abstraction leak here? always dependent
@@ -2148,8 +2158,8 @@ func (c *CommitScriptTree) WitnessScriptToSign() []byte {
21482158

21492159
// WitnessScriptForPath returns the witness script for the given spending path.
21502160
// An error is returned if the path is unknown.
2151-
func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
2152-
) ([]byte, error) {
2161+
func (c *CommitScriptTree) WitnessScriptForPath(
2162+
path ScriptPath) ([]byte, error) {
21532163

21542164
switch path {
21552165
// For the commitment output, the delay and success path are the same,
@@ -2167,8 +2177,8 @@ func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
21672177

21682178
// CtrlBlockForPath returns the control block for the given spending path. For
21692179
// script types that don't have a control block, nil is returned.
2170-
func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
2171-
) (*txscript.ControlBlock, error) {
2180+
func (c *CommitScriptTree) CtrlBlockForPath(
2181+
path ScriptPath) (*txscript.ControlBlock, error) {
21722182

21732183
switch path {
21742184
case ScriptPathDelay:
@@ -2188,6 +2198,11 @@ func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
21882198
}
21892199
}
21902200

2201+
// Tree returns the underlying ScriptTree of the CommitScriptTree.
2202+
func (c *CommitScriptTree) Tree() ScriptTree {
2203+
return c.ScriptTree
2204+
}
2205+
21912206
// NewLocalCommitScriptTree returns a new CommitScript tree that can be used to
21922207
// create and spend the commitment output for the local party.
21932208
func NewLocalCommitScriptTree(csvTimeout uint32, selfKey,
@@ -2315,7 +2330,7 @@ func TaprootCommitScriptToSelf(csvTimeout uint32,
23152330
return commitScriptTree.TaprootKey, nil
23162331
}
23172332

2318-
// MakeTaprootSCtrlBlock takes a leaf script, the internal key (usually the
2333+
// MakeTaprootCtrlBlock takes a leaf script, the internal key (usually the
23192334
// revoke key), and a script tree and creates a valid control block for a spend
23202335
// of the leaf.
23212336
func MakeTaprootCtrlBlock(leafScript []byte, internalKey *btcec.PublicKey,
@@ -2370,9 +2385,6 @@ func TaprootCommitSpendSuccess(signer Signer, signDesc *SignDescriptor,
23702385
witnessStack[0] = maybeAppendSighash(sweepSig, signDesc.HashType)
23712386
witnessStack[1] = signDesc.WitnessScript
23722387
witnessStack[2] = ctrlBlockBytes
2373-
if err != nil {
2374-
return nil, err
2375-
}
23762388

23772389
return witnessStack, nil
23782390
}
@@ -2854,8 +2866,8 @@ type AnchorScriptTree struct {
28542866

28552867
// NewAnchorScriptTree makes a new script tree for an anchor output with the
28562868
// passed anchor key.
2857-
func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
2858-
) (*AnchorScriptTree, error) {
2869+
func NewAnchorScriptTree(
2870+
anchorKey *btcec.PublicKey) (*AnchorScriptTree, error) {
28592871

28602872
// The main script used is just a OP_16 CSV (anyone can sweep after 16
28612873
// blocks).
@@ -2891,18 +2903,18 @@ func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
28912903
}, nil
28922904
}
28932905

2894-
// WitnessScript returns the witness script that we'll use when signing for the
2895-
// remote party, and also verifying signatures on our transactions. As an
2896-
// example, when we create an outgoing HTLC for the remote party, we want to
2906+
// WitnessScriptToSign returns the witness script that we'll use when signing
2907+
// for the remote party, and also verifying signatures on our transactions. As
2908+
// an example, when we create an outgoing HTLC for the remote party, we want to
28972909
// sign their success path.
28982910
func (a *AnchorScriptTree) WitnessScriptToSign() []byte {
28992911
return a.SweepLeaf.Script
29002912
}
29012913

29022914
// WitnessScriptForPath returns the witness script for the given spending path.
29032915
// An error is returned if the path is unknown.
2904-
func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
2905-
) ([]byte, error) {
2916+
func (a *AnchorScriptTree) WitnessScriptForPath(
2917+
path ScriptPath) ([]byte, error) {
29062918

29072919
switch path {
29082920
case ScriptPathDelay:
@@ -2917,8 +2929,8 @@ func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
29172929

29182930
// CtrlBlockForPath returns the control block for the given spending path. For
29192931
// script types that don't have a control block, nil is returned.
2920-
func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
2921-
) (*txscript.ControlBlock, error) {
2932+
func (a *AnchorScriptTree) CtrlBlockForPath(
2933+
path ScriptPath) (*txscript.ControlBlock, error) {
29222934

29232935
switch path {
29242936
case ScriptPathDelay:
@@ -2934,6 +2946,11 @@ func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
29342946
}
29352947
}
29362948

2949+
// Tree returns the underlying ScriptTree of the AnchorScriptTree.
2950+
func (a *AnchorScriptTree) Tree() ScriptTree {
2951+
return a.ScriptTree
2952+
}
2953+
29372954
// A compile time check to ensure AnchorScriptTree implements the
29382955
// TapscriptDescriptor interface.
29392956
var _ TapscriptDescriptor = (*AnchorScriptTree)(nil)

lnwallet/commitment.go

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

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

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

219219
return w.WitnessScript, nil
220220
}
@@ -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)