Skip to content

Commit

Permalink
ffi(nostr): fix tokio runtime for async functions
Browse files Browse the repository at this point in the history
* Specify tokio runtime for NIP-05 and NIP-11 functions

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Jun 7, 2024
1 parent 78970d6 commit 58ce4ef
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bindings/nostr-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["lib", "cdylib", "staticlib"]

[dependencies]
nostr = { workspace = true, features = ["std", "all-nips"] }
uniffi.workspace = true
uniffi = { workspace = true, features = ["tokio"] }

[dev-dependencies]
uniffi = { workspace = true, features = ["bindgen-tests"] }
4 changes: 2 additions & 2 deletions bindings/nostr-ffi/src/nips/nip05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::error::Result;
use crate::nips::nip19::Nip19Profile;
use crate::PublicKey;

#[uniffi::export(default(proxy = None))]
#[uniffi::export(async_runtime = "tokio", default(proxy = None))]
pub async fn verify_nip05(
public_key: &PublicKey,
nip05: &str,
Expand All @@ -25,7 +25,7 @@ pub async fn verify_nip05(
Ok(nip05::verify(public_key.deref(), nip05, proxy).await?)
}

#[uniffi::export(default(proxy = None))]
#[uniffi::export(async_runtime = "tokio", default(proxy = None))]
pub async fn get_nip05_profile(nip05: &str, proxy: Option<String>) -> Result<Arc<Nip19Profile>> {
let proxy: Option<SocketAddr> = match proxy {
Some(proxy) => Some(proxy.parse()?),
Expand Down
27 changes: 15 additions & 12 deletions bindings/nostr-ffi/src/nips/nip11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ impl From<nip11::RelayInformationDocument> for RelayInformationDocument {
}
}

#[uniffi::export(async_runtime = "tokio", default(proxy = None))]
pub async fn nip11_get_information_document(
url: &str,
proxy: Option<String>,
) -> Result<RelayInformationDocument> {
let url: Url = Url::parse(url)?;
let proxy: Option<SocketAddr> = match proxy {
Some(proxy) => Some(proxy.parse()?),
None => None,
};
Ok(RelayInformationDocument {
inner: nip11::RelayInformationDocument::get(url, proxy).await?,
})
}

#[uniffi::export]
impl RelayInformationDocument {
#[uniffi::constructor]
Expand All @@ -34,18 +49,6 @@ impl RelayInformationDocument {
}
}

#[uniffi::constructor(default(proxy = None))]
pub async fn get(url: &str, proxy: Option<String>) -> Result<Self> {
let url: Url = Url::parse(url)?;
let proxy: Option<SocketAddr> = match proxy {
Some(proxy) => Some(proxy.parse()?),
None => None,
};
Ok(Self {
inner: nip11::RelayInformationDocument::get(url, proxy).await?,
})
}

pub fn name(&self) -> Option<String> {
self.inner.name.clone()
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-ffi/tests/test_generated_bindings.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
uniffi::build_foreign_language_testcases!("tests/test_equality.py");
uniffi::build_foreign_language_testcases!("tests/test_equality.py", "tests/test_nip05.py");
14 changes: 14 additions & 0 deletions bindings/nostr-ffi/tests/test_nip05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import asyncio
from nostr_ffi import *

async def main():
nip_05 = "[email protected]"
public_key = PublicKey.parse("npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet")
proxy = None
if await verify_nip05(public_key, nip_05, proxy):
print(f" '{nip_05}' verified, for {public_key.to_bech32()}")
else:
print(f" Unable to verify NIP-05, for {public_key.to_bech32()}")

if __name__ == '__main__':
asyncio.run(main())

0 comments on commit 58ce4ef

Please sign in to comment.