• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12292179806

12 Dec 2024 08:00AM UTC coverage: 49.544% (+0.004%) from 49.54%
12292179806

Pull #9342

github

ellemouton
docs: add release notes entry
Pull Request #9342: protofsm: update GR Manager usage and start using structured logging

0 of 62 new or added lines in 2 files covered. (0.0%)

78 existing lines in 14 files now uncovered.

100377 of 202602 relevant lines covered (49.54%)

2.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.22
/input/taproot.go
1
package input
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btcd/btcec/v2"
7
        "github.com/btcsuite/btcd/btcec/v2/schnorr"
8
        "github.com/btcsuite/btcd/txscript"
9
        "github.com/btcsuite/btcd/wire"
10
        "github.com/btcsuite/btcwallet/waddrmgr"
11
        "github.com/lightningnetwork/lnd/fn/v2"
12
)
13

14
const (
15
        // PubKeyFormatCompressedOdd is the identifier prefix byte for a public
16
        // key whose Y coordinate is odd when serialized in the compressed
17
        // format per section 2.3.4 of
18
        // [SEC1](https://secg.org/sec1-v2.pdf#subsubsection.2.3.4).
19
        // This is copied from the github.com/decred/dcrd/dcrec/secp256k1/v4 to
20
        // avoid needing to directly reference (and by accident pull in
21
        // incompatible crypto primitives) the package.
22
        PubKeyFormatCompressedOdd byte = 0x03
23
)
24

25
// AuxTapLeaf is a type alias for an optional tapscript leaf that may be added
26
// to the tapscript tree of HTLC and commitment outputs.
27
type AuxTapLeaf = fn.Option[txscript.TapLeaf]
28

29
// NoneTapLeaf returns an empty optional tapscript leaf.
30
func NoneTapLeaf() AuxTapLeaf {
×
31
        return fn.None[txscript.TapLeaf]()
×
32
}
×
33

34
// HtlcIndex represents the monotonically increasing counter that is used to
35
// identify HTLCs created by a peer.
36
type HtlcIndex = uint64
37

38
// HtlcAuxLeaf is a type that represents an auxiliary leaf for an HTLC output.
39
// An HTLC may have up to two aux leaves: one for the output on the commitment
40
// transaction, and one for the second level HTLC.
41
type HtlcAuxLeaf struct {
42
        AuxTapLeaf
43

44
        // SecondLevelLeaf is the auxiliary leaf for the second level HTLC
45
        // success or timeout transaction.
46
        SecondLevelLeaf AuxTapLeaf
47
}
48

49
// HtlcAuxLeaves is a type alias for a map of optional tapscript leaves.
50
type HtlcAuxLeaves = map[HtlcIndex]HtlcAuxLeaf
51

52
// NewTxSigHashesV0Only returns a new txscript.TxSigHashes instance that will
53
// only calculate the sighash midstate values for segwit v0 inputs and can
54
// therefore never be used for transactions that want to spend segwit v1
55
// (taproot) inputs.
56
func NewTxSigHashesV0Only(tx *wire.MsgTx) *txscript.TxSigHashes {
4✔
57
        // The canned output fetcher returns a wire.TxOut instance with the
4✔
58
        // given pk script and amount. We can get away with nil since the first
4✔
59
        // thing the TxSigHashes constructor checks is the length of the pk
4✔
60
        // script and whether it matches taproot output script length. If the
4✔
61
        // length doesn't match it assumes v0 inputs only.
4✔
62
        nilFetcher := txscript.NewCannedPrevOutputFetcher(nil, 0)
4✔
63
        return txscript.NewTxSigHashes(tx, nilFetcher)
4✔
64
}
4✔
65

66
// MultiPrevOutFetcher returns a txscript.MultiPrevOutFetcher for the given set
67
// of inputs.
68
func MultiPrevOutFetcher(inputs []Input) (*txscript.MultiPrevOutFetcher, error) {
4✔
69
        fetcher := txscript.NewMultiPrevOutFetcher(nil)
4✔
70
        for _, inp := range inputs {
8✔
71
                op := inp.OutPoint()
4✔
72
                desc := inp.SignDesc()
4✔
73

4✔
74
                if op == EmptyOutPoint {
4✔
75
                        return nil, fmt.Errorf("missing input outpoint")
×
76
                }
×
77

78
                if desc == nil || desc.Output == nil {
4✔
79
                        return nil, fmt.Errorf("missing input utxo information")
×
80
                }
×
81

82
                fetcher.AddPrevOut(op, desc.Output)
4✔
83
        }
84

85
        return fetcher, nil
4✔
86
}
87

