use ed25519_dalek::SigningKey;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
pub const IDENTITY_BINDING_SCHEMA: &str = "vela.identity_binding.v0.1";
pub const IDENTITY_REVOCATION_SCHEMA: &str = "vela.identity_revocation.v0.1";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ActorClass {
Human,
Agent,
Org,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdentityBinding {
pub schema: String,
pub binding_id: String,
pub actor_id: String,
pub actor_class: ActorClass,
pub public_key_hex: String,
pub created_at: String,
pub signature: String,
}
pub struct IdentityBindingDraft {
pub actor_id: String,
pub actor_class: ActorClass,
pub created_at: String,
}
impl IdentityBinding {
pub fn build(draft: IdentityBindingDraft, key: &SigningKey) -> Result<Self, String> {
if draft.actor_id.trim().is_empty() {
return Err("identity_binding.actor_id cannot be empty".to_string());
}
let mut b = IdentityBinding {
schema: IDENTITY_BINDING_SCHEMA.to_string(),
binding_id: String::new(),
actor_id: draft.actor_id,
actor_class: draft.actor_class,
public_key_hex: hex::encode(key.verifying_key().to_bytes()),
created_at: draft.created_at,
signature: String::new(),
};
let preimage = b.id_preimage_bytes()?;
b.signature = hex::encode(crate::sign::sign_bytes(key, &preimage));
b.binding_id = b.derive_id()?;
Ok(b)
}
fn id_preimage_bytes(&self) -> Result<Vec<u8>, String> {
let mut p = self.clone();
p.binding_id = String::new();
p.signature = String::new();
crate::canonical::to_canonical_bytes(&p)
.map_err(|e| format!("canonicalize identity_binding preimage: {e}"))
}
pub fn derive_id(&self) -> Result<String, String> {
let bytes = self.id_preimage_bytes()?;
Ok(format!(
"vib_{}",
&hex::encode(Sha256::digest(&bytes))[..16]
))
}
pub fn credential_root(&self) -> Result<String, String> {
let bytes = self.id_preimage_bytes()?;
Ok(format!("sha256:{}", hex::encode(Sha256::digest(&bytes))))
}
pub fn verify(&self) -> Result<(), String> {
if self.schema != IDENTITY_BINDING_SCHEMA {
return Err(format!(
"identity_binding.schema must be `{IDENTITY_BINDING_SCHEMA}`"
));
}
if !self.binding_id.starts_with("vib_") {
return Err(format!(
"binding id must start with `vib_`, got `{}`",
self.binding_id
));
}
let preimage = self.id_preimage_bytes()?;
if !crate::sign::verify_action_signature(&preimage, &self.signature, &self.public_key_hex)?
{
return Err(
"identity_binding signature does not verify under the bound key \
(no proof of possession)"
.to_string(),
);
}
let rederived = self.derive_id()?;
if rederived != self.binding_id {
return Err(format!(
"binding_id mismatch: declared {}, rebuilt {}",
self.binding_id, rederived
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdentityRevocation {
pub schema: String,
pub revocation_id: String,
pub binding_id: String,
pub public_key_hex: String,
pub reason: String,
pub revoked_at: String,
pub signature: String,
}
pub struct IdentityRevocationDraft {
pub binding_id: String,
pub reason: String,
pub revoked_at: String,
}
impl IdentityRevocation {
pub fn build(draft: IdentityRevocationDraft, key: &SigningKey) -> Result<Self, String> {
if !draft.binding_id.starts_with("vib_") {
return Err("identity_revocation.binding_id must be a `vib_` id".to_string());
}
let mut r = IdentityRevocation {
schema: IDENTITY_REVOCATION_SCHEMA.to_string(),
revocation_id: String::new(),
binding_id: draft.binding_id,
public_key_hex: hex::encode(key.verifying_key().to_bytes()),
reason: draft.reason,
revoked_at: draft.revoked_at,
signature: String::new(),
};
let preimage = r.id_preimage_bytes()?;
r.signature = hex::encode(crate::sign::sign_bytes(key, &preimage));
r.revocation_id = format!("vir_{}", &hex::encode(Sha256::digest(&preimage))[..16]);
Ok(r)
}
fn id_preimage_bytes(&self) -> Result<Vec<u8>, String> {
let mut p = self.clone();
p.revocation_id = String::new();
p.signature = String::new();
crate::canonical::to_canonical_bytes(&p)
.map_err(|e| format!("canonicalize identity_revocation preimage: {e}"))
}
pub fn verify(&self) -> Result<(), String> {
let preimage = self.id_preimage_bytes()?;
if !crate::sign::verify_action_signature(&preimage, &self.signature, &self.public_key_hex)?
{
return Err(
"identity_revocation signature does not verify under the declared key".to_string(),
);
}
Ok(())
}
#[must_use]
pub fn authoritatively_revokes(&self, binding: &IdentityBinding) -> bool {
self.binding_id == binding.binding_id
&& self.public_key_hex == binding.public_key_hex
&& self.verify().is_ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::rngs::OsRng;
fn key() -> SigningKey {
SigningKey::generate(&mut OsRng)
}
fn draft() -> IdentityBindingDraft {
IdentityBindingDraft {
actor_id: "reviewer:will-blair".into(),
actor_class: ActorClass::Human,
created_at: "2026-06-09T00:00:00Z".into(),
}
}
#[test]
fn self_signed_binding_verifies() {
let b = IdentityBinding::build(draft(), &key()).unwrap();
assert!(b.binding_id.starts_with("vib_"));
let full = b.credential_root().unwrap();
assert!(full.starts_with("sha256:"));
assert_eq!(
&full["sha256:".len()..][..16],
&b.binding_id["vib_".len()..]
);
b.verify().unwrap();
}
#[test]
fn binding_signed_by_other_key_fails_possession() {
let mut b = IdentityBinding::build(draft(), &key()).unwrap();
b.public_key_hex = hex::encode(key().verifying_key().to_bytes());
assert!(b.verify().is_err());
}
#[test]
fn self_revocation_is_authoritative_and_foreign_is_not() {
let k = key();
let b = IdentityBinding::build(draft(), &k).unwrap();
let rev = IdentityRevocation::build(
IdentityRevocationDraft {
binding_id: b.binding_id.clone(),
reason: "key rotated".into(),
revoked_at: "2026-06-10T00:00:00Z".into(),
},
&k,
)
.unwrap();
assert!(rev.authoritatively_revokes(&b), "same key must revoke");
let foreign = IdentityRevocation::build(
IdentityRevocationDraft {
binding_id: b.binding_id.clone(),
reason: "malicious".into(),
revoked_at: "2026-06-10T00:00:00Z".into(),
},
&key(),
)
.unwrap();
assert!(
!foreign.authoritatively_revokes(&b),
"foreign key cannot revoke"
);
}
}