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).
9//! - **Service inboxes** — bounded, TTL-aware FIFO queues ([`Inbox`])
10//! for receiving validated messages on named protocol services.
11//! - **Outbox sending** — fire-and-forget delivery of validated [`Message`] objects
12//! to remote endpoints, serialized to CBOR on the wire ([`Outbox`]).
13//! - **Endpoint abstraction** — the [`MaEndpoint`] trait with an iroh-backed
14//! implementation ([`IrohEndpoint`], behind the `iroh` feature).
15//! - **Transport parsing** — extract endpoint IDs and protocols from DID document
16//! service strings (`/iroh/<id>/<protocol>`).
17//! - **Identity bootstrap** — secure secret key generation and persistence.
18//!
19//! ## Services
20//!
21//! Every endpoint must provide `/ma/inbox/0.0.1` (the default inbox).
22//! Endpoints may optionally provide `ma/ipfs/0.0.1` to publish DID documents
23//! on behalf of others.
24//!
25//! ## Feature flags
26//!
27//! - **`kubo`** — enables Kubo RPC client for IPFS publishing (native only).
28//! - **`iroh`** — enables the iroh QUIC transport backend ([`IrohEndpoint`],
29//! [`Channel`], [`Outbox`]).
30//! - **`gossip`** — enables iroh-gossip broadcast helpers.
31//! - **`config`** — enables [`Config`], [`SecretBundle`], and [`MaArgs`] for
32//! YAML-based daemon configuration, encrypted secret bundles, and CLI
33//! argument parsing.
34//!
35//! ## Platform support
36//!
37//! Core types (`Inbox`, `Service`, transport parsing, validation)
38//! compile on all targets including `wasm32-unknown-unknown`. Kubo/IPFS
39//! traffic requires a native target.
40//!
41//! ### wasm vs native
42//!
43//! - `ma-core` supports both wasm and native targets.
44//! - All IPFS-related APIs are native-only (`not(wasm32)` + `kubo` feature).
45//! - wasm builds do not expose the `ipfs` module or Kubo/IPFS helpers.
46//! - `config` serialization and `SecretBundle` crypto work on wasm.
47//! - `config` filesystem paths, CLI/env merging, and file I/O are native-only.
48//! - If your wasm application needs IPFS access, use a wasm-capable IPFS
49//! client in the application layer.
50
51#![forbid(unsafe_code)]
52#![allow(
53 clippy::cast_possible_truncation,
54 clippy::cast_precision_loss,
55 clippy::if_not_else,
56 clippy::items_after_statements,
57 clippy::manual_let_else,
58 clippy::map_unwrap_or,
59 clippy::missing_errors_doc,
60 clippy::must_use_candidate,
61 clippy::uninlined_format_args
62)]
63
64#[cfg(feature = "acl")]
65pub mod acl;
66#[cfg(feature = "config")]
67pub mod config;
68pub mod endpoint;
69pub mod error;
70pub mod identity;
71pub mod inbox;
72pub mod interfaces;
73#[cfg(all(not(target_arch = "wasm32"), feature = "kubo"))]
74pub mod ipfs;
75#[cfg(feature = "iroh")]
76pub mod iroh;
77#[cfg(feature = "iroh")]
78pub mod outbox;
79pub mod resolve;
80pub mod service;
81pub mod topic;
82pub mod transport;
83pub(crate) mod ttl_queue;
84
85// ─── Re-export did-ma types so users don't need a separate dependency ───────
86
87pub use did_ma::{
88 Did, Document, EncryptionKey, Headers, MaError, Message, Proof, ReplayGuard, SigningKey,
89 VerificationMethod, DEFAULT_MAX_CLOCK_SKEW_SECS, DEFAULT_MESSAGE_TTL_SECS,
90 DEFAULT_REPLAY_WINDOW_SECS,
91};
92
93// ─── Re-export core error type ──────────────────────────────────────────────
94
95pub use error::{Error, Result};
96
97#[cfg(feature = "acl")]
98pub use acl::Acl;
99
100// ─── Re-export service constants ────────────────────────────────────────────
101
102pub use service::{
103 Service, BROADCAST_PROTOCOL, BROADCAST_TOPIC, CONTENT_TYPE_BROADCAST, CONTENT_TYPE_DOC,
104 CONTENT_TYPE_IPFS_REQUEST, CONTENT_TYPE_MESSAGE, INBOX_PROTOCOL, INBOX_PROTOCOL_ID,
105 IPFS_PROTOCOL,
106};
107
108// ─── Re-export Inbox ────────────────────────────────────────────────────────
109
110pub use inbox::Inbox;
111
112// ─── Re-export Topic ────────────────────────────────────────────────────────
113
114pub use topic::{topic_id, Topic, TopicId};
115
116// ─── Re-export endpoint trait and implementations ───────────────────────────
117
118pub use endpoint::{MaEndpoint, DEFAULT_DELIVERY_PROTOCOL_ID};
119#[cfg(feature = "iroh")]
120pub use iroh::channel::Channel;
121#[cfg(feature = "iroh")]
122pub use iroh::IrohEndpoint;
123#[cfg(feature = "iroh")]
124pub use outbox::Outbox;
125
126// ─── Re-export iroh primitives so dependents don't need a direct iroh dep ───
127
128#[cfg(feature = "iroh")]
129pub use ::iroh::endpoint::{presets, Connection, RecvStream, SendStream};
130#[cfg(feature = "iroh")]
131pub use ::iroh::protocol::{AcceptError, ProtocolHandler, Router};
132#[cfg(feature = "iroh")]
133pub use ::iroh::{Endpoint, EndpointAddr, EndpointId, RelayUrl, SecretKey};
134
135// ─── Re-export gossip helpers ────────────────────────────────────────────────
136
137#[cfg(feature = "gossip")]
138pub use iroh::gossip::{
139 broadcast_topic_id, gossip_send, gossip_send_text, join_broadcast_channel, join_gossip_topic,
140 topic_id_for,
141};
142
143// ─── Re-export transport parsing ────────────────────────────────────────────
144
145pub use transport::{
146 endpoint_id_from_transport, endpoint_id_from_transport_value, normalize_endpoint_id,
147 protocol_from_transport, resolve_endpoint_for_protocol, resolve_inbox_endpoint_id,
148 transport_string,
149};
150
151// ─── Re-export identity helpers ─────────────────────────────────────────────
152
153pub use identity::{generate_secret_key_file, load_secret_key_bytes, socket_addr_to_multiaddr};
154
155// ─── Re-export config types ──────────────────────────────────────────────────
156
157#[cfg(all(feature = "config", not(target_arch = "wasm32")))]
158pub use config::MaArgs;
159#[cfg(feature = "config")]
160pub use config::{BrowserIdentityExport, Config, SecretBundle};
161
162// ─── Re-export DID resolution ───────────────────────────────────────────────
163
164pub use resolve::DidResolver;
165#[cfg(not(target_arch = "wasm32"))]
166pub use resolve::GatewayResolver;
167
168// ─── Re-export existing modules ─────────────────────────────────────────────
169
170pub use interfaces::{DidPublisher, IpfsPublisher};
171#[cfg(all(not(target_arch = "wasm32"), feature = "kubo"))]
172pub use ipfs::*;