Skip to main content

p2panda_core/
lib.rs

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