Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ClientMap with new Resolver #594

Merged
merged 13 commits into from
Feb 23, 2022
1 change: 1 addition & 0 deletions bindings/wasm/src/did/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use self::wasm_document::WasmDocument;
pub use self::wasm_document_metadata::WasmDocumentMetadata;
pub use self::wasm_method_scope::WasmMethodScope;
pub use self::wasm_method_type::WasmMethodType;
pub use self::wasm_resolved_document::PromiseArrayResolvedDocument;
pub use self::wasm_resolved_document::PromiseResolvedDocument;
pub use self::wasm_resolved_document::WasmResolvedDocument;
pub use self::wasm_verification_method::WasmVerificationMethod;
Expand Down
3 changes: 3 additions & 0 deletions bindings/wasm/src/did/wasm_resolved_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct WasmResolvedDocument(pub(crate) ResolvedIotaDocument);
extern "C" {
#[wasm_bindgen(typescript_type = "Promise<ResolvedDocument>")]
pub type PromiseResolvedDocument;

#[wasm_bindgen(typescript_type = "Promise<Array<ResolvedDocument>>")]
pub type PromiseArrayResolvedDocument;
}

#[wasm_bindgen(js_class = ResolvedDocument)]
Expand Down
56 changes: 30 additions & 26 deletions bindings/wasm/src/tangle/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::executor;
use identity::core::FromJson;
use identity::credential::Credential;
use identity::credential::Presentation;
use identity::iota::Client as IotaClient;
use identity::iota::Client;
use identity::iota::CredentialValidator;
use identity::iota::IotaDID;
use identity::iota::IotaDocument;
Expand Down Expand Up @@ -38,29 +38,29 @@ use crate::tangle::PromiseReceipt;
use crate::tangle::WasmNetwork;
use crate::tangle::WasmReceipt;

