-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- removes encoding functions from relay and lightpush - adds `encode_symmetric` and `encode_asymmetric` to `WakuMessage`
- Loading branch information
1 parent
a487f79
commit e64fb48
Showing
12 changed files
with
158 additions
and
371 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// std | ||
use std::ffi::CString; | ||
// crates | ||
use aes_gcm::{Aes256Gcm, Key}; | ||
use secp256k1::{PublicKey, SecretKey}; | ||
// internal | ||
use crate::general::{Result, WakuMessage}; | ||
use crate::utils::decode_and_free_response; | ||
|
||
/// Optionally sign and encrypt a message using asymmetric encryption | ||
pub fn waku_encode_asymmetric( | ||
message: &WakuMessage, | ||
public_key: &PublicKey, | ||
signing_key: Option<&SecretKey>, | ||
) -> Result<WakuMessage> { | ||
let pk = hex::encode(public_key.serialize_uncompressed()); | ||
let sk = signing_key | ||
.map(|signing_key| hex::encode(signing_key.secret_bytes())) | ||
.unwrap_or_else(String::new); | ||
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 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_encode_asymmetric(message_ptr, pk_ptr, sk_ptr); | ||
drop(CString::from_raw(message_ptr)); | ||
drop(CString::from_raw(pk_ptr)); | ||
drop(CString::from_raw(sk_ptr)); | ||
res | ||
}; | ||
|
||
decode_and_free_response(result_ptr) | ||
} | ||
|
||
/// Optionally sign and encrypt a message using symmetric encryption | ||
pub fn waku_encode_symmetric( | ||
message: &WakuMessage, | ||
symmetric_key: &Key<Aes256Gcm>, | ||
signing_key: Option<&SecretKey>, | ||
) -> Result<WakuMessage> { | ||
let symk = hex::encode(symmetric_key.as_slice()); | ||
let sk = signing_key | ||
.map(|signing_key| hex::encode(signing_key.secret_bytes())) | ||
.unwrap_or_else(String::new); | ||
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 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_encode_symmetric(message_ptr, symk_ptr, sk_ptr); | ||
drop(CString::from_raw(message_ptr)); | ||
drop(CString::from_raw(symk_ptr)); | ||
drop(CString::from_raw(sk_ptr)); | ||
res | ||
}; | ||
|
||
decode_and_free_response(result_ptr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.