< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Services.CommitmentKeyDerivationService
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Services/CommitmentKeyDerivationService.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 44
Coverable lines: 44
Total lines: 106
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
DeriveLocalCommitmentKeys(...)100%210%
DeriveRemoteCommitmentKeys(...)100%210%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Services/CommitmentKeyDerivationService.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Bitcoin.Services;
 2
 3using Domain.Bitcoin.Interfaces;
 4using Domain.Channels.ValueObjects;
 5using Domain.Crypto.ValueObjects;
 6using Domain.Protocol.Interfaces;
 7
 8public class CommitmentKeyDerivationService : ICommitmentKeyDerivationService
 9{
 10    private readonly IKeyDerivationService _keyDerivationService;
 11    private readonly ILightningSigner _lightningSigner;
 12
 013    public CommitmentKeyDerivationService(IKeyDerivationService keyDerivationService, ILightningSigner lightningSigner)
 14    {
 015        _keyDerivationService = keyDerivationService;
 016        _lightningSigner = lightningSigner;
 017    }
 18
 19    /// <inheritdoc />
 20    public CommitmentKeys DeriveLocalCommitmentKeys(uint localChannelKeyIndex, ChannelBasepoints localBasepoints,
 21                                                    ChannelBasepoints remoteBasepoints, ulong commitmentNumber)
 22    {
 23        // Get our per-commitment point for this commitment number from the signer
 024        var perCommitmentPoint = _lightningSigner.GetPerCommitmentPoint(localChannelKeyIndex, commitmentNumber);
 25
 26        // Get our per-commitment secret from the signer
 027        var perCommitmentSecret = _lightningSigner.ReleasePerCommitmentSecret(localChannelKeyIndex, commitmentNumber);
 28
 29        // For our local commitment transaction:
 30        // - localpubkey = our payment_basepoint + SHA256(our_per_commitment_point || our_payment_basepoint) * G
 031        var localPubKey = _keyDerivationService.DerivePublicKey(localBasepoints.PaymentBasepoint, perCommitmentPoint);
 32
 33        // - local_delayedpubkey = our delayed_payment_basepoint + SHA256(our_per_commitment_point || our_delayed_paymen
 034        var localDelayedPubKey =
 035            _keyDerivationService.DerivePublicKey(localBasepoints.DelayedPaymentBasepoint, perCommitmentPoint);
 36
 37        // - local_htlcpubkey = our htlc_basepoint + SHA256(our_per_commitment_point || our_htlc_basepoint) * G
 038        var localHtlcPubKey = _keyDerivationService.DerivePublicKey(localBasepoints.HtlcBasepoint, perCommitmentPoint);
 39
 40        // - remote_htlcpubkey = their htlc_basepoint + SHA256(our_per_commitment_point || their_htlc_basepoint) * G
 041        var remoteHtlcPubKey =
 042            _keyDerivationService.DerivePublicKey(remoteBasepoints.HtlcBasepoint, perCommitmentPoint);
 43
 44        // - revocationpubkey = derived from their revocation_basepoint and our per_commitment_point
 45        // This allows them to revoke our commitment if we broadcast an old one
 046        var revocationPubKey =
 047            _keyDerivationService.DeriveRevocationPubKey(remoteBasepoints.RevocationBasepoint, perCommitmentPoint);
 48
 49        // - remotepubkey = simply their payment_basepoint (NOT derived!)
 050        var remotePubKey = remoteBasepoints.PaymentBasepoint;
 51
 052        return new CommitmentKeys(
 053            localPubKey, // localpubkey (for to_local output)
 054            localDelayedPubKey, // local_delayedpubkey (for to_local output with delay)
 055            revocationPubKey, // revocationpubkey (allows them to revoke our commitment)
 056            localHtlcPubKey, // local_htlcpubkey (for our HTLC outputs)
 057            remoteHtlcPubKey, // remote_htlcpubkey (for their HTLC outputs)
 058            perCommitmentPoint, // our per_commitment_point
 059            perCommitmentSecret // our per_commitment_secret
 060        );
 61    }
 62
 63    /// <inheritdoc />
 64    public CommitmentKeys DeriveRemoteCommitmentKeys(uint localChannelKeyIndex, ChannelBasepoints localBasepoints,
 65                                                     ChannelBasepoints remoteBasepoints,
 66                                                     CompactPubKey remotePerCommitmentPoint, ulong commitmentNumber)
 67    {
 68        // For their commitment transaction, we use their provided per-commitment point
 69        // This should be provided by them via commitment_signed or update messages
 70
 71        // For their remote commitment transaction:
 72        // - localpubkey (from their perspective) = their payment_basepoint + SHA256(their_per_commitment_point || their
 073        var theirLocalPubKey = _keyDerivationService.DerivePublicKey(
 074            remoteBasepoints.PaymentBasepoint, remotePerCommitmentPoint);
 75
 76        // - local_delayedpubkey (from their perspective) = their delayed_payment_basepoint + SHA256(their_per_commitmen
 077        var theirDelayedPubKey = _keyDerivationService.DerivePublicKey(
 078            remoteBasepoints.DelayedPaymentBasepoint, remotePerCommitmentPoint);
 79
 80        // - revocationpubkey = derived from our revocation_basepoint and their per_commitment_point
 81        // This allows us to revoke their commitment if they broadcast an old one
 082        var revocationPubKey = _keyDerivationService.DeriveRevocationPubKey(
 083            localBasepoints.RevocationBasepoint, remotePerCommitmentPoint);
 84
 85        // - local_htlcpubkey (from their perspective) = their htlc_basepoint + SHA256(their_per_commitment_point || the
 086        var theirHtlcPubKey = _keyDerivationService.DerivePublicKey(
 087            remoteBasepoints.HtlcBasepoint, remotePerCommitmentPoint);
 88
 89        // - remote_htlcpubkey (from their perspective) = our htlc_basepoint + SHA256(their_per_commitment_point || our_
 090        var ourHtlcPubKey = _keyDerivationService.DerivePublicKey(
 091            localBasepoints.HtlcBasepoint, remotePerCommitmentPoint);
 92
 93        // - remotepubkey (from their perspective) = simply our payment_basepoint (NOT derived!)
 094        var ourPubKey = localBasepoints.PaymentBasepoint;
 95
 096        return new CommitmentKeys(
 097            theirLocalPubKey, // localpubkey (from their perspective, for their to_local output)
 098            theirDelayedPubKey, // local_delayedpubkey (from their perspective)
 099            revocationPubKey, // revocationpubkey (allows us to revoke their commitment)
 0100            theirHtlcPubKey, // local_htlcpubkey (from their perspective)
 0101            ourHtlcPubKey, // remote_htlcpubkey (from their perspective)
 0102            remotePerCommitmentPoint, // their per_commitment_point
 0103            null // We don't have their secret
 0104        );
 105    }
 106}