feat(js): use pkarr relay Client instead of inline impl

This commit is contained in:
nazeh
2024-09-22 18:50:57 +03:00
parent 1f73b72371
commit fff7b6af39
4 changed files with 13 additions and 38 deletions

2
Cargo.lock generated
View File

@@ -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",

View File

@@ -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<String>,

View File

@@ -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(),
}
}

View File

@@ -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<Option<SignedPacket>> {
//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?)
}
}