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

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

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

88.89
/keychain/ecdh.go
1
package keychain
2

3
import (
4
        "crypto/sha256"
5

6
        "github.com/btcsuite/btcd/btcec/v2"
7
)
8

9
// NewPubKeyECDH wraps the given key of the key ring so it adheres to the
10
// SingleKeyECDH interface.
11
func NewPubKeyECDH(keyDesc KeyDescriptor, ecdh ECDHRing) *PubKeyECDH {
135✔
12
        return &PubKeyECDH{
135✔
13
                keyDesc: keyDesc,
135✔
14
                ecdh:    ecdh,
135✔
15
        }
135✔
16
}
135✔
17

18
// PubKeyECDH is an implementation of the SingleKeyECDH interface. It wraps an
19
// ECDH key ring so it can perform ECDH shared key generation against a single
20
// abstracted away private key.
21
type PubKeyECDH struct {
22
        keyDesc KeyDescriptor
23
        ecdh    ECDHRing
24
}
25

26
// PubKey returns the public key of the private key that is abstracted away by
27
// the interface.
28
//
29
// NOTE: This is part of the SingleKeyECDH interface.
30
func (p *PubKeyECDH) PubKey() *btcec.PublicKey {
514✔
31
        return p.keyDesc.PubKey
514✔
32
}
514✔
33

34
// ECDH performs a scalar multiplication (ECDH-like operation) between the
35
// abstracted private key and a remote public key. The output returned will be
36
// the sha256 of the resulting shared point serialized in compressed format. If
37
// k is our private key, and P is the public key, we perform the following
38
// operation:
39
//
40
//        sx := k*P
41
//        s := sha256(sx.SerializeCompressed())
42
//
43
// NOTE: This is part of the SingleKeyECDH interface.
44
func (p *PubKeyECDH) ECDH(pubKey *btcec.PublicKey) ([32]byte, error) {
×
45
        return p.ecdh.ECDH(p.keyDesc, pubKey)
×
46
}
×
47

48
// PrivKeyECDH is an implementation of the SingleKeyECDH in which we do have the
49
// full private key. This can be used to wrap a temporary key to conform to the
50
// SingleKeyECDH interface.
51
type PrivKeyECDH struct {
52
        // PrivKey is the private key that is used for the ECDH operation.
53
        PrivKey *btcec.PrivateKey
54
}
55

56
// PubKey returns the public key of the private key that is abstracted away by
57
// the interface.
58
//
59
// NOTE: This is part of the SingleKeyECDH interface.
60
func (p *PrivKeyECDH) PubKey() *btcec.PublicKey {
768✔
61
        return p.PrivKey.PubKey()
768✔
62
}
768✔
63

64
// ECDH performs a scalar multiplication (ECDH-like operation) between the
65
// abstracted private key and a remote public key. The output returned will be
66
// the sha256 of the resulting shared point serialized in compressed format. If
67
// k is our private key, and P is the public key, we perform the following
68
// operation:
69
//
70
//        sx := k*P
71
//        s := sha256(sx.SerializeCompressed())
72
//
73
// NOTE: This is part of the SingleKeyECDH interface.
74
func (p *PrivKeyECDH) ECDH(pub *btcec.PublicKey) ([32]byte, error) {
2,027✔
75
        var (
2,027✔
76
                pubJacobian btcec.JacobianPoint
2,027✔
77
                s           btcec.JacobianPoint
2,027✔
78
        )
2,027✔
79
        pub.AsJacobian(&pubJacobian)
2,027✔
80

2,027✔
81
        btcec.ScalarMultNonConst(&p.PrivKey.Key, &pubJacobian, &s)
2,027✔
82
        s.ToAffine()
2,027✔
83
        sPubKey := btcec.NewPublicKey(&s.X, &s.Y)
2,027✔
84
        return sha256.Sum256(sPubKey.SerializeCompressed()), nil
2,027✔
85
}
2,027✔
86

87
var _ SingleKeyECDH = (*PubKeyECDH)(nil)
88
var _ SingleKeyECDH = (*PrivKeyECDH)(nil)
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