Skip to main content

wallet_adapter_common/
wallet_account.rs

1use std::borrow::Cow;
2
3use crate::{
4    chains::ChainSupport, feature_support::FeatureSupport, WalletCommonUtils, WalletUtilsError,
5    WalletUtilsResult,
6};
7
8/// A data URI containing a base64-encoded SVG, WebP, PNG, or GIF image.
9/// **NOTE** that this does not contain the browser functions that would be
10/// called to perform operations
11#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct WalletAccountData {
13    /// Address of the account, corresponding with a public key.
14    pub address: String,
15    /// Public key of the account, corresponding with a secret key to use.
16    pub public_key: [u8; 32],
17    /// Chains supported by the account.
18    /// This must be a subset of the {@link Wallet.chains | chains} of the Wallet.
19    pub chains: Vec<String>,
20    /// Feature names supported by the account.
21    /// This must be a subset of the names of {@link Wallet.features | features} of the Wallet.
22    pub features: Vec<String>,
23    /// Optional user-friendly descriptive label or name for the account. This may be displayed by the app.
24    pub label: Option<String>,
25    /// Optional user-friendly icon for the account. This may be displayed by the app. */
26    /// Format `data:image/${'svg+xml' | 'webp' | 'png' | 'gif'};base64,${string}`
27    pub icon: Option<String>,
28    /// Convenience field, instead of going through the `features` field
29    pub supported_features: FeatureSupport,
30    /// Convenience field, instead of iteration through the `chains` field
31    pub supported_chains: ChainSupport,
32}
33
34impl WalletAccountData {
35    /// Address of the account, corresponding with a public key.
36    pub fn address(&self) -> &str {
37        self.address.as_str()
38    }
39
40    /// Public key of the account, corresponding with a secret key to use.
41    pub fn public_key(&self) -> [u8; 32] {
42        self.public_key
43    }
44
45    /// Chains supported by the account.
46    /// This must be a subset of the {@link Wallet.chains | chains} of the Wallet.
47    pub fn chains(&self) -> &[String] {
48        self.chains.as_slice()
49    }
50
51    /// Feature names supported by the account.
52    /// This must be a subset of the names of {@link Wallet.features | features} of the Wallet.
53    pub fn features(&self) -> &[String] {
54        self.features.as_slice()
55    }
56
57    /// Optional user-friendly descriptive label or name for the account. This may be displayed by the app.
58    pub fn label(&self) -> Option<&String> {
59        self.label.as_ref()
60    }
61
62    /// An optional Wallet Icon
63    pub fn icon(&self) -> Option<&String> {
64        self.icon.as_ref()
65    }
66
67    /// Get the shortened address of the `Base58 address` .
68    /// It displays the first 4 characters and the last for characters
69    /// separated by ellipsis eg `FXdl...RGd4` .
70    /// If the address is less than 8 characters, an error is thrown
71    pub fn shorten_address<'a>(&'a self) -> WalletUtilsResult<Cow<'a, str>> {
72        WalletCommonUtils::shorten_base58(&self.address)
73    }
74
75    /// Same as [Self::shorten_address] but with a custom range
76    /// instead of taking the first 4 character and the last 4 characters
77    /// it uses a custom range.
78    pub fn custom_shorten_address<'a>(&'a self, take: usize) -> WalletUtilsResult<Cow<'a, str>> {
79        WalletCommonUtils::custom_shorten_base58(&self.address, take)
80    }
81
82    /// Same as [Self::shorten_address] but with a custom range
83    /// instead of taking the first 4 character and the last 4 characters
84    /// it uses a custom range for first characters before ellipsis and last characters after ellipsis.
85    pub fn custom_shorten_address_rl<'a>(
86        &'a self,
87        left: usize,
88        right: usize,
89    ) -> WalletUtilsResult<Cow<'a, str>> {
90        if self.address.len() < left + right {
91            return Err(WalletUtilsError::InvalidBase58Address);
92        }
93
94        let first_part = &self.address[..left];
95        let last_part = &self.address[self.address.len() - right..];
96
97        Ok(Cow::Borrowed(first_part) + "..." + last_part)
98    }
99
100    /// Checks if MainNet is supported
101    pub fn mainnet(&self) -> bool {
102        self.supported_chains.mainnet
103    }
104
105    /// Checks if DevNet is supported
106    pub fn devnet(&self) -> bool {
107        self.supported_chains.devnet
108    }
109
110    /// Checks if TestNet is supported
111    pub fn testnet(&self) -> bool {
112        self.supported_chains.testnet
113    }
114
115    /// Checks if LocalNet is supported
116    pub fn localnet(&self) -> bool {
117        self.supported_chains.localnet
118    }
119
120    /// Checks if `standard:connect` is supported
121    pub fn standard_connect(&self) -> bool {
122        self.supported_features.connect
123    }
124
125    /// Checks if `standard:disconnect` is supported
126    pub fn standard_disconnect(&self) -> bool {
127        self.supported_features.disconnect
128    }
129
130    /// Checks if `standard:events` is supported
131    pub fn standard_events(&self) -> bool {
132        self.supported_features.events
133    }
134
135    /// Checks if `solana:signIn` is supported
136    pub fn solana_signin(&self) -> bool {
137        self.supported_features.sign_in
138    }
139
140    /// Checks if `solana:signMessage` is supported
141    pub fn solana_sign_message(&self) -> bool {
142        self.supported_features.sign_message
143    }
144
145    /// Checks if `solana:signAndSendTransaction` is supported
146    pub fn solana_sign_and_send_transaction(&self) -> bool {
147        self.supported_features.sign_and_send_tx
148    }
149
150    /// Checks if `solana:signTransaction` is supported
151    pub fn solana_sign_transaction(&self) -> bool {
152        self.supported_features.sign_tx
153    }
154}