hyprstream_discovery/
at9p_alias.rs1use std::sync::Arc;
27
28use anyhow::Result;
29
30use hyprstream_pds::at9p_alias::{resolve_authoritative_alias, AuthoritativeIdentity};
31use hyprstream_pds::at9p_gate::{verify_did_at9p, VerifiedCapsule};
32use hyprstream_rpc::identity::Did;
33
34use crate::at9p_resolver::CapsuleSource;
35
36pub struct At9pAliasResolver {
42 source: Arc<dyn CapsuleSource>,
43}
44
45impl At9pAliasResolver {
46 pub fn new(source: Arc<dyn CapsuleSource>) -> Self {
48 Self { source }
49 }
50
51 pub async fn resolve_authoritative(
63 &self,
64 classical_did: &Did,
65 classical_aka_at9p: &Did,
66 ) -> Result<AuthoritativeIdentity> {
67 Ok(self
68 .resolve_authoritative_with_capsule(classical_did, classical_aka_at9p)
69 .await?
70 .0)
71 }
72
73 pub async fn resolve_authoritative_with_capsule(
79 &self,
80 classical_did: &Did,
81 classical_aka_at9p: &Did,
82 ) -> Result<(AuthoritativeIdentity, VerifiedCapsule)> {
83 let did_str = classical_aka_at9p.as_str();
84 let bytes = self.source.fetch_capsule(did_str).await?;
87 let verified = verify_did_at9p(did_str, &bytes)?;
89 let identity = resolve_authoritative_alias(classical_did, classical_aka_at9p, &verified)?;
91 Ok((identity, verified))
92 }
93}
94
95#[cfg(test)]
96#[allow(clippy::unwrap_used, clippy::expect_used)]
97mod tests {
98 use super::*;
99 use crate::at9p_resolver::CapsuleSource;
100
101 use ed25519_dalek::SigningKey;
102 use hyprstream_crypto::pq::{ml_dsa_generate_keypair, ml_dsa_vk_bytes, MlDsaSigningKey};
103 use hyprstream_pds::at9p::{
104 CapsuleBody, HybridKeyPair, ServiceEndpoint, ServiceEntry, ServiceType, Transport,
105 };
106 use hyprstream_pds::at9p_sign::sign_capsule;
107 use hyprstream_rpc::auth::mac::Assurance;
108
109 struct FixedSource {
111 bytes: Vec<u8>,
112 }
113
114 #[async_trait::async_trait]
115 impl CapsuleSource for FixedSource {
116 async fn fetch_capsule(&self, _did: &str) -> Result<Vec<u8>> {
117 Ok(self.bytes.clone())
118 }
119 }
120
121 struct Signer {
122 ed_sk: SigningKey,
123 pq_sk: MlDsaSigningKey,
124 keypair: HybridKeyPair,
125 }
126
127 fn signer(tag: u8) -> Signer {
128 let mut seed = [0u8; 32];
129 seed[0] = tag;
130 seed[31] = tag.wrapping_add(7);
131 let ed_sk = SigningKey::from_bytes(&seed);
132 let (pq_sk, pq_vk) = ml_dsa_generate_keypair();
133 let keypair = HybridKeyPair::new(
134 ed_sk.verifying_key().to_bytes().to_vec(),
135 ml_dsa_vk_bytes(&pq_vk),
136 )
137 .unwrap();
138 Signer {
139 ed_sk,
140 pq_sk,
141 keypair,
142 }
143 }
144
145 fn signed_with_aliases(aliases: Vec<String>) -> (Vec<u8>, String) {
147 let s = signer(5);
148 let endpoint = ServiceEndpoint::new(Transport::Iroh, "iroh://node5").unwrap();
149 let service = ServiceEntry::new("#ns", ServiceType::NinePExport, endpoint).unwrap();
150 let mut body = CapsuleBody::new(vec![s.keypair.clone()], vec![service]).unwrap();
151 if !aliases.is_empty() {
152 body.also_known_as = Some(aliases);
153 }
154 let capsule = sign_capsule(body, &s.ed_sk, &s.pq_sk).unwrap();
155 let bytes = capsule.to_dag_cbor().unwrap();
156 let did = format!("did:at9p:{}", capsule.cid512().unwrap());
157 (bytes, did)
158 }
159
160 #[tokio::test]
161 async fn resolver_mutual_attestation_yields_authoritative_at9p() {
162 let classical = Did::new("did:web:node.example".to_owned());
163 let (bytes, did) = signed_with_aliases(vec![classical.as_str().to_owned()]);
164 let at9p = Did::new(did);
165 let resolver = At9pAliasResolver::new(Arc::new(FixedSource { bytes }));
166 let res = resolver
167 .resolve_authoritative(&classical, &at9p)
168 .await
169 .unwrap();
170 assert_eq!(res.at9p_did, at9p);
171 assert_eq!(res.classical_did, classical);
172 assert_eq!(res.assurance, Assurance::PqHybrid);
173 }
174
175 #[tokio::test]
176 async fn resolver_one_way_capsule_claim_fails_closed() {
177 let (bytes, did) = signed_with_aliases(vec!["did:web:alice.example".to_owned()]);
179 let at9p = Did::new(did);
180 let bob = Did::new("did:web:bob.example".to_owned());
181 let resolver = At9pAliasResolver::new(Arc::new(FixedSource { bytes }));
182 let err = resolver
183 .resolve_authoritative(&bob, &at9p)
184 .await
185 .unwrap_err();
186 assert!(err.to_string().contains("does not name"), "{err}");
187 }
188
189 #[tokio::test]
190 async fn resolver_classical_leg_pointing_elsewhere_fails_closed() {
191 let classical = Did::new("did:web:node.example".to_owned());
194 let (bytes, _did) = signed_with_aliases(vec![classical.as_str().to_owned()]);
195 let (_other_bytes, other_did) = signed_with_aliases(Vec::new());
197 let wrong_at9p = Did::new(other_did);
198 let resolver = At9pAliasResolver::new(Arc::new(FixedSource { bytes }));
199 let err = resolver
200 .resolve_authoritative(&classical, &wrong_at9p)
201 .await
202 .unwrap_err();
203 let msg = err.to_string();
206 assert!(
207 msg.contains("hash-gate") || msg.contains("does not name the verified capsule"),
208 "expected GATE or aliasing rejection, got: {msg}"
209 );
210 }
211}