Skip to content

Commit

Permalink
feat: wasm client
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jan 31, 2024
1 parent baf4723 commit d3ec78d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jsonrpsee = { version = "0.20", features = ["http-client", "ws-client"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
jsonrpsee = { version = "0.20", features = ["wasm-client"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen-test = "0.3.0"

[dev-dependencies]
libp2p = { workspace = true, features = [
"tokio",
Expand Down
30 changes: 20 additions & 10 deletions rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#[cfg(not(target_arch = "wasm32"))]
pub use self::native::Client;

#[cfg(target_arch = "wasm32")]
pub use self::wasm::Client;

#[cfg(not(target_arch = "wasm32"))]
mod native {
use std::fmt;
Expand Down Expand Up @@ -155,28 +158,35 @@ mod wasm {
use std::fmt;
use std::result::Result as StdResult;

use crate::Result;
use crate::{Error, Result};
use async_trait::async_trait;
use jsonrpsee::core::client::{BatchResponse, ClientT, Subscription, SubscriptionClientT};
use jsonrpsee::core::params::BatchRequestBuilder;
use jsonrpsee::core::traits::ToRpcParams;
use jsonrpsee::core::Error as JrpcError;
use jsonrpsee::wasm_client::{Client, WasmClientBuilder};
use jsonrpsee::wasm_client::{Client as WasmClient, WasmClientBuilder};
use serde::de::DeserializeOwned;

pub struct WasmClient {
client: Client,
pub struct Client {
client: WasmClient,
}

impl WasmClient {
pub async fn new(_conn_str: &str) -> Result<Self> {
let client = WasmClientBuilder::default().build(_conn_str).await?;
Ok(WasmClient { client })
impl Client {
pub async fn new(conn_str: &str) -> Result<Self> {
// Since headers are not supported in the current version of `jsonrpsee-wasm-client`,
// celestia-node requires disabling authentication (--rpc.skip-auth) to use wasm.
let protocol = conn_str.split_once(':').map(|(proto, _)| proto);
let client = match protocol {
Some("ws") | Some("wss") => WasmClientBuilder::default().build(conn_str).await?,
_ => return Err(Error::ProtocolNotSupported(conn_str.into())),
};

Ok(Client { client })
}
}

#[async_trait]
impl ClientT for WasmClient {
impl ClientT for Client {
async fn notification<Params>(
&self,
method: &str,
Expand Down Expand Up @@ -208,7 +218,7 @@ mod wasm {
}

#[async_trait]
impl SubscriptionClientT for WasmClient {
impl SubscriptionClientT for Client {
async fn subscribe<'a, N, Params>(
&self,
subscribe_method: &'a str,
Expand Down
1 change: 0 additions & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod share;
mod state;

pub use crate::blob::BlobClient;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::client::Client;
pub use crate::error::{Error, Result};
pub use crate::header::HeaderClient;
Expand Down
25 changes: 25 additions & 0 deletions rpc/tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![cfg(target_arch = "wasm32")]

use celestia_rpc::client::Client;
use celestia_rpc::prelude::*;
use wasm_bindgen_test::*;

const CELESTIA_RPC_URL: &str = "ws://localhost:26658";

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
async fn network_head() {
let client = Client::new(CELESTIA_RPC_URL).await.unwrap();
let network_head = client.header_network_head().await.unwrap();

let genesis_header = client.header_get_by_height(1).await.unwrap();
let adjacent_header = client
.header_get_by_height(network_head.height().value() - 1)
.await
.unwrap();

network_head.validate().unwrap();
genesis_header.verify(&network_head).unwrap();
adjacent_header.verify(&network_head).unwrap();
}

0 comments on commit d3ec78d

Please sign in to comment.