Univrse.Envelope (Univrse v0.2.0) View Source

An Envelope is a structure for encoding any arbitrary data payload for data interchange and/or storage.

An Envelope consists of a set of headers and a data payload. Optionally one or more Univrse.Signature.t/0 structures may be used to protect data integrity with digital signature and MAC algorithms. And optionally, one or more Univrse.Recipient.t/0 structures may be used to ensure confidentiality of the data payload using encryption algorithms.

A Univrse Envelope can be serialised into several formats appropriate for data interchange or storage.

  • CBOR encoding
  • String encoding
  • Bitcoin script

Link to this section Summary

Types

Envelope encoding

t()

Envelope struct

Functions

Decodes the given binary into an Envelope structure.

Decodes the given binary into an Envelope structure, using the specified t:encoding().

Decodes the given CBOR encoded payload and puts it in the envelope struct.

Decrypts the envelope payload by first decrypting the content key for the recipient at the specified index with the given key.

Decodes the Envelope into a binary using the specified t:encoding().

CBOR encodes the Envelope payload and returns the encoded binary.

Parses the given Bitcoin Script and returns an Envelope structure.

Pushes the given t:Signature.t or t:Recipient.t into the Envelope.

Encodes the envelope into a valid Univrse OP_RETURN script and returns the script.

Wraps the given payload and headers in a new Envelope structure.

Link to this section Types

Specs

encoding() :: :cbor | :base64

Envelope encoding

Specs

t() :: %Univrse.Envelope{
  header: Univrse.Header.t(),
  payload: any(),
  recipient: nil,
  signature: Univrse.Signature.t() | [Univrse.Signature.t()] | nil
}

Envelope struct

Link to this section Functions

Specs

decode(binary()) :: {:ok, t()} | {:error, any()}

Decodes the given binary into an Envelope structure.

Automatically detects the correct encoding from the binary, assuming it is a supported t:encoding().

Specs

decode(binary(), encoding()) :: {:ok, t()} | {:error, any()}

Decodes the given binary into an Envelope structure, using the specified t:encoding().

Link to this function

decode_payload(env, payload)

View Source

Specs

decode_payload(t(), binary()) :: {:ok, t()} | {:error, any()}

Decodes the given CBOR encoded payload and puts it in the envelope struct.

Link to this function

decrypt(env, key, opts \\ [])

View Source

See Univrse.Recipient.decrypt/3.

Link to this function

decrypt_at(env, idx, key, opts \\ [])

View Source

Specs

decrypt_at(t(), integer(), Univrse.Key.t(), keyword()) ::
  {:ok, t()} | {:error, any()}

Decrypts the envelope payload by first decrypting the content key for the recipient at the specified index with the given key.

The envelope must contain multiple recipients.

Link to this function

encode(env, encoding \\ :cbor)

View Source

Specs

encode(t(), encoding()) :: binary() | String.t()

Decodes the Envelope into a binary using the specified t:encoding().

Default encoding is :cbor.

Link to this function

encode_payload(envelope)

View Source

Specs

encode_payload(t()) :: binary()

CBOR encodes the Envelope payload and returns the encoded binary.

Link to this function

encrypt(env, key, headers, opts \\ [])

View Source

See Univrse.Recipient.encrypt/4.

Specs

parse_script(BSV.Script.t()) :: {:ok, t()} | {:error, any()}

Parses the given Bitcoin Script and returns an Envelope structure.

Specs

Pushes the given t:Signature.t or t:Recipient.t into the Envelope.

Link to this function

sign(env, key, headers \\ %{})

View Source

See Univrse.Signature.sign/3.

Link to this function

to_script(env, false_return \\ true)

View Source

Specs

to_script(t(), boolean()) :: BSV.Script.t()

Encodes the envelope into a valid Univrse OP_RETURN script and returns the script.

See Univrse.Signature.verify/2.

Link to this function

wrap(payload, headers \\ %{})

View Source

Specs

wrap(any(), map() | Univrse.Header.t()) :: t()

Wraps the given payload and headers in a new Envelope structure.