diff --git a/Cargo.lock b/Cargo.lock index a170572..71da5f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1591,7 +1591,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkarr" version = "3.0.0" -source = "git+https://github.com/Pubky/pkarr?branch=v3#d0be23324bcf706f5a359236d2945944abbb40f8" +source = "git+https://github.com/Pubky/pkarr?branch=v3#445c0db448b9e70f2a63a5b91e6d84f1ef466aeb" dependencies = [ "base32", "bytes", diff --git a/pubky/src/lib.rs b/pubky/src/lib.rs index 56caada..36357ff 100644 --- a/pubky/src/lib.rs +++ b/pubky/src/lib.rs @@ -18,7 +18,6 @@ pub use crate::shared::list_builder::ListBuilder; #[wasm_bindgen] pub struct PubkyClient { http: reqwest::Client, - #[cfg(not(target_arch = "wasm32"))] pub(crate) pkarr: pkarr::Client, #[cfg(target_arch = "wasm32")] pub(crate) pkarr_relays: Vec, diff --git a/pubky/src/wasm.rs b/pubky/src/wasm.rs index 7cda1c5..990058c 100644 --- a/pubky/src/wasm.rs +++ b/pubky/src/wasm.rs @@ -12,7 +12,6 @@ impl Default for PubkyClient { } } -static DEFAULT_RELAYS: [&str; 1] = ["https://relay.pkarr.org"]; static TESTNET_RELAYS: [&str; 1] = ["http://localhost:15411/pkarr"]; #[wasm_bindgen] @@ -21,7 +20,8 @@ impl PubkyClient { pub fn new() -> Self { Self { http: reqwest::Client::builder().build().unwrap(), - pkarr_relays: DEFAULT_RELAYS.into_iter().map(|s| s.to_string()).collect(), + pkarr: pkarr::Client::builder().build().unwrap(), + pkarr_relays: vec!["https://relay.pkarr.org".to_string()], } } @@ -31,6 +31,10 @@ impl PubkyClient { pub fn testnet() -> Self { Self { http: reqwest::Client::builder().build().unwrap(), + pkarr: pkarr::Client::builder() + .relays(TESTNET_RELAYS.into_iter().map(|s| s.to_string()).collect()) + .build() + .unwrap(), pkarr_relays: TESTNET_RELAYS.into_iter().map(|s| s.to_string()).collect(), } } diff --git a/pubky/src/wasm/internals/pkarr.rs b/pubky/src/wasm/internals/pkarr.rs index 49726f6..7ada26d 100644 --- a/pubky/src/wasm/internals/pkarr.rs +++ b/pubky/src/wasm/internals/pkarr.rs @@ -1,48 +1,20 @@ -use reqwest::StatusCode; - -pub use pkarr::{PublicKey, SignedPacket}; +use pkarr::SignedPacket; +use pubky_common::crypto::PublicKey; use crate::error::Result; use crate::PubkyClient; -// TODO: Add an in memory cache of packets - impl PubkyClient { - //TODO: migrate to pkarr::PkarrRelayClient + // === Pkarr === + pub(crate) async fn pkarr_resolve( &self, public_key: &PublicKey, ) -> Result> { - //TODO: Allow multiple relays in parallel - let relay = self.pkarr_relays.first().expect("initialized with relays"); - - let res = self - .http - .get(format!("{relay}/{}", public_key)) - .send() - .await?; - - if res.status() == StatusCode::NOT_FOUND { - return Ok(None); - }; - - // TODO: guard against too large responses. - let bytes = res.bytes().await?; - - let existing = SignedPacket::from_relay_payload(public_key, &bytes)?; - - Ok(Some(existing)) + Ok(self.pkarr.resolve(public_key).await?) } pub(crate) async fn pkarr_publish(&self, signed_packet: &SignedPacket) -> Result<()> { - let relay = self.pkarr_relays.first().expect("initialized with relays"); - - self.http - .put(format!("{relay}/{}", signed_packet.public_key())) - .body(signed_packet.to_relay_payload()) - .send() - .await?; - - Ok(()) + Ok(self.pkarr.publish(signed_packet).await?) } }