use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
pub const HUB_SPEC_SCHEMA: &str = "vela.hub_spec.v0.1";
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct HubSpec {
pub schema: String,
pub spec_id: String,
pub hub_id: String,
pub display_name: String,
pub base_url: String,
pub operator_pubkey_hex: String,
pub substrate_version: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub contact: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub latest_checkpoint: Option<String>,
pub declared_at: String,
}
#[derive(Debug, Clone)]
pub struct HubSpecDraft {
pub hub_id: String,
pub display_name: String,
pub base_url: String,
pub operator_pubkey_hex: String,
pub substrate_version: String,
pub contact: Option<String>,
pub latest_checkpoint: Option<String>,
pub declared_at: String,
}
impl HubSpec {
pub fn from_draft(draft: HubSpecDraft) -> Result<Self, String> {
if draft.hub_id.trim().is_empty() {
return Err("hub_id must be non-empty".to_string());
}
if draft.display_name.trim().is_empty() {
return Err("display_name must be non-empty".to_string());
}
if !(draft.base_url.starts_with("https://") || draft.base_url.starts_with("http://")) {
return Err(format!(
"base_url must start with http:// or https://, got `{}`",
draft.base_url
));
}
if draft.operator_pubkey_hex.len() != 64
|| !draft
.operator_pubkey_hex
.chars()
.all(|c| c.is_ascii_hexdigit())
{
return Err(
"operator_pubkey_hex must be 64 hex chars (Ed25519 public key)".to_string(),
);
}
if draft.substrate_version.trim().is_empty() {
return Err("substrate_version must be non-empty".to_string());
}
if let Some(ref c) = draft.latest_checkpoint
&& !c.starts_with("vrc_")
{
return Err(format!(
"latest_checkpoint must start with `vrc_` when present, got `{c}`"
));
}
let mut spec = HubSpec {
schema: HUB_SPEC_SCHEMA.to_string(),
spec_id: String::new(),
hub_id: draft.hub_id,
display_name: draft.display_name,
base_url: draft.base_url,
operator_pubkey_hex: draft.operator_pubkey_hex,
substrate_version: draft.substrate_version,
contact: draft.contact,
latest_checkpoint: draft.latest_checkpoint,
declared_at: draft.declared_at,
};
spec.spec_id = spec.derive_id();
Ok(spec)
}
fn derive_id(&self) -> String {
let mut hasher = Sha256::new();
hasher.update(self.hub_id.as_bytes());
hasher.update(b"|");
hasher.update(self.base_url.as_bytes());
hasher.update(b"|");
hasher.update(self.operator_pubkey_hex.as_bytes());
hasher.update(b"|");
hasher.update(self.substrate_version.as_bytes());
hasher.update(b"|");
hasher.update(self.declared_at.as_bytes());
format!("vhs_{}", &hex::encode(hasher.finalize())[..16])
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ok_draft() -> HubSpecDraft {
HubSpecDraft {
hub_id: "vela-hub".to_string(),
display_name: "Vela Hub".to_string(),
base_url: "https://vela-hub.fly.dev".to_string(),
operator_pubkey_hex: "a".repeat(64),
substrate_version: "0.167.0".to_string(),
contact: Some("ops@example.org".to_string()),
latest_checkpoint: None,
declared_at: "2026-05-11T00:00:00Z".to_string(),
}
}
#[test]
fn from_draft_builds_and_round_trips() {
let s = HubSpec::from_draft(ok_draft()).expect("builds");
assert!(s.spec_id.starts_with("vhs_"));
let json = serde_json::to_string(&s).expect("ser");
let back: HubSpec = serde_json::from_str(&json).expect("de");
assert_eq!(s, back);
}
#[test]
fn rejects_bad_url() {
let mut d = ok_draft();
d.base_url = "ftp://example.org".to_string();
assert!(HubSpec::from_draft(d).is_err());
}
#[test]
fn rejects_bad_pubkey() {
let mut d = ok_draft();
d.operator_pubkey_hex = "nothex".to_string();
assert!(HubSpec::from_draft(d).is_err());
}
#[test]
fn rejects_bad_checkpoint_prefix() {
let mut d = ok_draft();
d.latest_checkpoint = Some("not_a_vrc".to_string());
assert!(HubSpec::from_draft(d).is_err());
}
#[test]
fn id_changes_when_url_changes() {
let mut a = ok_draft();
let mut b = a.clone();
b.base_url = "https://other-hub.fly.dev".to_string();
b.declared_at = a.declared_at.clone();
let s_a = HubSpec::from_draft(a.clone()).unwrap();
let s_b = HubSpec::from_draft(b).unwrap();
assert_ne!(s_a.spec_id, s_b.spec_id);
a.declared_at = s_a.declared_at.clone();
let s_a2 = HubSpec::from_draft(a).unwrap();
assert_eq!(s_a.spec_id, s_a2.spec_id);
}
}