Skip to content

Commit bb577aa

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

File tree

3 files changed

+67
-50
lines changed

3 files changed

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

729729
// CtrlBlockForPath returns the control block for the given spending path. For
730730
// script types that don't have a control block, nil is returned.
731-
func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
732-
) (*txscript.ControlBlock, error) {
731+
func (h *HtlcScriptTree) CtrlBlockForPath(
732+
path ScriptPath) (*txscript.ControlBlock, error) {
733733

734734
switch path {
735735
case ScriptPathSuccess:
@@ -747,6 +747,11 @@ func (h *HtlcScriptTree) CtrlBlockForPath(path ScriptPath,
747747
}
748748
}
749749

750+
// Tree returns the underlying ScriptTree of the HtlcScriptTree.
751+
func (h *HtlcScriptTree) Tree() ScriptTree {
752+
return h.ScriptTree
753+
}
754+
750755
// A compile time check to ensure HtlcScriptTree implements the
751756
// TapscriptMultiplexer interface.
752757
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)
@@ -2139,9 +2149,9 @@ type CommitScriptTree struct {
21392149
// TapscriptDescriptor interface.
21402150
var _ TapscriptDescriptor = (*CommitScriptTree)(nil)
21412151

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

21512161
// WitnessScriptForPath returns the witness script for the given spending path.
21522162
// An error is returned if the path is unknown.
2153-
func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
2154-
) ([]byte, error) {
2163+
func (c *CommitScriptTree) WitnessScriptForPath(
2164+
path ScriptPath) ([]byte, error) {
21552165

21562166
switch path {
21572167
// For the commitment output, the delay and success path are the same,
@@ -2169,8 +2179,8 @@ func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
21692179

21702180
// CtrlBlockForPath returns the control block for the given spending path. For
21712181
// script types that don't have a control block, nil is returned.
2172-
func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
2173-
) (*txscript.ControlBlock, error) {
2182+
func (c *CommitScriptTree) CtrlBlockForPath(
2183+
path ScriptPath) (*txscript.ControlBlock, error) {
21742184

21752185
switch path {
21762186
case ScriptPathDelay:
@@ -2190,6 +2200,11 @@ func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
21902200
}
21912201
}
21922202

2203+
// Tree returns the underlying ScriptTree of the CommitScriptTree.
2204+
func (c *CommitScriptTree) Tree() ScriptTree {
2205+
return c.ScriptTree
2206+
}
2207+
21932208
// NewLocalCommitScriptTree returns a new CommitScript tree that can be used to
21942209
// create and spend the commitment output for the local party.
21952210
func NewLocalCommitScriptTree(csvTimeout uint32,
@@ -2319,7 +2334,7 @@ func TaprootCommitScriptToSelf(csvTimeout uint32,
23192334
return commitScriptTree.TaprootKey, nil
23202335
}
23212336

2322-
// MakeTaprootSCtrlBlock takes a leaf script, the internal key (usually the
2337+
// MakeTaprootCtrlBlock takes a leaf script, the internal key (usually the
23232338
// revoke key), and a script tree and creates a valid control block for a spend
23242339
// of the leaf.
23252340
func MakeTaprootCtrlBlock(leafScript []byte, internalKey *btcec.PublicKey,
@@ -2374,9 +2389,6 @@ func TaprootCommitSpendSuccess(signer Signer, signDesc *SignDescriptor,
23742389
witnessStack[0] = maybeAppendSighash(sweepSig, signDesc.HashType)
23752390
witnessStack[1] = signDesc.WitnessScript
23762391
witnessStack[2] = ctrlBlockBytes
2377-
if err != nil {
2378-
return nil, err
2379-
}
23802392

23812393
return witnessStack, nil
23822394
}
@@ -2858,8 +2870,8 @@ type AnchorScriptTree struct {
28582870

28592871
// NewAnchorScriptTree makes a new script tree for an anchor output with the
28602872
// passed anchor key.
2861-
func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
2862-
) (*AnchorScriptTree, error) {
2873+
func NewAnchorScriptTree(
2874+
anchorKey *btcec.PublicKey) (*AnchorScriptTree, error) {
28632875

28642876
// The main script used is just a OP_16 CSV (anyone can sweep after 16
28652877
// blocks).
@@ -2895,18 +2907,18 @@ func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
28952907
}, nil
28962908
}
28972909

2898-
// WitnessScript returns the witness script that we'll use when signing for the
2899-
// remote party, and also verifying signatures on our transactions. As an
2900-
// example, when we create an outgoing HTLC for the remote party, we want to
2910+
// WitnessScriptToSign returns the witness script that we'll use when signing
2911+
// for the remote party, and also verifying signatures on our transactions. As
2912+
// an example, when we create an outgoing HTLC for the remote party, we want to
29012913
// sign their success path.
29022914
func (a *AnchorScriptTree) WitnessScriptToSign() []byte {
29032915
return a.SweepLeaf.Script
29042916
}
29052917

29062918
// WitnessScriptForPath returns the witness script for the given spending path.
29072919
// An error is returned if the path is unknown.
2908-
func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
2909-
) ([]byte, error) {
2920+
func (a *AnchorScriptTree) WitnessScriptForPath(
2921+
path ScriptPath) ([]byte, error) {
29102922

29112923
switch path {
29122924
case ScriptPathDelay:
@@ -2921,8 +2933,8 @@ func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
29212933

29222934
// CtrlBlockForPath returns the control block for the given spending path. For
29232935
// script types that don't have a control block, nil is returned.
2924-
func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
2925-
) (*txscript.ControlBlock, error) {
2936+
func (a *AnchorScriptTree) CtrlBlockForPath(
2937+
path ScriptPath) (*txscript.ControlBlock, error) {
29262938

29272939
switch path {
29282940
case ScriptPathDelay:
@@ -2938,6 +2950,11 @@ func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
29382950
}
29392951
}
29402952

2953+
// Tree returns the underlying ScriptTree of the AnchorScriptTree.
2954+
func (a *AnchorScriptTree) Tree() ScriptTree {
2955+
return a.ScriptTree
2956+
}
2957+
29412958
// A compile time check to ensure AnchorScriptTree implements the
29422959
// TapscriptDescriptor interface.
29432960
var _ TapscriptDescriptor = (*AnchorScriptTree)(nil)

lnwallet/commitment.go

Lines changed: 14 additions & 17 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
}
@@ -292,8 +292,7 @@ func CommitScriptToSelf(chanType channeldb.ChannelType, initiator bool,
292292
// what must be satisfied in order to spend the output.
293293
func CommitScriptToRemote(chanType channeldb.ChannelType, initiator bool,
294294
remoteKey *btcec.PublicKey, leaseExpiry uint32,
295-
auxLeaf input.AuxTapLeaf,
296-
) (input.ScriptDescriptor, uint32, error) {
295+
auxLeaf input.AuxTapLeaf) (input.ScriptDescriptor, uint32, error) {
297296

298297
switch {
299298
// If we are not the initiator of a leased channel, then the remote
@@ -534,8 +533,8 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
534533
input.ScriptDescriptor, input.ScriptDescriptor, error) {
535534

536535
var (
537-
anchorScript func(key *btcec.PublicKey) (
538-
input.ScriptDescriptor, error)
536+
anchorScript func(
537+
key *btcec.PublicKey) (input.ScriptDescriptor, error)
539538

540539
keySelector func(*channeldb.ChannelConfig,
541540
bool) *btcec.PublicKey
@@ -546,12 +545,10 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
546545
// level key is now the (relative) local delay and remote public key,
547546
// since these are fully revealed once the commitment hits the chain.
548547
case chanType.IsTaproot():
549-
anchorScript = func(key *btcec.PublicKey,
550-
) (input.ScriptDescriptor, error) {
548+
anchorScript = func(
549+
key *btcec.PublicKey) (input.ScriptDescriptor, error) {
551550

552-
return input.NewAnchorScriptTree(
553-
key,
554-
)
551+
return input.NewAnchorScriptTree(key)
555552
}
556553

557554
keySelector = func(cfg *channeldb.ChannelConfig,
@@ -569,8 +566,8 @@ func CommitScriptAnchors(chanType channeldb.ChannelType,
569566
default:
570567
// For normal channels, we'll create a p2wsh script based on
571568
// the target key.
572-
anchorScript = func(key *btcec.PublicKey,
573-
) (input.ScriptDescriptor, error) {
569+
anchorScript = func(
570+
key *btcec.PublicKey) (input.ScriptDescriptor, error) {
574571

575572
script, err := input.CommitScriptAnchor(key)
576573
if err != nil {

0 commit comments

Comments
 (0)