AnchorDigest

Builds and recognises the compact digest envelope anchored on-chain when a client runs in digest payload mode (see AbstractBlockchainAnchorClient.OPTION_PAYLOAD_MODE).

The envelope is a small JSON object that replaces the full payload on-chain so no payload content (PII, business data, …) ever leaves the caller's custody:

{"alg":"SHA-256","canon":"JCS","digest":"<base64url(sha256(JCS(payload)))>","mediaType":"application/json"}

Payload bytes are the RFC 8785 (JCS) canonical UTF-8 serialization of the payload (JsonCanonicalization), so any structurally equal payload — whatever its key order, whitespace or number spelling — verifies. The canon member records this.

Legacy envelopes (written before canonicalization was introduced) have no canon member; their digest is over the payload exactly as Json.encodeToString(JsonElement .serializer(), payload) serialized it, i.e. key-order sensitive. They are still recognised and verified with those legacy bytes, so existing anchors keep verifying. The version is explicit in the envelope rather than guessed by trying both encodings, so a JCS anchor can never be satisfied by a legacy-encoded payload or vice versa.

The digest is base64url-encoded without padding (RFC 4648 §5).

Properties

Link copied to clipboard
const val ALGORITHM: String

The only digest algorithm currently produced and recognised.

Link copied to clipboard

FIELD_CANONICALIZATION value for RFC 8785 JSON Canonicalization Scheme.

Link copied to clipboard
const val FIELD_ALG: String

Envelope field holding the digest algorithm name.

Link copied to clipboard

Envelope field naming the canonicalization applied before hashing (absent: legacy).

Link copied to clipboard
const val FIELD_DIGEST: String

Envelope field holding the unpadded base64url digest value.

Link copied to clipboard

Envelope field holding the media type of the original (off-chain) payload.

Functions

Link copied to clipboard

Unpadded base64url encoding of sha256(payloadBytes).

Link copied to clipboard
fun envelope(payloadBytes: ByteArray, mediaType: String): JsonObject

Builds a legacy (pre-canonicalization) digest envelope over raw payloadBytes.

fun envelope(payload: JsonElement, mediaType: String): JsonObject

Builds the RFC 8785 digest envelope anchored on-chain in digest payload mode: the digest is over JCS(payload) and the envelope carries canon = "JCS".

Link copied to clipboard
fun isCanonicalized(envelope: JsonObject): Boolean

Whether envelope is a JCS (RFC 8785) envelope rather than a legacy one.

Link copied to clipboard
fun isEnvelope(element: JsonElement): Boolean

Whether element has the shape of a digest envelope: a JSON object whose key set is exactly {alg, canon, digest, mediaType} with canon == "JCS", or the legacy {alg, digest, mediaType}, with alg == "SHA-256" and a string digest that base64url-decodes to exactly 32 bytes (the SHA-256 digest length).

Link copied to clipboard
fun matches(envelope: JsonObject, payloadBytes: ByteArray): Boolean

Whether envelope is a digest envelope whose digest matches payloadBytes. Uses a constant-time comparison; malformed envelopes or undecodable digests yield false.

fun matches(envelope: JsonObject, payload: JsonElement): Boolean

Whether envelope attests to payload, hashing the payload the way the envelope says it was hashed: JCS(payload) for envelopes with canon = "JCS", the legacy kotlinx serialization bytes for envelopes without it.

fun matches(envelope: JsonObject, payload: JsonElement, requireCanonicalEnvelope: Boolean): Boolean

Like matches with an option for verifiers that accept only RFC 8785 anchors.

Link copied to clipboard
fun sha256(payloadBytes: ByteArray): ByteArray

SHA-256 of payloadBytes.