Skip to content

Commit 66366a0

Browse files
committed
lnwallet: add Tree() method, fix formatting
1 parent 609e359 commit 66366a0

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

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

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

751+
// Tree returns the underlying ScriptTree of the HtlcScriptTree.
752+
func (h *HtlcScriptTree) Tree() ScriptTree {
753+
return h.ScriptTree
754+
}
755+
751756
// A compile time check to ensure HtlcScriptTree implements the
752757
// TapscriptMultiplexer interface.
753758
var _ TapscriptDescriptor = (*HtlcScriptTree)(nil)
@@ -1748,18 +1753,18 @@ func TaprootSecondLevelScriptTree(revokeKey, delayKey *btcec.PublicKey,
17481753
}, nil
17491754
}
17501755

1751-
// WitnessScript returns the witness script that we'll use when signing for the
1752-
// remote party, and also verifying signatures on our transactions. As an
1753-
// example, when we create an outgoing HTLC for the remote party, we want to
1756+
// WitnessScriptToSign returns the witness script that we'll use when signing
1757+
// for the remote party, and also verifying signatures on our transactions. As
1758+
// an example, when we create an outgoing HTLC for the remote party, we want to
17541759
// sign their success path.
17551760
func (s *SecondLevelScriptTree) WitnessScriptToSign() []byte {
17561761
return s.SuccessTapLeaf.Script
17571762
}
17581763

