hc_deepkey_types/
key_anchor.rs

1use hdi::prelude::*;
2
3pub type KeyBytes = [u8; 32];
4
5/// A deterministic entry that contains only the cord 32 bytes of a key
6///
7/// The core 32 bytes of a registered key is the `AgentPubKey` stripped of the 3 byte multihash
8/// prefix and 4 byte DHT location suffix.  The `EntryHash` can be derived so that the status of a
9/// key can be looked up in a single `get_details` call.
10#[hdk_entry_helper]
11#[derive(Clone, PartialEq)]
12pub struct KeyAnchor {
13    pub bytes: KeyBytes,
14}
15
16impl KeyAnchor {
17    pub fn new(bytes: KeyBytes) -> Self {
18        KeyAnchor { bytes }
19    }
20}
21
22impl TryFrom<AgentPubKey> for KeyAnchor {
23    type Error = WasmError;
24
25    fn try_from(input: AgentPubKey) -> Result<Self, Self::Error> {
26        Ok(Self {
27            bytes: input.get_raw_32().try_into().map_err(|e| {
28                wasm_error!(WasmErrorInner::Guest(format!(
29                    "Failed AgentPubKey to [u8;32] conversion: {:?}",
30                    e
31                )))
32            })?,
33        })
34    }
35}
36
37impl TryFrom<&AgentPubKey> for KeyAnchor {
38    type Error = WasmError;
39
40    fn try_from(input: &AgentPubKey) -> Result<Self, Self::Error> {
41        input.to_owned().try_into()
42    }
43}