< Summary

Information
Class: NostrSure.Domain.Services.SimpleEventIdCalculator
Assembly: NostrSure.Domain
File(s): /home/runner/work/NostrSure/NostrSure/NostrSure.Domain/Services/SimpleEventIdCalculator.cs
Line coverage
96%
Covered lines: 29
Uncovered lines: 1
Coverable lines: 30
Total lines: 54
Line coverage: 96.6%
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
.cctor()100%210%
.ctor()100%11100%
CalculateEventId(...)100%11100%

File(s)

/home/runner/work/NostrSure/NostrSure/NostrSure.Domain/Services/SimpleEventIdCalculator.cs

#LineLine coverage
 1using NostrSure.Domain.Entities;
 2using NostrSure.Domain.Validation;
 3using System.Security.Cryptography;
 4using System.Text;
 5using System.Text.Encodings.Web;
 6using System.Text.Json;
 7
 8namespace NostrSure.Domain.Services;
 9
 10/// <summary>
 11/// Simple event ID calculator without caching for scenarios where caching is not desired
 12/// This implementation closely matches the original legacy implementation to ensure compatibility
 13/// </summary>
 14public sealed class SimpleEventIdCalculator : IEventIdCalculator
 15{
 16    private readonly JsonSerializerOptions _jsonOptions;
 017    private static readonly SHA256 _sha256 = SHA256.Create();
 18
 2819    public SimpleEventIdCalculator()
 2820    {
 2821        _jsonOptions = new JsonSerializerOptions
 2822        {
 2823            WriteIndented = false,
 2824            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
 2825        };
 2826    }
 27
 28    public string CalculateEventId(NostrEvent evt)
 1129    {
 30        // Match the exact legacy implementation format for compatibility
 1131        var tagsArrays = evt.Tags.Select(tag =>
 1132        {
 1133            var array = new List<string> { tag.Name };
 1134            array.AddRange(tag.Values);
 1135            return array.ToArray();
 2236        }).ToArray();
 37
 1138        var eventArray = new object[]
 1139        {
 1140            0,
 1141            evt.Pubkey.Value,
 1142            evt.CreatedAt.ToUnixTimeSeconds(),
 1143            (int)evt.Kind,
 1144            tagsArrays,
 1145            evt.Content
 1146        };
 47
 1148        var serialized = JsonSerializer.Serialize(eventArray, _jsonOptions);
 49
 1150        var utf8Bytes = Encoding.UTF8.GetBytes(serialized);
 1151        var hash = NBitcoin.Crypto.Hashes.SHA256(utf8Bytes);
 1152        return Convert.ToHexString(hash).ToLowerInvariant();
 1153    }
 54}