exochain-sdk 0.2.0-beta

EXOCHAIN SDK — ergonomic Rust API for the constitutional governance fabric
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright 2026 Exochain Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

//! Decentralized identity — DIDs backed by Ed25519 keypairs.
//!
//! [`Identity`] is the SDK's ergonomic handle to a DID with its associated
//! Ed25519 keypair. The DID is derived deterministically from the public key
//! so that two identities generated with different entropy always produce
//! different DIDs, while an identity re-constructed from the same key bytes
//! always yields the same DID.
//!
//! ## When to reach for this module
//!
//! - Every actor in EXOCHAIN — human, AI agent, service, organization — needs
//!   an [`Identity`]. The identity's DID is the principal that appears in
//!   bailments, decisions, authority chains, and kernel adjudications.
//! - Signatures produced by [`Identity::sign`] feed straight into the kernel
//!   as proof of provenance.
//!
//! ## Quick start
//!
//! ```
//! use exochain_sdk::identity::Identity;
//!
//! let alice = Identity::generate("alice");
//! let sig = alice.sign(b"hello");
//! assert!(alice.verify(b"hello", &sig));
//! ```

use exo_core::{
    Did, PublicKey, SecretKey, Signature, Timestamp,
    crypto::{generate_keypair, sign as core_sign, verify as core_verify},
};
use exo_identity::did::DidDocument;

use crate::error::{ExoError, ExoResult};

const KEYPAIR_PROOF_MESSAGE: &[u8] = b"exo.sdk.identity.keypair.v1";

/// A DID paired with its Ed25519 keypair and a human-readable label.
///
/// Local identities are created either with [`Identity::generate`] (fresh
/// random keypair) or [`Identity::from_keypair`] (reuse an existing keypair,
/// e.g. loaded from a keystore). The local SDK DID is derived from the public
/// key as:
///
/// ```text
/// did:exo: + first 16 hex chars of BLAKE3(public_key_bytes)
/// ```
///
/// Use [`Identity::from_resolved_keypair`] when the canonical DID has already
/// been resolved from the fabric and must not be re-derived locally.
///
/// The `Debug` implementation deliberately redacts the secret key so identities
/// can be logged without leaking private material.
///
/// # Examples
///
/// ```
/// use exochain_sdk::identity::Identity;
///
/// let alice = Identity::generate("alice");
/// assert!(alice.did().as_str().starts_with("did:exo:"));
/// assert_eq!(alice.label(), "alice");
///
/// // Debug never leaks the secret key.
/// let debug = format!("{alice:?}");
/// assert!(debug.contains("***"));
/// ```
pub struct Identity {
    did: Did,
    public: PublicKey,
    secret: SecretKey,
    label: String,
}

impl Identity {
    /// Generate a fresh identity with a random Ed25519 keypair and a DID
    /// derived from the public key.
    ///
    /// The `label` is stored alongside the identity for developer convenience
    /// and is never cryptographically bound to the DID — renaming an identity
    /// does not change its DID.
    ///
    /// Use this when you need a brand-new principal. Use
    /// [`Identity::from_keypair`] when loading a previously generated keypair
    /// from storage.
    ///
    /// This method is infallible in practice: the method-specific portion of
    /// the DID is always 16 lowercase hex characters, which satisfies DID
    /// validation. In the unreachable case of validation failure, the DID
    /// falls back to `did:exo:sdk-fallback`.
    ///
    /// # Examples
    ///
    /// ```
    /// use exochain_sdk::identity::Identity;
    ///
    /// let id = Identity::generate("agent");
    /// assert!(id.did().as_str().starts_with("did:exo:"));
    /// assert_eq!(id.did().as_str().len(), "did:exo:".len() + 16);
    /// ```
    ///
    /// Two fresh identities have different DIDs:
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let a = Identity::generate("a");
    /// let b = Identity::generate("b");
    /// assert_ne!(a.did(), b.did());
    /// ```
    #[must_use]
    pub fn generate(label: &str) -> Self {
        let (public, secret) = generate_keypair();
        let did = derive_did(&public).unwrap_or_else(|_| fallback_did());
        Self {
            did,
            public,
            secret,
            label: label.to_owned(),
        }
    }

