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
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//!
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, SigningKey, Timestamp};
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 signing_key = SigningKey::generate();
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//!     verifying_key: signing_key.verifying_key(),
65//!     signature: None,
66//!     payload_size: body.size(),
67//!     payload_hash: Some(body.hash()),
68//!     timestamp: Timestamp::now(),
69//!     seq_num: 0,
70//!     backlink: None,
71//!     extensions: (),
72//! };
73//!
74//! // Sign the header with the author's private key. From now on it's ready to be sent!
75//! header.sign(&signing_key);
76//! ```
77pub mod cbor;
78pub mod cursor;
79pub mod extensions;
80pub mod hash;
81pub mod identity;
82pub mod logs;
83pub mod operation;
84#[cfg(feature = "prune")]
85pub mod prune;
86mod serde;
87#[cfg(any(test, feature = "test_utils"))]
88pub mod test_utils;
89pub mod timestamp;
90pub mod topic;
91pub mod traits;
92
93pub use cursor::Cursor;
94pub use extensions::{Extension, Extensions};
95pub use hash::{Hash, HashError};
96pub use identity::{IdentityError, Signature, SigningKey, VerifyingKey};
97pub use logs::{LogId, SeqNum};
98pub use operation::{
99    Body, Header, Operation, OperationError, RawOperation, validate_backlink, validate_header,
100    validate_operation,
101};
102#[cfg(feature = "prune")]
103pub use prune::PruneFlag;
104pub use timestamp::Timestamp;
105pub use topic::Topic;