From 6931cc151e56a533820ff6d628e820efd82532f1 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 9 Aug 2024 17:25:04 +0200 Subject: [PATCH] replace unwraps --- index.d.ts | 2 +- src/types.rs | 41 ++++++++++++++++++++++++++++++------- src/we_rust_handler.rs | 45 +++++++++++++++++++++-------------------- src/zome_call_signer.rs | 36 +++++++++++++++++++++------------ 4 files changed, 81 insertions(+), 43 deletions(-) diff --git a/index.d.ts b/index.d.ts index 897307e..ac3c466 100644 --- a/index.d.ts +++ b/index.d.ts @@ -32,7 +32,7 @@ export interface ZomeCallNapi { export type JsWeRustHandler = WeRustHandler export class WeRustHandler { constructor() - static connect(keystoreUrl: string, passphrase: string): Promise + connect(keystoreUrl: string, passphrase: string): Promise signZomeCall(zomeCallUnsignedJs: ZomeCallUnsignedNapi): Promise } export type JsZomeCallSigner = ZomeCallSigner diff --git a/src/types.rs b/src/types.rs index 35596f7..c8b7849 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,6 +3,7 @@ use holochain_types::prelude::{ AgentPubKey, CapSecret, CellId, DnaHash, ExternIO, FunctionName, Timestamp, ZomeCallUnsigned, ZomeName, }; +use napi::Result; #[derive(Clone)] #[napi(object)] @@ -17,12 +18,33 @@ pub struct ZomeCallUnsignedNapi { pub expires_at: i64, } -impl Into for ZomeCallUnsignedNapi { - fn into(self: Self) -> ZomeCallUnsigned { - ZomeCallUnsigned { +impl TryInto for ZomeCallUnsignedNapi { + type Error = napi::Error; + fn try_into(self: Self) -> Result { + Ok(ZomeCallUnsigned { cell_id: CellId::new( - DnaHash::from_raw_39(self.cell_id.get(0).unwrap().clone()).unwrap(), - AgentPubKey::from_raw_39(self.cell_id.get(1).unwrap().clone()).unwrap(), + DnaHash::from_raw_39( + self.cell_id + .get(0) + .ok_or(napi::Error::from_reason("CellId is of the wrong format."))? + .clone(), + ) + .map_err(|e| { + napi::Error::from_reason(format!( + "Failed to convert first element of CellId to DnaHash: {e}" + )) + })?, + AgentPubKey::from_raw_39( + self.cell_id + .get(1) + .ok_or(napi::Error::from_reason("CellId is of the wrong format."))? + .clone(), + ) + .map_err(|e| { + napi::Error::from_reason(format!( + "Failed to convert second element of CellId to AgentPubKey: {e}" + )) + })?, ), zome_name: ZomeName::from(self.zome_name), fn_name: FunctionName::from(self.fn_name), @@ -30,10 +52,15 @@ impl Into for ZomeCallUnsignedNapi { cap_secret: self .cap_secret .map_or(None, |c| Some(CapSecret::from(vec_to_arr(c)))), - provenance: AgentPubKey::from_raw_39(self.provenance).unwrap(), + provenance: AgentPubKey::from_raw_39(self.provenance) + .map_err(|e| { + napi::Error::from_reason(format!( + "Failed to convert provenance field to AgentPubKey: {e}" + )) + })?, nonce: vec_to_arr(self.nonce).into(), expires_at: Timestamp(self.expires_at), - } + }) } } diff --git a/src/we_rust_handler.rs b/src/we_rust_handler.rs index 232b5fa..a916d78 100644 --- a/src/we_rust_handler.rs +++ b/src/we_rust_handler.rs @@ -1,7 +1,11 @@ #![deny(clippy::all)] use holochain_types::prelude::{Signature, ZomeCallUnsigned}; -use lair_keystore_api::{dependencies::{sodoken::BufRead, url::Url}, ipc_keystore::ipc_keystore_connect, LairClient}; +use lair_keystore_api::{ + dependencies::{sodoken::BufRead, url::Url}, + ipc_keystore::ipc_keystore_connect, + LairClient, +}; use napi::Result; use std::ops::Deref; @@ -13,21 +17,17 @@ struct WeRustHandler { impl WeRustHandler { /// Connect to lair keystore - pub async fn new( - keystore_url: String, - passphrase: String, - ) -> Self { - let connection_url_parsed = Url::parse(keystore_url.deref()).unwrap(); + pub async fn new(keystore_url: String, passphrase: String) -> Result { + let connection_url_parsed = Url::parse(keystore_url.deref()) + .map_err(|e| napi::Error::from_reason(format!("Failed to parse keystore URL: {e}")))?; let passphrase_bufread: BufRead = passphrase.as_bytes().into(); // TODO graceful error handling below let lair_client = ipc_keystore_connect(connection_url_parsed, passphrase_bufread) .await - .unwrap(); + .map_err(|e| napi::Error::from_reason(format!("Failed to connect to keystore: {e}")))?; - Self { - lair_client, - } + Ok(Self { lair_client }) } /// Sign a zome call @@ -35,18 +35,23 @@ impl WeRustHandler { &self, zome_call_unsigned_js: ZomeCallUnsignedNapi, ) -> Result { - let zome_call_unsigned: ZomeCallUnsigned = zome_call_unsigned_js.clone().into(); + println!("Signing zome call [:)]"); + let zome_call_unsigned: ZomeCallUnsigned = zome_call_unsigned_js.clone().try_into()?; let pub_key = zome_call_unsigned.provenance.clone(); let mut pub_key_2 = [0; 32]; pub_key_2.copy_from_slice(pub_key.get_raw_32()); - let data_to_sign = zome_call_unsigned.data_to_sign().unwrap(); + let data_to_sign = zome_call_unsigned.data_to_sign().map_err(|e| { + napi::Error::from_reason(format!( + "Failed to get data to sign from unsigned zome call: {e}" + )) + })?; let sig = self .lair_client .sign_by_pub_key(pub_key_2.into(), None, data_to_sign) .await - .unwrap(); + .map_err(|e| napi::Error::from_reason(format!("Failed to sign by pub key: {e}")))?; let signature = Signature(*sig.0); @@ -81,16 +86,12 @@ impl JsWeRustHandler { } #[napi] - pub async fn connect( - keystore_url: String, - passphrase: String, - ) -> Self { - let we_rust_handler = - WeRustHandler::new(keystore_url, passphrase).await; - - JsWeRustHandler { + pub async fn connect(&self, keystore_url: String, passphrase: String) -> Result { + let we_rust_handler = WeRustHandler::new(keystore_url, passphrase).await?; + + Ok(JsWeRustHandler { we_rust_handler: Some(we_rust_handler), - } + }) } #[napi] diff --git a/src/zome_call_signer.rs b/src/zome_call_signer.rs index 2104de6..4ad289f 100644 --- a/src/zome_call_signer.rs +++ b/src/zome_call_signer.rs @@ -3,7 +3,11 @@ use std::ops::Deref; use holochain_types::prelude::{Signature, ZomeCallUnsigned}; -use lair_keystore_api::{dependencies::{sodoken::BufRead, url::Url}, ipc_keystore::ipc_keystore_connect, LairClient}; +use lair_keystore_api::{ + dependencies::{sodoken::BufRead, url::Url}, + ipc_keystore::ipc_keystore_connect, + LairClient, +}; use napi::Result; use crate::types::*; @@ -14,15 +18,17 @@ struct ZomeCallSigner { impl ZomeCallSigner { /// Connect to lair keystore - pub async fn new(connection_url: String, passphrase: String) -> Self { - let connection_url_parsed = Url::parse(connection_url.deref()).unwrap(); + pub async fn new(connection_url: String, passphrase: String) -> Result { + let connection_url_parsed = Url::parse(connection_url.deref()) + .map_err(|e| napi::Error::from_reason(format!("Failed to parse keystore URL: {e}")))?; + let passphrase_bufread: BufRead = passphrase.as_bytes().into(); let lair_client = ipc_keystore_connect(connection_url_parsed, passphrase_bufread) .await - .unwrap(); + .map_err(|e| napi::Error::from_reason(format!("Failed to connect to keystore: {e}")))?; - Self { lair_client } + Ok(Self { lair_client }) } /// Sign a zome call @@ -30,18 +36,22 @@ impl ZomeCallSigner { &self, zome_call_unsigned_js: ZomeCallUnsignedNapi, ) -> Result { - let zome_call_unsigned: ZomeCallUnsigned = zome_call_unsigned_js.clone().into(); + let zome_call_unsigned: ZomeCallUnsigned = zome_call_unsigned_js.clone().try_into()?; let pub_key = zome_call_unsigned.provenance.clone(); let mut pub_key_2 = [0; 32]; pub_key_2.copy_from_slice(pub_key.get_raw_32()); - let data_to_sign = zome_call_unsigned.data_to_sign().unwrap(); + let data_to_sign = zome_call_unsigned.data_to_sign().map_err(|e| { + napi::Error::from_reason(format!( + "Failed to get data to sign from unsigned zome call: {e}" + )) + })?; let sig = self .lair_client .sign_by_pub_key(pub_key_2.into(), None, data_to_sign) .await - .unwrap(); + .map_err(|e| napi::Error::from_reason(format!("Failed to sign by pub key: {e}")))?; let signature = Signature(*sig.0); @@ -76,12 +86,12 @@ impl JsZomeCallSigner { } #[napi] - pub async fn connect(connection_url: String, passphrase: String) -> Self { - let zome_call_signer = ZomeCallSigner::new(connection_url, passphrase).await; + pub async fn connect(connection_url: String, passphrase: String) -> Result { + let zome_call_signer = ZomeCallSigner::new(connection_url, passphrase).await?; - JsZomeCallSigner { + Ok(JsZomeCallSigner { zome_call_signer: Some(zome_call_signer), - } + }) } #[napi] @@ -91,7 +101,7 @@ impl JsZomeCallSigner { ) -> Result { self.zome_call_signer .as_ref() - .unwrap() + .ok_or(napi::Error::from_reason("Failed to get reference to ZomeCallSigner"))? .sign_zome_call(zome_call_unsigned_js) .await }