From c1a9bab788163a5ad1de7fc3a249549633361dc5 Mon Sep 17 00:00:00 2001 From: "Ryan.K" Date: Thu, 17 Aug 2023 16:08:51 +0800 Subject: [PATCH] one stop new function --- node/src/browser/client.rs | 70 ++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/node/src/browser/client.rs b/node/src/browser/client.rs index 1fb14206a..932fd8145 100644 --- a/node/src/browser/client.rs +++ b/node/src/browser/client.rs @@ -136,39 +136,59 @@ impl Client { let config: ProcessorConfig = serde_yaml::from_str(&config)?; Self::new_client_with_storage_internal(config, callback, storage_name).await } + + pub async fn new( + ice_servers: String, + stabilize_timeout: usize, + account: String, + account_type: String, + // Signer should be `async function (proof: string): Promise` + signer: js_sys::Function, + callback: Option, + ) -> Result { + let mut sk_builder = SessionSkBuilder::new(account, account_type); + let proof = sk_builder.unsigned_proof(); + let sig: js_sys::Uint8Array = Uint8Array::from( + JsFuture::from(js_sys::Promise::from( + signer.call1(&JsValue::NULL, &JsValue::from_str(&proof))?, + )) + .await?, + ); + sk_builder = sk_builder.set_session_sig(sig.to_vec()); + let session_sk = sk_builder.build().unwrap(); + let config = ProcessorConfig::new(ice_servers, session_sk, stabilize_timeout); + Ok(JsValue::from( + Self::new_client_with_storage_internal(config, callback, "rings-node".to_string()) + .await?, + )) + } } #[wasm_export] impl Client { #[wasm_bindgen(constructor)] - pub fn new( + pub fn new_instance( ice_servers: String, stabilize_timeout: usize, account: String, account_type: String, - // Signer should be `async function (proof: string): Promise` + // Signer should be `async function (proof: string): Promise` signer: js_sys::Function, callback: Option, - //) -> Result { + //) -> Result { ) -> js_sys::Promise { - future_to_promise(async move { - let mut sk_builder = SessionSkBuilder::new(account, account_type); - let proof = sk_builder.unsigned_proof(); - let sig: js_sys::Uint8Array = Uint8Array::from( - JsFuture::from( - js_sys::Promise::from( - signer - .call1(&JsValue::NULL, &JsValue::from_str(&proof))? - ) - ).await? - ); - sk_builder = sk_builder.set_session_sig(sig.to_vec()); - let session_sk = sk_builder.build().unwrap(); - let config = ProcessorConfig::new(ice_servers, session_sk, stabilize_timeout); - Ok(JsValue::from(Self::new_client_with_storage_internal(config, callback, "rings-node".to_string()).await?)) - }) + future_to_promise(async move { + Self::new( + ice_servers, + stabilize_timeout, + account, + account_type, + signer, + callback, + ) + .await + }) } - /// Create new client instance with serialized config (yaml/json) pub fn new_client_with_serialized_config( config: String, @@ -178,16 +198,6 @@ impl Client { Self::new_client_with_config(cfg, callback) } - /// Create new client instance with storage name and serialized config (yaml/json) - pub fn new_client_with_storage_and_serialized_config( - config: String, - callback: Option, - storage_name: String, - ) -> js_sys::Promise { - let cfg: ProcessorConfig = serde_yaml::from_str(&config).unwrap(); - Self::new_client_with_storage(cfg, callback, storage_name) - } - /// Create a new client instance. pub fn new_client_with_config( config: ProcessorConfig,