EventSourcingDB.Event (EventSourcingDB v1.0.1)

Copy Markdown View Source

An Event coming from the server.

Summary

Functions

Verifying an Event's Hash

Verifying an Event's Signature

Types

t()

@type t() :: %EventSourcingDB.Event{
  data: any(),
  datacontenttype: String.t(),
  hash: String.t(),
  id: String.t(),
  predecessorhash: String.t(),
  signature: String.t() | nil,
  source: String.t(),
  specversion: String.t(),
  subject: String.t(),
  time: String.t(),
  traceparent: String.t() | nil,
  tracestate: String.t() | nil,
  type: String.t()
}

Functions

new(value \\ %{})

@spec new(map()) :: t()

verify_hash(event)

@spec verify_hash(t()) ::
  :ok | {:error, EventSourcingDB.Errors.HashVerificationFailed.t()}

Verifying an Event's Hash

To verify the integrity of an event, call the Event.verify_hash function on the event. This recomputes the event's hash locally and compares it to the hash stored in the event. If the hashes differ, the function returns an error:

alias EventSourcingDB.Event

case Event.verify_hash(event) do
 :ok -> # hash is valid
 {:error, reason} -> # ...
end

Note that this only verifies the hash. If you also want to verify the signature, you can skip this step and call verify_signature directly, which performs a hash verification internally.

verify_signature(event, key)

Verifying an Event's Signature

To verify the authenticity of an event, call the Event.verify_signature function on the event. This requires the public key that matches the private key used for signing on the server.

The function first verifies the event's hash, and then checks the signature. If any verification step fails, it returns an error:

alias EventSourcingDB.Event

verification_key = # public key as Ed25519 binary

case Event.verify_signature(event, verification_key) do
  :ok -> # signature is valid
  {:error, reason} -> # ...
end