    /// Build an [`Identity`] from an existing Ed25519 keypair and label.
    ///
    /// The DID is derived from the public key in the same way as
    /// [`Identity::generate`]. Reconstructing an identity from the same
    /// keypair always produces the same DID.
    ///
    /// Use this to load a persisted identity, or to rehydrate the identity
    /// belonging to another actor when you only know their public material.
    ///
    /// # Errors
    ///
    /// Returns [`ExoError::InvalidDid`] only in the highly unlikely case that
    /// BLAKE3 output somehow failed DID validation — this should be
    /// unreachable in practice.
    ///
    /// # Examples
    ///
    /// Build an identity from a freshly generated keypair using the
    /// underlying core crypto primitives.
    ///
    /// ```
    /// use exochain_sdk::identity::Identity;
    /// use exo_core::crypto::generate_keypair;
    ///
    /// let (public, secret) = generate_keypair();
    /// let alice = Identity::from_keypair("alice", public, secret)?;
    /// assert!(alice.did().as_str().starts_with("did:exo:"));
    /// assert_eq!(alice.label(), "alice");
    /// # Ok::<(), exochain_sdk::error::ExoError>(())
    /// ```
    pub fn from_keypair(label: &str, public: PublicKey, secret: SecretKey) -> ExoResult<Self> {
        let did = derive_did(&public)?;
        Self::from_resolved_keypair(label, did, public, secret)
    }

    /// Build an [`Identity`] from an existing Ed25519 keypair and a DID that
    /// was resolved from the canonical fabric.
    ///
    /// Unlike [`Identity::from_keypair`], this method preserves the supplied
    /// DID instead of re-deriving a local SDK DID from the public key. It is
    /// intended for cross-language or gateway-backed flows where the fabric
    /// has already resolved the DID document and associated public key.
    ///
    /// # Errors
    ///
    /// Returns [`ExoError::Identity`] when the supplied secret key does not
    /// match the supplied public key.
    ///
    /// # Examples
    ///
    /// ```
    /// use exo_core::{Did, crypto::generate_keypair};
    /// use exochain_sdk::identity::Identity;
    ///
    /// let (public, secret) = generate_keypair();
    /// let did = Did::new("did:exo:fabric-resolved")?;
    /// let identity = Identity::from_resolved_keypair("alice", did.clone(), public, secret)?;
    /// assert_eq!(identity.did(), &did);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_resolved_keypair(
        label: &str,
        did: Did,
        public: PublicKey,
        secret: SecretKey,
    ) -> ExoResult<Self> {
        verify_keypair_match(&public, &secret)?;
        Ok(Self {
            did,
            public,
            secret,
            label: label.to_owned(),
        })
    }

    /// Return the DID for this identity.
    ///
    /// # Examples
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let id = Identity::generate("alice");
    /// assert!(id.did().as_str().starts_with("did:exo:"));
    /// ```
    #[must_use]
    pub fn did(&self) -> &Did {
        &self.did
    }

    /// Return the Ed25519 public key.
    ///
    /// Public keys are safe to share and can be handed to counterparties so
    /// they can verify signatures this identity produces.
    ///
    /// # Examples
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let id = Identity::generate("alice");
    /// let pk = id.public_key();
    /// assert_eq!(pk.as_bytes().len(), 32);
    /// ```
    #[must_use]
    pub fn public_key(&self) -> &PublicKey {
        &self.public
    }

    /// Return the human-readable label.
    ///
    /// Labels are developer affordances and have no cryptographic meaning.
    ///
    /// # Examples
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let id = Identity::generate("alice");
    /// assert_eq!(id.label(), "alice");
    /// ```
    #[must_use]
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Sign `message` with this identity's secret key (Ed25519).
    ///
    /// The returned [`Signature`] is suitable for feeding into
    /// [`Identity::verify`] or into the kernel's provenance field.
    ///
    /// # Examples
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let id = Identity::generate("signer");
    /// let sig = id.sign(b"hello");
    /// assert!(id.verify(b"hello", &sig));
    /// ```
    #[must_use]
    pub fn sign(&self, message: &[u8]) -> Signature {
        core_sign(message, &self.secret)
    }

    /// Verify `signature` over `message` against this identity's public key.
    ///
    /// Returns `true` only if the signature was produced by the matching
    /// secret key over exactly these message bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let id = Identity::generate("signer");
    /// let sig = id.sign(b"hello");
    /// assert!(id.verify(b"hello", &sig));
    /// assert!(!id.verify(b"tampered", &sig));
    /// ```
    #[must_use]
    pub fn verify(&self, message: &[u8], signature: &Signature) -> bool {
        core_verify(message, signature, &self.public)
    }

    /// Build a minimal [`DidDocument`] describing this identity.
    ///
    /// The resulting document contains the identity's single Ed25519 public
    /// key, with empty authentication/verification-method/service-endpoint
    /// lists and `created == updated == Timestamp::ZERO`. Callers can augment
    /// the document after construction if they need richer DID metadata.
    ///
    /// # Examples
    ///
    /// ```
    /// # use exochain_sdk::identity::Identity;
    /// let id = Identity::generate("alice");
    /// let doc = id.did_document();
    /// assert_eq!(&doc.id, id.did());
    /// assert_eq!(doc.public_keys.len(), 1);
    /// assert!(!doc.revoked);
    /// ```
    #[must_use]
    pub fn did_document(&self) -> DidDocument {
        DidDocument {
            id: self.did.clone(),
            public_keys: vec![self.public],
            authentication: Vec::new(),
            verification_methods: Vec::new(),
            hybrid_verification_methods: Vec::new(),
            service_endpoints: Vec::new(),
            created: Timestamp::ZERO,
            updated: Timestamp::ZERO,
            revoked: false,
        }
    }
}

