pub struct AnyHeader {
pub version: Version,
pub verifying_key: VerifyingKey,
pub signature: Signature,
pub payload_size: PayloadSize,
pub payload_hash: Option<Hash>,
pub seq_num: SeqNum,
pub backlink: Option<Hash>,
/* private fields */
}Expand description
Header of a p2panda operation.
The header holds all metadata required to cryptographically secure and authenticate a message
Body and it’s custom extensions.
§Extensions
AnyHeader does not know the concrete extensions type. On this level it is only concerned with
the validity and integrity of the append-only log type itself which is enough for most low-level
protocols, such as the sync protocol.
Applications usually want to attach custom extensions to the header, if you need to know the
type you can easily convert from AnyHeader to Header with an explicit E extensions
type.
use p2panda_core::{AnyHeader, Hash, Header, SigningKey};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
struct MyExtensions {
dependencies: Vec<Hash>,
}
let signing_key = SigningKey::generate();
// Create a Header with concrete extension type `MyExtensions`.
let header = Header::builder()
.build(&signing_key, MyExtensions {
dependencies: vec![Hash::from([0; 32])],
});
// Encode it to CBOR bytes, this is how we transmit operations over the network.
let bytes = header.encode();
// Convert it to `AnyHeader` which doesn't know the extensions type.
let any_header = AnyHeader::decode(&bytes)?;
// Bring it back to a concrete Header type with `MyExtensions`.
let header_again = Header::try_from(any_header)?;
assert_eq!(header, header_again);Please note that at this stage we can only verify the integrity and authenticity of the attached extensions, we don’t know if the extensions themselves are valid. We can only find out if this is correct if we know the concrete E type.
Fields§
§version: VersionOperation format version, allowing backwards compatibility when specification changes.
verifying_key: VerifyingKeyAuthor of this operation.
signature: SignatureSignature by author over all fields in header, providing authenticity.
payload_size: PayloadSizeNumber 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.
seq_num: SeqNumNumber 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.
Implementations§
Source§impl AnyHeader
impl AnyHeader
Sourcepub fn decode(bytes: &[u8]) -> Result<Self, HeaderError>
pub fn decode(bytes: &[u8]) -> Result<Self, HeaderError>
Attempts decoding header from bytes.
This fails if integrity checks failed or header formatting is invalid.