···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 PublicKeyOld {
1212- pub(crate) key: [u8; 32],
1313-}
1414-1515-impl From<iroh::PublicKey> for PublicKeyOld {
1616- fn from(key: iroh::PublicKey) -> Self {
1717- PublicKeyOld {
1818- key: *key.as_bytes(),
1919- }
2020- }
2121-}
2222-impl From<&PublicKeyOld> for iroh::PublicKey {
2323- fn from(key: &PublicKeyOld) -> Self {
2424- iroh::PublicKey::from_bytes(&key.key).unwrap()
2525- }
2626-}
2727-2828-#[uniffi::export]
2929-impl PublicKeyOld {
3030- /// Returns true if the PublicKeys are equal
3131- pub fn equal(&self, other: &PublicKeyOld) -> 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 PublicKeyOld {
6666- fn eq(&self, other: &PublicKeyOld) -> bool {
6767- self.key == other.key
6868- }
6969-}
7070-7171-impl std::fmt::Display for PublicKeyOld {
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 = PublicKeyOld::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 = PublicKeyOld::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-}
+2-2
rust/iroh-streamplace/src/lib.rs
···33pub mod c2pa;
44pub mod endpoint;
55pub mod error;
66-pub mod key;
66+pub mod public_key;
77pub mod node;
88pub mod receiver;
99pub mod sender;
1010-pub mod utils;
1010+pub mod node_addr;
11111212mod api;
+3-3
rust/iroh-streamplace/src/node/db.rs
···1212use snafu::Snafu;
1313use tokio::sync::Mutex;
14141515+use crate::public_key::PublicKey;
1616+1517use super::db;
16181719// the files here are just copied from iroh-smol-kv-uniffi/src/code
1820mod kv {
1919- mod public_key;
2020- pub use public_key::PublicKey;
2121 mod time_bound;
2222 pub use time_bound::TimeBound;
2323 mod subscribe_mode;
2424 pub use subscribe_mode::SubscribeMode;
2525}
2626use db::util::format_bytes;
2727-pub use kv::{PublicKey, SubscribeMode, TimeBound};
2727+pub use kv::{SubscribeMode, TimeBound};
28282929/// Error creating a new database node.
3030#[derive(Debug, Snafu, uniffi::Error)]