diff --git a/Cargo.lock b/Cargo.lock index 11a210e..5846143 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -80,10 +80,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cbbc9d0964165b47557570cce6c952866c2678457aca742aafc9fb771d30270" [[package]] -name = "base64" -version = "0.13.0" +name = "base16ct" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" + +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] +name = "base64" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a" + +[[package]] +name = "base64ct" +version = "1.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" [[package]] name = "bindgen" @@ -113,6 +131,21 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bs58" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" + [[package]] name = "bumpalo" version = "3.11.1" @@ -204,6 +237,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "const-oid" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cec318a675afcb6a1ea1d4340e2d377e56e47c266f28043ceccbf4412ddfdd3b" + [[package]] name = "const_format" version = "0.2.26" @@ -273,6 +312,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "crypto-bigint" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef2b4b23cddf68b89b8f8069890e8c270d54e2d5fe1b143820234805e4cb17ef" +dependencies = [ + "generic-array", + "rand_core", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.6" @@ -376,12 +427,95 @@ dependencies = [ "syn", ] +[[package]] +name = "der" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1a467a65c5e759bce6e65eaf91cc29f466cdc57cb65777bd646872a8a1fd4de" +dependencies = [ + "const-oid", + "zeroize", +] + +[[package]] +name = "digest" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8168378f4e5023e7218c89c891c0fd8ecdb5e5e4f18cb78f38cf245dd021e76f" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "ecdsa" +version = "0.14.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413301934810f597c1d19ca71c8710e99a3f1ba28a0d2ebc01551a2daeea3c5c" +dependencies = [ + "der", + "elliptic-curve", + "rfc6979", + "signature", +] + [[package]] name = "either" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "elliptic-curve" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7bb888ab5300a19b8e5bceef25ac745ad065f3c9f7efc6de1b91958110891d3" +dependencies = [ + "base16ct", + "crypto-bigint", + "der", + "digest", + "ff", + "generic-array", + "group", + "pkcs8", + "rand_core", + "sec1", + "subtle", + "zeroize", +] + +[[package]] +name = "enr" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "492a7e5fc2504d5fdce8e124d3e263b244a68b283cac67a69eda0cd43e0aebad" +dependencies = [ + "base64 0.13.1", + "bs58", + "bytes", + "hex", + "k256", + "log", + "rand", + "rlp", + "secp256k1 0.24.3", + "serde", + "sha3", + "zeroize", +] + +[[package]] +name = "ff" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d013fc25338cc558c5c2cfbad646908fb23591e2404481826742b651c9af7160" +dependencies = [ + "rand_core", + "subtle", +] + [[package]] name = "form_urlencoded" version = "1.1.0" @@ -517,6 +651,17 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "group" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfbfb3a6cfbd390d5c9564ab283a0349b9b9fcd46a706c1eb10e0db70bfbac7" +dependencies = [ + "ff", + "rand_core", + "subtle", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -529,6 +674,15 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "iana-time-zone" version = "0.1.51" @@ -596,6 +750,27 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "k256" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72c1e0b51e7ec0a97369623508396067a486bd0cbed95a2659a4b863d28cfc8b" +dependencies = [ + "cfg-if", + "ecdsa", + "elliptic-curve", + "sha2", +] + +[[package]] +name = "keccak" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3afef3b6eff9ce9d8ff9b3601125eec7f0c8cbac7abd14f355d053fa56c98768" +dependencies = [ + "cpufeatures", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -678,9 +853,9 @@ dependencies = [ [[package]] name = "multiaddr" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4aebdb21e90f81d13ed01dc84123320838e53963c2ca94b60b305d3fa64f31e" +checksum = "3b53e0cc5907a5c216ba6584bf74be8ab47d6d6289f72793b2dddbf15dc3bf8c" dependencies = [ "arrayref", "byteorder", @@ -707,9 +882,9 @@ dependencies = [ [[package]] name = "multihash" -version = "0.16.3" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c346cf9999c631f002d8f977c4eaeaa0e6386f16007202308d0b3757522c2cc" +checksum = "835d6ff01d610179fbce3de1694d007e500bf33a7f29689838941d6bf783ae40" dependencies = [ "core2", "multihash-derive", @@ -818,6 +993,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkcs8" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eca2c590a5f85da82668fa685c09ce2888b9430e83299debf1f34b65fd4a4ba" +dependencies = [ + "der", + "spki", +] + [[package]] name = "polyval" version = "0.6.0" @@ -968,12 +1153,39 @@ version = "0.6.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +[[package]] +name = "rfc6979" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7743f17af12fa0b03b803ba12cd6a8d9483a587e89c69445e3909655c0b9fabb" +dependencies = [ + "crypto-bigint", + "hmac", + "zeroize", +] + +[[package]] +name = "rlp" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb919243f34364b6bd2fc10ef797edbfa75f33c252e7998527479c6d6b47e1ec" +dependencies = [ + "bytes", + "rustc-hex", +] + [[package]] name = "rustc-hash" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-hex" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" + [[package]] name = "ryu" version = "1.0.11" @@ -993,13 +1205,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" [[package]] -name = "secp256k1" -version = "0.24.1" +name = "sec1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff55dc09d460954e9ef2fa8a7ced735a964be9981fd50e870b2b3b0705e14964" +checksum = "3be24c1842290c45df0a7bf069e0c268a747ad05a192f2fd7dcfdbc1cba40928" +dependencies = [ + "base16ct", + "der", + "generic-array", + "pkcs8", + "subtle", + "zeroize", +] + +[[package]] +name = "secp256k1" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1629c9c557ef9b293568b338dddfc8208c98a18c59d722a9d53f859d9c9b62" +dependencies = [ + "secp256k1-sys 0.6.1", +] + +[[package]] +name = "secp256k1" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4124a35fe33ae14259c490fd70fa199a32b9ce9502f2ee6bc4f81ec06fa65894" dependencies = [ "rand", - "secp256k1-sys", + "secp256k1-sys 0.8.0", "serde", ] @@ -1012,6 +1247,15 @@ dependencies = [ "cc", ] +[[package]] +name = "secp256k1-sys" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "642a62736682fdd8c71da0eb273e453c8ac74e33b9fb310e22ba5b03ec7651ff" +dependencies = [ + "cc", +] + [[package]] name = "serde" version = "1.0.145" @@ -1045,9 +1289,9 @@ dependencies = [ [[package]] name = "serial_test" -version = "0.10.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c789ec87f4687d022a2405cf46e0cd6284889f1839de292cadeb6c6019506f2" +checksum = "538c30747ae860d6fb88330addbbd3e0ddbe46d662d032855596d8a8ca260611" dependencies = [ "dashmap", "futures", @@ -1059,15 +1303,36 @@ dependencies = [ [[package]] name = "serial_test_derive" -version = "0.10.0" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b64f9e531ce97c88b4778aad0ceee079216071cffec6ac9b904277f8f92e7fe3" +checksum = "079a83df15f85d89a68d64ae1238f142f172b1fa915d0d76b26a7cba1b659a69" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "sha2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha3" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdf0c33fae925bdc080598b84bc15c55e7b9a4a43b3c704da051f977469691c9" +dependencies = [ + "digest", + "keccak", +] + [[package]] name = "shlex" version = "1.1.0" @@ -1104,6 +1369,16 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "1.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" +dependencies = [ + "digest", + "rand_core", +] + [[package]] name = "slab" version = "0.4.7" @@ -1131,10 +1406,20 @@ dependencies = [ ] [[package]] -name = "sscanf" -version = "0.3.1" +name = "spki" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ecdd7ea17bcadebf81d656db919f58f96c1d194d748cf0839a44a220123eedd" +checksum = "67cf02bbac7a337dc36e4f5a693db6c21e7863f45070f7064577eb4367a3212b" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "sscanf" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "887a5b09bbf30cc01d059ccb4b7a0b508a0cef3028df2f2ee0d745bc9e624c56" dependencies = [ "const_format", "lazy_static", @@ -1144,14 +1429,16 @@ dependencies = [ [[package]] name = "sscanf_macro" -version = "0.3.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe2309d255caf220c1ff9f380d89420a1377de1cabc1d57e0b308e53b0406bed" +checksum = "b124cd4c68600cc3188a26987b1c3bed8cadcfd1be93124026096c668f2c0ee8" dependencies = [ "proc-macro2", "quote", "regex-syntax", + "strsim", "syn", + "unicode-width", ] [[package]] @@ -1160,6 +1447,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "subtle" version = "2.4.1" @@ -1381,16 +1674,17 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "waku-bindings" -version = "0.1.0-beta4" +version = "0.1.0-beta.5" dependencies = [ "aes-gcm", - "base64", + "base64 0.21.0", + "enr", "futures", "hex", "multiaddr", "once_cell", "rand", - "secp256k1", + "secp256k1 0.26.0", "serde", "serde_json", "serial_test", @@ -1403,7 +1697,7 @@ dependencies = [ [[package]] name = "waku-sys" -version = "0.1.0-beta4" +version = "0.1.0-beta.5" dependencies = [ "bindgen", ] @@ -1615,3 +1909,9 @@ name = "windows_x86_64_msvc" version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" + +[[package]] +name = "zeroize" +version = "1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c394b5bd0c6f669e7275d9c20aa90ae064cb22e75a1cad54e1b34088034b149f" diff --git a/waku-bindings/Cargo.toml b/waku-bindings/Cargo.toml index f8f28df..e43d71f 100644 --- a/waku-bindings/Cargo.toml +++ b/waku-bindings/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "waku-bindings" -version = "0.1.0-beta4" +version = "0.1.0-beta.5" edition = "2021" authors = [ "Daniel Sanchez Quiros " @@ -14,20 +14,21 @@ categories = ["network-programming"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] aes-gcm = { version = "0.10", features = ["aes"] } -base64 = "0.13" +base64 = "0.21" +enr = { version = "0.7", features = ["serde", "rust-secp256k1"] } hex = "0.4" -multiaddr = "0.16" +multiaddr = "0.17" once_cell = "1.15" rand = "0.8" -secp256k1 = { version = "0.24", features = ["rand", "recovery", "serde"] } +secp256k1 = { version = "0.26", features = ["rand", "recovery", "serde"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -sscanf = "0.3" +sscanf = "0.4" smart-default = "0.6" url = "2.3" -waku-sys = { version = "0.1.0-beta4", path = "../waku-sys" } +waku-sys = { version = "0.1.0-beta.5", path = "../waku-sys" } [dev-dependencies] futures = "0.3.25" -serial_test = "0.10.0" +serial_test = "1.0.0" tokio = { version = "1.24.2", features = ["macros", "rt", "sync", "time"] } diff --git a/waku-bindings/src/general/mod.rs b/waku-bindings/src/general/mod.rs index 6148e2a..b3a5c16 100644 --- a/waku-bindings/src/general/mod.rs +++ b/waku-bindings/src/general/mod.rs @@ -1,11 +1,12 @@ //! Waku [general](https://rfc.vac.dev/spec/36/#general) types -use std::borrow::Cow; // std +use std::borrow::Cow; use std::fmt::{Display, Formatter}; use std::str::FromStr; // crates use aes_gcm::{Aes256Gcm, Key}; +use base64::Engine; use secp256k1::{ecdsa::Signature, PublicKey, SecretKey}; use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; use sscanf::{scanf, RegexRepresentation}; @@ -312,14 +313,17 @@ impl Display for Encoding { } impl FromStr for Encoding { - type Err = String; + type Err = std::io::Error; fn from_str(s: &str) -> std::result::Result { match s.to_lowercase().as_str() { "proto" => Ok(Self::Proto), "rlp" => Ok(Self::Rlp), "rfc26" => Ok(Self::Rfc26), - encoding => Err(format!("Unrecognized encoding: {encoding}")), + encoding => Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + format!("Unrecognized encoding: {encoding}"), + )), } } } @@ -477,6 +481,7 @@ impl<'de> Deserialize<'de> for WakuPubSubTopic { } mod base64_serde { + use base64::Engine; use serde::de::Error; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -484,7 +489,9 @@ mod base64_serde { where S: Serializer, { - base64::encode(value).serialize(serializer) + base64::engine::general_purpose::STANDARD + .encode(value) + .serialize(serializer) } pub fn deserialize<'de, D>(deserializer: D) -> std::result::Result, D::Error> @@ -492,7 +499,9 @@ mod base64_serde { D: Deserializer<'de>, { let base64_str: String = String::deserialize(deserializer)?; - base64::decode(base64_str).map_err(D::Error::custom) + base64::engine::general_purpose::STANDARD + .decode(base64_str) + .map_err(D::Error::custom) } } @@ -505,7 +514,9 @@ where let base64_str: Option = Option::::deserialize(deserializer)?; base64_str .map(|base64_str| { - let raw_bytes = base64::decode(base64_str).map_err(D::Error::custom)?; + let raw_bytes = base64::engine::general_purpose::STANDARD + .decode(base64_str) + .map_err(D::Error::custom)?; PublicKey::from_slice(&raw_bytes).map_err(D::Error::custom) }) .transpose() diff --git a/waku-bindings/src/lib.rs b/waku-bindings/src/lib.rs index c51ae0f..dc5f1f1 100644 --- a/waku-bindings/src/lib.rs +++ b/waku-bindings/src/lib.rs @@ -8,9 +8,10 @@ mod node; mod utils; pub use node::{ - waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic, waku_new, - Aes256Gcm, Initialized, Key, Multiaddr, Protocol, PublicKey, Running, SecretKey, WakuLogLevel, - WakuNodeConfig, WakuNodeHandle, WakuPeerData, WakuPeers, + waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic, + waku_dns_discovery, waku_new, Aes256Gcm, DnsInfo, Initialized, Key, Multiaddr, Protocol, + PublicKey, Running, SecretKey, WakuLogLevel, WakuNodeConfig, WakuNodeHandle, WakuPeerData, + WakuPeers, }; pub use general::{ diff --git a/waku-bindings/src/node/discovery.rs b/waku-bindings/src/node/discovery.rs index d9164ab..9a1827b 100644 --- a/waku-bindings/src/node/discovery.rs +++ b/waku-bindings/src/node/discovery.rs @@ -2,11 +2,23 @@ use std::ffi::CString; use std::time::Duration; // crates +use enr::Enr; use multiaddr::Multiaddr; +use serde::Deserialize; use url::{Host, Url}; // internal use crate::utils::decode_and_free_response; -use crate::Result; +use crate::{PeerId, Result}; + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct DnsInfo { + #[serde(alias = "peerID")] + pub peer_id: PeerId, + #[serde(default, alias = "multiaddrs")] + pub addresses: Vec, + pub enr: Option>, +} /// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree. /// The nameserver can optionally be specified to resolve the enrtree url. Otherwise uses the default system dns. @@ -14,7 +26,7 @@ pub fn waku_dns_discovery( url: &Url, server: Option<&Host>, timeout: Option, -) -> Result> { +) -> Result> { let url = CString::new(url.to_string()) .expect("CString should build properly from a valid Url") .into_raw(); @@ -46,3 +58,18 @@ pub fn waku_dns_discovery( decode_and_free_response(result_ptr) } + +#[cfg(test)] +mod test { + use url::Url; + + #[test] + fn test_dns_discovery() { + let enrtree: Url = + "enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@test.waku.nodes.status.im".parse().unwrap(); + let result = super::waku_dns_discovery(&enrtree, None, None); + assert!(result.is_ok()); + assert!(!result.as_ref().unwrap().is_empty()); + println!("{result:?}"); + } +} diff --git a/waku-bindings/src/node/management.rs b/waku-bindings/src/node/management.rs index 7459d5a..72f4242 100644 --- a/waku-bindings/src/node/management.rs +++ b/waku-bindings/src/node/management.rs @@ -2,11 +2,11 @@ // std use multiaddr::Multiaddr; -use std::ffi::{CStr, CString}; +use std::ffi::CString; // crates // internal use super::config::WakuNodeConfig; -use crate::general::{JsonResponse, PeerId, Result}; +use crate::general::{PeerId, Result}; use crate::utils::decode_and_free_response; /// Instantiates a Waku node @@ -27,72 +27,28 @@ pub fn waku_new(config: Option) -> Result { res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let json_response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - - json_response.into() + decode_and_free_response(result_ptr) } /// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation. /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start) pub fn waku_start() -> Result { let response_ptr = unsafe { waku_sys::waku_start() }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let json_response: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - json_response.into() + decode_and_free_response(response_ptr) } /// Stops a Waku node /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop) pub fn waku_stop() -> Result { let response_ptr = unsafe { waku_sys::waku_stop() }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let json_response: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - json_response.into() + decode_and_free_response(response_ptr) } /// If the execution is successful, the result is the peer ID as a string (base58 encoded) /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop) pub fn waku_peer_id() -> Result { let response_ptr = unsafe { waku_sys::waku_peerid() }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let json_response: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - json_response.into() + decode_and_free_response(response_ptr) } /// Get the multiaddresses the Waku node is listening to diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index d601e53..0ba72f1 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -17,7 +17,6 @@ use std::marker::PhantomData; use std::sync::Mutex; use std::time::Duration; // crates -use url::{Host, Url}; // internal use crate::general::{ @@ -26,9 +25,10 @@ use crate::general::{ }; pub use config::{WakuLogLevel, WakuNodeConfig}; +pub use discovery::{waku_dns_discovery, DnsInfo}; pub use peers::{Protocol, WakuPeerData, WakuPeers}; pub use relay::{waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic}; -pub use store::waku_store_query; +pub use store::{waku_local_store_query, waku_store_query}; /// Shared flag to check if a waku node is already running in the current process static WAKU_NODE_INITIALIZED: Mutex = Mutex::new(false); @@ -307,15 +307,6 @@ impl WakuNodeHandle { ) -> Result<()> { filter::waku_filter_unsubscribe(filter_subscription, timeout) } - - pub fn dns_discovery( - &self, - url: &Url, - nameserver: Option<&Host>, - timeout: Option, - ) -> Result> { - discovery::waku_dns_discovery(url, nameserver, timeout) - } } /// Spawn a new Waku node with the given configuration (default configuration if `None` provided) diff --git a/waku-bindings/src/node/peers.rs b/waku-bindings/src/node/peers.rs index 29c3823..ca463b7 100644 --- a/waku-bindings/src/node/peers.rs +++ b/waku-bindings/src/node/peers.rs @@ -1,13 +1,13 @@ //! Waku [peer handling and connection](https://rfc.vac.dev/spec/36/#connecting-to-peers) methods // std -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::time::Duration; // crates use multiaddr::Multiaddr; use serde::Deserialize; // internal -use crate::general::{JsonResponse, PeerId, ProtocolId, Result}; +use crate::general::{PeerId, ProtocolId, Result}; use crate::utils::decode_and_free_response; /// Add a node multiaddress and protocol to the waku node’s peerstore. @@ -27,18 +27,7 @@ pub fn waku_add_peers(address: &Multiaddr, protocol_id: ProtocolId) -> Result = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - result.into() + decode_and_free_response(response_ptr) } /// Dial peer using a multiaddress @@ -64,18 +53,7 @@ pub fn waku_connect_peer_with_address( res }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("&str should build properly from the returning response"); - - let result: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - Result::from(result).map(|_| ()) + decode_and_free_response::(response_ptr).map(|_| ()) } /// Dial peer using a peer id @@ -87,7 +65,7 @@ pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option) -> let peer_id_ptr = CString::new(peer_id.as_bytes()) .expect("CString should build properly from peer id") .into_raw(); - let response_ptr = unsafe { + let result_ptr = unsafe { let res = waku_sys::waku_connect_peerid( peer_id_ptr, timeout @@ -98,18 +76,7 @@ pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option) -> res }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("&str should build properly from the returning response"); - - let result: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - Result::from(result).map(|_| ()) + decode_and_free_response::(result_ptr).map(|_| ()) } /// Disconnect a peer using its peer id @@ -124,37 +91,14 @@ pub fn waku_disconnect_peer_with_id(peer_id: &PeerId) -> Result<()> { drop(CString::from_raw(peer_id_ptr)); res }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("&str should build properly from the returning response"); - - let result: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - Result::from(result).map(|_| ()) + decode_and_free_response::(response_ptr).map(|_| ()) } /// Get number of connected peers /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peer_count) pub fn waku_peer_count() -> Result { let response_ptr = unsafe { waku_sys::waku_peer_cnt() }; - - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("&str should build properly from the returning response"); - - let result: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - result.into() + decode_and_free_response(response_ptr) } /// Waku peer supported protocol diff --git a/waku-bindings/src/node/relay.rs b/waku-bindings/src/node/relay.rs index f025850..4c0c8a4 100644 --- a/waku-bindings/src/node/relay.rs +++ b/waku-bindings/src/node/relay.rs @@ -7,9 +7,7 @@ use std::time::Duration; use aes_gcm::{Aes256Gcm, Key}; use secp256k1::{PublicKey, SecretKey}; // internal -use crate::general::{ - Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic, -}; +use crate::general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic}; use crate::utils::decode_and_free_response; /// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/) @@ -73,7 +71,7 @@ pub fn waku_create_pubsub_topic(topic_name: &str, encoding: Encoding) -> WakuPub res }; - let result = unsafe { CString::from_raw(result_ptr) } + let result = unsafe { CStr::from_ptr(result_ptr) } .to_str() .expect("&str from result should always be extracted") .parse() @@ -88,7 +86,7 @@ pub fn waku_create_pubsub_topic(topic_name: &str, encoding: Encoding) -> WakuPub pub fn waku_dafault_pubsub_topic() -> WakuPubSubTopic { let result_ptr = unsafe { waku_sys::waku_default_pubsub_topic() }; - let result = unsafe { CString::from_raw(result_ptr) } + let result = unsafe { CStr::from_ptr(result_ptr) } .to_str() .expect("&str from result should always be extracted") .parse() @@ -272,16 +270,7 @@ pub fn waku_enough_peers(pubsub_topic: Option) -> Result res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("&str from result should always be extracted"); - - let enough_peers: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - enough_peers.into() + decode_and_free_response(result_ptr) } pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> { diff --git a/waku-sys/Cargo.toml b/waku-sys/Cargo.toml index 37dcc1a..9c62a41 100644 --- a/waku-sys/Cargo.toml +++ b/waku-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "waku-sys" -version = "0.1.0-beta4" +version = "0.1.0-beta.5" edition = "2021" authors = [ "Daniel Sanchez Quiros " diff --git a/waku-sys/vendor b/waku-sys/vendor index 11161b8..4804588 160000 --- a/waku-sys/vendor +++ b/waku-sys/vendor @@ -1 +1 @@ -Subproject commit 11161b8919d2d2ad28b9f51dc38ca28ce2c94b72 +Subproject commit 4804588197c3626b8a19b3c200774b04ecb08fde