diff --git a/README.md b/README.md index 1bd9b09..72aa4f7 100644 --- a/README.md +++ b/README.md @@ -132,31 +132,31 @@ While running, a solar node can be queried using JSON-RPC over HTTP. | Method | Parameters | Response | Description | | --- | --- | --- | --- | -| `blocks` | `"<@...=.ed25519>"` | `[<@...=.ed25519>]` | Returns an array of public keys | -| `blockers` | `"<@...=.ed25519>"` | `[<@...=.ed25519>]` | Returns an array of public keys | -| `descriptions` | `"<@...=.ed25519>"` | `[]` | Returns an array of descriptions | -| `self_descriptions` | `"<@...=.ed25519>"` | `[]` | Returns an array of descriptions | -| `latest_description` | `"<@...=.ed25519>"` | `` | Returns a single description | -| `latest_self_description` | `"<@...=.ed25519>"` | `` | Returns a single description | -| `feed` | `"<@...=.ed25519>"` | `[{ "key": "<%...=.sha256>", "value": , "timestamp": , "rts": null }]` | Returns an array of message KVTs (key, value, timestamp) from the local database | -| `follows` | `"<@...=.ed25519>"` | `[<@...=.ed25519>]` | Returns an array of public keys | -| `followers` | `"<@...=.ed25519>"` | `[<@...=.ed25519>]` | Returns an array of public keys | -| `is_following` | `{ "peer_a": "<@...=.ed25519>", "peer_b": "<@...=.ed25519>" }` | `[<@...=.ed25519>]` | Returns a boolean | -| `friends` | `"<@...=.ed25519>"` | `[<@...=.ed25519>]` | Returns an array of public keys | -| `images` | `"<@...=.ed25519>"` | `[<&...=.sha256>]` | Returns an array of image references | -| `self_images` | `"<@...=.ed25519>"` | `[<&...=.sha256>]` | Returns an array of image references | -| `latest_image` | `"<@...=.ed25519>"` | `<&...=.sha256>` | Returns a single image reference | -| `latest_self_image` | `"<@...=.ed25519>"` | `<&...=.sha256>` | Returns a single image reference | -| `message` | `"<%...=.sha256>"` | `{ "key": "<%...=.sha256>", "value": , "timestamp": , "rts": null }` | Returns a single message KVT (key, value, timestamp) from the local database | -| `names` | `"<@...=.ed25519>"` | `[]` | Returns an array of names | -| `self_names` | `"<@...=.ed25519>"` | `[]` | Returns an array of names | -| `latest_name` | `"<@...=.ed25519>"` | `` | Returns a single name | -| `latest_self_name` | `"<@...=.ed25519>"` | `` | Returns a single name | +| `blocks` | `["<@...=.ed25519>"]` | `[<@...=.ed25519>]` | Returns an array of public keys | +| `blockers` | `["<@...=.ed25519>"]` | `[<@...=.ed25519>]` | Returns an array of public keys | +| `descriptions` | `["<@...=.ed25519>"]` | `[]` | Returns an array of descriptions | +| `self_descriptions` | `["<@...=.ed25519>"]` | `[]` | Returns an array of descriptions | +| `latest_description` | `["<@...=.ed25519>"]` | `` | Returns a single description | +| `latest_self_description` | `["<@...=.ed25519>"]` | `` | Returns a single description | +| `feed` | `["<@...=.ed25519>"]` | `[{ "key": "<%...=.sha256>", "value": , "timestamp": , "rts": null }]` | Returns an array of message KVTs (key, value, timestamp) from the local database | +| `follows` | `["<@...=.ed25519>"]` | `[<@...=.ed25519>]` | Returns an array of public keys | +| `followers` | `["<@...=.ed25519>"]` | `[<@...=.ed25519>]` | Returns an array of public keys | +| `is_following` | `["<@...=.ed25519>", "<@...=.ed25519>"]` | `` | Returns a boolean | +| `friends` | `["<@...=.ed25519>"]` | `[<@...=.ed25519>]` | Returns an array of public keys | +| `images` | `["<@...=.ed25519>"]` | `[<&...=.sha256>]` | Returns an array of image references | +| `self_images` | `["<@...=.ed25519>"]` | `[<&...=.sha256>]` | Returns an array of image references | +| `latest_image` | `["<@...=.ed25519>"]` | `<&...=.sha256>` | Returns a single image reference | +| `latest_self_image` | `["<@...=.ed25519>"]` | `<&...=.sha256>` | Returns a single image reference | +| `message` | `["<%...=.sha256>"]` | `{ "key": "<%...=.sha256>", "value": , "timestamp": , "rts": null }` | Returns a single message KVT (key, value, timestamp) from the local database | +| `names` | `["<@...=.ed25519>"]` | `[]` | Returns an array of names | +| `self_names` | `["<@...=.ed25519>"]` | `[]` | Returns an array of names | +| `latest_name` | `["<@...=.ed25519>"]` | `` | Returns a single name | +| `latest_self_name` | `["<@...=.ed25519>"]` | `` | Returns a single name | | `peers` | | `[{ "pub_key": "<@...=.ed25519>", "seq_num": }` | Returns an array of public key and latest sequence number for each peer in the local database | | `ping` | | `pong!` | Responds if the JSON-RPC server is running | | `publish` | `` | `{ "msg_ref": "<%...=.sha256>", "seq_num": }` | Publishes a message and returns the reference (message hash) and sequence number | -| `subscribers` | `""` | `[<@...=.ed25519>]` | Returns an array of public keys | -| `subscriptions` | `"<@...=.ed25519>"` | `[]` | Returns an array of channel names | +| `subscribers` | `[""]` | `[<@...=.ed25519>]` | Returns an array of public keys | +| `subscriptions` | `["<@...=.ed25519>"]` | `[]` | Returns an array of channel names | | `whoami` | | `<@...=.ed25519>` | Returns the public key of the local node | ### Examples diff --git a/solar/src/actors/jsonrpc/server.rs b/solar/src/actors/jsonrpc/server.rs index c25d017..c882723 100644 --- a/solar/src/actors/jsonrpc/server.rs +++ b/solar/src/actors/jsonrpc/server.rs @@ -8,32 +8,10 @@ use jsonrpsee::server::{logger::Params, RpcModule, ServerBuilder}; use jsonrpsee::types::error::ErrorObject as JsonRpcError; use kuska_ssb::{api::dto::content::TypedMessage, feed::Message, keystore::OwnedIdentity}; use log::{info, warn}; -use serde::Deserialize; use serde_json::{json, Value}; use crate::{broker::*, error::Error, node::KV_STORE, Result}; -/// The name of a channel. -#[derive(Debug, Deserialize)] -struct Channel(String); - -/// The public keys (ID) of two peers. -#[derive(Debug, Deserialize)] -struct IsFollowing { - peer_a: String, - peer_b: String, -} - -/// Message reference containing the key (sha256 hash) of a message. -/// Used to parse the key from the parameters supplied to the `message` -/// endpoint. -#[derive(Debug, Deserialize)] -struct MsgRef(String); - -/// The public key (ID) of a peer. -#[derive(Debug, Deserialize)] -struct PubKey(String); - /// Register the JSON-RPC server endpoint, define the JSON-RPC methods /// and spawn the server. /// @@ -61,13 +39,13 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< rpc_module.register_method("blocks", move |params: Params, _| { task::block_on(async { // Parse the parameter containing the public key. - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; // Open the primary KV database for reading. let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let blocks = indexes.get_blocks(&pub_key.0)?; + let blocks = indexes.get_blocks(&pub_key[0])?; let response = json!(blocks); Ok::(response) @@ -79,12 +57,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of public keys. rpc_module.register_method("blockers", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let blockers = indexes.get_blockers(&pub_key.0)?; + let blockers = indexes.get_blockers(&pub_key[0])?; let response = json!(blockers); Ok::(response) @@ -96,12 +74,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of descriptions. rpc_module.register_method("descriptions", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let descriptions = indexes.get_descriptions(&pub_key.0)?; + let descriptions = indexes.get_descriptions(&pub_key[0])?; let response = json!(descriptions); Ok::(response) @@ -113,12 +91,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of descriptions. rpc_module.register_method("self_descriptions", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let descriptions = indexes.get_self_assigned_descriptions(&pub_key.0)?; + let descriptions = indexes.get_self_assigned_descriptions(&pub_key[0])?; let response = json!(descriptions); Ok::(response) @@ -130,12 +108,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns a string. rpc_module.register_method("latest_description", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let description = indexes.get_latest_description(&pub_key.0)?; + let description = indexes.get_latest_description(&pub_key[0])?; let response = json!(description); Ok::(response) @@ -148,12 +126,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns a string. rpc_module.register_method("latest_self_description", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let description = indexes.get_latest_self_assigned_description(&pub_key.0)?; + let description = indexes.get_latest_self_assigned_description(&pub_key[0])?; let response = json!(description); Ok::(response) @@ -165,12 +143,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of public keys. rpc_module.register_method("follows", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let follows = indexes.get_follows(&pub_key.0)?; + let follows = indexes.get_follows(&pub_key[0])?; let response = json!(follows); Ok::(response) @@ -182,12 +160,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of public keys. rpc_module.register_method("followers", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let followers = indexes.get_followers(&pub_key.0)?; + let followers = indexes.get_followers(&pub_key[0])?; let response = json!(followers); Ok::(response) @@ -199,12 +177,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns a boolean. rpc_module.register_method("is_following", move |params: Params, _| { task::block_on(async { - let peers: IsFollowing = params.parse()?; + let peers = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let is_following = indexes.is_following(&peers.peer_a, &peers.peer_b)?; + let is_following = indexes.is_following(&peers[0], &peers[1])?; let response = json!(is_following); Ok::(response) @@ -217,12 +195,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of public keys. rpc_module.register_method("friends", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let friends = indexes.get_friends(&pub_key.0)?; + let friends = indexes.get_friends(&pub_key[0])?; let response = json!(friends); Ok::(response) @@ -234,12 +212,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of strings. rpc_module.register_method("images", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let images = indexes.get_images(&pub_key.0)?; + let images = indexes.get_images(&pub_key[0])?; let response = json!(images); Ok::(response) @@ -251,12 +229,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of strings. rpc_module.register_method("self_images", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let images = indexes.get_self_assigned_images(&pub_key.0)?; + let images = indexes.get_self_assigned_images(&pub_key[0])?; let response = json!(images); Ok::(response) @@ -268,12 +246,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns a string. rpc_module.register_method("latest_image", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let image = indexes.get_latest_image(&pub_key.0)?; + let image = indexes.get_latest_image(&pub_key[0])?; let response = json!(image); Ok::(response) @@ -286,12 +264,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of strings. rpc_module.register_method("latest_self_image", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let image = indexes.get_latest_self_assigned_image(&pub_key.0)?; + let image = indexes.get_latest_self_assigned_image(&pub_key[0])?; let response = json!(image); Ok::(response) @@ -303,12 +281,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of strings. rpc_module.register_method("names", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let names = indexes.get_names(&pub_key.0)?; + let names = indexes.get_names(&pub_key[0])?; let response = json!(names); Ok::(response) @@ -320,12 +298,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of strings. rpc_module.register_method("self_names", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let names = indexes.get_self_assigned_names(&pub_key.0)?; + let names = indexes.get_self_assigned_names(&pub_key[0])?; let response = json!(names); Ok::(response) @@ -337,12 +315,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns a string. rpc_module.register_method("latest_name", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let names = indexes.get_latest_name(&pub_key.0)?; + let names = indexes.get_latest_name(&pub_key[0])?; let response = json!(names); Ok::(response) @@ -354,12 +332,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns a string. rpc_module.register_method("latest_self_name", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let names = indexes.get_latest_self_assigned_name(&pub_key.0)?; + let names = indexes.get_latest_self_assigned_name(&pub_key[0])?; let response = json!(names); Ok::(response) @@ -371,12 +349,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of public keys. rpc_module.register_method("subscribers", move |params: Params, _| { task::block_on(async { - let channel: Channel = params.parse()?; + let channel = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let subscribers = indexes.get_channel_subscribers(&channel.0)?; + let subscribers = indexes.get_channel_subscribers(&channel[0])?; let response = json!(subscribers); Ok::(response) @@ -388,12 +366,12 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< // Returns an array of channel names. rpc_module.register_method("subscriptions", move |params: Params, _| { task::block_on(async { - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; let db = KV_STORE.read().await; let indexes = &db.indexes.as_ref().ok_or(Error::Indexes)?; - let subscriptions = indexes.get_channel_subscriptions(&pub_key.0)?; + let subscriptions = indexes.get_channel_subscriptions(&pub_key[0])?; let response = json!(subscriptions); Ok::(response) @@ -405,13 +383,13 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< rpc_module.register_method("feed", move |params: Params, _| { task::block_on(async { // Parse the parameter containing the public key. - let pub_key: PubKey = params.parse()?; + let pub_key = params.parse::>()?; // Open the primary KV database for reading. let db = KV_STORE.read().await; // Retrieve the message value for the requested message. - let feed = db.get_feed(&pub_key.0)?; + let feed = db.get_feed(&pub_key[0])?; let response = json!(feed); Ok::(response) @@ -423,13 +401,13 @@ pub async fn actor(server_id: OwnedIdentity, server_addr: SocketAddr) -> Result< rpc_module.register_method("message", move |params: Params, _| { task::block_on(async { // Parse the parameter containing the message reference (key). - let msg_ref: MsgRef = params.parse()?; + let msg_ref = params.parse::>()?; // Open the primary KV database for reading. let db = KV_STORE.read().await; // Retrieve the message value for the requested message. - let msg_val = db.get_msg_val(&msg_ref.0)?; + let msg_val = db.get_msg_val(&msg_ref[0])?; // Retrieve the message KVT for the requested message using the // author and sequence fields from the message value.