Use decode response

This commit is contained in:
Daniel Sanchez Quiros 2023-02-14 16:19:14 +01:00
parent 6e972a76c1
commit b0ea59a29d
7 changed files with 28 additions and 152 deletions

View File

@ -1,11 +1,11 @@
// std
use std::ffi::{CStr, CString};
use std::ffi::CString;
use std::time::Duration;
// crates
use multiaddr::Multiaddr;
use url::{Host, Url};
// internal
use crate::general::JsonResponse;
use crate::utils::decode_response;
use crate::Result;
/// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree.
@ -43,14 +43,6 @@ pub fn waku_dns_discovery(
drop(CString::from_raw(server));
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<Vec<Multiaddr>> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
response.into()
decode_response(result_ptr)
}

View File

@ -1,13 +1,14 @@
//! Waku [filter](https://rfc.vac.dev/spec/36/#waku-filter) protocol related methods
// std
use std::ffi::{CStr, CString};
use std::ffi::CString;
use std::time::Duration;
// crates
// internal
use crate::general::Result;
use crate::general::{FilterSubscription, JsonResponse, PeerId};
use crate::general::{FilterSubscription, PeerId};
use crate::utils::decode_response;
/// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`)
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_subscribechar-filterjson-char-peerid-int-timeoutms)
@ -39,16 +40,7 @@ pub fn waku_filter_subscribe(
drop(CString::from_raw(peer_id_ptr));
result_ptr
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
Result::from(response).map(|_| ())
decode_response::<bool>(result_ptr).map(|_| ())
}
/// Removes subscriptions in a light node matching a content filter and, optionally, a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`)
@ -74,12 +66,6 @@ pub fn waku_filter_unsubscribe(
drop(CString::from_raw(filter_subscription_ptr));
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
Result::from(response).map(|_| ())
decode_response::<bool>(result_ptr).map(|_| ())
}

View File

@ -1,14 +1,15 @@
//! Waku [lightpush](https://rfc.vac.dev/spec/36/#waku-lightpush) protocol related methods
// std
use std::ffi::{CStr, CString};
use std::ffi::CString;
use std::time::Duration;
// crates
use aes_gcm::{Aes256Gcm, Key};
use secp256k1::{PublicKey, SecretKey};
// internal
use crate::general::{JsonResponse, MessageId, PeerId, Result, WakuMessage, WakuPubSubTopic};
use crate::general::{MessageId, PeerId, Result, WakuMessage, WakuPubSubTopic};
use crate::node::waku_dafault_pubsub_topic;
use crate::utils::decode_response;
/// Publish a message using Waku Lightpush
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publishchar-messagejson-char-topic-char-peerid-int-timeoutms)
@ -53,18 +54,7 @@ pub fn waku_lightpush_publish(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
response.into()
decode_response(result_ptr)
}
/// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Lightpush
@ -127,16 +117,7 @@ pub fn waku_lightpush_publish_encrypt_asymmetric(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
message_id.into()
decode_response(result_ptr)
}
/// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Lightpush
@ -198,16 +179,5 @@ pub fn waku_lightpush_publish_encrypt_symmetric(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
message_id.into()
decode_response(result_ptr)
}

View File

@ -7,6 +7,7 @@ use std::ffi::{CStr, CString};
// internal
use super::config::WakuNodeConfig;
use crate::general::{JsonResponse, PeerId, Result};
use crate::utils::decode_response;
/// Instantiates a Waku node
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
@ -98,18 +99,7 @@ pub fn waku_peer_id() -> Result<PeerId> {
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
pub fn waku_listen_addresses() -> Result<Vec<Multiaddr>> {
let response_ptr = unsafe { waku_sys::waku_listen_addresses() };
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let json_response: JsonResponse<Vec<Multiaddr>> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
json_response.into()
decode_response(response_ptr)
}
#[cfg(test)]

View File

@ -8,6 +8,7 @@ use multiaddr::Multiaddr;
use serde::Deserialize;
// internal
use crate::general::{JsonResponse, PeerId, ProtocolId, Result};
use crate::utils::decode_response;
/// Add a node multiaddress and protocol to the waku nodes peerstore.
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_add_peerchar-address-char-protocolid)
@ -205,18 +206,7 @@ pub type WakuPeers = Vec<WakuPeerData>;
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peers)
pub fn waku_peers() -> Result<WakuPeers> {
let response_ptr = unsafe { waku_sys::waku_peers() };
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("&str should build properly from the returning response");
let result: JsonResponse<WakuPeers> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
result.into()
decode_response(response_ptr)
}
#[cfg(test)]

View File

@ -10,6 +10,7 @@ use secp256k1::{PublicKey, SecretKey};
use crate::general::{
Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic,
};
use crate::utils::decode_response;
/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding)
@ -137,16 +138,7 @@ pub fn waku_relay_publish_message(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
message_id.into()
decode_response(result_ptr)
}
/// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay
@ -204,16 +196,7 @@ pub fn waku_relay_publish_encrypt_asymmetric(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
message_id.into()
decode_response(result_ptr)
}
/// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Relay
@ -271,16 +254,7 @@ pub fn waku_relay_publish_encrypt_symmetric(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
message_id.into()
decode_response(result_ptr)
}
pub fn waku_enough_peers(pubsub_topic: Option<WakuPubSubTopic>) -> Result<bool> {
@ -325,16 +299,7 @@ pub fn waku_relay_subscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<()>
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let enough_peers: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
Result::from(enough_peers).map(|_| ())
decode_response::<bool>(result_ptr).map(|_| ())
}
pub fn waku_relay_unsubscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<()> {
@ -352,14 +317,5 @@ pub fn waku_relay_unsubscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let enough_peers: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
Result::from(enough_peers).map(|_| ())
decode_response::<bool>(result_ptr).map(|_| ())
}

View File

@ -1,11 +1,12 @@
//! Waku [store](https://rfc.vac.dev/spec/36/#waku-store) handling methods
// std
use std::ffi::{CStr, CString};
use std::ffi::CString;
use std::time::Duration;
// crates
// internal
use crate::general::{JsonResponse, PeerId, Result, StoreQuery, StoreResponse};
use crate::general::{PeerId, Result, StoreQuery, StoreResponse};
use crate::utils::decode_response;
/// Retrieves historical messages on specific content topics. This method may be called with [`PagingOptions`](`crate::general::PagingOptions`),
/// to retrieve historical messages on a per-page basis. If the request included [`PagingOptions`](`crate::general::PagingOptions`),
@ -43,14 +44,5 @@ pub fn waku_store_query(
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<StoreResponse> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
response.into()
decode_response(result_ptr)
}