Skip to content

Commit

Permalink
examples: add testnet example
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuhvi committed Nov 13, 2024
1 parent b29c384 commit eb10086
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 87 deletions.
472 changes: 388 additions & 84 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ path = "./authz/authenticator.rs"
name = "request"
path = "./request/main.rs"

[[bin]]
name = "testnet"
path = "./testnet/main.rs"

[dependencies]
anyhow = "1.0.86"
base64 = "0.22.1"
bytes = "1.8.0"
clap = { version = "4.5.16", features = ["derive"] }
pubky = { path = "../pubky" }
pubky-common = { version = "0.1.0", path = "../pubky-common" }
pubky-homeserver = { version = "0.1.0", path = "../pubky-homeserver" }
reqwest = "0.12.8"
rpassword = "7.3.1"
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
Expand Down
13 changes: 13 additions & 0 deletions examples/testnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Testnet

Example of using a testnet Homeserver.

## Usage

If you have a homeserver running in testnet mode, run:

```bash
cargo run --bin testnet
```

If you want to run a Homeserver in testnet, check the `Testnet` section in the [Homeserver](../../pubky-homeserver/README.md).
33 changes: 33 additions & 0 deletions examples/testnet/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Example of using a Testnet configuration in pubky Client
use pubky::PubkyClient;
use pubky_common::crypto::{Keypair, PublicKey};

#[tokio::main]
async fn main() {
let server_public_key =
PublicKey::try_from("8pinxxgqs41n4aididenw5apqp1urfmzdztr8jt4abrkdn435ewo").unwrap();

let client = PubkyClient::testnet();

let keypair = Keypair::random();

client.signup(&keypair, &server_public_key).await.unwrap();

let url = format!("pubky://{}/pub/foo.txt", keypair.public_key());
let url = url.as_str();

client.put(url, &[0, 1, 2, 3, 4]).await.unwrap();

let response = client.get(url).await.unwrap().unwrap();

assert_eq!(response, bytes::Bytes::from(vec![0, 1, 2, 3, 4]));

client.delete(url).await.unwrap();

let response = client.get(url).await.unwrap();

assert_eq!(response, None);

println!("Successfully performed PUT, GET and DELETE requests to the testnet server")
}
2 changes: 1 addition & 1 deletion pubky-homeserver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "pubky_homeserver"
name = "pubky-homeserver"
version = "0.1.0"
edition = "2021"

Expand Down
12 changes: 12 additions & 0 deletions pubky-homeserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ Run with an optional config file
```bash
../target/release/pubky_homeserver --config=./src/config.toml
```

## Testnet

Testnet is a mode where the Homeserver is running locally, connected to an internal Mainline Testnet (not the public DHT), and acting as a Pkarr relay for clients in web browsers.

You can run a homeserver in Testnet by passing an argument:

```bash
cargo run --testnet
```

Or set the `testnet` field in the passed config file to true.
2 changes: 1 addition & 1 deletion pubky/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ wasm-bindgen = "0.2.92"
wasm-bindgen-futures = "0.4.42"

[dev-dependencies]
pubky_homeserver = { path = "../pubky-homeserver" }
pubky-homeserver = { path = "../pubky-homeserver" }
tokio = "1.37.0"

[features]
Expand Down
2 changes: 1 addition & 1 deletion pubky/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"url": "https://github.com/pubky/pubky"
},
"scripts": {
"testnet": "cargo run -p pubky_homeserver -- --testnet",
"testnet": "cargo run -p pubky-homeserver -- --testnet",
"test": "npm run test-nodejs && npm run test-browser",
"test-nodejs": "tape test/*.js -cov",
"test-browser": "browserify test/*.js -p esmify | npx tape-run",
Expand Down

0 comments on commit eb10086

Please sign in to comment.