use cid::Cid;
use noosphere_core::{authority::Authorization, tracing::initialize_tracing};
use url::Url;
use wasm_bindgen::prelude::*;
use crate::{
wasm::SphereContext, NoosphereContext as NoosphereContextImpl, NoosphereContextConfiguration,
NoosphereNetwork, NoosphereSecurity, NoosphereStorage, NoosphereStorageConfig,
NoosphereStoragePath,
};
#[wasm_bindgen]
pub struct SphereReceipt {
#[wasm_bindgen(skip)]
pub identity: String,
#[wasm_bindgen(skip)]
pub mnemonic: String,
}
#[wasm_bindgen]
impl SphereReceipt {
#[wasm_bindgen(getter)]
pub fn identity(&self) -> String {
self.identity.clone()
}
#[wasm_bindgen(getter)]
pub fn mnemonic(&self) -> String {
self.mnemonic.clone()
}
}
#[wasm_bindgen]
pub struct NoosphereContext {
inner: NoosphereContextImpl,
}
#[wasm_bindgen]
impl NoosphereContext {
#[wasm_bindgen(constructor)]
pub fn new(
storage_namespace: String,
gateway_api: Option<String>,
ipfs_gateway_url: Option<String>,
) -> Self {
initialize_tracing(None);
info!("Hello, Noosphere!");
let gateway_api = if let Some(gateway_api) = gateway_api {
Url::parse(&gateway_api).ok()
} else {
None
};
let ipfs_gateway_url = if let Some(ipfs_gateway_url) = ipfs_gateway_url {
Url::parse(&ipfs_gateway_url).ok()
} else {
None
};
let noosphere_context = NoosphereContextImpl::new(NoosphereContextConfiguration {
storage: NoosphereStorage {
path: NoosphereStoragePath::Scoped(storage_namespace.into()),
config: NoosphereStorageConfig::default(),
},
security: NoosphereSecurity::Opaque,
network: NoosphereNetwork::Http {
gateway_api,
ipfs_gateway_url,
},
})
.unwrap();
NoosphereContext {
inner: noosphere_context,
}
}
#[wasm_bindgen(js_name = "createKey")]
pub async fn create_key(&self, key_name: String) {
self.inner
.create_key(&key_name)
.await
.unwrap_or_else(|error| error!("{:?}", error));
}
#[wasm_bindgen(js_name = "hasKey")]
pub async fn has_key(&self, key_name: String) -> Result<bool, String> {
self.inner
.has_key(&key_name)
.await
.map_err(|error| format!("{:?}", error))
}
#[wasm_bindgen(js_name = "createSphere")]
pub async fn create_sphere(&self, key: String) -> Result<SphereReceipt, String> {
let receipt = self
.inner
.create_sphere(&key)
.await
.map_err(|error| format!("{:?}", error))?;
Ok(SphereReceipt {
identity: receipt.identity.into(),
mnemonic: receipt.mnemonic.into(),
})
}
#[wasm_bindgen(js_name = "joinSphere")]
pub async fn join_sphere(
&self,
identity: String,
key: String,
authorization: Option<String>,
) -> Result<(), String> {
let authorization = match authorization {
Some(cid_string) => Some(Authorization::Cid(
Cid::try_from(cid_string).map_err(|error| format!("{:?}", error))?,
)),
None => None,
};
self.inner
.join_sphere(&identity.into(), &key, authorization.as_ref())
.await
.map_err(|error| format!("{:?}", error))?;
Ok(())
}
#[wasm_bindgen(js_name = "getSphereContext")]
pub async fn get_sphere_context(&self, identity: String) -> Result<SphereContext, String> {
Ok(SphereContext {
inner: self
.inner
.get_sphere_channel(&identity.into())
.await
.map_err(|error| format!("{:?}", error))?,
})
}
}