Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub context: Vec<String>,
    pub id: String,
    pub controller: Vec<String>,
    pub verification_method: Vec<VerificationMethod>,
    pub assertion_method: Vec<String>,
    pub key_agreement: Vec<String>,
    pub proof: Proof,
    pub identity: Option<String>,
    pub created_at: String,
    pub updated_at: String,
    pub ma: Option<Ipld>,
}
Expand description

A did:ma: DID document.

Contains verification methods, proof, and optional extension data. Documents are signed with Ed25519 over a BLAKE3 hash of the dag-cbor-serialized payload (all fields except proof).

§Examples

use ma_did::{generate_identity, Document};

let id = generate_identity(
    "k51qzi5uqu5dj9807pbuod1pplf0vxh8m4lfy3ewl9qbm2s8dsf9ugdf9gedhr"
).unwrap();

// Verify the signature
id.document.verify().unwrap();

// Validate structural correctness
id.document.validate().unwrap();

// Round-trip through JSON
let json = id.document.marshal().unwrap();
let restored = Document::unmarshal(&json).unwrap();
assert_eq!(id.document, restored);

// Round-trip through CBOR
let cbor = id.document.to_cbor().unwrap();
let restored = Document::from_cbor(&cbor).unwrap();
assert_eq!(id.document, restored);

§Extension namespace

The ma field is an opaque IPLD value for application-defined extension data. did-ma does not interpret or validate its contents. Using Ipld gives native support for CID links and dag-cbor/dag-json round-tripping.

use std::collections::BTreeMap;
use ipld_core::ipld::Ipld;
use ma_did::{Did, Document};

let did = Did::new_url("k51qzi5uqu5dj9807pbuod1pplf0vxh8m4lfy3ewl9qbm2s8dsf9ugdf9gedhr", None::<String>).unwrap();
let mut doc = Document::new(&did, &did);
let ma = Ipld::Map(BTreeMap::from([
    ("type".into(), Ipld::String("agent".into())),
    ("services".into(), Ipld::Map(BTreeMap::new())),
]));
doc.set_ma(ma);
assert!(doc.ma.is_some());
doc.clear_ma();
assert!(doc.ma.is_none());

Fields§

§context: Vec<String>§id: String§controller: Vec<String>§verification_method: Vec<VerificationMethod>§assertion_method: Vec<String>§key_agreement: Vec<String>§proof: Proof§identity: Option<String>§created_at: String§updated_at: String§ma: Option<Ipld>

Implementations§

Source§

impl Document

Source

pub fn new(identity: &Did, controller: &Did) -> Self

Source

pub fn set_ma(&mut self, ma: Ipld)

Set the opaque ma extension namespace.

Source

pub fn clear_ma(&mut self)

Clear the ma extension namespace.

Source

pub fn to_cbor(&self) -> Result<Vec<u8>>

Source

pub fn from_cbor(bytes: &[u8]) -> Result<Self>

Source

pub fn marshal(&self) -> Result<String>

Source

pub fn unmarshal(s: &str) -> Result<Self>

Source

pub fn add_controller(&mut self, controller: impl Into<String>) -> Result<()>

Source

pub fn add_verification_method( &mut self, method: VerificationMethod, ) -> Result<()>

Source

pub fn get_verification_method_by_id( &self, method_id: &str, ) -> Result<&VerificationMethod>

Source

pub fn set_identity(&mut self, identity: impl Into<String>) -> Result<()>

Source

pub fn touch(&mut self)

Update the updatedAt timestamp to the current time.

Source

pub fn assertion_method_public_key(&self) -> Result<VerifyingKey>

Source

pub fn key_agreement_public_key_bytes(&self) -> Result<[u8; 32]>

Source

pub fn payload_document(&self) -> Self

Source

pub fn payload_bytes(&self) -> Result<Vec<u8>>

Source

pub fn payload_hash(&self) -> Result<[u8; 32]>

Source

pub fn sign( &mut self, signing_key: &SigningKey, verification_method: &VerificationMethod, ) -> Result<()>

Source

pub fn verify(&self) -> Result<()>

Source

pub fn validate(&self) -> Result<()>

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Document

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Document

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Document

Source§

fn eq(&self, other: &Document) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Document

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Document

Source§

impl StructuralPartialEq for Document

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,