Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ bip39 = { version = "2.1.0", features = ["rand"] }
bitcoin = "0.32"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_cbor = "0.11.2"
log = "^0.4"
dirs = "3.0.1"
socks = "0.3.4"
Expand All @@ -37,6 +36,7 @@ zmq = "0.10.0"
tungstenite = { version = "0.28.0", features = ["native-tls"]}
nostr = "0.44.2"
crossbeam-channel = "0.5.15"
minicbor = { version = "0.24.4", features = ["derive", "alloc", "std"] }

[dev-dependencies]
flate2 = {version = "1.0.35"}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/maker-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn send_rpc_req(mut stream: TcpStream, req: RpcMsgReq) -> Result<(), MakerError>
send_message(&mut stream, &req)?;

let response_bytes = read_message(&mut stream)?;
let response: RpcMsgResp = serde_cbor::from_slice(&response_bytes)?;
let response: RpcMsgResp = minicbor::decode(&response_bytes)?;

if matches!(response, RpcMsgResp::Pong) {
println!("success");
Expand Down
17 changes: 13 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ pub enum NetError {

/// Error related to CBOR (Concise Binary Object Representation) serialization or deserialization.
///
/// This variant wraps a [`serde_cbor::Error`] to provide details about the issue.
Cbor(serde_cbor::Error),
/// This variant wraps a [`minicbor::decode::Error`] to provide details about the issue.
Cbor(minicbor::decode::Error),

/// Error related to CBOR serialization.
Encode(String),

/// Error indicating an invalid CLI application network.
InvalidAppNetwork,
Expand All @@ -46,8 +49,8 @@ impl From<std::io::Error> for NetError {
}
}

impl From<serde_cbor::Error> for NetError {
fn from(value: serde_cbor::Error) -> Self {
impl From<minicbor::decode::Error> for NetError {
fn from(value: minicbor::decode::Error) -> Self {
Self::Cbor(value)
}
}
Expand Down Expand Up @@ -80,3 +83,9 @@ impl From<minreq::Error> for FeeEstimatorError {
FeeEstimatorError::HttpError(err)
}
}

impl<W: core::fmt::Debug> From<minicbor::encode::Error<W>> for NetError {
fn from(err: minicbor::encode::Error<W>) -> Self {
NetError::Encode(format!("Encode error: {:?}", err))
}
}
4 changes: 2 additions & 2 deletions src/maker/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl From<std::io::Error> for MakerError {
}
}

impl From<serde_cbor::Error> for MakerError {
fn from(value: serde_cbor::Error) -> Self {
impl From<minicbor::decode::Error> for MakerError {
fn from(value: minicbor::decode::Error) -> Self {
Self::Net(NetError::Cbor(value))
}
}
Expand Down
103 changes: 93 additions & 10 deletions src/maker/rpc/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,89 +12,139 @@ use crate::wallet::Balances;
///
/// These messages are used for various operations in the Maker-rpc communication.
/// Each variant corresponds to a specific action or query in the RPC protocol.
#[derive(Serialize, Deserialize, Debug)]
#[derive(minicbor::Encode, minicbor::Decode, Serialize, Deserialize, Debug)]
pub enum RpcMsgReq {
/// Ping request to check connectivity.
#[n(0)]
Ping,
/// Request to fetch all utxos in the wallet.
#[n(1)]
Utxo,
/// Request to fetch only swap utxos in the wallet.
#[n(2)]
SwapUtxo,
/// Request to fetch UTXOs in the contract pool.
#[n(3)]
ContractUtxo,
/// Request to fetch UTXOs in the fidelity pool.
#[n(4)]
FidelityUtxo,
/// Request to retrieve the total wallet balances of different categories.
#[n(5)]
Balances,
/// Request for generating a new wallet address.
#[n(6)]
NewAddress,
/// Request to send funds to a specific address.
#[n(7)]
SendToAddress {
/// The recipient's address.
#[n(0)]
address: String,
/// The amount to send.
#[n(1)]
amount: u64,
/// The transaction fee to include.
#[n(2)]
feerate: f64,
},
/// Request to retrieve the Tor address of the Maker.
#[n(8)]
GetTorAddress,
/// Request to retrieve the data directory path.
#[n(9)]
GetDataDir,
/// Request to stop the Maker server.
#[n(10)]
Stop,
/// Request to list all active and past fidelity bonds.
#[n(11)]
ListFidelity,
/// Request to sync the internal wallet with blockchain.
#[n(12)]
SyncWallet,
}

/// Enum representing RPC message responses.
///
/// These messages are sent in response to RPC requests and carry the results
/// of the corresponding actions or queries.
#[derive(Serialize, Deserialize, Debug)]
#[derive(minicbor::Encode, minicbor::Decode, Serialize, Deserialize, Debug)]
pub enum RpcMsgResp {
/// Response to a Ping request.
#[n(0)]
Pong,
/// Response containing all spendable UTXOs
#[n(1)]
UtxoResp {
/// List of spendable UTXOs in the wallet.
#[n(0)]
#[cbor(encode_with = "encode_json_bytes", decode_with = "decode_json_bytes")]
utxos: Vec<UTXO>,
},
/// Response containing UTXOs in the swap pool.
#[n(2)]
SwapUtxoResp {
/// List of UTXOs in the swap pool.
#[n(0)]
#[cbor(encode_with = "encode_json_bytes", decode_with = "decode_json_bytes")]
utxos: Vec<UTXO>,
},
/// Response containing UTXOs in the fidelity pool.
#[n(3)]
FidelityUtxoResp {
/// List of UTXOs in the fidelity pool.
#[n(0)]
#[cbor(encode_with = "encode_json_bytes", decode_with = "decode_json_bytes")]
utxos: Vec<UTXO>,
},
/// Response containing UTXOs in the contract pool.
#[n(4)]
ContractUtxoResp {
/// List of UTXOs in the contract pool.
#[n(0)]
#[cbor(encode_with = "encode_json_bytes", decode_with = "decode_json_bytes")]
utxos: Vec<UTXO>,
},
/// Response containing the total wallet balances of different categories.
TotalBalanceResp(Balances),
#[n(5)]
TotalBalanceResp(
#[n(0)]
#[cbor(encode_with = "encode_json_bytes", decode_with = "decode_json_bytes")]
Balances,
),
/// Response containing a newly generated wallet address.
NewAddressResp(String),
#[n(6)]
NewAddressResp(#[n(0)] String),
/// Response to a send-to-address request.
SendToAddressResp(String),
#[n(7)]
SendToAddressResp(#[n(0)] String),
/// Response containing the Tor address of the Maker.
GetTorAddressResp(String),
#[n(8)]
GetTorAddressResp(#[n(0)] String),
/// Response containing the path to the data directory.
GetDataDirResp(PathBuf),
#[n(9)]
GetDataDirResp(
#[n(0)]
#[cbor(encode_with = "encode_path_buf", decode_with = "decode_path_buf")]
PathBuf,
),
/// Response indicating the server has been shut down.
#[n(10)]
Shutdown,
/// Response with the fidelity spending txid.
FidelitySpend(Txid),
#[n(11)]
FidelitySpend(
#[n(0)]
#[cbor(encode_with = "encode_json_bytes", decode_with = "decode_json_bytes")]
Txid,
),
/// Response with the internal server error.
ServerError(String),
#[n(12)]
ServerError(#[n(0)] String),
/// Response listing all current and past fidelity bonds.
ListBonds(String),
#[n(13)]
ListBonds(#[n(0)] String),
}

impl Display for RpcMsgResp {
Expand Down Expand Up @@ -136,3 +186,36 @@ impl Display for RpcMsgResp {
}
}
}

// Inline minicbor helpers
#[allow(dead_code)]
fn encode_json_bytes<T: serde::Serialize, W: minicbor::encode::Write, C>(
x: &T,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.bytes(&serde_json::to_vec(x).map_err(|_| minicbor::encode::Error::message("json error"))?)?;
Ok(())
}
fn decode_json_bytes<T: serde::de::DeserializeOwned, C>(
d: &mut minicbor::Decoder<'_>,
_ctx: &mut C,
) -> Result<T, minicbor::decode::Error> {
serde_json::from_slice(d.bytes()?)
.map_err(|_| minicbor::decode::Error::message("json decode error"))
}

fn encode_path_buf<W: minicbor::encode::Write, C>(
x: &std::path::PathBuf,
e: &mut minicbor::Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.encode(x.to_str().unwrap_or(""))?;
Ok(())
}
fn decode_path_buf<C>(
d: &mut minicbor::Decoder<'_>,
_ctx: &mut C,
) -> Result<std::path::PathBuf, minicbor::decode::Error> {
Ok(std::path::PathBuf::from(d.decode::<String>()?))
}
Comment thread
Ferryx349 marked this conversation as resolved.
2 changes: 1 addition & 1 deletion src/maker/rpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub trait MakerRpc {

fn handle_request<M: MakerRpc>(maker: &Arc<M>, socket: &mut TcpStream) -> Result<(), MakerError> {
let msg_bytes = read_message(socket)?;
let rpc_request: RpcMsgReq = serde_cbor::from_slice(&msg_bytes)?;
let rpc_request: RpcMsgReq = minicbor::decode(&msg_bytes)?;
log::info!("RPC request received: {rpc_request:?}");

let resp = match rpc_request {
Expand Down
2 changes: 1 addition & 1 deletion src/maker/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ fn handle_client(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(), Maker
}
}
}
let taker_msg = serde_cbor::from_slice::<TakerToMakerMessage>(&bytes)?;
let taker_msg = minicbor::decode::<TakerToMakerMessage>(&bytes)?;

log::info!("[{}] <=== {}", maker.config.network_port, taker_msg);

Expand Down
2 changes: 1 addition & 1 deletion src/maker/server2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ fn handle_client_taproot(maker: &Arc<Maker>, stream: &mut TcpStream) -> Result<(
message_bytes.len(),
ip
);
let message = match serde_cbor::from_slice::<TakerToMakerMessage>(&message_bytes) {
let message = match minicbor::decode::<TakerToMakerMessage>(&message_bytes) {
Ok(msg) => {
log::info!("[{}] <=== {}", maker.config.network_port, msg);
msg
Expand Down
Loading