| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Integrity; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the outcome of provider-neutral audit integrity verification. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class AuditIntegrityVerificationResult |
| | | 9 | | { |
| | 0 | 10 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 0 | 11 | | new ReadOnlyDictionary<string, string>(new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 12 | | |
| | 10 | 13 | | private AuditIntegrityVerificationResult( |
| | 10 | 14 | | bool isValid, |
| | 10 | 15 | | AuditIntegrityVerificationCategory category, |
| | 10 | 16 | | string status, |
| | 10 | 17 | | string? failureCode, |
| | 10 | 18 | | string? failureMessage, |
| | 10 | 19 | | string? chainId, |
| | 10 | 20 | | long? sequence, |
| | 10 | 21 | | string? recordId, |
| | 10 | 22 | | IReadOnlyDictionary<string, string> safeMetadata) |
| | | 23 | | { |
| | 10 | 24 | | ArgumentException.ThrowIfNullOrWhiteSpace(status); |
| | | 25 | | |
| | 10 | 26 | | if (!Enum.IsDefined(category)) |
| | | 27 | | { |
| | 0 | 28 | | throw new ArgumentOutOfRangeException(nameof(category), category, "Verification category must be defined."); |
| | | 29 | | } |
| | | 30 | | |
| | 10 | 31 | | IsValid = isValid; |
| | 10 | 32 | | Category = category; |
| | 10 | 33 | | Status = status.Trim(); |
| | 10 | 34 | | FailureCode = NormalizeOptional(failureCode); |
| | 10 | 35 | | FailureMessage = NormalizeOptional(failureMessage); |
| | 10 | 36 | | ChainId = NormalizeOptional(chainId); |
| | 10 | 37 | | Sequence = sequence; |
| | 10 | 38 | | RecordId = NormalizeOptional(recordId); |
| | 10 | 39 | | SafeMetadata = safeMetadata; |
| | 10 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets a value indicating whether the integrity chain verified successfully. |
| | | 44 | | /// </summary> |
| | 10 | 45 | | public bool IsValid { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the provider-neutral verification category. |
| | | 49 | | /// </summary> |
| | 9 | 50 | | public AuditIntegrityVerificationCategory Category { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a provider-neutral status string. |
| | | 54 | | /// </summary> |
| | 0 | 55 | | public string Status { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the failure code when verification did not succeed. |
| | | 59 | | /// </summary> |
| | 8 | 60 | | public string? FailureCode { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the failure message when verification did not succeed. |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public string? FailureMessage { get; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the chain identifier associated with the result, when known. |
| | | 69 | | /// </summary> |
| | 1 | 70 | | public string? ChainId { get; } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets the sequence associated with the result, when known. |
| | | 74 | | /// </summary> |
| | 0 | 75 | | public long? Sequence { get; } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the record identifier associated with the result, when known. |
| | | 79 | | /// </summary> |
| | 0 | 80 | | public string? RecordId { get; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets safe-to-log verification metadata. |
| | | 84 | | /// </summary> |
| | 6 | 85 | | public IReadOnlyDictionary<string, string> SafeMetadata { get; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates a valid verification result. |
| | | 89 | | /// </summary> |
| | | 90 | | public static AuditIntegrityVerificationResult Valid(string chainId, long linkCount, string tipHash) |
| | | 91 | | { |
| | 2 | 92 | | Dictionary<string, string> metadata = new(StringComparer.Ordinal) |
| | 2 | 93 | | { |
| | 2 | 94 | | ["chain_id"] = chainId, |
| | 2 | 95 | | ["link_count"] = linkCount.ToString(System.Globalization.CultureInfo.InvariantCulture), |
| | 2 | 96 | | ["tip_hash"] = tipHash |
| | 2 | 97 | | }; |
| | | 98 | | |
| | 2 | 99 | | return new AuditIntegrityVerificationResult( |
| | 2 | 100 | | true, |
| | 2 | 101 | | AuditIntegrityVerificationCategory.Valid, |
| | 2 | 102 | | "Valid", |
| | 2 | 103 | | null, |
| | 2 | 104 | | null, |
| | 2 | 105 | | chainId, |
| | 2 | 106 | | linkCount, |
| | 2 | 107 | | null, |
| | 2 | 108 | | new ReadOnlyDictionary<string, string>(metadata)); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Creates a failed verification result. |
| | | 113 | | /// </summary> |
| | | 114 | | public static AuditIntegrityVerificationResult Failed( |
| | | 115 | | AuditIntegrityVerificationCategory category, |
| | | 116 | | string failureCode, |
| | | 117 | | string? failureMessage = null, |
| | | 118 | | AuditIntegrityLink? link = null, |
| | | 119 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 120 | | { |
| | 8 | 121 | | ArgumentException.ThrowIfNullOrWhiteSpace(failureCode); |
| | | 122 | | |
| | 8 | 123 | | Dictionary<string, string> safeMetadata = new(StringComparer.Ordinal) |
| | 8 | 124 | | { |
| | 8 | 125 | | ["category"] = category.ToString(), |
| | 8 | 126 | | ["failure_code"] = failureCode.Trim() |
| | 8 | 127 | | }; |
| | | 128 | | |
| | 8 | 129 | | if (link is not null) |
| | | 130 | | { |
| | 7 | 131 | | safeMetadata["chain_id"] = link.ChainId; |
| | 7 | 132 | | safeMetadata["record_id"] = link.RecordId; |
| | 7 | 133 | | safeMetadata["sequence"] = link.Sequence.ToString(System.Globalization.CultureInfo.InvariantCulture); |
| | | 134 | | } |
| | | 135 | | |
| | 8 | 136 | | if (metadata is not null) |
| | | 137 | | { |
| | 18 | 138 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 139 | | { |
| | 6 | 140 | | if (!string.IsNullOrWhiteSpace(item.Key)) |
| | | 141 | | { |
| | 6 | 142 | | safeMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | 8 | 147 | | return new AuditIntegrityVerificationResult( |
| | 8 | 148 | | false, |
| | 8 | 149 | | category, |
| | 8 | 150 | | "Failed", |
| | 8 | 151 | | failureCode, |
| | 8 | 152 | | failureMessage, |
| | 8 | 153 | | link?.ChainId, |
| | 8 | 154 | | link?.Sequence, |
| | 8 | 155 | | link?.RecordId, |
| | 8 | 156 | | safeMetadata.Count == 0 ? EmptyMetadata : new ReadOnlyDictionary<string, string>(safeMetadata)); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | private static string? NormalizeOptional(string? value) |
| | | 160 | | { |
| | 40 | 161 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 162 | | } |
| | | 163 | | } |