diff --git a/lib-xps/Cargo.toml b/lib-xps/Cargo.toml index 667d3b7..99ce550 100644 --- a/lib-xps/Cargo.toml +++ b/lib-xps/Cargo.toml @@ -11,6 +11,7 @@ log.workspace = true tracing.workspace = true tracing-subscriber.workspace = true serde.workspace = true +serde_json.workspace = true tokio.workspace = true async-trait.workspace = true jsonrpsee.workspace = true @@ -30,4 +31,3 @@ messaging = { path = "../messaging" } jsonrpsee = { workspace = true, features = ["macros", "server", "client"] } tokio = { workspace = true, features = ["macros", "rt", "time"] } futures = "0.3" -serde_json.workspace = true diff --git a/lib-xps/src/lib.rs b/lib-xps/src/lib.rs index 1cde25c..71c6a3b 100644 --- a/lib-xps/src/lib.rs +++ b/lib-xps/src/lib.rs @@ -7,7 +7,7 @@ use ethers::{ abi::Address, providers::{Provider, Ws}, }; -use jsonrpsee::server::Server; +use jsonrpsee::{server::Server, RpcModule}; use std::str::FromStr; use xps_types::{CONVERSATION, DID_ETH_REGISTRY}; @@ -29,10 +29,33 @@ pub async fn run>(host: String, port: u16, provider: P) -> Result< let provider = Provider::::connect(provider.as_ref()).await.unwrap(); let context = GatewayContext::new(registry_contract, conversation_contract, provider).await?; - let xps_methods = rpc::XpsMethods::new(&context); - let handle = server.start(xps_methods.into_rpc()); + let mut methods = RpcModule::new(()); + methods.merge(rpc::XpsMethods::new(&context).into_rpc())?; + let methods = build_rpc_api(methods); + + let handle = server.start(methods); log::info!("Server Started at {addr}"); handle.stopped().await; Ok(()) } + +// create an endpoint that lists all the methods available on the server, at the +// endpoint `/rpc_methods` +fn build_rpc_api(mut rpc_api: RpcModule) -> RpcModule { + let mut available_methods = rpc_api.method_names().collect::>(); + // The "rpc_methods" is defined below and we want it to be part of the reported methods. + available_methods.push("rpc_methods"); + available_methods.sort(); + + rpc_api + .register_method("rpc_methods", move |_, _| { + serde_json::json!({ + "methods": available_methods, + }) + }) + .expect("infallible all other methods have their own address space; qed"); + + rpc_api +} +