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

lightningnetwork / lnd / 9935147745

15 Jul 2024 07:07AM UTC coverage: 49.819% (+0.6%) from 49.268%
9935147745

Pull #8900

github

guggero
Makefile: add GOCC variable
Pull Request #8900: Makefile: add GOCC variable

93876 of 188433 relevant lines covered (49.82%)

2.07 hits per line

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

93.26
/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
)
12

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

24
// NewTxSigHashesV0Only returns a new txscript.TxSigHashes instance that will
25
// only calculate the sighash midstate values for segwit v0 inputs and can
26
// therefore never be used for transactions that want to spend segwit v1
27
// (taproot) inputs.
28
func NewTxSigHashesV0Only(tx *wire.MsgTx) *txscript.TxSigHashes {
4✔
29
        // The canned output fetcher returns a wire.TxOut instance with the
4✔
30
        // given pk script and amount. We can get away with nil since the first
4✔
31
        // thing the TxSigHashes constructor checks is the length of the pk
4✔
32
        // script and whether it matches taproot output script length. If the
4✔
33
        // length doesn't match it assumes v0 inputs only.
4✔
34
        nilFetcher := txscript.NewCannedPrevOutputFetcher(nil, 0)
4✔
35
        return txscript.NewTxSigHashes(tx, nilFetcher)
4✔
36
}
4✔
37

38
// MultiPrevOutFetcher returns a txscript.MultiPrevOutFetcher for the given set
39
// of inputs.
40
func MultiPrevOutFetcher(inputs []Input) (*txscript.MultiPrevOutFetcher, error) {
4✔
41
        fetcher := txscript.NewMultiPrevOutFetcher(nil)
4✔
42
        for _, inp := range inputs {
8✔
43
                op := inp.OutPoint()
4✔
44
                desc := inp.SignDesc()
4✔
45

4✔
46
                if op == EmptyOutPoint {
4✔
47
                        return nil, fmt.Errorf("missing input outpoint")
×
48
                }
×
49

50
                if desc == nil || desc.Output == nil {
4✔
51
                        return nil, fmt.Errorf("missing input utxo information")
×
52
                }
×
53

54
                fetcher.AddPrevOut(op, desc.Output)
4✔
55
        }
56

57
        return fetcher, nil
4✔
58
}
59

60
// TapscriptFullTree creates a waddrmgr.Tapscript for the given internal key and
61
// tree leaves.
62
func TapscriptFullTree(internalKey *btcec.PublicKey,
63
        allTreeLeaves ...txscript.TapLeaf) *waddrmgr.Tapscript {
4✔
64

4✔
65
        tree := txscript.AssembleTaprootScriptTree(allTreeLeaves...)
4✔
66
        rootHash := tree.RootNode.TapHash()
4✔
67
        tapKey := txscript.ComputeTaprootOutputKey(internalKey, rootHash[:])
4✔
68

4✔
69
        var outputKeyYIsOdd bool
4✔
70
        if tapKey.SerializeCompressed()[0] == PubKeyFormatCompressedOdd {
6✔
71
                outputKeyYIsOdd = true
2✔
72
        }
2✔
73

74
        return &waddrmgr.Tapscript{
4✔
75
                Type: waddrmgr.TapscriptTypeFullTree,
4✔
76
                ControlBlock: &txscript.ControlBlock{
4✔
77
                        InternalKey:     internalKey,
4✔
78
                        OutputKeyYIsOdd: outputKeyYIsOdd,
4✔
79
                        LeafVersion:     txscript.BaseLeafVersion,
4✔
80
                },
4✔
81
                Leaves: allTreeLeaves,
4✔
82
        }
4✔
83
}
84

85
// TapscriptPartialReveal creates a waddrmgr.Tapscript for the given internal
86
// key and revealed script.
87
func TapscriptPartialReveal(internalKey *btcec.PublicKey,
88
        revealedLeaf txscript.TapLeaf,
89
        inclusionProof []byte) *waddrmgr.Tapscript {
4✔
90

4✔
91
        controlBlock := &txscript.ControlBlock{
4✔
92
                InternalKey:    internalKey,
4✔
93
                LeafVersion:    txscript.BaseLeafVersion,
4✔
94
                InclusionProof: inclusionProof,
4✔
95
        }
4✔
96
        rootHash := controlBlock.RootHash(revealedLeaf.Script)
4✔
97
        tapKey := txscript.ComputeTaprootOutputKey(internalKey, rootHash)
4✔
98

4✔
99
        if tapKey.SerializeCompressed()[0] == PubKeyFormatCompressedOdd {
5✔
100
                controlBlock.OutputKeyYIsOdd = true
1✔
101
        }
1✔
102

103
        return &waddrmgr.Tapscript{
4✔
104
                Type:           waddrmgr.TapscriptTypePartialReveal,
4✔
105
                ControlBlock:   controlBlock,
4✔
106
                RevealedScript: revealedLeaf.Script,
4✔
107
        }
4✔
108
}
109

110
// TapscriptRootHashOnly creates a waddrmgr.Tapscript for the given internal key
111
// and root hash.
112
func TapscriptRootHashOnly(internalKey *btcec.PublicKey,
113
        rootHash []byte) *waddrmgr.Tapscript {
4✔
114

4✔
115
        controlBlock := &txscript.ControlBlock{
4✔
116
                InternalKey: internalKey,
4✔
117
        }
4✔
118

4✔
119
        tapKey := txscript.ComputeTaprootOutputKey(internalKey, rootHash)
4✔
120
        if tapKey.SerializeCompressed()[0] == PubKeyFormatCompressedOdd {
4✔
121
                controlBlock.OutputKeyYIsOdd = true
×
122
        }
×
123

124
        return &waddrmgr.Tapscript{
4✔
125
                Type:         waddrmgr.TaprootKeySpendRootHash,
4✔
126
                ControlBlock: controlBlock,
4✔
127
                RootHash:     rootHash,
4✔
128
        }
4✔
129
}
130

131
// TapscriptFullKeyOnly creates a waddrmgr.Tapscript for the given full Taproot
132
// key.
133
func TapscriptFullKeyOnly(taprootKey *btcec.PublicKey) *waddrmgr.Tapscript {
4✔
134
        return &waddrmgr.Tapscript{
4✔
135
                Type:          waddrmgr.TaprootFullKeyOnly,
4✔
136
                FullOutputKey: taprootKey,
4✔
137
        }
4✔
138
}
4✔
139

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

4✔
146
        builder.AddOp(txscript.OP_1)
4✔
147
        builder.AddData(schnorr.SerializePubKey(taprootKey))
4✔
148

4✔
149
        return builder.Script()
4✔
150
}
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