Skip to main content

vta_sdk/keys/
mod.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5#[serde(rename_all = "lowercase")]
6#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
7pub enum KeyType {
8    Ed25519,
9    X25519,
10    /// ECDSA P-256 key for ES256 signing.
11    P256,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
15#[serde(rename_all = "lowercase")]
16#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
17pub enum KeyStatus {
18    Active,
19    Revoked,
20}
21
22/// Whether a key was derived from the BIP-32 seed or imported externally.
23#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
24#[serde(rename_all = "lowercase")]
25#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
26pub enum KeyOrigin {
27    Derived,
28    Imported,
29}
30
31fn default_derived() -> KeyOrigin {
32    KeyOrigin::Derived
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
37pub struct KeyRecord {
38    pub key_id: String,
39    pub derivation_path: String,
40    pub key_type: KeyType,
41    pub status: KeyStatus,
42    pub public_key: String,
43    pub label: Option<String>,
44    #[serde(default)]
45    pub context_id: Option<String>,
46    #[serde(default)]
47    pub seed_id: Option<u32>,
48    #[serde(default = "default_derived")]
49    pub origin: KeyOrigin,
50    pub created_at: DateTime<Utc>,
51    pub updated_at: DateTime<Utc>,
52}
53
54impl std::fmt::Display for KeyType {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        match self {
57            KeyType::Ed25519 => write!(f, "ed25519"),
58            KeyType::X25519 => write!(f, "x25519"),
59            KeyType::P256 => write!(f, "p256"),
60        }
61    }
62}
63
64impl std::fmt::Display for KeyStatus {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        match self {
67            KeyStatus::Active => write!(f, "active"),
68            KeyStatus::Revoked => write!(f, "revoked"),
69        }
70    }
71}