From fbad4fa2a784f75e13701f2d2f75d8127fc13b29 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 30 Sep 2024 16:51:07 -0400 Subject: [PATCH] Fix kt tests --- bindings/web5_uniffi/src/web5.udl | 4 +++- bindings/web5_uniffi_wrapper/Cargo.toml | 2 +- .../credentials/presentation_definition.rs | 10 +++++---- .../src/credentials/status_list_credential.rs | 5 +++-- .../credentials/verifiable_credential_1_1.rs | 8 ++++--- .../verifiable_presentation_1_1.rs | 9 ++++---- .../src/dids/methods/did_dht.rs | 18 ++++++++-------- .../src/dids/methods/did_web.rs | 10 ++++----- .../src/dids/resolution/resolution_result.rs | 7 ++++--- bindings/web5_uniffi_wrapper/src/lib.rs | 10 +++++++++ .../src/main/kotlin/web5/sdk/rust/UniFFI.kt | 21 +++++++++++-------- 11 files changed, 63 insertions(+), 41 deletions(-) diff --git a/bindings/web5_uniffi/src/web5.udl b/bindings/web5_uniffi/src/web5.udl index 33a835b9..70128d7d 100644 --- a/bindings/web5_uniffi/src/web5.udl +++ b/bindings/web5_uniffi/src/web5.udl @@ -8,12 +8,14 @@ namespace web5 { [Throws=Web5Error] BearerDid did_web_create(string domain, DidWebCreateOptions? options); + [Throws=Web5Error] ResolutionResult did_web_resolve([ByRef] string uri); [Throws=Web5Error] BearerDid did_dht_create(DidDhtCreateOptions? options); [Throws=Web5Error] void did_dht_publish(BearerDid bearer_did, string? gateway_url); + [Throws=Web5Error] ResolutionResult did_dht_resolve([ByRef] string uri, string? gateway_url); }; @@ -190,7 +192,7 @@ dictionary ResolutionResultData { }; interface ResolutionResult { - [Name=resolve] + [Name=resolve, Throws=Web5Error] constructor([ByRef] string uri); ResolutionResultData get_data(); }; diff --git a/bindings/web5_uniffi_wrapper/Cargo.toml b/bindings/web5_uniffi_wrapper/Cargo.toml index 956c43e4..d8621950 100644 --- a/bindings/web5_uniffi_wrapper/Cargo.toml +++ b/bindings/web5_uniffi_wrapper/Cargo.toml @@ -7,7 +7,7 @@ repository.workspace = true license-file.workspace = true [dependencies] -futures = "0.3.30" serde_json = { workspace = true } thiserror = { workspace = true } +tokio = { version = "1.38.0", features = ["full"] } web5 = { path = "../../crates/web5" } \ No newline at end of file diff --git a/bindings/web5_uniffi_wrapper/src/credentials/presentation_definition.rs b/bindings/web5_uniffi_wrapper/src/credentials/presentation_definition.rs index 3e8c50d3..863d97be 100644 --- a/bindings/web5_uniffi_wrapper/src/credentials/presentation_definition.rs +++ b/bindings/web5_uniffi_wrapper/src/credentials/presentation_definition.rs @@ -1,5 +1,4 @@ -use crate::errors::Result; -use futures::executor::block_on; +use crate::{errors::Result, get_rt}; use web5::credentials::presentation_definition::PresentationDefinition as InnerPresentationDefinition; pub struct PresentationDefinition(pub InnerPresentationDefinition); @@ -14,11 +13,14 @@ impl PresentationDefinition { } pub fn select_credentials(&self, vc_jwts: &Vec) -> Result> { - Ok(block_on(self.0.select_credentials(vc_jwts))?) + let rt = get_rt()?; + Ok(rt.block_on(self.0.select_credentials(vc_jwts))?) } pub fn create_presentation_from_credentials(&self, vc_jwts: &Vec) -> Result { - let presentation_result = block_on(self.0.create_presentation_from_credentials(vc_jwts))?; + let rt = get_rt()?; + let presentation_result = + rt.block_on(self.0.create_presentation_from_credentials(vc_jwts))?; let json_serialized_presentation_result = serde_json::to_string(&presentation_result)?; Ok(json_serialized_presentation_result) diff --git a/bindings/web5_uniffi_wrapper/src/credentials/status_list_credential.rs b/bindings/web5_uniffi_wrapper/src/credentials/status_list_credential.rs index f88265bb..70d3345f 100644 --- a/bindings/web5_uniffi_wrapper/src/credentials/status_list_credential.rs +++ b/bindings/web5_uniffi_wrapper/src/credentials/status_list_credential.rs @@ -1,6 +1,6 @@ use crate::credentials::verifiable_credential_1_1::VerifiableCredential; use crate::errors::Result; -use futures::executor::block_on; +use crate::get_rt; use std::sync::Arc; use web5::credentials::Issuer; use web5::{ @@ -26,7 +26,8 @@ impl StatusListCredential { .collect() }); - Ok(Self(block_on(InnerStatusListCredential::create( + let rt = get_rt()?; + Ok(Self(rt.block_on(InnerStatusListCredential::create( issuer, status_purpose, inner_vcs, diff --git a/bindings/web5_uniffi_wrapper/src/credentials/verifiable_credential_1_1.rs b/bindings/web5_uniffi_wrapper/src/credentials/verifiable_credential_1_1.rs index 1f08acc1..0cfa0306 100644 --- a/bindings/web5_uniffi_wrapper/src/credentials/verifiable_credential_1_1.rs +++ b/bindings/web5_uniffi_wrapper/src/credentials/verifiable_credential_1_1.rs @@ -1,5 +1,5 @@ +use crate::get_rt; use crate::{dids::bearer_did::BearerDid, errors::Result}; -use futures::executor::block_on; use std::{sync::Arc, time::SystemTime}; use web5::credentials::CredentialStatus; use web5::credentials::Issuer; @@ -61,7 +61,8 @@ impl VerifiableCredential { evidence, }; - let inner_vc = block_on(InnerVerifiableCredential::create( + let rt = get_rt()?; + let inner_vc = rt.block_on(InnerVerifiableCredential::create( issuer, credential_subject, Some(inner_options), @@ -95,7 +96,8 @@ impl VerifiableCredential { } pub fn from_vc_jwt(vc_jwt: String, verify: bool) -> Result { - let inner_vc = block_on(InnerVerifiableCredential::from_vc_jwt(&vc_jwt, verify))?; + let rt = get_rt()?; + let inner_vc = rt.block_on(InnerVerifiableCredential::from_vc_jwt(&vc_jwt, verify))?; let json_serialized_issuer = serde_json::to_string(&inner_vc.issuer)?; let json_serialized_credential_subject = serde_json::to_string(&inner_vc.credential_subject)?; diff --git a/bindings/web5_uniffi_wrapper/src/credentials/verifiable_presentation_1_1.rs b/bindings/web5_uniffi_wrapper/src/credentials/verifiable_presentation_1_1.rs index c9831dfb..be81aee4 100644 --- a/bindings/web5_uniffi_wrapper/src/credentials/verifiable_presentation_1_1.rs +++ b/bindings/web5_uniffi_wrapper/src/credentials/verifiable_presentation_1_1.rs @@ -1,5 +1,5 @@ +use crate::get_rt; use crate::{dids::bearer_did::BearerDid, errors::Result}; -use futures::executor::block_on; use serde_json::Value; use std::collections::HashMap; use std::sync::Arc; @@ -45,7 +45,8 @@ impl VerifiablePresentation { additional_data, }; - let inner_vp = block_on(InnerVerifiablePresentation::create( + let rt = get_rt()?; + let inner_vp = rt.block_on(InnerVerifiablePresentation::create( holder, vc_jwts, Some(inner_options), @@ -73,8 +74,8 @@ impl VerifiablePresentation { } pub fn from_vp_jwt(vp_jwt: String, verify: bool) -> Result { - let inner_vp = block_on(InnerVerifiablePresentation::from_vp_jwt(&vp_jwt, verify))?; - + let rt = get_rt()?; + let inner_vp = rt.block_on(InnerVerifiablePresentation::from_vp_jwt(&vp_jwt, verify))?; Ok(Self { inner_vp }) } diff --git a/bindings/web5_uniffi_wrapper/src/dids/methods/did_dht.rs b/bindings/web5_uniffi_wrapper/src/dids/methods/did_dht.rs index 4973c861..9c692f0b 100644 --- a/bindings/web5_uniffi_wrapper/src/dids/methods/did_dht.rs +++ b/bindings/web5_uniffi_wrapper/src/dids/methods/did_dht.rs @@ -2,17 +2,18 @@ use crate::{ crypto::key_manager::{KeyManager, ToInnerKeyManager}, dids::{bearer_did::BearerDid, resolution::resolution_result::ResolutionResult}, errors::Result, + get_rt, }; -use futures::executor::block_on; use std::sync::Arc; use web5::dids::{ data_model::{service::Service, verification_method::VerificationMethod}, methods::did_dht::{DidDht as InnerDidDht, DidDhtCreateOptions as InnerDidDhtCreateOptions}, }; -pub fn did_dht_resolve(uri: &str, gateway_url: Option) -> Arc { - let resolution_result = block_on(InnerDidDht::resolve(uri, gateway_url)); - Arc::new(ResolutionResult(resolution_result)) +pub fn did_dht_resolve(uri: &str, gateway_url: Option) -> Result> { + let rt = get_rt()?; + let resolution_result = rt.block_on(InnerDidDht::resolve(uri, gateway_url)); + Ok(Arc::new(ResolutionResult(resolution_result))) } #[derive(Default)] @@ -40,13 +41,12 @@ pub fn did_dht_create(options: Option) -> Result, gateway_url: Option) -> Result<()> { - Ok(block_on(InnerDidDht::publish( - bearer_did.0.clone(), - gateway_url, - ))?) + let rt = get_rt()?; + Ok(rt.block_on(InnerDidDht::publish(bearer_did.0.clone(), gateway_url))?) } diff --git a/bindings/web5_uniffi_wrapper/src/dids/methods/did_web.rs b/bindings/web5_uniffi_wrapper/src/dids/methods/did_web.rs index 42c6c66e..49d218cd 100644 --- a/bindings/web5_uniffi_wrapper/src/dids/methods/did_web.rs +++ b/bindings/web5_uniffi_wrapper/src/dids/methods/did_web.rs @@ -1,9 +1,8 @@ use crate::{ crypto::key_manager::{KeyManager, ToInnerKeyManager}, dids::{bearer_did::BearerDid, resolution::resolution_result::ResolutionResult}, - errors::Result, + errors::Result, get_rt, }; -use futures::executor::block_on; use std::sync::Arc; use web5::{ crypto::dsa::Dsa, @@ -15,9 +14,10 @@ use web5::{ }, }; -pub fn did_web_resolve(uri: &str) -> Arc { - let resolution_result = block_on(InnerDidWeb::resolve(uri)); - Arc::new(ResolutionResult(resolution_result)) +pub fn did_web_resolve(uri: &str) -> Result> { + let rt = get_rt()?; + let resolution_result = rt.block_on(InnerDidWeb::resolve(uri)); + Ok(Arc::new(ResolutionResult(resolution_result))) } #[derive(Default)] diff --git a/bindings/web5_uniffi_wrapper/src/dids/resolution/resolution_result.rs b/bindings/web5_uniffi_wrapper/src/dids/resolution/resolution_result.rs index 19a3a65d..dcd4c7c1 100644 --- a/bindings/web5_uniffi_wrapper/src/dids/resolution/resolution_result.rs +++ b/bindings/web5_uniffi_wrapper/src/dids/resolution/resolution_result.rs @@ -1,11 +1,12 @@ -use futures::executor::block_on; +use crate::{errors::Result, get_rt}; use web5::dids::resolution::resolution_result::ResolutionResult as InnerResolutionResult; pub struct ResolutionResult(pub InnerResolutionResult); impl ResolutionResult { - pub fn resolve(uri: &str) -> Self { - Self(block_on(InnerResolutionResult::resolve(uri))) + pub fn resolve(uri: &str) -> Result { + let rt = get_rt()?; + Ok(Self(rt.block_on(InnerResolutionResult::resolve(uri)))) } pub fn get_data(&self) -> InnerResolutionResult { diff --git a/bindings/web5_uniffi_wrapper/src/lib.rs b/bindings/web5_uniffi_wrapper/src/lib.rs index e4dde532..9d5e36d4 100644 --- a/bindings/web5_uniffi_wrapper/src/lib.rs +++ b/bindings/web5_uniffi_wrapper/src/lib.rs @@ -1,5 +1,15 @@ +use errors::Result; +use tokio::runtime::Runtime; +use web5::errors::Web5Error; + pub mod credentials; pub mod crypto; pub mod dids; pub mod errors; + +pub fn get_rt() -> Result { + let rt = Runtime::new() + .map_err(|e| Web5Error::Unknown(format!("unable to instantiate tokio runtime {}", e)))?; + Ok(rt) +} diff --git a/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt b/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt index 5dc9078b..010858cb 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt @@ -1429,7 +1429,7 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) { if (lib.uniffi_web5_uniffi_checksum_func_did_dht_publish() != 17158.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_web5_uniffi_checksum_func_did_dht_resolve() != 56140.toShort()) { + if (lib.uniffi_web5_uniffi_checksum_func_did_dht_resolve() != 25411.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_web5_uniffi_checksum_func_did_jwk_create() != 64914.toShort()) { @@ -1441,7 +1441,7 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) { if (lib.uniffi_web5_uniffi_checksum_func_did_web_create() != 8722.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_web5_uniffi_checksum_func_did_web_resolve() != 15538.toShort()) { + if (lib.uniffi_web5_uniffi_checksum_func_did_web_resolve() != 6380.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_web5_uniffi_checksum_func_ed25519_generator_generate() != 57849.toShort()) { @@ -1585,7 +1585,7 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) { if (lib.uniffi_web5_uniffi_checksum_constructor_presentationdefinition_new() != 13282.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } - if (lib.uniffi_web5_uniffi_checksum_constructor_resolutionresult_resolve() != 11404.toShort()) { + if (lib.uniffi_web5_uniffi_checksum_constructor_resolutionresult_resolve() != 14670.toShort()) { throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project") } if (lib.uniffi_web5_uniffi_checksum_constructor_secp256k1signer_new() != 58975.toShort()) { @@ -4986,9 +4986,10 @@ open class ResolutionResult: Disposable, AutoCloseable, ResolutionResultInterfac companion object { - fun `resolve`(`uri`: kotlin.String): ResolutionResult { + + @Throws(Web5Exception::class) fun `resolve`(`uri`: kotlin.String): ResolutionResult { return FfiConverterTypeResolutionResult.lift( - uniffiRustCall() { _status -> + uniffiRustCallWithError(Web5Exception) { _status -> UniffiLib.INSTANCE.uniffi_web5_uniffi_fn_constructor_resolutionresult_resolve( FfiConverterString.lower(`uri`),_status) } @@ -8588,9 +8589,10 @@ public object FfiConverterMapStringString: FfiConverterRustBuffer + uniffiRustCallWithError(Web5Exception) { _status -> UniffiLib.INSTANCE.uniffi_web5_uniffi_fn_func_did_dht_resolve( FfiConverterString.lower(`uri`),FfiConverterOptionalString.lower(`gatewayUrl`),_status) } @@ -8626,9 +8628,10 @@ public object FfiConverterMapStringString: FfiConverterRustBuffer + uniffiRustCallWithError(Web5Exception) { _status -> UniffiLib.INSTANCE.uniffi_web5_uniffi_fn_func_did_web_resolve( FfiConverterString.lower(`uri`),_status) }