Free strings on relay
This commit is contained in:
parent
fe7b2e75e6
commit
282673c3af
|
@ -19,54 +19,83 @@ pub fn waku_create_content_topic(
|
|||
content_topic_name: &str,
|
||||
encoding: Encoding,
|
||||
) -> WakuContentTopic {
|
||||
unsafe {
|
||||
CStr::from_ptr(waku_sys::waku_content_topic(
|
||||
CString::new(application_name)
|
||||
.expect("Application name should always transform to CString")
|
||||
.into_raw(),
|
||||
application_version
|
||||
.try_into()
|
||||
.expect("Version should fit within an u32"),
|
||||
CString::new(content_topic_name)
|
||||
.expect("Content topic should always transform to CString")
|
||||
.into_raw(),
|
||||
CString::new(encoding.to_string())
|
||||
.expect("Encoding should always transform to CString")
|
||||
.into_raw(),
|
||||
))
|
||||
}
|
||||
.to_str()
|
||||
.expect("&str from result should always be extracted")
|
||||
.parse()
|
||||
.expect("Content topic data should be always parseable")
|
||||
let application_name_ptr = CString::new(application_name)
|
||||
.expect("Application name should always transform to CString")
|
||||
.into_raw();
|
||||
let application_version = application_version
|
||||
.try_into()
|
||||
.expect("Version should fit within an u32");
|
||||
let content_topic_name_ptr = CString::new(content_topic_name)
|
||||
.expect("Content topic should always transform to CString")
|
||||
.into_raw();
|
||||
let encoding_ptr = CString::new(encoding.to_string())
|
||||
.expect("Encoding should always transform to CString")
|
||||
.into_raw();
|
||||
|
||||
let result_ptr = unsafe {
|
||||
let res = waku_sys::waku_content_topic(
|
||||
application_name_ptr,
|
||||
application_version,
|
||||
content_topic_name_ptr,
|
||||
encoding_ptr,
|
||||
);
|
||||
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/)
|
||||
/// 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 {
|
||||
unsafe {
|
||||
CStr::from_ptr(waku_sys::waku_pubsub_topic(
|
||||
CString::new(topic_name)
|
||||
.expect("Topic name should always transform to CString")
|
||||
.into_raw(),
|
||||
CString::new(encoding.to_string())
|
||||
.expect("Encoding should always transform to CString")
|
||||
.into_raw(),
|
||||
))
|
||||
}
|
||||
.to_str()
|
||||
.expect("&str from result should always be extracted")
|
||||
.parse()
|
||||
.expect("Pubsub topic data should be always parseable")
|
||||
let topic_name_ptr = CString::new(topic_name)
|
||||
.expect("Topic name should always transform to CString")
|
||||
.into_raw();
|
||||
let encoding_ptr = CString::new(encoding.to_string())
|
||||
.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));
|
||||
drop(CString::from_raw(encoding_ptr));
|
||||
res
|
||||
};
|
||||
|
||||
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/)
|
||||
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()
|
||||
.expect("&str from result should always be extracted")
|
||||
.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
|
||||
|
@ -79,17 +108,21 @@ pub fn waku_relay_publish_message(
|
|||
let pubsub_topic = pubsub_topic
|
||||
.unwrap_or_else(waku_dafault_pubsub_topic)
|
||||
.to_string();
|
||||
let result = unsafe {
|
||||
CStr::from_ptr(waku_sys::waku_relay_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(),
|
||||
|
||||
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 result_ptr = unsafe {
|
||||
let res = waku_sys::waku_relay_publish(
|
||||
message_ptr,
|
||||
pubsub_topic_ptr,
|
||||
timeout
|
||||
.map(|duration| {
|
||||
duration
|
||||
|
@ -98,12 +131,21 @@ pub fn waku_relay_publish_message(
|
|||
.expect("Duration as milliseconds should fit in a i32")
|
||||
})
|
||||
.unwrap_or(0),
|
||||
))
|
||||
}
|
||||
.to_str()
|
||||
.expect("&str from result should always be extracted");
|
||||
);
|
||||
drop(CString::from_raw(message_ptr));
|
||||
drop(CString::from_raw(pubsub_topic_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> =
|
||||
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
|
||||
|
||||
unsafe { waku_sys::waku_utils_free(result_ptr) };
|
||||
|
||||
message_id.into()
|
||||
}
|
||||
|
||||
|
@ -123,23 +165,29 @@ pub fn waku_relay_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_relay_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(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 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 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 result_ptr = unsafe {
|
||||
let res = waku_sys::waku_relay_publish_enc_asymmetric(
|
||||
message_ptr,
|
||||
pubsub_topic_ptr,
|
||||
pk_ptr,
|
||||
sk_ptr,
|
||||
timeout
|
||||
.map(|timeout| {
|
||||
timeout
|
||||
|
@ -148,12 +196,23 @@ pub fn waku_relay_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(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> =
|
||||
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
|
||||
|
||||
unsafe { waku_sys::waku_utils_free(result_ptr) };
|
||||
|
||||
message_id.into()
|
||||
}
|
||||
|
||||
|
@ -173,23 +232,29 @@ pub fn waku_relay_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_relay_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(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 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_relay_publish_enc_symmetric(
|
||||
message_ptr,
|
||||
pubsub_topic_ptr,
|
||||
symk_ptr,
|
||||
sk_ptr,
|
||||
timeout
|
||||
.map(|timeout| {
|
||||
timeout
|
||||
|
@ -198,12 +263,23 @@ pub fn waku_relay_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(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> =
|
||||
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
|
||||
|
||||
unsafe { waku_sys::waku_utils_free(result_ptr) };
|
||||
|
||||
message_id.into()
|
||||
}
|
||||
|
||||
|
@ -211,17 +287,26 @@ pub fn waku_enough_peers(pubsub_topic: Option<WakuPubSubTopic>) -> Result<bool>
|
|||
let pubsub_topic = pubsub_topic
|
||||
.unwrap_or_else(waku_dafault_pubsub_topic)
|
||||
.to_string();
|
||||
let result = unsafe {
|
||||
CStr::from_ptr(waku_sys::waku_relay_enough_peers(
|
||||
CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
.into_raw(),
|
||||
))
|
||||
}
|
||||
.to_str()
|
||||
.expect("Response should always succeed to load to a &str");
|
||||
|
||||
let pubsub_topic_ptr = CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
.into_raw();
|
||||
|
||||
let result_ptr = unsafe {
|
||||
let res = waku_sys::waku_relay_enough_peers(pubsub_topic_ptr);
|
||||
drop(CString::from_raw(pubsub_topic_ptr));
|
||||
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) };
|
||||
|
||||
enough_peers.into()
|
||||
}
|
||||
|
||||
|
@ -229,17 +314,26 @@ pub fn waku_relay_subscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<()>
|
|||
let pubsub_topic = pubsub_topic
|
||||
.unwrap_or_else(waku_dafault_pubsub_topic)
|
||||
.to_string();
|
||||
|
||||
let pubsub_topic_ptr = CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
.into_raw();
|
||||
|
||||
let result = unsafe {
|
||||
CStr::from_ptr(waku_sys::waku_relay_subscribe(
|
||||
CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
.into_raw(),
|
||||
))
|
||||
}
|
||||
.to_str()
|
||||
.expect("Response should always succeed to load to a &str");
|
||||
let res = waku_sys::waku_relay_subscribe(pubsub_topic_ptr);
|
||||
drop(CString::from_raw(pubsub_topic_ptr));
|
||||
res
|
||||
};
|
||||
|
||||
let result = unsafe { CStr::from_ptr(result) }
|
||||
.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(|_| ())
|
||||
}
|
||||
|
||||
|
@ -247,16 +341,25 @@ pub fn waku_relay_unsubscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<(
|
|||
let pubsub_topic = pubsub_topic
|
||||
.unwrap_or_else(waku_dafault_pubsub_topic)
|
||||
.to_string();
|
||||
|
||||
let pubsub_topic_ptr = CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
.into_raw();
|
||||
|
||||
let result = unsafe {
|
||||
CStr::from_ptr(waku_sys::waku_relay_unsubscribe(
|
||||
CString::new(pubsub_topic)
|
||||
.expect("CString should build properly from pubsub topic")
|
||||
.into_raw(),
|
||||
))
|
||||
}
|
||||
.to_str()
|
||||
.expect("Response should always succeed to load to a &str");
|
||||
let res = waku_sys::waku_relay_unsubscribe(pubsub_topic_ptr);
|
||||
drop(CString::from_raw(pubsub_topic_ptr));
|
||||
res
|
||||
};
|
||||
|
||||
let result = unsafe { CStr::from_ptr(result) }
|
||||
.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(|_| ())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue