penumbra_sdk_keys/keys/
wallet_id.rs1use serde::{Deserialize, Serialize};
2
3use penumbra_sdk_proto::core::keys::v1;
4use penumbra_sdk_proto::{penumbra::core::keys::v1 as pb, serializers::bech32str, DomainType};
5
6#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
8#[serde(try_from = "pb::WalletId", into = "pb::WalletId")]
9pub struct WalletId(pub [u8; 32]);
10
11impl DomainType for WalletId {
12 type Proto = pb::WalletId;
13}
14
15impl TryFrom<v1::WalletId> for WalletId {
16 type Error = anyhow::Error;
17
18 fn try_from(value: v1::WalletId) -> Result<Self, Self::Error> {
19 Ok(WalletId(
20 value
21 .inner
22 .try_into()
23 .map_err(|_| anyhow::anyhow!("expected 32 byte array"))?,
24 ))
25 }
26}
27
28impl From<WalletId> for v1::WalletId {
29 fn from(value: WalletId) -> v1::WalletId {
30 v1::WalletId {
31 inner: value.0.to_vec(),
32 }
33 }
34}
35
36impl std::fmt::Debug for WalletId {
37 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38 <Self as std::fmt::Display>::fmt(self, f)
39 }
40}
41
42impl std::fmt::Display for WalletId {
43 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
44 f.write_str(&bech32str::encode(
45 &self.0,
46 bech32str::wallet_id::BECH32_PREFIX,
47 bech32str::Bech32m,
48 ))
49 }
50}