penumbra_keys/keys/
wallet_id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use serde::{Deserialize, Serialize};

use penumbra_proto::core::keys::v1;
use penumbra_proto::{penumbra::core::keys::v1 as pb, serializers::bech32str, DomainType};

/// The hash of a full viewing key, used as an account identifier.
#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(try_from = "pb::WalletId", into = "pb::WalletId")]
pub struct WalletId(pub [u8; 32]);

impl DomainType for WalletId {
    type Proto = pb::WalletId;
}

impl TryFrom<v1::WalletId> for WalletId {
    type Error = anyhow::Error;

    fn try_from(value: v1::WalletId) -> Result<Self, Self::Error> {
        Ok(WalletId(
            value
                .inner
                .try_into()
                .map_err(|_| anyhow::anyhow!("expected 32 byte array"))?,
        ))
    }
}

impl From<WalletId> for v1::WalletId {
    fn from(value: WalletId) -> v1::WalletId {
        v1::WalletId {
            inner: value.0.to_vec(),
        }
    }
}

impl std::fmt::Debug for WalletId {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        <Self as std::fmt::Display>::fmt(self, f)
    }
}

impl std::fmt::Display for WalletId {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&bech32str::encode(
            &self.0,
            bech32str::wallet_id::BECH32_PREFIX,
            bech32str::Bech32m,
        ))
    }
}