< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Bitcoin.Crypto.Functions.Ecdh
Assembly: NLightning.Infrastructure.Bitcoin
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Bitcoin/Crypto/Functions/Ecdh.cs
Tag: 36_15743069263
Line coverage
80%
Covered lines: 12
Uncovered lines: 3
Coverable lines: 15
Total lines: 52
Line coverage: 80%
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%210%
GenerateKeyPair(...)50%22100%

File(s)

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

#LineLine coverage
 1using NBitcoin;
 2
 3namespace NLightning.Infrastructure.Bitcoin.Crypto.Functions;
 4
 5using Domain.Crypto.Constants;
 6using Domain.Crypto.ValueObjects;
 7using Infrastructure.Crypto.Hashes;
 8using Infrastructure.Crypto.Interfaces;
 9
 10/// <summary>
 11/// The SecP256k1 DH function implementation.
 12/// </summary>
 13/// <see href="https://github.com/lightning/bolts/blob/master/08-transport.md#handshake-state"/>
 14internal sealed class Ecdh : IEcdh
 15{
 16    /// <inheritdoc/>
 17    /// <param name="k">Private Key</param>
 18    /// <param name="rk">Remote Static PubKey</param>
 19    /// <param name="sharedKey"></param>
 20    public void SecP256K1Dh(PrivKey k, ReadOnlySpan<byte> rk, Span<byte> sharedKey)
 21    {
 20022        PubKey pubKey = new(rk);
 18823        using Key key = new(k);
 24
 25        // ECDH operation
 18826        var sharedPubKey = pubKey.GetSharedPubkey(key);
 27
 28        // Shared public key's Compressed format's SHA256
 18829        using var sha256 = new Sha256();
 18830        sha256.AppendData(sharedPubKey.Compress().ToBytes());
 18831        sha256.GetHashAndReset(sharedKey);
 37632    }
 33
 34    /// <inheritdoc/>
 35    public CryptoKeyPair GenerateKeyPair()
 36    {
 037        using var key = new Key();
 038        return new CryptoKeyPair(key.ToBytes(), key.PubKey.ToBytes());
 039    }
 40
 41    /// <inheritdoc/>
 42    public CryptoKeyPair GenerateKeyPair(ReadOnlySpan<byte> privateKey)
 43    {
 18844        if (privateKey.Length != CryptoConstants.PrivkeyLen)
 45        {
 446            throw new ArgumentException("Invalid private key length");
 47        }
 48
 18449        using var key = new Key(privateKey.ToArray());
 18450        return new CryptoKeyPair(key.ToBytes(), key.PubKey.ToBytes());
 18451    }
 52}