< Summary

Information
Class: NostrSure.Domain.Services.EventSignatureValidator
Assembly: NostrSure.Domain
File(s): /home/runner/work/NostrSure/NostrSure/NostrSure.Domain/Services/EventSignatureValidator.cs
Line coverage
76%
Covered lines: 26
Uncovered lines: 8
Coverable lines: 34
Total lines: 66
Line coverage: 76.4%
Branch coverage
59%
Covered branches: 13
Total branches: 22
Branch coverage: 59%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ValidateSignatureAsync(...)100%11100%
ValidateSignature(...)59.09%382268%

File(s)

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

#LineLine coverage
 1using NostrSure.Domain.Entities;
 2using NostrSure.Domain.Validation;
 3
 4namespace NostrSure.Domain.Services;
 5
 6/// <summary>
 7/// Validates cryptographic signatures using optimized Schnorr verification
 8/// </summary>
 9public sealed class EventSignatureValidator : IEventSignatureValidator
 10{
 11    private readonly ICryptographicService _cryptographicService;
 12    private readonly IHexConverter _hexConverter;
 13
 1414    public EventSignatureValidator(ICryptographicService cryptographicService, IHexConverter hexConverter)
 1415    {
 1416        _cryptographicService = cryptographicService;
 1417        _hexConverter = hexConverter;
 1418    }
 19
 20    public Task<ValidationResult> ValidateSignatureAsync(NostrEvent evt, CancellationToken cancellationToken = default)
 10221    {
 10222        var result = ValidateSignature(evt);
 10223        return Task.FromResult(result);
 10224    }
 25
 26    public ValidationResult ValidateSignature(NostrEvent evt)
 10427    {
 28        // Quick validation of required fields
 10429        if (string.IsNullOrWhiteSpace(evt.Sig))
 030            return ValidationResult.Failure("Signature is empty", "EMPTY_SIGNATURE");
 31
 10432        if (string.IsNullOrWhiteSpace(evt.Pubkey?.Value))
 033            return ValidationResult.Failure("Pubkey is empty", "EMPTY_PUBKEY");
 34
 10435        if (string.IsNullOrWhiteSpace(evt.Id))
 036            return ValidationResult.Failure("Event ID is empty", "EMPTY_EVENT_ID");
 37
 38        try
 10439        {
 40            // Parse hex strings using optimized converter
 10441            Span<byte> eventIdBytes = stackalloc byte[32];
 10442            Span<byte> pubkeyBytes = stackalloc byte[32];
 10443            Span<byte> sigBytes = stackalloc byte[64];
 44
 10445            if (!_hexConverter.TryParseHex(evt.Id, eventIdBytes, out var eventIdLength) || eventIdLength != 32)
 046                return ValidationResult.Failure("Invalid event ID length (must be 32 bytes)", "INVALID_EVENT_ID_LENGTH")
 47
 10448            if (!_hexConverter.TryParseHex(evt.Pubkey.Value, pubkeyBytes, out var pubkeyLength) || pubkeyLength != 32)
 049                return ValidationResult.Failure("Invalid pubkey length (must be 32 bytes)", "INVALID_PUBKEY_LENGTH");
 50
 10451            if (!_hexConverter.TryParseHex(evt.Sig, sigBytes, out var sigLength) || sigLength != 64)
 152                return ValidationResult.Failure("Invalid signature length (must be 64 bytes)", "INVALID_SIGNATURE_LENGTH
 53
 54            // Verify signature using cryptographic service
 10355            var isValid = _cryptographicService.VerifySchnorrSignature(sigBytes, eventIdBytes, pubkeyBytes);
 56
 10357            return isValid
 10358                ? ValidationResult.Success()
 10359                : ValidationResult.Failure("Signature verification failed", "SIGNATURE_VERIFICATION_FAILED");
 60        }
 061        catch (System.Exception ex)
 062        {
 063            return ValidationResult.Failure($"Exception during signature validation: {ex.Message}", ex, ValidationSeveri
 64        }
 10465    }
 66}