Skip to content

Commit

Permalink
0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg committed Sep 26, 2023
1 parent 3f7e498 commit 1e50f77
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 109 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate napi_build;

fn main() {
napi_build::setup();
napi_build::setup();
}
144 changes: 76 additions & 68 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,100 @@ extern crate napi_derive;

use std::ops::Deref;

use holochain_zome_types::{Signature, ZomeCallUnsigned};
use lair_keystore_api::{dependencies::url::Url, ipc_keystore::ipc_keystore_connect, LairClient};
use napi::Result;
use lair_keystore_api::{ipc_keystore::ipc_keystore_connect, dependencies::url::Url, LairClient};
use holochain_zome_types::{ZomeCallUnsigned, Signature};
use sodoken::BufRead;

mod types;
use types::*;
pub mod utils;

struct ZomeCallSigner {
lair_client: LairClient,
lair_client: LairClient,
}

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();
let passphrase_bufread: BufRead = passphrase.as_bytes().into();

let lair_client = ipc_keystore_connect(connection_url_parsed, passphrase_bufread)
.await
.unwrap();

Self {
lair_client
/// Connect to lair keystore
pub async fn new(connection_url: String, passphrase: String) -> Self {
let connection_url_parsed = Url::parse(connection_url.deref()).unwrap();
let passphrase_bufread: BufRead = passphrase.as_bytes().into();

let lair_client = ipc_keystore_connect(connection_url_parsed, passphrase_bufread)
.await
.unwrap();

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 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 sig = self.lair_client.sign_by_pub_key(
pub_key_2.into(),
None,
data_to_sign)
.await
.unwrap();

let signature = Signature(*sig.0);

let signed_zome_call = ZomeCallNapi {
cell_id: zome_call_unsigned_js.cell_id,
zome_name: zome_call_unsigned.zome_name.to_string(),
fn_name: zome_call_unsigned.fn_name.0,
payload: zome_call_unsigned_js.payload,
cap_secret: zome_call_unsigned_js.cap_secret,
provenance: zome_call_unsigned_js.provenance,
nonce: zome_call_unsigned_js.nonce,
expires_at: zome_call_unsigned_js.expires_at,
signature: signature.0.to_vec()
};

Ok(signed_zome_call)
}
}

/// 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 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 sig = self
.lair_client
.sign_by_pub_key(pub_key_2.into(), None, data_to_sign)
.await
.unwrap();

let signature = Signature(*sig.0);

let signed_zome_call = ZomeCallNapi {
cell_id: zome_call_unsigned_js.cell_id,
zome_name: zome_call_unsigned.zome_name.to_string(),
fn_name: zome_call_unsigned.fn_name.0,
payload: zome_call_unsigned_js.payload,
cap_secret: zome_call_unsigned_js.cap_secret,
provenance: zome_call_unsigned_js.provenance,
nonce: zome_call_unsigned_js.nonce,
expires_at: zome_call_unsigned_js.expires_at,
signature: signature.0.to_vec(),
};

Ok(signed_zome_call)
}
}

#[napi(js_name = "ZomeCallSigner")]
pub struct JsZomeCallSigner {
zome_call_signer: Option<ZomeCallSigner>,
zome_call_signer: Option<ZomeCallSigner>,
}

#[napi]
impl JsZomeCallSigner {
#[napi(constructor)]
pub fn new() -> Self {
Self {
zome_call_signer: None
#[napi(constructor)]
pub fn new() -> Self {
Self {
zome_call_signer: None,
}
}
}

#[napi]
pub async fn connect(connection_url: String, passphrase: String) -> Self {
let zome_call_signer = ZomeCallSigner::new(connection_url, passphrase).await;

JsZomeCallSigner { zome_call_signer: Some(zome_call_signer) }
}

#[napi]
pub async fn sign_zome_call(&self, zome_call_unsigned_js: ZomeCallUnsignedNapi) -> Result<ZomeCallNapi> {
self.zome_call_signer.as_ref().unwrap().sign_zome_call(zome_call_unsigned_js).await
}
}

#[napi]
pub async fn connect(connection_url: String, passphrase: String) -> Self {
let zome_call_signer = ZomeCallSigner::new(connection_url, passphrase).await;

JsZomeCallSigner {
zome_call_signer: Some(zome_call_signer),
}
}

#[napi]
pub async fn sign_zome_call(
&self,
zome_call_unsigned_js: ZomeCallUnsignedNapi,
) -> Result<ZomeCallNapi> {
self.zome_call_signer
.as_ref()
.unwrap()
.sign_zome_call(zome_call_unsigned_js)
.await
}
}
75 changes: 38 additions & 37 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
use kitsune_p2p_timestamp::Timestamp;
use holochain_zome_types::CellId;
use holochain_integrity_types::{ZomeName, FunctionName};
use holo_hash::{DnaHash, AgentPubKey};
use holochain_zome_types::{ExternIO, CapSecret, ZomeCallUnsigned};
use crate::utils::*;
use holo_hash::{AgentPubKey, DnaHash};
use holochain_integrity_types::{FunctionName, ZomeName};
use holochain_zome_types::CellId;
use holochain_zome_types::{CapSecret, ExternIO, ZomeCallUnsigned};
use kitsune_p2p_timestamp::Timestamp;

#[derive(Clone)]
#[napi(object)]
pub struct ZomeCallUnsignedNapi {
pub cell_id: Vec<Vec<u8>>,
pub zome_name: String,
pub fn_name: String,
pub payload: Vec<u8>,
pub cap_secret: Option<Vec<u8>>,
pub provenance: Vec<u8>,
pub nonce: Vec<u8>,
pub expires_at: i64,
pub cell_id: Vec<Vec<u8>>,
pub zome_name: String,
pub fn_name: String,
pub payload: Vec<u8>,
pub cap_secret: Option<Vec<u8>>,
pub provenance: Vec<u8>,
pub nonce: Vec<u8>,
pub expires_at: i64,
}


impl Into<ZomeCallUnsigned> for ZomeCallUnsignedNapi {
fn into(self: Self) -> ZomeCallUnsigned {
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()
),
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(),
nonce: vec_to_arr(self.nonce).into(),
expires_at: Timestamp(self.expires_at)
fn into(self: Self) -> ZomeCallUnsigned {
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(),
),
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(),
nonce: vec_to_arr(self.nonce).into(),
expires_at: Timestamp(self.expires_at),
}
}
}
}

#[napi(object)]
pub struct ZomeCallNapi {
pub cell_id: Vec<Vec<u8>>,
pub zome_name: String,
pub fn_name: String,
pub payload: Vec<u8>,
pub cap_secret: Option<Vec<u8>>,
pub provenance: Vec<u8>,
pub nonce: Vec<u8>,
pub expires_at: i64,
pub signature: Vec<u8>
pub cell_id: Vec<Vec<u8>>,
pub zome_name: String,
pub fn_name: String,
pub payload: Vec<u8>,
pub cap_secret: Option<Vec<u8>>,
pub provenance: Vec<u8>,
pub nonce: Vec<u8>,
pub expires_at: i64,
pub signature: Vec<u8>,
}
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub fn vec_to_arr<T, const N: usize>(v: Vec<T>) -> [T; N] {
v.try_into()
.unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
}
v.try_into()
.unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
}

0 comments on commit 1e50f77

Please # to comment.