casper_client/types/
named_key.rs

1use std::fmt::{self, Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5use casper_types::{Key, KeyFromStrError};
6
7#[cfg(doc)]
8use crate::types::{Account, Contract};
9
10/// A named key.  An [`Account`] or [`Contract`] may store a collection of named keys in global
11/// state.
12#[derive(Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
13#[serde(deny_unknown_fields)]
14pub struct NamedKey {
15    name: String,
16    key: String,
17}
18
19impl NamedKey {
20    /// Returns the name under which the key is held.
21    pub fn name(&self) -> &str {
22        &self.name
23    }
24
25    /// Returns the key.
26    pub fn key(&self) -> Result<Key, KeyFromStrError> {
27        Key::from_formatted_str(&self.key)
28    }
29}
30
31impl Display for NamedKey {
32    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
33        write!(formatter, "{}: {}", self.name, self.key)
34    }
35}