p2panda_core/
lib.rs

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