#[wasm_bindgen]
#[wasm_bindgen(js_name = Client)]
#[derive(Debug)]
pub struct Client {
pub(crate) client: Rc<IotaClient>,
pub struct WasmClient {
pub(crate) client: Rc<Client>,
}

#[wasm_bindgen]
impl Client {
fn from_client(client: IotaClient) -> Client {
#[wasm_bindgen(js_class = Client)]
impl WasmClient {
fn from_client(client: Client) -> WasmClient {
Self {
client: Rc::new(client),
}
}

/// Creates a new `Client` with default settings.
#[wasm_bindgen(constructor)]
pub fn new() -> Result<Client> {
pub fn new() -> Result<WasmClient> {
Self::from_config(&mut Config::new())
}

/// Creates a new `Client` with settings from the given `Config`.
#[wasm_bindgen(js_name = fromConfig)]
pub fn from_config(config: &mut Config) -> Result<Client> {
pub fn from_config(config: &mut Config) -> Result<WasmClient> {
let future = config.take_builder()?.build();
let output = executor::block_on(future).wasm_result();

Expand All @@ -69,8 +69,8 @@ impl Client {

/// Creates a new `Client` with default settings for the given `Network`.
#[wasm_bindgen(js_name = fromNetwork)]
pub fn from_network(network: WasmNetwork) -> Result<Client> {
let future = IotaClient::from_network(network.into());
pub fn from_network(network: WasmNetwork) -> Result<WasmClient> {
let future = Client::from_network(network.into());
let output = executor::block_on(future).wasm_result();

output.map(Self::from_client)
Expand All @@ -86,7 +86,7 @@ impl Client {
#[wasm_bindgen(js_name = publishDocument)]
pub fn publish_document(&self, document: &WasmDocument) -> Result<PromiseReceipt> {
let document: IotaDocument = document.0.clone();
let client: Rc<IotaClient> = self.client.clone();
let client: Rc<Client> = self.client.clone();

let promise: Promise = future_to_promise(async move {
client
Expand All @@ -105,7 +105,7 @@ impl Client {
#[wasm_bindgen(js_name = publishDiff)]
pub fn publish_diff(&self, message_id: &str, diff: WasmDiffMessage) -> Result<PromiseReceipt> {
let message: MessageId = MessageId::from_str(message_id).wasm_result()?;
let client: Rc<IotaClient> = self.client.clone();
let client: Rc<Client> = self.client.clone();

let promise: Promise = future_to_promise(async move {
client
Expand All @@ -122,11 +122,10 @@ impl Client {

/// Publishes arbitrary JSON data to the specified index on the Tangle.
#[wasm_bindgen(js_name = publishJSON)]
pub fn publish_json(&self, index: &str, data: &JsValue) -> Result<PromiseReceipt> {
let client: Rc<IotaClient> = self.client.clone();

let index = index.to_owned();
pub fn publish_json(&self, index: String, data: &JsValue) -> Result<PromiseReceipt> {
let value: serde_json::Value = data.into_serde().wasm_result()?;

let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.publish_json(&index, &value)
Expand All @@ -146,15 +145,14 @@ impl Client {
#[wasm_bindgen(js_name = publishJsonWithRetry)]
pub fn publish_json_with_retry(
&self,
index: &str,
index: String,
data: &JsValue,
interval: Option<u32>,
max_attempts: Option<u32>,
) -> Result<Promise> {
let client: Rc<IotaClient> = self.client.clone();

let index = index.to_owned();
let value: serde_json::Value = data.into_serde().wasm_result()?;

let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.publish_json_with_retry(
Expand All @@ -176,7 +174,7 @@ impl Client {
pub fn resolve(&self, did: UWasmDID) -> Result<PromiseResolvedDocument> {
let did: IotaDID = IotaDID::try_from(did)?;

let client: Rc<IotaClient> = self.client.clone();
let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.resolve(&did)
Expand All @@ -195,7 +193,7 @@ impl Client {
pub fn resolve_history(&self, did: UWasmDID) -> Result<PromiseDocumentHistory> {
let did: IotaDID = IotaDID::try_from(did)?;

let client: Rc<IotaClient> = self.client.clone();
let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.resolve_history(&did)
Expand All @@ -216,9 +214,9 @@ impl Client {
/// capability invocation method.
#[wasm_bindgen(js_name = resolveDiffHistory)]
pub fn resolve_diff_history(&self, document: &WasmResolvedDocument) -> Result<PromiseDiffChainHistory> {
let client: Rc<IotaClient> = self.client.clone();
let resolved_document: ResolvedIotaDocument = document.0.clone();

let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
client
.resolve_diff_history(&resolved_document)
Expand All @@ -236,9 +234,9 @@ impl Client {
// TODO: move out of client to dedicated verifier
#[wasm_bindgen(js_name = checkCredential)]
pub fn check_credential(&self, data: &str, options: WasmVerifierOptions) -> Result<Promise> {
let client: Rc<IotaClient> = self.client.clone();
let data: Credential = Credential::from_json(&data).wasm_result()?;

let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
CredentialValidator::new(&*client)
.validate_credential(data, options.0)
Expand All @@ -254,9 +252,9 @@ impl Client {
// TODO: move out of client to dedicated verifier
#[wasm_bindgen(js_name = checkPresentation)]
pub fn check_presentation(&self, data: &str, options: WasmVerifierOptions) -> Result<Promise> {
let client: Rc<IotaClient> = self.client.clone();
let data: Presentation = Presentation::from_json(&data).wasm_result()?;

let client: Rc<Client> = self.client.clone();
let promise: Promise = future_to_promise(async move {
CredentialValidator::new(&*client)
.validate_presentation(data, options.0)
Expand All @@ -268,3 +266,9 @@ impl Client {
Ok(promise)
}
}

impl From<Rc<Client>> for WasmClient {
fn from(client: Rc<Client>) -> Self {
Self { client }
}
}
4 changes: 3 additions & 1 deletion bindings/wasm/src/tangle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 IOTA Stiftung
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

pub use self::client::*;
Expand All @@ -7,10 +7,12 @@ pub use self::explorer::*;
pub use self::message::*;
pub use self::network::*;
pub use self::receipt::*;
pub use self::resolver::*;

mod client;
mod config;
mod explorer;
mod message;
mod network;
mod receipt;
mod resolver;
Loading