| | | 1 | | using System.Globalization; |
| | | 2 | | using AsiBackbone.Core.Serialization; |
| | | 3 | | using AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.HostIntegration; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Builds deterministic signing payloads for governed operation execution receipts. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class GovernedOperationExecutionReceiptCanonicalPayload |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Builds a provider-neutral canonical payload for an execution receipt. |
| | | 14 | | /// </summary> |
| | | 15 | | public static CanonicalPayload Create( |
| | | 16 | | GovernedOperationExecutionReceipt receipt, |
| | | 17 | | CanonicalPayloadOptions? options = null) |
| | | 18 | | { |
| | 4 | 19 | | ArgumentNullException.ThrowIfNull(receipt); |
| | 4 | 20 | | CanonicalPayloadOptions effectiveOptions = options ?? CanonicalPayloadOptions.Default; |
| | | 21 | | |
| | 4 | 22 | | SortedDictionary<string, object?> content = new(StringComparer.Ordinal) |
| | 4 | 23 | | { |
| | 4 | 24 | | ["completedUtc"] = receipt.CompletedUtc.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'", Cultu |
| | 4 | 25 | | ["completedWithoutMutation"] = receipt.CompletedWithoutMutation, |
| | 4 | 26 | | ["decisionAuditRecordId"] = receipt.DecisionAuditRecordId, |
| | 4 | 27 | | ["executionAttemptId"] = receipt.ExecutionAttemptId, |
| | 4 | 28 | | ["metadata"] = FilterMetadata(receipt.Metadata, effectiveOptions), |
| | 4 | 29 | | ["mutationBatchId"] = receipt.MutationBatchId, |
| | 4 | 30 | | ["mutationManifestAlgorithm"] = receipt.MutationManifestAlgorithm, |
| | 4 | 31 | | ["mutationManifestHash"] = receipt.MutationManifestHash, |
| | 4 | 32 | | ["mutationRecordCount"] = receipt.MutationRecordCount, |
| | 4 | 33 | | ["operationExecutionId"] = receipt.OperationExecutionId, |
| | 4 | 34 | | ["persistenceOutcome"] = receipt.PersistenceOutcome.ToString(), |
| | 4 | 35 | | ["persistenceProvider"] = receipt.PersistenceProvider |
| | 4 | 36 | | }; |
| | | 37 | | |
| | 4 | 38 | | return CanonicalPayload.Create( |
| | 4 | 39 | | CanonicalArtifactTypes.GovernedOperationExecutionReceipt, |
| | 4 | 40 | | BuildArtifactId(receipt), |
| | 4 | 41 | | AsiBackboneSchemaVersions.StableArtifactsV1, |
| | 4 | 42 | | effectiveOptions.CanonicalizationVersion, |
| | 4 | 43 | | content); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static string BuildArtifactId(GovernedOperationExecutionReceipt receipt) |
| | | 47 | | { |
| | 4 | 48 | | return receipt.ExecutionAttemptId is null |
| | 4 | 49 | | ? receipt.OperationExecutionId |
| | 4 | 50 | | : $"{receipt.OperationExecutionId}:{receipt.ExecutionAttemptId}"; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static SortedDictionary<string, object?> FilterMetadata( |
| | | 54 | | IReadOnlyDictionary<string, string> metadata, |
| | | 55 | | CanonicalPayloadOptions options) |
| | | 56 | | { |
| | 4 | 57 | | SortedDictionary<string, object?> filtered = new(StringComparer.Ordinal); |
| | 24 | 58 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 59 | | { |
| | 8 | 60 | | if (options.AllowsMetadataKey(item.Key)) |
| | | 61 | | { |
| | 4 | 62 | | filtered[item.Key] = item.Value; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | 4 | 66 | | return filtered; |
| | | 67 | | } |
| | | 68 | | } |