use affinidi_tdk::secrets_resolver::secrets::Secret;
use chrono::Utc;
use vta_sdk::keys::KeyRecord;
use super::errors::UpdateDidWebvhError;
use crate::operations::did_webvh::webvh_keys::{WebvhKeyHandle, WebvhKeyRole};
use crate::store::KeyspaceHandle;
pub(in crate::operations::did_webvh) async fn legacy_lookup_pre_rotation_by_hash(
keys_ks: &KeyspaceHandle,
scid: &str,
target_hash: &str,
) -> Result<Option<WebvhKeyHandle>, UpdateDidWebvhError> {
let raw_keys = keys_ks
.prefix_keys(b"key:".to_vec())
.await
.map_err(|e| UpdateDidWebvhError::Persistence(format!("legacy scan: {e}")))?;
for raw in raw_keys {
let record: Option<KeyRecord> = keys_ks
.get(raw)
.await
.map_err(|e| UpdateDidWebvhError::Persistence(format!("legacy load: {e}")))?;
let Some(record) = record else { continue };
let computed = match Secret::base58_hash_string(&record.public_key) {
Ok(h) => h,
Err(_) => continue,
};
if computed != target_hash {
continue;
}
return Ok(Some(WebvhKeyHandle {
scid: scid.to_string(),
version_id: "legacy".into(),
hash: target_hash.to_string(),
public_key: record.public_key.clone(),
derivation_path: record.derivation_path.clone(),
seed_id: record.seed_id,
role: WebvhKeyRole::PreRotation,
label: record
.label
.unwrap_or_else(|| format!("legacy pre-rotation key for {scid}")),
created_at: Utc::now(),
}));
}
Ok(None)
}
pub(in crate::operations::did_webvh) async fn legacy_lookup_by_public_key(
keys_ks: &KeyspaceHandle,
scid: &str,
target_pubkey: &str,
hash: &str,
) -> Result<Option<WebvhKeyHandle>, UpdateDidWebvhError> {
let raw_keys = keys_ks
.prefix_keys(b"key:".to_vec())
.await
.map_err(|e| UpdateDidWebvhError::Persistence(format!("legacy scan: {e}")))?;
for raw in raw_keys {
let record: Option<KeyRecord> = keys_ks
.get(raw)
.await
.map_err(|e| UpdateDidWebvhError::Persistence(format!("legacy load: {e}")))?;
let Some(record) = record else { continue };
if record.public_key != target_pubkey {
continue;
}
return Ok(Some(WebvhKeyHandle {
scid: scid.to_string(),
version_id: "legacy".into(),
hash: hash.to_string(),
public_key: target_pubkey.to_string(),
derivation_path: record.derivation_path.clone(),
seed_id: record.seed_id,
role: WebvhKeyRole::UpdateKey,
label: record
.label
.unwrap_or_else(|| format!("legacy update key for {scid}")),
created_at: Utc::now(),
}));
}
Ok(None)
}