Skip to content

Commit

Permalink
replace unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
matthme committed Aug 9, 2024
1 parent c748693 commit 6931cc1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 43 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ZomeCallNapi {
export type JsWeRustHandler = WeRustHandler
export class WeRustHandler {
constructor()
static connect(keystoreUrl: string, passphrase: string): Promise<WeRustHandler>
connect(keystoreUrl: string, passphrase: string): Promise<WeRustHandler>
signZomeCall(zomeCallUnsignedJs: ZomeCallUnsignedNapi): Promise<ZomeCallNapi>
}
export type JsZomeCallSigner = ZomeCallSigner
Expand Down
41 changes: 34 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use holochain_types::prelude::{
AgentPubKey, CapSecret, CellId, DnaHash, ExternIO, FunctionName, Timestamp, ZomeCallUnsigned,
ZomeName,
};
use napi::Result;

#[derive(Clone)]
#[napi(object)]
Expand All @@ -17,23 +18,49 @@ pub struct ZomeCallUnsignedNapi {
pub expires_at: i64,
}

impl Into<ZomeCallUnsigned> for ZomeCallUnsignedNapi {
fn into(self: Self) -> ZomeCallUnsigned {
ZomeCallUnsigned {
impl TryInto<ZomeCallUnsigned> for ZomeCallUnsignedNapi {
type Error = napi::Error;
fn try_into(self: Self) -> Result<ZomeCallUnsigned> {
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),
payload: ExternIO::from(self.payload),
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),
}
})
}
}

Expand Down
45 changes: 23 additions & 22 deletions src/we_rust_handler.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,40 +17,41 @@ 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<Self> {
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
pub async fn sign_zome_call(
&self,
zome_call_unsigned_js: ZomeCallUnsignedNapi,
) -> Result<ZomeCallNapi> {
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);

Expand Down Expand Up @@ -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<JsWeRustHandler> {
let we_rust_handler = WeRustHandler::new(keystore_url, passphrase).await?;

Ok(JsWeRustHandler {
we_rust_handler: Some(we_rust_handler),
}
})
}

#[napi]
Expand Down
36 changes: 23 additions & 13 deletions src/zome_call_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -14,34 +18,40 @@ 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<Self> {
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
pub async fn sign_zome_call(
&self,
zome_call_unsigned_js: ZomeCallUnsignedNapi,
) -> Result<ZomeCallNapi> {
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);

Expand Down Expand Up @@ -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<JsZomeCallSigner> {
let zome_call_signer = ZomeCallSigner::new(connection_url, passphrase).await?;

JsZomeCallSigner {
Ok(JsZomeCallSigner {
zome_call_signer: Some(zome_call_signer),
}
})
}

#[napi]
Expand All @@ -91,7 +101,7 @@ impl JsZomeCallSigner {
) -> Result<ZomeCallNapi> {
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
}
Expand Down

0 comments on commit 6931cc1

Please sign in to comment.