Free strings on relay

This commit is contained in:
Daniel Sanchez Quiros 2023-02-14 13:30:21 +01:00
parent fe7b2e75e6
commit 282673c3af

View File

@ -19,54 +19,83 @@ pub fn waku_create_content_topic(
content_topic_name: &str, content_topic_name: &str,
encoding: Encoding, encoding: Encoding,
) -> WakuContentTopic { ) -> WakuContentTopic {
unsafe { let application_name_ptr = CString::new(application_name)
CStr::from_ptr(waku_sys::waku_content_topic( .expect("Application name should always transform to CString")
CString::new(application_name) .into_raw();
.expect("Application name should always transform to CString") let application_version = application_version
.into_raw(), .try_into()
application_version .expect("Version should fit within an u32");
.try_into() let content_topic_name_ptr = CString::new(content_topic_name)
.expect("Version should fit within an u32"), .expect("Content topic should always transform to CString")
CString::new(content_topic_name) .into_raw();
.expect("Content topic should always transform to CString") let encoding_ptr = CString::new(encoding.to_string())
.into_raw(), .expect("Encoding should always transform to CString")
CString::new(encoding.to_string()) .into_raw();
.expect("Encoding should always transform to CString")
.into_raw(), let result_ptr = unsafe {
)) let res = waku_sys::waku_content_topic(
} application_name_ptr,
.to_str() application_version,
.expect("&str from result should always be extracted") content_topic_name_ptr,
.parse() encoding_ptr,
.expect("Content topic data should be always parseable") );
drop(CString::from_raw(application_name_ptr));
drop(CString::from_raw(content_topic_name_ptr));
drop(CString::from_raw(encoding_ptr));
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted")
.parse()
.expect("Content topic data should be always parseable");
unsafe { waku_sys::waku_utils_free(result_ptr) };
result
} }
/// Create a pubsub topic according to [RFC 23](https://rfc.vac.dev/spec/23/) /// Create a pubsub topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding)
pub fn waku_create_pubsub_topic(topic_name: &str, encoding: Encoding) -> WakuPubSubTopic { pub fn waku_create_pubsub_topic(topic_name: &str, encoding: Encoding) -> WakuPubSubTopic {
unsafe { let topic_name_ptr = CString::new(topic_name)
CStr::from_ptr(waku_sys::waku_pubsub_topic( .expect("Topic name should always transform to CString")
CString::new(topic_name) .into_raw();
.expect("Topic name should always transform to CString") let encoding_ptr = CString::new(encoding.to_string())
.into_raw(), .expect("Encoding should always transform to CString")
CString::new(encoding.to_string()) .into_raw();
.expect("Encoding should always transform to CString")
.into_raw(), let result_ptr = unsafe {
)) let res = waku_sys::waku_pubsub_topic(topic_name_ptr, encoding_ptr);
} drop(CString::from_raw(topic_name_ptr));
.to_str() drop(CString::from_raw(encoding_ptr));
.expect("&str from result should always be extracted") res
.parse() };
.expect("Pubsub topic data should be always parseable")
let result = unsafe { CString::from_raw(result_ptr) }
.to_str()
.expect("&str from result should always be extracted")
.parse()
.expect("Pubsub topic data should be always parseable");
unsafe { waku_sys::waku_utils_free(result_ptr) };
result
} }
/// Default pubsub topic used for exchanging waku messages defined in [RFC 10](https://rfc.vac.dev/spec/10/) /// Default pubsub topic used for exchanging waku messages defined in [RFC 10](https://rfc.vac.dev/spec/10/)
pub fn waku_dafault_pubsub_topic() -> WakuPubSubTopic { pub fn waku_dafault_pubsub_topic() -> WakuPubSubTopic {
unsafe { CStr::from_ptr(waku_sys::waku_default_pubsub_topic()) } let result_ptr = unsafe { waku_sys::waku_default_pubsub_topic() };
let result = unsafe { CString::from_raw(result_ptr) }
.to_str() .to_str()
.expect("&str from result should always be extracted") .expect("&str from result should always be extracted")
.parse() .parse()
.expect("Default pubsub topic should always be parseable") .expect("Default pubsub topic should always be parseable");
unsafe { waku_sys::waku_utils_free(result_ptr) };
result
} }
/// Publish a message using Waku Relay /// Publish a message using Waku Relay
@ -79,17 +108,21 @@ pub fn waku_relay_publish_message(
let pubsub_topic = pubsub_topic let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic) .unwrap_or_else(waku_dafault_pubsub_topic)
.to_string(); .to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_relay_publish( let message_ptr = CString::new(
CString::new( serde_json::to_string(&message)
serde_json::to_string(&message) .expect("WakuMessages should always be able to success serializing"),
.expect("WakuMessages should always be able to success serializing"), )
) .expect("CString should build properly from the serialized waku message")
.expect("CString should build properly from the serialized waku message") .into_raw();
.into_raw(), let pubsub_topic_ptr = CString::new(pubsub_topic)
CString::new(pubsub_topic) .expect("CString should build properly from pubsub topic")
.expect("CString should build properly from pubsub topic") .into_raw();
.into_raw(),
let result_ptr = unsafe {
let res = waku_sys::waku_relay_publish(
message_ptr,
pubsub_topic_ptr,
timeout timeout
.map(|duration| { .map(|duration| {
duration duration
@ -98,12 +131,21 @@ pub fn waku_relay_publish_message(
.expect("Duration as milliseconds should fit in a i32") .expect("Duration as milliseconds should fit in a i32")
}) })
.unwrap_or(0), .unwrap_or(0),
)) );
} drop(CString::from_raw(message_ptr));
.to_str() drop(CString::from_raw(pubsub_topic_ptr));
.expect("&str from result should always be extracted"); res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let message_id: JsonResponse<MessageId> = let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
message_id.into() message_id.into()
} }
@ -123,23 +165,29 @@ pub fn waku_relay_publish_encrypt_asymmetric(
let pubsub_topic = pubsub_topic let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic) .unwrap_or_else(waku_dafault_pubsub_topic)
.to_string(); .to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_relay_publish_enc_asymmetric( let message_ptr = CString::new(
CString::new( serde_json::to_string(&message)
serde_json::to_string(&message) .expect("WakuMessages should always be able to success serializing"),
.expect("WakuMessages should always be able to success serializing"), )
) .expect("CString should build properly from the serialized waku message")
.expect("CString should build properly from the serialized waku message") .into_raw();
.into_raw(), let pubsub_topic_ptr = CString::new(pubsub_topic)
CString::new(pubsub_topic) .expect("CString should build properly from pubsub topic")
.expect("CString should build properly from pubsub topic") .into_raw();
.into_raw(), let pk_ptr = CString::new(pk)
CString::new(pk) .expect("CString should build properly from hex encoded public key")
.expect("CString should build properly from hex encoded public key") .into_raw();
.into_raw(), let sk_ptr = CString::new(sk)
CString::new(sk) .expect("CString should build properly from hex encoded signing key")
.expect("CString should build properly from hex encoded signing key") .into_raw();
.into_raw(),
let result_ptr = unsafe {
let res = waku_sys::waku_relay_publish_enc_asymmetric(
message_ptr,
pubsub_topic_ptr,
pk_ptr,
sk_ptr,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -148,12 +196,23 @@ pub fn waku_relay_publish_encrypt_asymmetric(
.expect("Duration as milliseconds should fit in a i32") .expect("Duration as milliseconds should fit in a i32")
}) })
.unwrap_or(0), .unwrap_or(0),
)) );
.to_str() drop(CString::from_raw(message_ptr));
.expect("Response should always succeed to load to a &str") drop(CString::from_raw(pubsub_topic_ptr));
drop(CString::from_raw(pk_ptr));
drop(CString::from_raw(sk_ptr));
res
}; };
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let message_id: JsonResponse<MessageId> = let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
message_id.into() message_id.into()
} }
@ -173,23 +232,29 @@ pub fn waku_relay_publish_encrypt_symmetric(
let pubsub_topic = pubsub_topic let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic) .unwrap_or_else(waku_dafault_pubsub_topic)
.to_string(); .to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_relay_publish_enc_symmetric( let message_ptr = CString::new(
CString::new( serde_json::to_string(&message)
serde_json::to_string(&message) .expect("WakuMessages should always be able to success serializing"),
.expect("WakuMessages should always be able to success serializing"), )
) .expect("CString should build properly from the serialized waku message")
.expect("CString should build properly from the serialized waku message") .into_raw();
.into_raw(), let pubsub_topic_ptr = CString::new(pubsub_topic)
CString::new(pubsub_topic) .expect("CString should build properly from pubsub topic")
.expect("CString should build properly from pubsub topic") .into_raw();
.into_raw(), let symk_ptr = CString::new(symk)
CString::new(symk) .expect("CString should build properly from hex encoded symmetric key")
.expect("CString should build properly from hex encoded symmetric key") .into_raw();
.into_raw(), let sk_ptr = CString::new(sk)
CString::new(sk) .expect("CString should build properly from hex encoded signing key")
.expect("CString should build properly from hex encoded signing key") .into_raw();
.into_raw(),
let result_ptr = unsafe {
let res = waku_sys::waku_relay_publish_enc_symmetric(
message_ptr,
pubsub_topic_ptr,
symk_ptr,
sk_ptr,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -198,12 +263,23 @@ pub fn waku_relay_publish_encrypt_symmetric(
.expect("Duration as milliseconds should fit in a i32") .expect("Duration as milliseconds should fit in a i32")
}) })
.unwrap_or(0), .unwrap_or(0),
)) );
.to_str() drop(CString::from_raw(message_ptr));
.expect("Response should always succeed to load to a &str") drop(CString::from_raw(pubsub_topic_ptr));
drop(CString::from_raw(symk_ptr));
drop(CString::from_raw(sk_ptr));
res
}; };
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let message_id: JsonResponse<MessageId> = let message_id: JsonResponse<MessageId> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
message_id.into() message_id.into()
} }
@ -211,17 +287,26 @@ pub fn waku_enough_peers(pubsub_topic: Option<WakuPubSubTopic>) -> Result<bool>
let pubsub_topic = pubsub_topic let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic) .unwrap_or_else(waku_dafault_pubsub_topic)
.to_string(); .to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_relay_enough_peers( let pubsub_topic_ptr = CString::new(pubsub_topic)
CString::new(pubsub_topic) .expect("CString should build properly from pubsub topic")
.expect("CString should build properly from pubsub topic") .into_raw();
.into_raw(),
)) let result_ptr = unsafe {
} let res = waku_sys::waku_relay_enough_peers(pubsub_topic_ptr);
.to_str() drop(CString::from_raw(pubsub_topic_ptr));
.expect("Response should always succeed to load to a &str"); res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let enough_peers: JsonResponse<bool> = let enough_peers: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
enough_peers.into() enough_peers.into()
} }
@ -229,17 +314,26 @@ pub fn waku_relay_subscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<()>
let pubsub_topic = pubsub_topic let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic) .unwrap_or_else(waku_dafault_pubsub_topic)
.to_string(); .to_string();
let pubsub_topic_ptr = CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw();
let result = unsafe { let result = unsafe {
CStr::from_ptr(waku_sys::waku_relay_subscribe( let res = waku_sys::waku_relay_subscribe(pubsub_topic_ptr);
CString::new(pubsub_topic) drop(CString::from_raw(pubsub_topic_ptr));
.expect("CString should build properly from pubsub topic") res
.into_raw(), };
))
} let result = unsafe { CStr::from_ptr(result) }
.to_str() .to_str()
.expect("Response should always succeed to load to a &str"); .expect("&str from result should always be extracted");
let enough_peers: JsonResponse<bool> = let enough_peers: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); 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(|_| ()) Result::from(enough_peers).map(|_| ())
} }
@ -247,16 +341,25 @@ pub fn waku_relay_unsubscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<(
let pubsub_topic = pubsub_topic let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic) .unwrap_or_else(waku_dafault_pubsub_topic)
.to_string(); .to_string();
let pubsub_topic_ptr = CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw();
let result = unsafe { let result = unsafe {
CStr::from_ptr(waku_sys::waku_relay_unsubscribe( let res = waku_sys::waku_relay_unsubscribe(pubsub_topic_ptr);
CString::new(pubsub_topic) drop(CString::from_raw(pubsub_topic_ptr));
.expect("CString should build properly from pubsub topic") res
.into_raw(), };
))
} let result = unsafe { CStr::from_ptr(result) }
.to_str() .to_str()
.expect("Response should always succeed to load to a &str"); .expect("&str from result should always be extracted");
let enough_peers: JsonResponse<bool> = let enough_peers: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); 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(|_| ()) Result::from(enough_peers).map(|_| ())
} }