88
// TapscriptFullTree creates a waddrmgr.Tapscript for the given internal key and
89
// tree leaves.
90
func TapscriptFullTree(internalKey *btcec.PublicKey,
91
        allTreeLeaves ...txscript.TapLeaf) *waddrmgr.Tapscript {
4✔
92

4✔
93
        tree := txscript.AssembleTaprootScriptTree(allTreeLeaves...)
4✔
94
        rootHash := tree.RootNode.TapHash()
4✔
95
        tapKey := txscript.ComputeTaprootOutputKey(internalKey, rootHash[:])
4✔
96

4✔
97
        var outputKeyYIsOdd bool
4✔
98
        if tapKey.SerializeCompressed()[0] == PubKeyFormatCompressedOdd {
4✔
UNCOV
99
                outputKeyYIsOdd = true
×
UNCOV
100
        }
×
101

102
        return &waddrmgr.Tapscript{
4✔
103
                Type: waddrmgr.TapscriptTypeFullTree,
4✔
104
                ControlBlock: &txscript.ControlBlock{
4✔
105
                        InternalKey:     internalKey,
4✔
106
                        OutputKeyYIsOdd: outputKeyYIsOdd,
4✔
107
                        LeafVersion:     txscript.BaseLeafVersion,
4✔
108
                },
4✔
109
                Leaves: allTreeLeaves,
4✔
110
        }
4✔
111
}
112

113
// TapscriptPartialReveal creates a waddrmgr.Tapscript for the given internal
114
// key and revealed script.
115
func TapscriptPartialReveal(internalKey *btcec.PublicKey,
116
        revealedLeaf txscript.TapLeaf,
117
        inclusionProof []byte) *waddrmgr.Tapscript {
4✔
118

4✔
119
        controlBlock := &txscript.ControlBlock{
4✔
120
                InternalKey:    internalKey,
4✔
121
                LeafVersion:    txscript.BaseLeafVersion,
4✔
122
                InclusionProof: inclusionProof,
4✔
123
        }
4✔
124
        rootHash := controlBlock.RootHash(revealedLeaf.Script)
4✔
125
        tapKey := txscript.ComputeTaprootOutputKey(internalKey, rootHash)
4✔
126

4✔
127
        if tapKey.SerializeCompressed()[0] == PubKeyFormatCompressedOdd {
5✔
128
                controlBlock.OutputKeyYIsOdd = true
1✔
129
        }
1✔
130

131
        return &waddrmgr.Tapscript{
4✔
132
                Type:           waddrmgr.TapscriptTypePartialReveal,
4✔
133
                ControlBlock:   controlBlock,
4✔
134
                RevealedScript: revealedLeaf.Script,
4✔
135
        }
4✔
136
}
137

138
// TapscriptRootHashOnly creates a waddrmgr.Tapscript for the given internal key
139
// and root hash.
140
func TapscriptRootHashOnly(internalKey *btcec.PublicKey,
141
        rootHash []byte) *waddrmgr.Tapscript {
4✔
142

4✔
143
        controlBlock := &txscript.ControlBlock{
4✔
144
                InternalKey: internalKey,
4✔
145
        }
4✔
146

4✔
147
        tapKey := txscript.ComputeTaprootOutputKey(internalKey, rootHash)
4✔
148
        if tapKey.SerializeCompressed()[0] == PubKeyFormatCompressedOdd {
6✔
149
                controlBlock.OutputKeyYIsOdd = true
2✔
150
        }
2✔
151

152
        return &waddrmgr.Tapscript{
4✔
153
                Type:         waddrmgr.TaprootKeySpendRootHash,
4✔
154
                ControlBlock: controlBlock,
4✔
155
                RootHash:     rootHash,
4✔
156
        }
4✔
157
}
158

159
// TapscriptFullKeyOnly creates a waddrmgr.Tapscript for the given full Taproot
160
// key.
161
func TapscriptFullKeyOnly(taprootKey *btcec.PublicKey) *waddrmgr.Tapscript {
4✔
162
        return &waddrmgr.Tapscript{
4✔
163
                Type:          waddrmgr.TaprootFullKeyOnly,
4✔
164
                FullOutputKey: taprootKey,
4✔
165
        }
4✔
166
}
4✔
167

168
// PayToTaprootScript creates a new script to pay to a version 1 (taproot)
169
// witness program. The passed public key will be serialized as an x-only key
170
// to create the witness program.
171
func PayToTaprootScript(taprootKey *btcec.PublicKey) ([]byte, error) {
4✔
172
        builder := txscript.NewScriptBuilder()
4✔
173

4✔
174
        builder.AddOp(txscript.OP_1)
4✔
175
        builder.AddData(schnorr.SerializePubKey(taprootKey))
4✔
176

4✔
177
        return builder.Script()
4✔
178
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc