| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | | using NostrSure.Domain.Entities; |
| | 3 | | using NostrSure.Domain.Interfaces; |
| | 4 | | using NostrSure.Domain.Validation; |
| | 5 | |
|
| | 6 | | namespace NostrSure.Domain.Services; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Main validator that orchestrates the validation pipeline with high performance and backward compatibility |
| | 10 | | /// </summary> |
| | 11 | | public sealed class ModularNostrEventValidator : INostrEventValidator |
| | 12 | | { |
| | 13 | | private readonly IEventSignatureValidator _signatureValidator; |
| | 14 | | private readonly IEventIdValidator _eventIdValidator; |
| | 15 | | private readonly IEventKindValidator _kindValidator; |
| | 16 | | private readonly IEventTagValidator _tagValidator; |
| | 17 | | private readonly ILogger<ModularNostrEventValidator>? _logger; |
| | 18 | |
|
| 14 | 19 | | public ModularNostrEventValidator( |
| 14 | 20 | | IEventSignatureValidator signatureValidator, |
| 14 | 21 | | IEventIdValidator eventIdValidator, |
| 14 | 22 | | IEventKindValidator kindValidator, |
| 14 | 23 | | IEventTagValidator tagValidator, |
| 14 | 24 | | ILogger<ModularNostrEventValidator>? logger = null) |
| 14 | 25 | | { |
| 14 | 26 | | _signatureValidator = signatureValidator; |
| 14 | 27 | | _eventIdValidator = eventIdValidator; |
| 14 | 28 | | _kindValidator = kindValidator; |
| 14 | 29 | | _tagValidator = tagValidator; |
| 14 | 30 | | _logger = logger; |
| 14 | 31 | | } |
| | 32 | |
|
| | 33 | | public async Task<ValidationResult> ValidateAsync(NostrEvent evt, CancellationToken cancellationToken = default) |
| 104 | 34 | | { |
| | 35 | | // Fast path: validate cheap operations first |
| 104 | 36 | | var kindResult = _kindValidator.ValidateKind(evt); |
| 104 | 37 | | if (!kindResult.IsValid) |
| 1 | 38 | | { |
| 1 | 39 | | _logger?.LogWarning("Event kind validation failed: {Error}", kindResult.Error?.Message); |
| 1 | 40 | | return kindResult; |
| | 41 | | } |
| | 42 | |
|
| 103 | 43 | | var tagResult = _tagValidator.ValidateTags(evt); |
| 103 | 44 | | if (!tagResult.IsValid) |
| 1 | 45 | | { |
| 1 | 46 | | _logger?.LogWarning("Event tag validation failed: {Error}", tagResult.Error?.Message); |
| 1 | 47 | | return tagResult; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | // Expensive operations with async support for potential parallelization |
| 102 | 51 | | var eventIdTask = _eventIdValidator.ValidateEventIdAsync(evt, cancellationToken); |
| 102 | 52 | | var signatureTask = _signatureValidator.ValidateSignatureAsync(evt, cancellationToken); |
| | 53 | |
|
| | 54 | | // Wait for both expensive operations to complete |
| 102 | 55 | | await Task.WhenAll(eventIdTask, signatureTask); |
| | 56 | |
|
| 102 | 57 | | var eventIdResult = eventIdTask.Result; |
| 102 | 58 | | if (!eventIdResult.IsValid) |
| 0 | 59 | | { |
| 0 | 60 | | _logger?.LogWarning("Event ID validation failed: {Error}", eventIdResult.Error?.Message); |
| 0 | 61 | | return eventIdResult; |
| | 62 | | } |
| | 63 | |
|
| 102 | 64 | | var signatureResult = signatureTask.Result; |
| 102 | 65 | | if (!signatureResult.IsValid) |
| 1 | 66 | | { |
| 1 | 67 | | _logger?.LogWarning("Signature validation failed: {Error}", signatureResult.Error?.Message); |
| 1 | 68 | | return signatureResult; |
| | 69 | | } |
| | 70 | |
|
| 101 | 71 | | _logger?.LogDebug("Event validation completed successfully for event {EventId}", evt.Id); |
| 101 | 72 | | return ValidationResult.Success(); |
| 104 | 73 | | } |
| | 74 | |
|
| | 75 | | public ValidationResult Validate(NostrEvent evt) |
| 0 | 76 | | { |
| 0 | 77 | | return ValidateAsync(evt).GetAwaiter().GetResult(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | #region Legacy Compatibility Methods |
| | 81 | |
|
| | 82 | | public bool ValidateSignature(NostrEvent evt, out string error) |
| 1 | 83 | | { |
| 1 | 84 | | var result = _signatureValidator.ValidateSignature(evt); |
| 1 | 85 | | error = result.Error?.Message ?? string.Empty; |
| 1 | 86 | | return result.IsValid; |
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | public bool ValidateKind(NostrEvent evt, out string error) |
| 1 | 90 | | { |
| 1 | 91 | | var result = _kindValidator.ValidateKind(evt); |
| 1 | 92 | | error = result.Error?.Message ?? string.Empty; |
| 1 | 93 | | return result.IsValid; |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | public bool ValidateTags(NostrEvent evt, out string error) |
| 1 | 97 | | { |
| 1 | 98 | | var result = _tagValidator.ValidateTags(evt); |
| 1 | 99 | | error = result.Error?.Message ?? string.Empty; |
| 1 | 100 | | return result.IsValid; |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | public bool ValidateEventId(NostrEvent evt, out string error) |
| 1 | 104 | | { |
| 1 | 105 | | var result = _eventIdValidator.ValidateEventId(evt); |
| 1 | 106 | | error = result.Error?.Message ?? string.Empty; |
| 1 | 107 | | return result.IsValid; |
| 1 | 108 | | } |
| | 109 | |
|
| | 110 | | #endregion |
| | 111 | | } |