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,
timeout: Duration,
) -> Result<()> {
let filter_subscription = CString::new(
let filter_subscription_ptr = CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always succeed to serialize"),
)
.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");
.expect("FilterSubscription 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 filter_subscription_ptr = filter_subscription.into_raw();
let peer_id_ptr = peer_id.into_raw();
let result_ptr = waku_sys::waku_filter_subscribe(
filter_subscription_ptr,
peer_id_ptr,
@ -56,13 +57,13 @@ pub fn waku_filter_unsubscribe(
filter_subscription: &FilterSubscription,
timeout: Duration,
) -> Result<()> {
let filter_subscription = CString::new(
let filter_subscription_ptr = CString::new(
serde_json::to_string(filter_subscription)
.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 filter_subscription_ptr = filter_subscription.into_raw();
let res = waku_sys::waku_filter_unsubscribe(
filter_subscription_ptr,
timeout

View File

@ -21,20 +21,23 @@ pub fn waku_lightpush_publish(
let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic)
.to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_lightpush_publish(
CString::new(
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(),
CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw(),
CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw(),
let message_ptr = CString::new(
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();
let topic_ptr = CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw();
let peer_id_ptr = CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw();
let result_ptr = unsafe {
let res = waku_sys::waku_lightpush_publish(
message_ptr,
topic_ptr,
peer_id_ptr,
timeout
.map(|timeout| {
timeout
@ -43,14 +46,24 @@ pub fn waku_lightpush_publish(
.expect("Duration as milliseconds should fit in a i32")
})
.unwrap_or(0),
))
}
.to_str()
.expect("Response should always succeed to load to a &str");
);
drop(CString::from_raw(message_ptr));
drop(CString::from_raw(topic_ptr));
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> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
response.into()
}
@ -71,26 +84,32 @@ pub fn waku_lightpush_publish_encrypt_asymmetric(
let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic)
.to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_lightpush_publish_enc_asymmetric(
CString::new(
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(),
CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw(),
CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw(),
CString::new(pk)
.expect("CString should build properly from hex encoded public key")
.into_raw(),
CString::new(sk)
.expect("CString should build properly from hex encoded signing key")
.into_raw(),
let pubsub_topic_ptr = CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw();
let peer_id_ptr = CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw();
let pk_ptr = CString::new(pk)
.expect("CString should build properly from hex encoded public key")
.into_raw();
let sk_ptr = CString::new(sk)
.expect("CString should build properly from hex encoded signing key")
.into_raw();
let message_ptr = CString::new(
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();
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
.map(|timeout| {
timeout
@ -99,12 +118,24 @@ pub fn waku_lightpush_publish_encrypt_asymmetric(
.expect("Duration as milliseconds should fit in a i32")
})
.unwrap_or(0),
))
.to_str()
.expect("Response should always succeed to load to a &str")
);
drop(CString::from_raw(message_ptr));
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> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
message_id.into()
}
@ -125,26 +156,31 @@ pub fn waku_lightpush_publish_encrypt_symmetric(
let pubsub_topic = pubsub_topic
.unwrap_or_else(waku_dafault_pubsub_topic)
.to_string();
let result = unsafe {
CStr::from_ptr(waku_sys::waku_lightpush_publish_enc_symmetric(
CString::new(
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(),
CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw(),
CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw(),
CString::new(symk)
.expect("CString should build properly from hex encoded symmetric key")
.into_raw(),
CString::new(sk)
.expect("CString should build properly from hex encoded signing key")
.into_raw(),
let message_ptr = CString::new(
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();
let pubsub_topic_ptr = CString::new(pubsub_topic)
.expect("CString should build properly from pubsub topic")
.into_raw();
let peer_id_ptr = CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw();
let symk_ptr = CString::new(symk)
.expect("CString should build properly from hex encoded symmetric key")
.into_raw();
let sk_ptr = CString::new(sk)
.expect("CString should build properly from hex encoded signing key")
.into_raw();
let result_ptr = unsafe {
let res = waku_sys::waku_lightpush_publish_enc_symmetric(
message_ptr,
pubsub_topic_ptr,
peer_id_ptr,
symk_ptr,
sk_ptr,
timeout
.map(|timeout| {
timeout
@ -153,11 +189,25 @@ pub fn waku_lightpush_publish_encrypt_symmetric(
.expect("Duration as milliseconds should fit in a i32")
})
.unwrap_or(0),
))
.to_str()
.expect("Response should always succeed to load to a &str")
);
drop(CString::from_raw(message_ptr));
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> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
message_id.into()
}