17591764
// WitnessScriptForPath returns the witness script for the given spending path.
17601765
// An error is returned if the path is unknown.
1761-
func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
1762-
) ([]byte, error) {
1766+
func (s *SecondLevelScriptTree) WitnessScriptForPath(
1767+
path ScriptPath) ([]byte, error) {
17631768

17641769
switch path {
17651770
case ScriptPathDelay:
@@ -1774,8 +1779,8 @@ func (s *SecondLevelScriptTree) WitnessScriptForPath(path ScriptPath,
17741779

17751780
// CtrlBlockForPath returns the control block for the given spending path. For
17761781
// script types that don't have a control block, nil is returned.
1777-
func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
1778-
) (*txscript.ControlBlock, error) {
1782+
func (s *SecondLevelScriptTree) CtrlBlockForPath(
1783+
path ScriptPath) (*txscript.ControlBlock, error) {
17791784

17801785
switch path {
17811786
case ScriptPathDelay:
@@ -1791,6 +1796,11 @@ func (s *SecondLevelScriptTree) CtrlBlockForPath(path ScriptPath,
17911796
}
17921797
}
17931798

1799+
// Tree returns the underlying ScriptTree of the SecondLevelScriptTree.
1800+
func (s *SecondLevelScriptTree) Tree() ScriptTree {
1801+
return s.ScriptTree
1802+
}
1803+
17941804
// A compile time check to ensure SecondLevelScriptTree implements the
17951805
// TapscriptDescriptor interface.
17961806
var _ TapscriptDescriptor = (*SecondLevelScriptTree)(nil)
@@ -2133,9 +2143,9 @@ type CommitScriptTree struct {
21332143
// TapscriptDescriptor interface.
21342144
var _ TapscriptDescriptor = (*CommitScriptTree)(nil)
21352145

2136-
// WitnessScript returns the witness script that we'll use when signing for the
2137-
// remote party, and also verifying signatures on our transactions. As an
2138-
// example, when we create an outgoing HTLC for the remote party, we want to
2146+
// WitnessScriptToSign returns the witness script that we'll use when signing
2147+
// for the remote party, and also verifying signatures on our transactions. As
2148+
// an example, when we create an outgoing HTLC for the remote party, we want to
21392149
// sign their success path.
21402150
func (c *CommitScriptTree) WitnessScriptToSign() []byte {
21412151
// TODO(roasbeef): abstraction leak here? always dependent
@@ -2144,8 +2154,8 @@ func (c *CommitScriptTree) WitnessScriptToSign() []byte {
21442154

21452155
// WitnessScriptForPath returns the witness script for the given spending path.
21462156
// An error is returned if the path is unknown.
2147-
func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
2148-
) ([]byte, error) {
2157+
func (c *CommitScriptTree) WitnessScriptForPath(
2158+
path ScriptPath) ([]byte, error) {
21492159

21502160
switch path {
21512161
// For the commitment output, the delay and success path are the same,
@@ -2163,8 +2173,8 @@ func (c *CommitScriptTree) WitnessScriptForPath(path ScriptPath,
21632173

21642174
// CtrlBlockForPath returns the control block for the given spending path. For
21652175
// script types that don't have a control block, nil is returned.
2166-
func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
2167-
) (*txscript.ControlBlock, error) {
2176+
func (c *CommitScriptTree) CtrlBlockForPath(
2177+
path ScriptPath) (*txscript.ControlBlock, error) {
21682178

21692179
switch path {
21702180
case ScriptPathDelay:
@@ -2184,6 +2194,11 @@ func (c *CommitScriptTree) CtrlBlockForPath(path ScriptPath,
21842194
}
21852195
}
21862196

2197+
// Tree returns the underlying ScriptTree of the CommitScriptTree.
2198+
func (c *CommitScriptTree) Tree() ScriptTree {
2199+
return c.ScriptTree
2200+
}
2201+
21872202
// NewLocalCommitScriptTree returns a new CommitScript tree that can be used to
21882203
// create and spend the commitment output for the local party.
21892204
func NewLocalCommitScriptTree(csvTimeout uint32, selfKey,
@@ -2311,7 +2326,7 @@ func TaprootCommitScriptToSelf(csvTimeout uint32,
23112326
return commitScriptTree.TaprootKey, nil
23122327
}
23132328

2314-
// MakeTaprootSCtrlBlock takes a leaf script, the internal key (usually the
2329+
// MakeTaprootCtrlBlock takes a leaf script, the internal key (usually the
23152330
// revoke key), and a script tree and creates a valid control block for a spend
23162331
// of the leaf.
23172332
func MakeTaprootCtrlBlock(leafScript []byte, internalKey *btcec.PublicKey,
@@ -2366,9 +2381,6 @@ func TaprootCommitSpendSuccess(signer Signer, signDesc *SignDescriptor,
23662381
witnessStack[0] = maybeAppendSighash(sweepSig, signDesc.HashType)
23672382
witnessStack[1] = signDesc.WitnessScript
23682383
witnessStack[2] = ctrlBlockBytes
2369-
if err != nil {
2370-
return nil, err
2371-
}
23722384

23732385
return witnessStack, nil
23742386
}
@@ -2850,8 +2862,8 @@ type AnchorScriptTree struct {
28502862

28512863
// NewAnchorScriptTree makes a new script tree for an anchor output with the
28522864
// passed anchor key.
2853-
func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
2854-
) (*AnchorScriptTree, error) {
2865+
func NewAnchorScriptTree(
2866+
anchorKey *btcec.PublicKey) (*AnchorScriptTree, error) {
28552867

28562868
// The main script used is just a OP_16 CSV (anyone can sweep after 16
28572869
// blocks).
@@ -2887,18 +2899,18 @@ func NewAnchorScriptTree(anchorKey *btcec.PublicKey,
28872899
}, nil
28882900
}
28892901

2890-
// WitnessScript returns the witness script that we'll use when signing for the
2891-
// remote party, and also verifying signatures on our transactions. As an
2892-
// example, when we create an outgoing HTLC for the remote party, we want to
2902+
// WitnessScriptToSign returns the witness script that we'll use when signing
2903+
// for the remote party, and also verifying signatures on our transactions. As
2904+
// an example, when we create an outgoing HTLC for the remote party, we want to
28932905
// sign their success path.
28942906
func (a *AnchorScriptTree) WitnessScriptToSign() []byte {
28952907
return a.SweepLeaf.Script
28962908
}
28972909

28982910
// WitnessScriptForPath returns the witness script for the given spending path.
28992911
// An error is returned if the path is unknown.
2900-
func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
2901-
) ([]byte, error) {
2912+
func (a *AnchorScriptTree) WitnessScriptForPath(
2913+
path ScriptPath) ([]byte, error) {
29022914

29032915
switch path {
29042916
case ScriptPathDelay:
@@ -2913,8 +2925,8 @@ func (a *AnchorScriptTree) WitnessScriptForPath(path ScriptPath,
29132925

29142926
// CtrlBlockForPath returns the control block for the given spending path. For
29152927
// script types that don't have a control block, nil is returned.
2916-
func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
2917-
) (*txscript.ControlBlock, error) {
2928+
func (a *AnchorScriptTree) CtrlBlockForPath(
2929+
path ScriptPath) (*txscript.ControlBlock, error) {
29182930

29192931
switch path {
29202932
case ScriptPathDelay:
@@ -2930,6 +2942,11 @@ func (a *AnchorScriptTree) CtrlBlockForPath(path ScriptPath,
29302942
}
29312943
}
29322944

2945+
// Tree returns the underlying ScriptTree of the AnchorScriptTree.
2946+
func (a *AnchorScriptTree) Tree() ScriptTree {
2947+
return a.ScriptTree
2948+
}
2949+
29332950
// A compile time check to ensure AnchorScriptTree implements the
29342951
// TapscriptDescriptor interface.
29352952
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)