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

lightningnetwork / lnd / 12375116696

17 Dec 2024 02:29PM UTC coverage: 58.366% (-0.2%) from 58.595%
12375116696

Pull #8777

github

ziggie1984
docs: add release-notes
Pull Request #8777: multi: make deletion of edge atomic.

132 of 177 new or added lines in 6 files covered. (74.58%)

670 existing lines in 37 files now uncovered.

133926 of 229458 relevant lines covered (58.37%)

19223.6 hits per line

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

93.48
/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 {
19✔
31
        return fn.None[txscript.TapLeaf]()
19✔
32
}
19✔
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 {
6,387✔
57
        // The canned output fetcher returns a wire.TxOut instance with the
6,387✔
58
        // given pk script and amount. We can get away with nil since the first
6,387✔
59
        // thing the TxSigHashes constructor checks is the length of the pk
6,387✔
60
        // script and whether it matches taproot output script length. If the
6,387✔
61
        // length doesn't match it assumes v0 inputs only.
6,387✔
62
        nilFetcher := txscript.NewCannedPrevOutputFetcher(nil, 0)
6,387✔
63
        return txscript.NewTxSigHashes(tx, nilFetcher)
6,387✔
64
}
6,387✔
65

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

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

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

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

85
        return fetcher, nil
63✔
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 {
2✔
92

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

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

102
        return &waddrmgr.Tapscript{
2✔
103
                Type: waddrmgr.TapscriptTypeFullTree,
2✔
104
                ControlBlock: &txscript.ControlBlock{
2✔
105
                        InternalKey:     internalKey,
2✔
106
                        OutputKeyYIsOdd: outputKeyYIsOdd,
2✔
107
                        LeafVersion:     txscript.BaseLeafVersion,
2✔
108
                },
2✔
109
                Leaves: allTreeLeaves,
2✔
110
        }
2✔
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 {
2✔
118

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

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

131
        return &waddrmgr.Tapscript{
2✔
132
                Type:           waddrmgr.TapscriptTypePartialReveal,
2✔
133
                ControlBlock:   controlBlock,
2✔
134
                RevealedScript: revealedLeaf.Script,
2✔
135
        }
2✔
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 {
1✔
142

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

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

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

159
// TapscriptFullKeyOnly creates a waddrmgr.Tapscript for the given full Taproot
160
// key.
161
func TapscriptFullKeyOnly(taprootKey *btcec.PublicKey) *waddrmgr.Tapscript {
1✔
162
        return &waddrmgr.Tapscript{
1✔
163
                Type:          waddrmgr.TaprootFullKeyOnly,
1✔
164
                FullOutputKey: taprootKey,
1✔
165
        }
1✔
166
}
1✔
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) {
2,847✔
172
        builder := txscript.NewScriptBuilder()
2,847✔
173

2,847✔
174
        builder.AddOp(txscript.OP_1)
2,847✔
175
        builder.AddData(schnorr.SerializePubKey(taprootKey))
2,847✔
176

2,847✔
177
        return builder.Script()
2,847✔
178
}
2,847✔
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