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

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

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

83.33
/lntypes/preimage.go
1
package lntypes
2

3
import (
4
        "crypto/sha256"
5
        "encoding/hex"
6
        "fmt"
7
)
8

9
// PreimageSize of array used to store preimagees.
10
const PreimageSize = 32
11

12
// Preimage is used in several of the lightning messages and common structures.
13
// It represents a payment preimage.
14
type Preimage [PreimageSize]byte
15

16
// String returns the Preimage as a hexadecimal string.
17
func (p Preimage) String() string {
18✔
18
        return hex.EncodeToString(p[:])
18✔
19
}
18✔
20

21
// MakePreimage returns a new Preimage from a bytes slice. An error is returned
22
// if the number of bytes passed in is not PreimageSize.
23
func MakePreimage(newPreimage []byte) (Preimage, error) {
96,204✔
24
        nhlen := len(newPreimage)
96,204✔
25
        if nhlen != PreimageSize {
96,207✔
26
                return Preimage{}, fmt.Errorf("invalid preimage length of %v, "+
3✔
27
                        "want %v", nhlen, PreimageSize)
3✔
28
        }
3✔
29

30
        var preimage Preimage
96,201✔
31
        copy(preimage[:], newPreimage)
96,201✔
32

96,201✔
33
        return preimage, nil
96,201✔
34
}
35

36
// MakePreimageFromStr creates a Preimage from a hex preimage string.
37
func MakePreimageFromStr(newPreimage string) (Preimage, error) {
154✔
38
        // Return error if preimage string is of incorrect length.
154✔
39
        if len(newPreimage) != PreimageSize*2 {
154✔
40
                return Preimage{}, fmt.Errorf("invalid preimage string length "+
×
41
                        "of %v, want %v", len(newPreimage), PreimageSize*2)
×
42
        }
×
43

44
        preimage, err := hex.DecodeString(newPreimage)
154✔
45
        if err != nil {
154✔
46
                return Preimage{}, err
×
47
        }
×
48

49
        return MakePreimage(preimage)
154✔
50
}
51

52
// Hash returns the sha256 hash of the preimage.
53
func (p *Preimage) Hash() Hash {
4,038✔
54
        return Hash(sha256.Sum256(p[:]))
4,038✔
55
}
4,038✔
56

57
// Matches returns whether this preimage is the preimage of the given hash.
58
func (p *Preimage) Matches(h Hash) bool {
90✔
59
        return h == p.Hash()
90✔
60
}
90✔
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