use cesride::{Diger, Matter, Salter, Verfer, matter};
use crate::keys::KeriDecodeError;
pub fn encode_salt_128(raw: &[u8; 16]) -> Result<String, KeriDecodeError> {
Salter::new_with_raw(raw, Some(matter::Codex::Salt_128), None)
.and_then(|s| s.qb64())
.map_err(|e| KeriDecodeError::DecodeError(e.to_string()))
}
pub(crate) fn encode_verkey(raw: &[u8], code: &str) -> Result<String, KeriDecodeError> {
Verfer::new(Some(code), Some(raw), None, None, None)
.and_then(|v| v.qb64())
.map_err(|e| KeriDecodeError::DecodeError(e.to_string()))
}
pub(crate) fn decode_verkey(qb64: &str) -> Result<(Vec<u8>, String), KeriDecodeError> {
let verfer = Verfer::new(None, None, None, Some(qb64), None)
.map_err(|e| KeriDecodeError::DecodeError(e.to_string()))?;
Ok((verfer.raw(), verfer.code()))
}
pub(crate) fn encode_blake3_digest(digest: &[u8]) -> Result<String, KeriDecodeError> {
Diger::new(
None,
Some(matter::Codex::Blake3_256),
Some(digest),
None,
None,
None,
)
.and_then(|d| d.qb64())
.map_err(|e| KeriDecodeError::DecodeError(e.to_string()))
}
pub(crate) fn verkey_code(curve: auths_crypto::CurveType, transferable: bool) -> &'static str {
match (curve, transferable) {
(auths_crypto::CurveType::Ed25519, true) => matter::Codex::Ed25519,
(auths_crypto::CurveType::Ed25519, false) => matter::Codex::Ed25519N,
(auths_crypto::CurveType::P256, true) => matter::Codex::ECDSA_256r1,
(auths_crypto::CurveType::P256, false) => matter::Codex::ECDSA_256r1N,
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn encode_decode_matches_keripy() {
let raw: Vec<u8> = (0u8..32).collect();
let qb64 = encode_verkey(&raw, matter::Codex::Ed25519).unwrap();
assert_eq!(qb64, "DAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4f");
let (decoded, code) = decode_verkey(&qb64).unwrap();
assert_eq!(decoded, raw, "round-trip must recover the raw bytes");
assert_eq!(code, matter::Codex::Ed25519);
let commitment = encode_blake3_digest(blake3::hash(qb64.as_bytes()).as_bytes()).unwrap();
assert_eq!(commitment, "EF_M_u7ASVHXfI8QzdWLq3V9ocSKqxkbujXGbi9QMtP9");
}
}