From 3d1cad1c6fc4180156507474e0af599b29b3c5ce Mon Sep 17 00:00:00 2001 From: Abdul Date: Mon, 12 Aug 2024 16:03:04 +0100 Subject: [PATCH 1/2] add syscoin client --- .../src/syscoin_client/client.rs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 core/lib/default_da_clients/src/syscoin_client/client.rs diff --git a/core/lib/default_da_clients/src/syscoin_client/client.rs b/core/lib/default_da_clients/src/syscoin_client/client.rs new file mode 100644 index 00000000000..9741c00ca43 --- /dev/null +++ b/core/lib/default_da_clients/src/syscoin_client/client.rs @@ -0,0 +1,93 @@ +pub mod types; + +use async_trait::async_trait; +use reqwest::Client; +use serde::{Serialize, Deserialize}; +use std::sync::Arc; +use hex::encode as hex_encode; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum DAError { + #[error("Network error: {0}")] + NetworkError(reqwest::Error), + #[error("Serialization error: {0}")] + SerializationError(serde_json::Error), +} + +type Result = std::result::Result; + +#[derive(Serialize, Deserialize, Debug)] +struct DispatchResponse { + vh: String, // Version hash returned by Syscoin RPC +} + +#[async_trait] +pub trait DataAvailabilityClient: Sync + Send + std::fmt::Debug { + async fn dispatch_blob(&self, data: Vec) -> Result; + fn clone_boxed(&self) -> Box; +} + +#[derive(Clone, Debug)] +struct SyscoinClient { + client: Client, + rpc_url: String, + user: String, + password: String, +} + +#[async_trait] +impl DataAvailabilityClient for SyscoinClient { + async fn dispatch_blob(&self, data: Vec) -> Result { + let endpoint = format!("{}/", self.rpc_url); + let params = serde_json::json!({ + "method": "syscoincreatenevmblob", + "params": { + "data": hex_encode(data) + } + }); + + let client = self.client.clone(); + let response = client.post(endpoint) + .basic_auth(&self.user, Some(&self.password)) + .json(¶ms) + .send() + .await + .map_err(DAError::NetworkError)?; + + let result = response + .json::() + .await + .map_err(DAError::SerializationError)?; + + if let Some(error) = result.error { + Err(DAError::NetworkError(reqwest::Error::new( + reqwest::ErrorKind::Other, + format!("RPC error: {}", error.message), + ))) + } else { + Ok(DispatchResponse { vh: result.result.vh }) + } + } + + fn clone_boxed(&self) -> Box { + Box::new(self.clone()) + } +} + +#[derive(Serialize, Deserialize, Debug)] +struct RpcResponse { + result: DispatchResult, + error: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +struct DispatchResult { + vh: String, +} + +#[derive(Serialize, Deserialize, Debug)] +struct RpcError { + code: i32, + message: String, +} From e2676aaa58f259b08cb5e8c339f7e8e62c1288be Mon Sep 17 00:00:00 2001 From: Abdul Date: Tue, 13 Aug 2024 17:29:09 +0100 Subject: [PATCH 2/2] add clone_boxed --- core/lib/default_da_clients/src/syscoin_client/client.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/lib/default_da_clients/src/syscoin_client/client.rs b/core/lib/default_da_clients/src/syscoin_client/client.rs index 9741c00ca43..e69e5b3f10c 100644 --- a/core/lib/default_da_clients/src/syscoin_client/client.rs +++ b/core/lib/default_da_clients/src/syscoin_client/client.rs @@ -73,6 +73,10 @@ impl DataAvailabilityClient for SyscoinClient { fn clone_boxed(&self) -> Box { Box::new(self.clone()) } + + fn blob_size_limit(&self) -> Option { + None + } } #[derive(Serialize, Deserialize, Debug)]