solana_web3_sys/
publickey.rs

1//!
2//! [`PublicKey`](https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html) class bindings.
3//!
4
5use crate::imports::*;
6
7#[wasm_bindgen]
8extern "C" {
9    #[wasm_bindgen(js_namespace=solanaWeb3, js_name = PublicKey)]
10    #[derive(Debug)]
11    /// PublicKey
12    ///
13    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html)
14    ///
15    pub type PublicKey;
16
17    #[wasm_bindgen(constructor, js_namespace=solanaWeb3)]
18    /// Create [`PublicKey`] from sring
19    ///
20    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html#constructor)
21    ///
22    pub fn new(str: String) -> PublicKey;
23
24    #[wasm_bindgen(constructor, js_namespace=solanaWeb3)]
25    /// Create [`PublicKey`] from bytes array
26    ///
27    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html#constructor)
28    ///
29    pub fn new_from_array(bytes: Vec<u8>) -> PublicKey;
30
31    #[wasm_bindgen(method, js_name = "toBytes")]
32    /// Convert [`PublicKey`] to bytes array
33    ///
34    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html#toBytes)
35    ///
36    pub fn to_bytes(this: &PublicKey) -> Vec<u8>;
37
38    #[wasm_bindgen(method, js_name = "toString")]
39    /// Convert [`PublicKey`] to string
40    ///
41    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/PublicKey.html#toString)
42    ///
43    pub fn to_string(this: &PublicKey) -> String;
44}
45
46impl TryFrom<PublicKey> for Pubkey {
47    type Error = crate::error::Error;
48
49    fn try_from(key: PublicKey) -> Result<Self> {
50        match Pubkey::try_from(key.to_bytes()) {
51            Ok(value) => Ok(value),
52            Err(_) => Err(JsValue::from("Invalid pubkey").into()),
53        }
54    }
55}
56
57impl TryFrom<&Pubkey> for PublicKey {
58    type Error = crate::error::Error;
59
60    fn try_from(key: &Pubkey) -> Result<Self> {
61        Ok(PublicKey::new_from_array(key.to_bytes().to_vec()))
62    }
63}
64
65impl TryFrom<&[u8]> for PublicKey {
66    type Error = crate::error::Error;
67
68    fn try_from(bytes: &[u8]) -> Result<Self> {
69        Ok(PublicKey::new_from_array(bytes.to_vec()))
70    }
71}
72
73impl TryFrom<&str> for PublicKey {
74    type Error = crate::error::Error;
75
76    fn try_from(str: &str) -> Result<Self> {
77        Ok(PublicKey::new(str.to_string()))
78    }
79}