diff --git a/common/src/rpc_primitives/message.rs b/common/src/rpc_primitives/message.rs index 8207267..9886744 100644 --- a/common/src/rpc_primitives/message.rs +++ b/common/src/rpc_primitives/message.rs @@ -62,6 +62,16 @@ pub struct Request { } impl Request { + pub fn from_payload_version_2_0(method: String, payload: serde_json::Value) -> Self { + Self { + jsonrpc: Version, + method, + params: payload, + // ToDo: Correct checking of id + id: 1.into(), + } + } + /// Answer the request with a (positive) reply. /// /// The ID is taken from the request. diff --git a/common/src/rpc_primitives/requests.rs b/common/src/rpc_primitives/requests.rs index e0c6d31..7164193 100644 --- a/common/src/rpc_primitives/requests.rs +++ b/common/src/rpc_primitives/requests.rs @@ -141,16 +141,13 @@ mod base64_deser { pub mod vec { use super::*; - pub fn serialize(bytes: &[Vec], serializer: S) -> Result + pub fn serialize(bytes_vec: &[Vec], serializer: S) -> Result where S: Serializer, { - let base64_strings: Vec = bytes - .iter() - .map(|b| general_purpose::STANDARD.encode(b)) - .collect(); - let mut seq = serializer.serialize_seq(Some(base64_strings.len()))?; - for s in base64_strings { + let mut seq = serializer.serialize_seq(Some(bytes_vec.len()))?; + for bytes in bytes_vec { + let s = general_purpose::STANDARD.encode(bytes); seq.serialize_element(&s)?; } seq.end() @@ -161,15 +158,14 @@ mod base64_deser { D: Deserializer<'de>, { let base64_strings: Vec = Deserialize::deserialize(deserializer)?; - let bytes_vec: Result>, D::Error> = base64_strings + base64_strings .into_iter() .map(|s| { general_purpose::STANDARD .decode(&s) .map_err(serde::de::Error::custom) }) - .collect(); - bytes_vec + .collect() } } } @@ -213,3 +209,10 @@ pub struct GetProofForCommitmentResponse { pub struct GetProgramIdsResponse { pub program_ids: HashMap, } + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct GetInitialTestnetAccountsResponse { + /// Hex encoded account id + pub account_id: String, + pub balance: u64, +} diff --git a/common/src/sequencer_client/mod.rs b/common/src/sequencer_client.rs similarity index 89% rename from common/src/sequencer_client/mod.rs rename to common/src/sequencer_client.rs index 7a3956b..d3c5f23 100644 --- a/common/src/sequencer_client/mod.rs +++ b/common/src/sequencer_client.rs @@ -1,9 +1,9 @@ use std::{collections::HashMap, ops::RangeInclusive}; use anyhow::Result; -use json::{SendTxRequest, SendTxResponse, SequencerRpcRequest, SequencerRpcResponse}; use nssa_core::program::ProgramId; use reqwest::Client; +use serde::Deserialize; use serde_json::Value; use super::rpc_primitives::requests::{ @@ -12,19 +12,20 @@ use super::rpc_primitives::requests::{ }; use crate::{ error::{SequencerClientError, SequencerRpcError}, - rpc_primitives::requests::{ - GetAccountRequest, GetAccountResponse, GetAccountsNoncesRequest, GetAccountsNoncesResponse, - GetBlockRangeDataRequest, GetBlockRangeDataResponse, GetLastBlockRequest, - GetLastBlockResponse, GetProgramIdsRequest, GetProgramIdsResponse, - GetProofForCommitmentRequest, GetProofForCommitmentResponse, GetTransactionByHashRequest, - GetTransactionByHashResponse, + rpc_primitives::{ + self, + requests::{ + GetAccountRequest, GetAccountResponse, GetAccountsNoncesRequest, + GetAccountsNoncesResponse, GetBlockRangeDataRequest, GetBlockRangeDataResponse, + GetInitialTestnetAccountsResponse, GetLastBlockRequest, GetLastBlockResponse, + GetProgramIdsRequest, GetProgramIdsResponse, GetProofForCommitmentRequest, + GetProofForCommitmentResponse, GetTransactionByHashRequest, + GetTransactionByHashResponse, SendTxRequest, SendTxResponse, + }, }, - sequencer_client::json::AccountInitialData, transaction::{EncodedTransaction, NSSATransaction}, }; -pub mod json; - #[derive(Clone)] pub struct SequencerClient { pub client: reqwest::Client, @@ -47,7 +48,8 @@ impl SequencerClient { method: &str, payload: Value, ) -> Result { - let request = SequencerRpcRequest::from_payload_version_2_0(method.to_string(), payload); + let request = + rpc_primitives::message::Request::from_payload_version_2_0(method.to_string(), payload); let call_builder = self.client.post(&self.sequencer_addr); @@ -55,6 +57,15 @@ impl SequencerClient { let response_vall = call_res.json::().await?; + // TODO: Actually why we need separation of `result` and `error` in rpc response? + #[derive(Debug, Clone, Deserialize)] + #[allow(dead_code)] + pub struct SequencerRpcResponse { + pub jsonrpc: String, + pub result: serde_json::Value, + pub id: u64, + } + if let Ok(response) = serde_json::from_value::(response_vall.clone()) { Ok(response.result) @@ -244,7 +255,7 @@ impl SequencerClient { /// Get initial testnet accounts from sequencer pub async fn get_initial_testnet_accounts( &self, - ) -> Result, SequencerClientError> { + ) -> Result, SequencerClientError> { let acc_req = GetInitialTestnetAccountsRequest {}; let req = serde_json::to_value(acc_req).unwrap(); diff --git a/common/src/sequencer_client/json.rs b/common/src/sequencer_client/json.rs deleted file mode 100644 index d47aea4..0000000 --- a/common/src/sequencer_client/json.rs +++ /dev/null @@ -1,53 +0,0 @@ -use serde::{Deserialize, Serialize}; - -// Requests - -#[derive(Serialize, Deserialize, Debug)] -pub struct SendTxRequest { - pub transaction: Vec, -} - -// Responses - -#[derive(Serialize, Deserialize, Debug)] -pub struct SendTxResponse { - pub status: String, - pub tx_hash: String, -} - -// General - -#[derive(Debug, Clone, Serialize)] -pub struct SequencerRpcRequest { - jsonrpc: String, - pub method: String, - pub params: serde_json::Value, - pub id: u64, -} - -impl SequencerRpcRequest { - pub fn from_payload_version_2_0(method: String, payload: serde_json::Value) -> Self { - Self { - jsonrpc: "2.0".to_string(), - method, - params: payload, - // ToDo: Correct checking of id - id: 1, - } - } -} - -#[derive(Debug, Clone, Deserialize)] -pub struct SequencerRpcResponse { - pub jsonrpc: String, - pub result: serde_json::Value, - pub id: u64, -} - -#[derive(Debug, Serialize, Deserialize, Clone)] -/// Helperstruct for account serialization -pub struct AccountInitialData { - /// Hex encoded account id - pub account_id: String, - pub balance: u64, -} diff --git a/wallet/src/lib.rs b/wallet/src/lib.rs index 13812be..91a0e4b 100644 --- a/wallet/src/lib.rs +++ b/wallet/src/lib.rs @@ -5,7 +5,8 @@ use base64::{Engine, engine::general_purpose::STANDARD as BASE64}; use chain_storage::WalletChainStore; use common::{ error::ExecutionFailureKind, - sequencer_client::{SequencerClient, json::SendTxResponse}, + rpc_primitives::requests::SendTxResponse, + sequencer_client::SequencerClient, transaction::{EncodedTransaction, NSSATransaction}, }; use config::WalletConfig; diff --git a/wallet/src/program_facades/native_token_transfer/deshielded.rs b/wallet/src/program_facades/native_token_transfer/deshielded.rs index a25be2c..35a13ba 100644 --- a/wallet/src/program_facades/native_token_transfer/deshielded.rs +++ b/wallet/src/program_facades/native_token_transfer/deshielded.rs @@ -1,4 +1,4 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; use nssa::AccountId; use super::{NativeTokenTransfer, auth_transfer_preparation}; diff --git a/wallet/src/program_facades/native_token_transfer/private.rs b/wallet/src/program_facades/native_token_transfer/private.rs index fcf6eee..320027b 100644 --- a/wallet/src/program_facades/native_token_transfer/private.rs +++ b/wallet/src/program_facades/native_token_transfer/private.rs @@ -1,6 +1,6 @@ use std::vec; -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; use nssa::{AccountId, program::Program}; use nssa_core::{NullifierPublicKey, SharedSecretKey, encryption::IncomingViewingPublicKey}; diff --git a/wallet/src/program_facades/native_token_transfer/public.rs b/wallet/src/program_facades/native_token_transfer/public.rs index 2edab15..7981c19 100644 --- a/wallet/src/program_facades/native_token_transfer/public.rs +++ b/wallet/src/program_facades/native_token_transfer/public.rs @@ -1,4 +1,4 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; use nssa::{ AccountId, PublicTransaction, program::Program, diff --git a/wallet/src/program_facades/native_token_transfer/shielded.rs b/wallet/src/program_facades/native_token_transfer/shielded.rs index c049b13..0802d6e 100644 --- a/wallet/src/program_facades/native_token_transfer/shielded.rs +++ b/wallet/src/program_facades/native_token_transfer/shielded.rs @@ -1,4 +1,4 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; use nssa::AccountId; use nssa_core::{NullifierPublicKey, SharedSecretKey, encryption::IncomingViewingPublicKey}; diff --git a/wallet/src/program_facades/pinata.rs b/wallet/src/program_facades/pinata.rs index 46bc7a1..41e7510 100644 --- a/wallet/src/program_facades/pinata.rs +++ b/wallet/src/program_facades/pinata.rs @@ -1,4 +1,4 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; use nssa::AccountId; use nssa_core::SharedSecretKey; diff --git a/wallet/src/program_facades/token.rs b/wallet/src/program_facades/token.rs index 298c4f4..7c97155 100644 --- a/wallet/src/program_facades/token.rs +++ b/wallet/src/program_facades/token.rs @@ -1,4 +1,4 @@ -use common::{error::ExecutionFailureKind, sequencer_client::json::SendTxResponse}; +use common::{error::ExecutionFailureKind, rpc_primitives::requests::SendTxResponse}; use nssa::{AccountId, program::Program}; use nssa_core::{ NullifierPublicKey, SharedSecretKey, encryption::IncomingViewingPublicKey,