Skip to main content

ma_did/
lib.rs

1#![forbid(unsafe_code)]
2//! # ma-did
3//!
4//! DID and message primitives for the `did:ma:` method.
5//!
6//! This crate provides everything needed to create, sign, and verify DID
7//! documents and encrypted actor-to-actor messages.
8//!
9//! ## Quick Start — Identity
10//!
11//! ```rust
12//! use ma_did::{generate_identity, Did};
13//!
14//! // Generate a complete identity (keys + signed document)
15//! let ipns = "k51qzi5uqu5dj9807pbuod1pplf0vxh8m4lfy3ewl9qbm2s8dsf9ugdf9gedhr";
16//! let identity = generate_identity(ipns).unwrap();
17//!
18//! // The document is already signed and valid
19//! identity.document.verify().unwrap();
20//! identity.document.validate().unwrap();
21//!
22//! // Serialize to JSON or CBOR
23//! let json = identity.document.marshal().unwrap();
24//! let cbor = identity.document.to_cbor().unwrap();
25//! ```
26//!
27//! ## Quick Start — Messages
28//!
29//! ```rust
30//! use ma_did::{generate_identity, Message, SigningKey, Did};
31//!
32//! let alice = generate_identity("k51qzi5uqu5dj9807pbuod1pplf0vxh8m4lfy3ewl9qbm2s8dsf9ugdf9gedhr").unwrap();
33//! let bob = generate_identity("k51qzi5uqu5dl96qbq93mwl5drvk2z83fk4s6h4n7xgqnwrxlscs11i1bja7uk").unwrap();
34//!
35//! // Reconstruct signing key from stored private key bytes
36//! let alice_sign_url = Did::new_url(&alice.subject_url.ipns, None::<String>).unwrap();
37//! let alice_signing_key = SigningKey::from_private_key_bytes(
38//!     alice_sign_url,
39//!     hex::decode(&alice.signing_private_key_hex).unwrap().try_into().unwrap(),
40//! ).unwrap();
41//!
42//! // Create a signed message
43//! let msg = Message::new(
44//!     alice.document.id.clone(),
45//!     bob.document.id.clone(),
46//!     "text/plain",
47//!     b"hello".to_vec(),
48//!     &alice_signing_key,
49//! ).unwrap();
50//!
51//! // Verify message signature against sender's document
52//! msg.verify_with_document(&alice.document).unwrap();
53//!
54//! // Encrypt for recipient as an Envelope
55//! let envelope = msg.enclose_for(&bob.document).unwrap();
56//! ```
57
58pub mod constants;
59pub mod did;
60pub mod doc;
61pub mod error;
62pub mod identity;
63pub mod key;
64pub mod msg;
65mod multiformat;
66
67pub use did::{DID_PREFIX, Did};
68pub use doc::{
69    DEFAULT_DID_CONTEXT, DEFAULT_PROOF_PURPOSE, DEFAULT_PROOF_TYPE, Document, Proof,
70    VerificationMethod, now_iso_utc,
71};
72pub use error::{MaError, Result};
73pub use identity::{GeneratedIdentity, generate_identity};
74pub use ipld_core::ipld::Ipld;
75pub use key::{
76    ASSERTION_METHOD_KEY_TYPE, ED25519_PUB_CODEC, EDDSA_SIG_CODEC, EncryptionKey,
77    KEY_AGREEMENT_KEY_TYPE, SigningKey, X25519_PUB_CODEC,
78};
79pub use msg::{
80    DEFAULT_MAX_CLOCK_SKEW_SECS, DEFAULT_MESSAGE_TTL_SECS, DEFAULT_REPLAY_WINDOW_SECS, Envelope,
81    Headers, MESSAGE_PREFIX, Message, ReplayGuard,
82};