Skip to content

Commit

Permalink
feat(jstzd): add gen keys for client
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Oct 2, 2024
1 parent ff43399 commit ec05ff1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
37 changes: 36 additions & 1 deletion crates/jstzd/src/task/octez_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{directory::Directory, endpoint::Endpoint, octez_node::DEFAULT_RPC_EN
use anyhow::{anyhow, bail, Result};
use http::Uri;
use std::path::Path;
use std::{ffi::OsStr, path::PathBuf, str::FromStr};
use std::{ffi::OsStr, fmt, path::PathBuf, str::FromStr};
use tempfile::tempdir;
use tokio::process::{Child, Command};

Expand Down Expand Up @@ -76,6 +76,25 @@ impl OctezClientBuilder {
}
}

#[derive(Debug)]
pub enum Signature {
ED25519,
SECP256K1,
P256,
BLS,
}

impl fmt::Display for Signature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Signature::ED25519 => write!(f, "ed25519"),
Signature::SECP256K1 => write!(f, "secp256k1"),
Signature::P256 => write!(f, "p256"),
Signature::BLS => write!(f, "bls"),
}
}
}

#[derive(Debug)]
pub struct OctezClient {
binary_path: PathBuf,
Expand Down Expand Up @@ -121,6 +140,22 @@ impl OctezClient {
.await?;
Ok(())
}

pub async fn gen_keys(
&self,
alias: &str,
signature: Option<Signature>,
) -> Result<()> {
let mut command = self.command(["gen", "keys", alias])?;
if let Some(signature) = signature {
command.args(["--sig", &signature.to_string()]);
}
let status = command.spawn()?.wait().await?;
match status.code() {
Some(0) => Ok(()),
_ => bail!("failed to generate keys for {}", alias),
}
}
}

#[cfg(test)]
Expand Down
47 changes: 46 additions & 1 deletion crates/jstzd/tests/octez_client_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
use jstzd::task::{endpoint::Endpoint, octez_client::OctezClientBuilder};
use serde_json::Value;
use std::fs::{read_to_string, remove_file};
use std::{
fs::{read_to_string, remove_file},
path::Path,
};
use tempfile::{NamedTempFile, TempDir};

fn read_file(path: &Path) -> Value {
serde_json::from_str(&read_to_string(path).expect("Unable to read file"))
.expect("Unable to parse JSON")
}

fn first_item(json: Value) -> Value {
json.as_array().unwrap()[0].clone()
}

#[tokio::test]
async fn config_init() {
let temp_dir = TempDir::new().unwrap();
Expand All @@ -26,3 +38,36 @@ async fn config_init() {
);
assert_eq!(actual["endpoint"], expected_endpoint.to_string());
}

#[tokio::test]
async fn generates_keys() {
let temp_dir = TempDir::new().unwrap();
let base_dir = temp_dir.path().to_path_buf();
let octez_client = OctezClientBuilder::new()
.set_base_dir(base_dir.clone())
.build()
.unwrap();
let alias = "test_alias".to_string();
let res = octez_client.gen_keys(&alias, None).await;
assert!(res.is_ok());
let hashes = first_item(read_file(&base_dir.join("public_key_hashs")));
let pub_keys = first_item(read_file(&base_dir.join("public_keys")));
let secret_keys = first_item(read_file(&base_dir.join("secret_keys")));
assert_eq!(hashes["name"], alias);
assert_eq!(pub_keys["name"], alias);
assert_eq!(secret_keys["name"], alias);
}

#[tokio::test]
async fn generates_keys_throws() {
let temp_dir = TempDir::new().unwrap();
let base_dir = temp_dir.path().to_path_buf();
let octez_client = OctezClientBuilder::new()
.set_base_dir(base_dir.clone())
.build()
.unwrap();
let alias = "test_alias".to_string();
let _ = octez_client.gen_keys(&alias, None).await;
let res = octez_client.gen_keys(&alias, None).await;
assert!(res.is_err_and(|e| e.to_string().contains("failed to generate key")));
}

0 comments on commit ec05ff1

Please sign in to comment.