impl core::fmt::Debug for Identity {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Identity")
            .field("did", &self.did)
            .field("label", &self.label)
            .field("public", &self.public)
            .field("secret", &"***")
            .finish()
    }
}

/// Derive `did:exo:<first 16 hex chars of BLAKE3(public_key_bytes)>`.
fn derive_did(public: &PublicKey) -> ExoResult<Did> {
    let digest = blake3::hash(public.as_bytes());
    let bytes = digest.as_bytes();
    let mut hex = String::with_capacity(16);
    for byte in bytes.iter().take(8) {
        hex.push_str(&format!("{byte:02x}"));
    }
    let did_str = format!("did:exo:{hex}");
    Did::new(&did_str).map_err(|e| ExoError::InvalidDid(e.to_string()))
}

fn verify_keypair_match(public: &PublicKey, secret: &SecretKey) -> ExoResult<()> {
    let signature = core_sign(KEYPAIR_PROOF_MESSAGE, secret);
    if core_verify(KEYPAIR_PROOF_MESSAGE, &signature, public) {
        Ok(())
    } else {
        Err(ExoError::Identity(
            "secret key does not match public key".to_owned(),
        ))
    }
}

/// Fallback DID for the unreachable case where hex-derived DIDs fail
/// validation.  Never observed in practice; present only so that
/// [`Identity::generate`] remains infallible.
#[allow(clippy::expect_used)] // static DID "did:exo:sdk-fallback" is unconditionally valid
fn fallback_did() -> Did {
    Did::new("did:exo:sdk-fallback").expect("did:exo:sdk-fallback is a well-formed DID")
}

