ma_core/lib.rs
1//! # ma-core
2//!
3//! A lean `DIDComm` service library for the ma ecosystem.
4//!
5//! `ma-core` provides the building blocks for ma-capable endpoints:
6//!
7//! - **DID documents** — create, validate, resolve, and publish `did:ma:` documents
8//! to IPFS/IPNS (via Kubo on native targets). Use [`MaExtension`] to build the
9//! `ma:` extension field, and [`config::SecretBundle::build_document`] (`config`
10//! feature) as the single entry point for a complete, signed document.
11//! - **Service inboxes** — bounded, TTL-aware FIFO queues ([`Inbox`])
12//! for receiving validated messages on named protocol services.
13//! - **Outbound sending** — fire-and-forget delivery of validated [`Message`] objects
14//! to remote endpoints, serialized to CBOR on the wire.
15//! - **Endpoint abstraction** — the [`MaEndpoint`] trait with pluggable
16//! transport backends.
17//! - **Transport parsing** — extract endpoint IDs and protocols from DID document
18//! service strings (`/iroh/<id>/<protocol>`).
19//! - **Identity bootstrap** — secure secret key generation and persistence.
20//!
21//! ## Services
22//!
23//! Every endpoint should provide `/ma/inbox/0.0.1` (the default inbox) when it
24//! wants to receive direct messages. Endpoints may optionally provide
25//! `/ma/ipfs/0.0.1` to publish DID documents
26//! on behalf of others.
27//!
28//! ## Feature flags
29//!
30//! - **`kubo`** — enables native IPFS RPC backend for publishing (native only).
31//! - **`iroh`** — enables the internal iroh QUIC transport backend.
32//! - **`acl`** — enables [`AclMap`], [`check_cap`], capability constants, and
33//! ACL validation helpers.
34//! - **`config`** — enables [`Config`], [`SecretBundle`], and [`MaArgs`] for
35//! YAML-based daemon configuration, encrypted secret bundles, and CLI
36//! argument parsing. Also provides [`config::SecretBundle::build_document`] and
37//! [`config::SecretBundle::signing_key`] for constructing ready-to-publish DID documents.
38//!
39//! ## Platform support
40//!
41//! Core types (`Inbox`, `Service`, transport parsing, validation)
42//! compile on all targets including `wasm32-unknown-unknown`.
43//!
44//! ### wasm vs native
45//!
46//! - `ma-core` supports both wasm and native targets.
47//! - `IpfsGatewayResolver` (HTTP gateway DID fetch) is available on wasm and native.
48//! - Native IPFS RPC write/pin APIs are native-only (`not(wasm32)` + `kubo` feature).
49//! - wasm builds expose only `ipfs::gateway_resolver` (no native RPC helpers).
50//! - `config` serialization and `SecretBundle` crypto work on wasm.
51//! - `config` filesystem paths, CLI/env merging, and file I/O are native-only.
52//! - If your wasm application needs native IPFS RPC write/pin operations, provide
53//! them in a native companion layer.
54
55#![forbid(unsafe_code)]
56#![allow(
57 clippy::cast_possible_truncation,
58 clippy::cast_precision_loss,
59 clippy::if_not_else,
60 clippy::items_after_statements,
61 clippy::manual_let_else,
62 clippy::map_unwrap_or,
63 clippy::missing_errors_doc,
64 clippy::must_use_candidate,
65 clippy::uninlined_format_args
66)]
67
68#[cfg(feature = "acl")]
69pub mod acl;
70#[cfg(feature = "config")]
71pub mod config;
72pub mod constants;
73pub mod did;
74pub mod doc;
75pub mod endpoint;
76pub mod error;
77pub mod identity;
78pub mod inbox;
79pub mod interfaces;
80pub mod ipfs;
81#[cfg(feature = "iroh")]
82#[allow(dead_code)]
83mod iroh;
84pub mod key;
85#[cfg(all(feature = "kubo", not(target_arch = "wasm32")))]
86mod kubo;
87#[cfg(all(feature = "kubo", not(target_arch = "wasm32")))]
88pub use kubo::{cat_bytes, ipfs_add};
89pub mod msg;
90mod multiformat;
91#[cfg(feature = "iroh")]
92#[allow(dead_code)]
93mod outbox;
94pub mod service;
95pub mod transport;
96pub(crate) mod ttl_queue;
97
98// ─── Re-export DID/message primitives ───────────────────────────────────────
99
100pub use did::{Did, DID_PREFIX};
101pub use doc::{
102 now_iso_utc, Document, MaExtension, Proof, VerificationMethod, DEFAULT_DID_CONTEXT,
103 DEFAULT_PROOF_PURPOSE, DEFAULT_PROOF_TYPE,
104};
105pub use error::{Error, MaError, Result};
106pub use identity::{
107 generate_identity, generate_identity_from_secret, ipns_from_secret, GeneratedIdentity,
108};
109pub use ipld_core::ipld::Ipld;
110pub use key::{
111 EncryptionKey, SigningKey, ASSERTION_METHOD_KEY_TYPE, CODEC_ED25519_PUB, CODEC_EDDSA_SIG,
112 CODEC_X25519_PUB, KEY_AGREEMENT_KEY_TYPE,
113};
114pub use msg::{
115 decode_content, encode_content, Envelope, Headers, Message, ReplayGuard,
116 DEFAULT_MAX_CLOCK_SKEW_SECS, DEFAULT_MESSAGE_TTL_SECS, DEFAULT_REPLAY_WINDOW_SECS,
117 MESSAGE_PREFIX,
118};
119pub use multiformat::{
120 CODEC_CBOR, CODEC_DAG_CBOR, CODEC_DAG_JSON, CODEC_IDENTITY, CODEC_JSON, CODEC_RAW,
121};
122
123#[cfg(feature = "acl")]
124pub use acl::{
125 check_cap, is_principal_key, is_valid_acl_key, normalize_principal, validate_acl_map, AclMap,
126 CapabilityEntry, CAP_ACL, CAP_CREATE, CAP_CRUD, CAP_DELETE, CAP_IDENTITY_PUBLISH, CAP_INBOX,
127 CAP_IPFS, CAP_READ, CAP_RPC, CAP_UPDATE, GROUP_PREFIX, LOCAL_ENTITY_WILDCARD,
128};
129
130// ─── Re-export service constants ────────────────────────────────────────────
131
132pub use service::{
133 Service, CONTENT_TYPE_CBOR, CONTENT_TYPE_TERM, CONTENT_TYPE_TERM_CBOR, CONTENT_TYPE_TERM_YAML,
134 CRUD_PROTOCOL_ID, INBOX_PROTOCOL_ID, IPFS_PROTOCOL_ID, MESSAGE_TYPE_BROADCAST,
135 MESSAGE_TYPE_CHAT, MESSAGE_TYPE_CRUD, MESSAGE_TYPE_CRUD_REPLY, MESSAGE_TYPE_DOC,
136 MESSAGE_TYPE_EMOTE, MESSAGE_TYPE_IDENTITY_PUBLISH_REQUEST, MESSAGE_TYPE_IPFS_REQUEST,
137 MESSAGE_TYPE_MESSAGE, MESSAGE_TYPE_RPC, MESSAGE_TYPE_RPC_REPLY, RPC_PROTOCOL_ID,
138};
139
140// ─── Re-export Inbox ────────────────────────────────────────────────────────
141
142pub use inbox::Inbox;
143
144// ─── Re-export endpoint trait and implementations ───────────────────────────
145
146pub use endpoint::{MaEndpoint, DEFAULT_DELIVERY_PROTOCOL_ID};
147#[cfg(feature = "iroh")]
148pub use outbox::Outbox;
149
150/// Create a default ma endpoint backend from 32-byte secret key material.
151///
152/// This keeps the transport backend type internal while exposing
153/// [`MaEndpoint`] and [`Outbox`] as stable API surfaces.
154#[cfg(feature = "iroh")]
155pub async fn new_ma_endpoint(secret_bytes: [u8; 32], ipv6: bool) -> Result<Box<dyn MaEndpoint>> {
156 let endpoint = iroh::new_endpoint(secret_bytes, ipv6).await?;
157 Ok(Box::new(endpoint))
158}
159
160// ─── Re-export transport parsing ────────────────────────────────────────────
161
162pub use transport::{
163 endpoint_id_from_transport, endpoint_id_from_transport_value, normalize_endpoint_id,
164 protocol_from_transport, resolve_endpoint_for_protocol, resolve_inbox_endpoint_id,
165 transport_string,
166};
167
168// ─── Re-export identity helpers ─────────────────────────────────────────────
169
170pub use identity::{generate_secret_key_file, load_secret_key_bytes, socket_addr_to_multiaddr};
171
172// ─── Re-export config types ──────────────────────────────────────────────────
173
174#[cfg(all(feature = "config", not(target_arch = "wasm32")))]
175pub use config::MaArgs;
176#[cfg(feature = "config")]
177pub use config::{BrowserIdentityExport, Config, SecretBundle};
178
179// ─── Re-export DID resolution ───────────────────────────────────────────────
180
181pub use ipfs::gateway_resolver::{DidDocumentResolver, IpfsGatewayResolver};
182
183// ─── Re-export existing modules ─────────────────────────────────────────────
184
185pub use interfaces::{DidPublisher, IpfsPublisher};
186pub use ipfs::*;