···11+use std::str::FromStr;
22+33+use crate::error::{Error, InvalidPublicKeySnafu};
44+55+/// A public key.
66+///
77+/// The key itself is just a 32 byte array, but a key has associated crypto
88+/// information that is cached for performance reasons.
99+#[derive(Debug, Clone, Eq, uniffi::Object)]
1010+#[uniffi::export(Display)]
1111+pub struct PublicKey {
1212+ pub(crate) key: [u8; 32],
1313+}
1414+1515+impl From<iroh::PublicKey> for PublicKey {
1616+ fn from(key: iroh::PublicKey) -> Self {
1717+ PublicKey {
1818+ key: *key.as_bytes(),
1919+ }
2020+ }
2121+}
2222+impl From<&PublicKey> for iroh::PublicKey {
2323+ fn from(key: &PublicKey) -> Self {
2424+ iroh::PublicKey::from_bytes(&key.key).unwrap()
2525+ }
2626+}
2727+2828+#[uniffi::export]
2929+impl PublicKey {
3030+ /// Returns true if the PublicKeys are equal
3131+ pub fn equal(&self, other: &PublicKey) -> bool {
3232+ *self == *other
3333+ }
3434+3535+ /// Express the PublicKey as a byte array
3636+ pub fn to_bytes(&self) -> Vec<u8> {
3737+ self.key.to_vec()
3838+ }
3939+4040+ /// Make a PublicKey from base32 string
4141+ #[uniffi::constructor]
4242+ pub fn from_string(s: String) -> Result<Self, Error> {
4343+ let key = iroh::PublicKey::from_str(&s).map_err(|_| InvalidPublicKeySnafu.build())?;
4444+ Ok(key.into())
4545+ }
4646+4747+ /// Make a PublicKey from byte array
4848+ #[uniffi::constructor]
4949+ pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, Error> {
5050+ if bytes.len() != 32 {
5151+ InvalidPublicKeySnafu.fail()?;
5252+ }
5353+ let bytes: [u8; 32] = bytes.try_into().expect("checked above");
5454+ let key = iroh::PublicKey::from_bytes(&bytes).map_err(|_| InvalidPublicKeySnafu.build())?;
5555+ Ok(key.into())
5656+ }
5757+5858+ /// Convert to a base32 string limited to the first 10 bytes for a friendly string
5959+ /// representation of the key.
6060+ pub fn fmt_short(&self) -> String {
6161+ iroh::PublicKey::from(self).fmt_short()
6262+ }
6363+}
6464+6565+impl PartialEq for PublicKey {
6666+ fn eq(&self, other: &PublicKey) -> bool {
6767+ self.key == other.key
6868+ }
6969+}
7070+7171+impl std::fmt::Display for PublicKey {
7272+ fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
7373+ iroh::PublicKey::from(self).fmt(f)
7474+ }
7575+}
7676+7777+#[cfg(test)]
7878+mod tests {
7979+ use super::*;
8080+8181+ #[test]
8282+ fn test_public_key() {
8383+ let key_str =
8484+ String::from("523c7996bad77424e96786cf7a7205115337a5b4565cd25506a0f297b191a5ea");
8585+ let fmt_str = String::from("523c7996ba");
8686+ let bytes = b"\x52\x3c\x79\x96\xba\xd7\x74\x24\xe9\x67\x86\xcf\x7a\x72\x05\x11\x53\x37\xa5\xb4\x56\x5c\xd2\x55\x06\xa0\xf2\x97\xb1\x91\xa5\xea";
8787+ //
8888+ // create key from string
8989+ let key = PublicKey::from_string(key_str.clone()).unwrap();
9090+ //
9191+ // test methods are as expected
9292+ assert_eq!(key_str, key.to_string());
9393+ assert_eq!(bytes.to_vec(), key.to_bytes());
9494+ assert_eq!(fmt_str, key.fmt_short());
9595+ //
9696+ // create key from bytes
9797+ let key_0 = PublicKey::from_bytes(bytes.to_vec()).unwrap();
9898+ //
9999+ // test methods are as expected
100100+ assert_eq!(key_str, key_0.to_string());
101101+ assert_eq!(bytes.to_vec(), key_0.to_bytes());
102102+ assert_eq!(fmt_str, key_0.fmt_short());
103103+ //
104104+ // test that the eq function works
105105+ assert!(key.equal(&key_0));
106106+ assert!(key_0.equal(&key));
107107+ }
108108+}
+10
rust/iroh-streamplace/src/lib.rs
···11+uniffi::setup_scaffolding!();
22+33+pub mod endpoint;
44+pub mod error;
55+pub mod key;
66+pub mod receiver;
77+pub mod sender;
88+pub mod utils;
99+1010+const ALPN: &[u8] = b"/iroh/streamplace/1";