// ===========================================================================
// Tests
// ===========================================================================

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use serde::Deserialize;

    use super::*;
    use crate::error::ExoError;

    #[derive(Debug, Deserialize)]
    struct DidDerivationFixtures {
        vectors: Vec<DidDerivationVector>,
    }

    #[derive(Debug, Deserialize)]
    struct DidDerivationVector {
        name: String,
        public_key_hex: String,
        expected_did: String,
    }

    fn decode_public_key_hex(hex: &str) -> [u8; 32] {
        assert_eq!(hex.len(), 64, "fixture public key must be 32 bytes");
        let mut bytes = [0u8; 32];
        for (index, chunk) in hex.as_bytes().chunks_exact(2).enumerate() {
            let hex_byte = core::str::from_utf8(chunk).expect("fixture hex is UTF-8");
            bytes[index] = u8::from_str_radix(hex_byte, 16).expect("fixture hex is valid");
        }
        bytes
    }

    #[test]
    fn generate_produces_valid_did() {
        let id = Identity::generate("alice");
        assert!(id.did().as_str().starts_with("did:exo:"));
        // 16 hex chars after the prefix.
        assert_eq!(id.did().as_str().len(), "did:exo:".len() + 16);
    }

    #[test]
    fn generate_stores_label() {
        let id = Identity::generate("alice");
        assert_eq!(id.label(), "alice");
    }

    #[test]
    fn sign_verify_roundtrip() {
        let id = Identity::generate("signer");
        let sig = id.sign(b"hello");
        assert!(id.verify(b"hello", &sig));
    }

    #[test]
    fn verify_rejects_wrong_message() {
        let id = Identity::generate("signer");
        let sig = id.sign(b"original");
        assert!(!id.verify(b"tampered", &sig));
    }

    #[test]
    fn different_identities_produce_different_dids() {
        let a = Identity::generate("a");
        let b = Identity::generate("b");
        // Random keypairs should never collide in practice; the check is on
        // the DID (derived from the public key) not the label.
        assert_ne!(a.did(), b.did());
    }

    #[test]
    fn from_keypair_derives_same_did_as_generate() {
        let id = Identity::generate("first");
        let rebuilt = Identity::from_keypair(
            "rebuilt",
            *id.public_key(),
            SecretKey::from_bytes(*id.secret.as_bytes()),
        )
        .expect("ok");
        assert_eq!(id.did(), rebuilt.did());
        assert_eq!(rebuilt.label(), "rebuilt");
    }

    #[test]
    fn did_derivation_matches_cross_language_vectors() {
        let fixtures: DidDerivationFixtures =
            serde_json::from_str(include_str!("../../../tests/fixtures/did-derivation.json"))
                .expect("DID derivation fixtures parse");

        for vector in fixtures.vectors {
            let public = PublicKey::from_bytes(decode_public_key_hex(&vector.public_key_hex));
            let did = derive_did(&public).expect("fixture DID derives");
            assert_eq!(
                did.as_str(),
                vector.expected_did,
                "fixture {} must match canonical BLAKE3 derivation",
                vector.name
            );
        }
    }

    #[test]
    fn from_resolved_keypair_preserves_fabric_resolved_did() {
        let id = Identity::generate("local");
        let fabric_did = Did::new("did:exo:fabric-resolved").unwrap();

        let rebuilt = Identity::from_resolved_keypair(
            "fabric",
            fabric_did.clone(),
            *id.public_key(),
            SecretKey::from_bytes(*id.secret.as_bytes()),
        )
        .expect("resolved identity");

        assert_eq!(rebuilt.did(), &fabric_did);
        assert_ne!(rebuilt.did(), id.did());
        assert_eq!(rebuilt.label(), "fabric");
        let sig = rebuilt.sign(b"resolved fabric DID");
        assert!(rebuilt.verify(b"resolved fabric DID", &sig));
    }

    #[test]
    fn from_resolved_keypair_rejects_mismatched_public_and_secret_keys() {
        let public_source = Identity::generate("public");
        let secret_source = Identity::generate("secret");
        let fabric_did = Did::new("did:exo:fabric-mismatch").unwrap();

        let err = Identity::from_resolved_keypair(
            "fabric",
            fabric_did,
            *public_source.public_key(),
            SecretKey::from_bytes(*secret_source.secret.as_bytes()),
        )
        .unwrap_err();

        assert!(matches!(
            err,
            ExoError::Identity(msg) if msg.contains("does not match public key")
        ));
    }

    #[test]
    fn did_document_contains_identity_fields() {
        let id = Identity::generate("doc");
        let doc = id.did_document();
        assert_eq!(&doc.id, id.did());
        assert_eq!(doc.public_keys.len(), 1);
        assert_eq!(&doc.public_keys[0], id.public_key());
        assert!(!doc.revoked);
        assert!(doc.authentication.is_empty());
        assert!(doc.verification_methods.is_empty());
        assert!(doc.hybrid_verification_methods.is_empty());
        assert!(doc.service_endpoints.is_empty());
    }

    #[test]
    fn debug_redacts_secret() {
        let id = Identity::generate("secret-test");
        let dbg = format!("{id:?}");
        assert!(dbg.contains("***"));
        assert!(dbg.contains("Identity"));
    }

    #[test]
    fn public_key_accessor() {
        let id = Identity::generate("pk");
        // PublicKey implements Copy; the accessor returns a reference we can
        // compare against itself via dereference.
        let pk = *id.public_key();
        assert_eq!(&pk, id.public_key());
    }
}