Header

Struct Header 

Source
pub struct Header<E = ()> {
    pub version: u64,
    pub public_key: PublicKey,
    pub signature: Option<Signature>,
    pub payload_size: u64,
    pub payload_hash: Option<Hash>,
    pub timestamp: u64,
    pub seq_num: u64,
    pub backlink: Option<Hash>,
    pub previous: Vec<Hash>,
    pub extensions: Option<E>,
}
Expand description

Header of a p2panda operation.

The header holds all metadata required to cryptographically secure and authenticate a message Body and, if required, apply ordering to collections of messages from the same or many authors.

§Example

use p2panda_core::{Body, Header, Operation, PrivateKey};

let private_key = PrivateKey::new();

let body = Body::new("Hello, Sloth!".as_bytes());
let mut header = Header {
    version: 1,
    public_key: private_key.public_key(),
    signature: None,
    payload_size: body.size(),
    payload_hash: Some(body.hash()),
    timestamp: 1733170247,
    seq_num: 0,
    backlink: None,
    previous: vec![],
    extensions: None::<()>,
};

// Sign the header with the author's private key.
header.sign(&private_key);

Fields§

§version: u64

Operation format version, allowing backwards compatibility when specification changes.

§public_key: PublicKey

Author of this operation.

§signature: Option<Signature>

Signature by author over all fields in header, providing authenticity.

§payload_size: u64

Number of bytes of the body of this operation, must be zero if no body is given.

§payload_hash: Option<Hash>

Hash of the body of this operation, must be included if payload_size is non-zero and omitted otherwise.

Keeping the hash here allows us to delete the payload (off-chain data) while retaining the ability to check the signature of the header.

§timestamp: u64

Time in microseconds since the Unix epoch.

§seq_num: u64

Number of operations this author has published to this log, begins with 0 and is always incremented by 1 with each new operation by the same author.

§backlink: Option<Hash>

Hash of the previous operation of the same author and log. Can be omitted if first operation in log.

§previous: Vec<Hash>

List of hashes of the operations we refer to as the “previous” ones. These are operations from other authors. Can be left empty if no partial ordering is required or no other author has been observed yet.

§extensions: Option<E>

Custom meta data.

Implementations§

Source§

impl<E> Header<E>
where E: Extensions,

Source

pub fn to_bytes(&self) -> Vec<u8>

Header encoded to bytes in CBOR format.

Source

pub fn sign(&mut self, private_key: &PrivateKey)

Add a signature to the header using the provided PrivateKey.

This method signs the byte representation of a header with any existing signature removed before adding back the newly generated signature.

Source

pub fn verify(&self) -> bool

Verify that the signature contained in this Header was generated by the claimed public key.

Source

pub fn hash(&self) -> Hash

BLAKE3 hash of the header bytes.

This hash is used as the unique identifier of an operation, aka the Operation Id.

Source

pub fn extension<T>(&self) -> Option<T>
where E: Extension<T>,

Extract an extension value from the header.

Trait Implementations§

Source§

impl<'arbitrary, E: Arbitrary<'arbitrary>> Arbitrary<'arbitrary> for Header<E>

Source§

fn arbitrary(u: &mut Unstructured<'arbitrary>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'arbitrary>) -> Result<Self>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl<E: Clone> Clone for Header<E>

Source§

fn clone(&self) -> Header<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Debug> Debug for Header<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E> Default for Header<E>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, E> Deserialize<'de> for Header<E>
where E: Deserialize<'de>,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<E: PartialEq> PartialEq for Header<E>

Source§

fn eq(&self, other: &Header<E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<E> Serialize for Header<E>
where E: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&[u8]> for Header

Source§

type Error = DecodeError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<E> StructuralPartialEq for Header<E>

Auto Trait Implementations§

§

impl<E> Freeze for Header<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for Header<E>
where E: RefUnwindSafe,

§

impl<E> Send for Header<E>
where E: Send,

§

impl<E> Sync for Header<E>
where E: Sync,

§

impl<E> Unpin for Header<E>
where E: Unpin,

§

impl<E> UnwindSafe for Header<E>
where E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Extensions for T
where T: Clone + Debug + for<'de> Deserialize<'de> + Serialize,