Free strings on lightpush

This commit is contained in:
Daniel Sanchez Quiros 2023-02-13 19:26:48 +01:00
parent 98da6f99a1
commit 9375f7ea60
2 changed files with 123 additions and 72 deletions

View File

@ -16,16 +16,17 @@ pub fn waku_filter_subscribe(
peer_id: PeerId, peer_id: PeerId,
timeout: Duration, timeout: Duration,
) -> Result<()> { ) -> Result<()> {
let filter_subscription = CString::new( let filter_subscription_ptr = CString::new(
serde_json::to_string(filter_subscription) serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always succeed to serialize"), .expect("FilterSubscription should always succeed to serialize"),
) )
.expect("FilterSubscription should always be able to be serialized"); .expect("FilterSubscription should always be able to be serialized")
let peer_id = CString::new(peer_id).expect("PeerId should always be able to be serialized"); .into_raw();
let peer_id_ptr = CString::new(peer_id)
.expect("PeerId should always be able to be serialized")
.into_raw();
let result_ptr = unsafe { let result_ptr = unsafe {
let filter_subscription_ptr = filter_subscription.into_raw();
let peer_id_ptr = peer_id.into_raw();
let result_ptr = waku_sys::waku_filter_subscribe( let result_ptr = waku_sys::waku_filter_subscribe(
filter_subscription_ptr, filter_subscription_ptr,
peer_id_ptr, peer_id_ptr,
@ -56,13 +57,13 @@ pub fn waku_filter_unsubscribe(
filter_subscription: &FilterSubscription, filter_subscription: &FilterSubscription,
timeout: Duration, timeout: Duration,
) -> Result<()> { ) -> Result<()> {
let filter_subscription = CString::new( let filter_subscription_ptr = CString::new(
serde_json::to_string(filter_subscription) serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always succeed to serialize"), .expect("FilterSubscription should always succeed to serialize"),
) )
.expect("CString should build properly from the serialized filter subscription"); .expect("CString should build properly from the serialized filter subscription")
.into_raw();
let result_ptr = unsafe { let result_ptr = unsafe {
let filter_subscription_ptr = filter_subscription.into_raw();
let res = waku_sys::waku_filter_unsubscribe( let res = waku_sys::waku_filter_unsubscribe(
filter_subscription_ptr, filter_subscription_ptr,
timeout timeout

View File

@ -21,20 +21,23 @@ pub fn waku_lightpush_publish(
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 { let message_ptr = CString::new(
CStr::from_ptr(waku_sys::waku_lightpush_publish( serde_json::to_string(&message)
CString::new( .expect("WakuMessages should always be able to success serializing"),
serde_json::to_string(&message) )
.expect("WakuMessages should always be able to success serializing"), .expect("CString should build properly from the serialized waku message")
) .into_raw();
.expect("CString should build properly from the serialized waku message") let topic_ptr = CString::new(pubsub_topic)
.into_raw(), .expect("CString should build properly from pubsub topic")
CString::new(pubsub_topic) .into_raw();
.expect("CString should build properly from pubsub topic") let peer_id_ptr = CString::new(peer_id)
.into_raw(), .expect("CString should build properly from peer id")
CString::new(peer_id) .into_raw();
.expect("CString should build properly from peer id") let result_ptr = unsafe {
.into_raw(), let res = waku_sys::waku_lightpush_publish(
message_ptr,
topic_ptr,
peer_id_ptr,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -43,14 +46,24 @@ pub fn waku_lightpush_publish(
.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(topic_ptr));
.expect("Response should always succeed to load to a &str"); drop(CString::from_raw(peer_id_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<MessageId> = let response: 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);
}
response.into() response.into()
} }
@ -71,26 +84,32 @@ pub fn waku_lightpush_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_lightpush_publish_enc_asymmetric( let pubsub_topic_ptr = CString::new(pubsub_topic)
CString::new( .expect("CString should build properly from pubsub topic")
serde_json::to_string(&message) .into_raw();
.expect("WakuMessages should always be able to success serializing"), let peer_id_ptr = CString::new(peer_id)
) .expect("CString should build properly from peer id")
.expect("CString should build properly from the serialized waku message") .into_raw();
.into_raw(), let pk_ptr = CString::new(pk)
CString::new(pubsub_topic) .expect("CString should build properly from hex encoded public key")
.expect("CString should build properly from pubsub topic") .into_raw();
.into_raw(), let sk_ptr = CString::new(sk)
CString::new(peer_id) .expect("CString should build properly from hex encoded signing key")
.expect("CString should build properly from peer id") .into_raw();
.into_raw(), let message_ptr = CString::new(
CString::new(pk) serde_json::to_string(&message)
.expect("CString should build properly from hex encoded public key") .expect("WakuMessages should always be able to success serializing"),
.into_raw(), )
CString::new(sk) .expect("CString should build properly from the serialized waku message")
.expect("CString should build properly from hex encoded signing key") .into_raw();
.into_raw(), let result_ptr = unsafe {
let res = waku_sys::waku_lightpush_publish_enc_asymmetric(
message_ptr,
pubsub_topic_ptr,
peer_id_ptr,
pk_ptr,
sk_ptr,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -99,12 +118,24 @@ pub fn waku_lightpush_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(peer_id_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("Response should always succeed to load to a &str");
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()
} }
@ -125,26 +156,31 @@ pub fn waku_lightpush_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 { let message_ptr = CString::new(
CStr::from_ptr(waku_sys::waku_lightpush_publish_enc_symmetric( serde_json::to_string(&message)
CString::new( .expect("WakuMessages should always be able to success serializing"),
serde_json::to_string(&message) )
.expect("WakuMessages should always be able to success serializing"), .expect("CString should build properly from the serialized waku message")
) .into_raw();
.expect("CString should build properly from the serialized waku message") let pubsub_topic_ptr = CString::new(pubsub_topic)
.into_raw(), .expect("CString should build properly from pubsub topic")
CString::new(pubsub_topic) .into_raw();
.expect("CString should build properly from pubsub topic") let peer_id_ptr = CString::new(peer_id)
.into_raw(), .expect("CString should build properly from peer id")
CString::new(peer_id) .into_raw();
.expect("CString should build properly from peer id") let symk_ptr = CString::new(symk)
.into_raw(), .expect("CString should build properly from hex encoded symmetric key")
CString::new(symk) .into_raw();
.expect("CString should build properly from hex encoded symmetric key") let sk_ptr = CString::new(sk)
.into_raw(), .expect("CString should build properly from hex encoded signing key")
CString::new(sk) .into_raw();
.expect("CString should build properly from hex encoded signing key") let result_ptr = unsafe {
.into_raw(), let res = waku_sys::waku_lightpush_publish_enc_symmetric(
message_ptr,
pubsub_topic_ptr,
peer_id_ptr,
symk_ptr,
sk_ptr,
timeout timeout
.map(|timeout| { .map(|timeout| {
timeout timeout
@ -153,11 +189,25 @@ pub fn waku_lightpush_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(peer_id_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("Response should always succeed to load to a &str");
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()
} }