mirror of
https://github.com/aljazceru/pubky-core.git
synced 2025-12-31 21:04:34 +01:00
feat(pubky): override default pkarr relays
This commit is contained in:
@@ -11,7 +11,7 @@ npm install @synonymdev/pubky
|
||||
## Getting started
|
||||
|
||||
```js
|
||||
import PubkyClient from "@synonymdev/pubky";
|
||||
import { PubkyClient, Keypair, PublicKey } from '../index.js'
|
||||
|
||||
// Initialize PubkyClient with Pkarr relay(s).
|
||||
let client = new PubkyClient();
|
||||
@@ -20,12 +20,26 @@ let client = new PubkyClient();
|
||||
let keypair = Keypair.random();
|
||||
|
||||
// Create a new account
|
||||
let homeserver = PublicKey.try_from("8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo");
|
||||
let homeserver = PublicKey.from("8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo");
|
||||
|
||||
await client.signup(keypair, homeserver)
|
||||
|
||||
// Verify that you are signed in.
|
||||
const session = await client.session(publicKey)
|
||||
|
||||
const publicKey = keypair.public_key();
|
||||
|
||||
const body = Buffer.from(JSON.stringify({ foo: 'bar' }))
|
||||
|
||||
// PUT public data, by authorized client
|
||||
await client.put(publicKey, "/pub/example.com/arbitrary", body);
|
||||
|
||||
// GET public data without signup or signin
|
||||
{
|
||||
const client = new PubkyClient();
|
||||
|
||||
let response = await client.get(publicKey, "/pub/example.com/arbitrary");
|
||||
}
|
||||
```
|
||||
|
||||
## Test and Development
|
||||
@@ -50,3 +64,13 @@ Run the local testnet server
|
||||
```bash
|
||||
npm run testnet
|
||||
```
|
||||
|
||||
Pass the logged addresses as inputs to `PubkyClient`
|
||||
|
||||
```js
|
||||
import { PubkyClient, PublicKey } from '../index.js'
|
||||
|
||||
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"]);
|
||||
|
||||
let homeserver = PublicKey.from("8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo");
|
||||
```
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "@synonymdev/pubky",
|
||||
"type": "module",
|
||||
"description": "Pubky client",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
@@ -2,8 +2,8 @@ import test from 'tape'
|
||||
|
||||
import { PubkyClient, Keypair, PublicKey } from '../index.js'
|
||||
|
||||
test('seed auth', async (t) => {
|
||||
const client = new PubkyClient()
|
||||
test('auth', async (t) => {
|
||||
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"])
|
||||
|
||||
const keypair = Keypair.random()
|
||||
const publicKey = keypair.public_key()
|
||||
@@ -12,19 +12,19 @@ test('seed auth', async (t) => {
|
||||
await client.signup(keypair, homeserver)
|
||||
|
||||
const session = await client.session(publicKey)
|
||||
t.ok(session)
|
||||
t.ok(session, "signup")
|
||||
|
||||
{
|
||||
await client.signout(publicKey)
|
||||
|
||||
const session = await client.session(publicKey)
|
||||
t.notOk(session)
|
||||
t.notOk(session, "singout")
|
||||
}
|
||||
|
||||
{
|
||||
await client.signin(keypair)
|
||||
|
||||
const session = await client.session(publicKey)
|
||||
t.ok(session)
|
||||
t.ok(session, "signin")
|
||||
}
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ import test from 'tape'
|
||||
import { PubkyClient, Keypair, PublicKey } from '../index.js'
|
||||
|
||||
test('public: put/get', async (t) => {
|
||||
const client = new PubkyClient();
|
||||
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"])
|
||||
|
||||
const keypair = Keypair.random();
|
||||
|
||||
@@ -20,7 +20,7 @@ test('public: put/get', async (t) => {
|
||||
|
||||
// GET public data without signup or signin
|
||||
{
|
||||
const client = new PubkyClient();
|
||||
const client = new PubkyClient().setPkarrRelays(["http://localhost:15411/pkarr"])
|
||||
|
||||
let response = await client.get(publicKey, "/pub/example.com/arbitrary");
|
||||
|
||||
|
||||
@@ -30,4 +30,6 @@ pub struct PubkyClient {
|
||||
/// A cookie jar for nodejs fetch.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub(crate) session_cookies: Arc<RwLock<HashSet<String>>>,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub(crate) pkarr_relays: Vec<String>,
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ impl Default for PubkyClient {
|
||||
}
|
||||
}
|
||||
|
||||
static DEFAULT_RELAYS: [&str; 1] = ["https://relay.pkarr.org"];
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl PubkyClient {
|
||||
#[wasm_bindgen(constructor)]
|
||||
@@ -31,9 +33,33 @@ impl PubkyClient {
|
||||
Self {
|
||||
http: reqwest::Client::builder().build().unwrap(),
|
||||
session_cookies: Arc::new(RwLock::new(HashSet::new())),
|
||||
pkarr_relays: DEFAULT_RELAYS.into_iter().map(|s| s.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the relays used for publishing and resolving Pkarr packets.
|
||||
///
|
||||
/// By default, [PubkyClient] will use `["https://relay.pkarr.org"]`
|
||||
#[wasm_bindgen(js_name = "setPkarrRelays")]
|
||||
pub fn set_pkarr_relays(mut self, relays: Vec<JsValue>) -> Self {
|
||||
let relays: Vec<String> = relays
|
||||
.into_iter()
|
||||
.filter_map(|name| name.as_string())
|
||||
.collect();
|
||||
|
||||
self.pkarr_relays = relays;
|
||||
self
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = "getPkarrRelays")]
|
||||
pub fn get_pkarr_relays(&self) -> Vec<JsValue> {
|
||||
self.pkarr_relays
|
||||
.clone()
|
||||
.into_iter()
|
||||
.map(JsValue::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Signup to a homeserver and update Pkarr accordingly.
|
||||
///
|
||||
/// The homeserver is a Pkarr domain name, where the TLD is a Pkarr public key
|
||||
|
||||
@@ -5,20 +5,20 @@ pub use pkarr::{PublicKey, SignedPacket};
|
||||
use crate::error::Result;
|
||||
use crate::PubkyClient;
|
||||
|
||||
const TEST_RELAY: &str = "http://localhost:15411/pkarr";
|
||||
|
||||
// TODO: Add an in memory cache of packets
|
||||
|
||||
impl PubkyClient {
|
||||
//TODO: Allow multiple relays in parallel
|
||||
//TODO: migrate to pkarr::PkarrRelayClient
|
||||
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!("{TEST_RELAY}/{}", public_key))
|
||||
.get(format!("{relay}/{}", public_key))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
@@ -35,8 +35,10 @@ impl PubkyClient {
|
||||
}
|
||||
|
||||
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!("{TEST_RELAY}/{}", signed_packet.public_key()))
|
||||
.put(format!("{relay}/{}", signed_packet.public_key()))
|
||||
.body(signed_packet.to_relay_payload())
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user