< 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: 49_19945309242
Line coverage
0%
Covered lines: 0
Uncovered lines: 42
Coverable lines: 42
Total lines: 100
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
 049        return new CommitmentKeys(
 050            localPubKey, // localpubkey (for to_local output)
 051            localDelayedPubKey, // local_delayedpubkey (for to_local output with delay)
 052            revocationPubKey, // revocationpubkey (allows them to revoke our commitment)
 053            localHtlcPubKey, // local_htlcpubkey (for our HTLC outputs)
 054            remoteHtlcPubKey, // remote_htlcpubkey (for their HTLC outputs)
 055            perCommitmentPoint, // our per_commitment_point
 056            perCommitmentSecret // our per_commitment_secret
 057        );
 58    }
 59
 60    /// <inheritdoc />
 61    public CommitmentKeys DeriveRemoteCommitmentKeys(uint localChannelKeyIndex, ChannelBasepoints localBasepoints,
 62                                                     ChannelBasepoints remoteBasepoints,
 63                                                     CompactPubKey remotePerCommitmentPoint, ulong commitmentNumber)
 64    {
 65        // For their commitment transaction, we use their provided per-commitment point
 66        // they should provide this via commitment_signed or update messages
 67
 68        // For their remote commitment transaction:
 69        // - localpubkey (from their perspective) = their payment_basepoint + SHA256(their_per_commitment_point || their
 070        var theirLocalPubKey = _keyDerivationService.DerivePublicKey(
 071            remoteBasepoints.PaymentBasepoint, remotePerCommitmentPoint);
 72
 73        // - local_delayedpubkey (from their perspective) = their delayed_payment_basepoint + SHA256(their_per_commitmen
 074        var theirDelayedPubKey = _keyDerivationService.DerivePublicKey(
 075            remoteBasepoints.DelayedPaymentBasepoint, remotePerCommitmentPoint);
 76
 77        // - revocationpubkey = derived from our revocation_basepoint and their per_commitment_point
 78        // This allows us to revoke their commitment if they broadcast an old one
 079        var revocationPubKey = _keyDerivationService.DeriveRevocationPubKey(
 080            localBasepoints.RevocationBasepoint, remotePerCommitmentPoint);
 81
 82        // - local_htlcpubkey (from their perspective) = their htlc_basepoint + SHA256(their_per_commitment_point || the
 083        var theirHtlcPubKey = _keyDerivationService.DerivePublicKey(
 084            remoteBasepoints.HtlcBasepoint, remotePerCommitmentPoint);
 85
 86        // - remote_htlcpubkey (from their perspective) = our htlc_basepoint + SHA256(their_per_commitment_point || our_
 087        var ourHtlcPubKey = _keyDerivationService.DerivePublicKey(
 088            localBasepoints.HtlcBasepoint, remotePerCommitmentPoint);
 89
 090        return new CommitmentKeys(
 091            theirLocalPubKey, // localpubkey (from their perspective, for their to_local output)
 092            theirDelayedPubKey, // local_delayedpubkey (from their perspective)
 093            revocationPubKey, // revocationpubkey (allows us to revoke their commitment)
 094            theirHtlcPubKey, // local_htlcpubkey (from their perspective)
 095            ourHtlcPubKey, // remote_htlcpubkey (from their perspective)
 096            remotePerCommitmentPoint, // their per_commitment_point
 097            null // We don't have their secret
 098        );
 99    }
 100}