solana_web3_sys/
publickey.rs1use crate::imports::*;
6
7#[wasm_bindgen]
8extern "C" {
9 #[wasm_bindgen(js_namespace=solanaWeb3, js_name = PublicKey)]
10 #[derive(Debug)]
11 pub type PublicKey;
16
17 #[wasm_bindgen(constructor, js_namespace=solanaWeb3)]
18 pub fn new(str: String) -> PublicKey;
23
24 #[wasm_bindgen(constructor, js_namespace=solanaWeb3)]
25 pub fn new_from_array(bytes: Vec<u8>) -> PublicKey;
30
31 #[wasm_bindgen(method, js_name = "toBytes")]
32 pub fn to_bytes(this: &PublicKey) -> Vec<u8>;
37
38 #[wasm_bindgen(method, js_name = "toString")]
39 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}