namespace StellaOps.Canonical.Json;
///
/// Canonicalization version identifiers for content-addressed hashing.
///
///
/// Version markers are embedded in canonical JSON to ensure hash stability across
/// algorithm evolution. When canonicalization logic changes (bug fixes, spec updates,
/// optimizations), a new version constant is introduced, allowing:
///
/// - Verifiers to select the correct canonicalization algorithm
/// - Graceful migration without invalidating existing hashes
/// - Clear audit trail of which algorithm produced each hash
///
///
public static class CanonVersion
{
///
/// Version 1: RFC 8785 JSON Canonicalization Scheme (JCS) with:
///
/// - Ordinal key sorting (case-sensitive, lexicographic)
/// - No whitespace or formatting variations
/// - UTF-8 encoding without BOM
/// - IEEE 754 number formatting
/// - Minimal escape sequences in strings
///
///
public const string V1 = "stella:canon:v1";
///
/// Field name for version marker in canonical JSON.
/// Underscore prefix ensures it sorts first lexicographically,
/// making version detection a simple prefix check.
///
public const string VersionFieldName = "_canonVersion";
///
/// Current default version for new hashes.
/// All new content-addressed IDs use this version.
///
public const string Current = V1;
///
/// Prefix bytes for detecting versioned canonical JSON.
/// Versioned JSON starts with: {"_canonVersion":"
///
internal static ReadOnlySpan VersionedPrefixBytes => "{\"_canonVersion\":\""u8;
///
/// Checks if canonical JSON bytes are versioned (contain version marker).
///
/// UTF-8 encoded canonical JSON bytes.
/// True if the JSON contains a version marker at the expected position.
public static bool IsVersioned(ReadOnlySpan canonicalJson)
{
// Versioned canonical JSON always starts with: {"_canonVersion":"stella:canon:v
// Minimum length: {"_canonVersion":"stella:canon:v1"} = 35 bytes
return canonicalJson.Length >= 35 &&
canonicalJson.StartsWith(VersionedPrefixBytes);
}
///
/// Extracts the version string from versioned canonical JSON.
///
/// UTF-8 encoded canonical JSON bytes.
/// The version string, or null if not versioned or invalid format.
public static string? ExtractVersion(ReadOnlySpan canonicalJson)
{
if (!IsVersioned(canonicalJson))
{
return null;
}
// Find the closing quote after the version value
var prefixLength = VersionedPrefixBytes.Length;
var remaining = canonicalJson[prefixLength..];
var quoteIndex = remaining.IndexOf((byte)'"');
if (quoteIndex <= 0)
{
return null;
}
var versionBytes = remaining[..quoteIndex];
return System.Text.Encoding.UTF8.GetString(versionBytes);
}
}