p2panda_core/lib.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3//! Core data types used across the p2panda stack to offer distributed, secure and efficient data
4//! transfer between peers.
5//!
6//! The main data type is a highly extensible, cryptographically secure append-only log
7//! implementation. It provides all the basic features required to implement more advanced
8//! distributed data types commonly required when building peer-to-peer and local-first
9//! applications.
10//!
11//! ## Features
12//!
13//! - Cryptographic signatures for authorship verification and tamper-proof messages
14//! - Authors can maintain one or many logs
15//! - Single-writer logs which can be combined to support multi-writer collaboration
16//! - Compatible with any application data and CRDT
17//! - Various ordering algorithms
18//! - Supports efficient, partial sync
19//! - Compatible with any networking scenario (even broadcast-only, for example for packet radio)
20//! - Fork-tolerant
21//! - Pruning of outdated messages
22//! - Highly extensible with custom features, for example prefix-deletion, ephemeral
23//! "self-destructing" messages, etc.
24//!
25//! p2panda logs are made up of [`Operation`]s. Authors sign operations using their cryptographic
26//! key pair and append them to a log. An author may have one or many logs. The precise means of
27//! identifying logs is not defined by this crate (see extensions).
28//!
29//! A common challenge in distributed systems is how to order operations written concurrently by
30//! different authors and/or processes. Operations contain information which can be used for
31//! establishing order depending on one's use case:
32//! - `timestamp`: UNIX timestamp describing when the operation was created.
33//! - `previous`: List of hashes referring to the previously observed operations to establish
34//! cryptographically secure partial-ordering.
35//!
36//! Custom extension fields can be defined by users of this library to introduce additional
37//! functionality depending on their particular use cases. p2panda provides our own extensions
38//! which are required when using our other crates offering more advanced functionality needed for
39//! application building (CRDTs, access control, encryption, ephemeral data, garbage collection,
40//! etc.), but it's entirely possible for users to define their own extensions as well.
41//!
42//! An operation is constructed from a [`Header`] and a [`Body`], the `Header` contains all
43//! metadata associated with the particular operation, and the `Body` contains the actual
44//! application message bytes. This allows "off-chain" handling, where the important bits in the
45//! headers are transmitted via an prioritised channel and secondary information can be loaded
46//! "lazily". Additionally it allows deletion of payloads without breaking the integrity of the
47//! append-only log.
48//!
49//! ## Example
50//!
51//! ```
52//! use p2panda_core::{Body, Header, Operation, PrivateKey};
53//!
54//! // Every operation is cryptographically authenticated by an author by signing it with an
55//! // Ed25519 key pair. This method generates a new private key for us which needs to be securely
56//! // stored for re-use.
57//! let private_key = PrivateKey::new();
58//!
59//! // Operations consist of an body (with the actual application data) and a header,
60//! // enhancing the data to be used in distributed networks.
61//! let body = Body::new("Hello, Sloth!".as_bytes());
62//! let mut header = Header {
63//! version: 1,
64//! public_key: private_key.public_key(),
65//! signature: None,
66//! payload_size: body.size(),
67//! payload_hash: Some(body.hash()),
68//! timestamp: 1733170247,
69//! seq_num: 0,
70//! backlink: None,
71//! previous: vec![],
72//! extensions: None::<()>,
73//! };
74//!
75//! // Sign the header with the author's private key. From now on it's ready to be sent!
76//! header.sign(&private_key);
77//! ```
78pub mod cbor;
79pub mod extensions;
80pub mod hash;
81pub mod identity;
82pub mod operation;
83#[cfg(feature = "prune")]
84pub mod prune;
85mod serde;
86
87pub use extensions::{Extension, Extensions};
88pub use hash::{Hash, HashError};
89pub use identity::{IdentityError, PrivateKey, PublicKey, Signature};
90pub use operation::{
91 validate_backlink, validate_header, validate_operation, Body, Header, Operation,
92 OperationError, RawOperation,
93};
94#[cfg(feature = "prune")]
95pub use prune::PruneFlag;