< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Crypto.Functions.Ecdh
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Crypto/Functions/Ecdh.cs
Tag: 30_15166811759
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 47
Line coverage: 100%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SecP256K1Dh(...)100%11100%
GenerateKeyPair()100%11100%
GenerateKeyPair(...)50%22100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Crypto/Functions/Ecdh.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Crypto.Functions;
 2
 3using Domain.Crypto.Constants;
 4using Hashes;
 5using Interfaces;
 6using Primitives;
 7
 8/// <summary>
 9/// The SecP256k1 DH function (
 10/// <see href="https://github.com/lightning/bolts/blob/master/08-transport.md#handshake-state">Bolt 8 - Handshake State<
 11/// </summary>
 12internal sealed class Ecdh : IEcdh
 13{
 14    /// <inheritdoc/>
 15    /// <param name="k">Private Key</param>
 16    /// <param name="rk">Remote Static PubKey</param>
 17    /// <param name="sharedKey"></param>
 18    public void SecP256K1Dh(NBitcoin.Key k, ReadOnlySpan<byte> rk, Span<byte> sharedKey)
 19    {
 19220        NBitcoin.PubKey pubKey = new(rk);
 21
 22        // ECDH operation
 18023        var sharedPubKey = pubKey.GetSharedPubkey(k);
 24
 25        // SHA256 hash of the compressed format of the shared public key
 18026        using var sha256 = new Sha256();
 18027        sha256.AppendData(sharedPubKey.Compress().ToBytes());
 18028        sha256.GetHashAndReset(sharedKey);
 36029    }
 30
 31    /// <inheritdoc/>
 32    public KeyPair GenerateKeyPair()
 33    {
 834        return new KeyPair(new NBitcoin.Key());
 35    }
 36
 37    /// <inheritdoc/>
 38    public KeyPair GenerateKeyPair(ReadOnlySpan<byte> privateKey)
 39    {
 16840        if (privateKey.Length != CryptoConstants.PRIVKEY_LEN)
 41        {
 442            throw new ArgumentException("Invalid private key length");
 43        }
 44
 16445        return new KeyPair(new NBitcoin.Key(privateKey.ToArray()));
 46    }
 47}