Skip to content

Commit

Permalink
Created SecretKey struct + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alanmarkoTrilitech committed Oct 24, 2023
1 parent 76b2570 commit b1aff83
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 146 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 0 additions & 64 deletions jstz_cli/src/accounts/account.rs

This file was deleted.

54 changes: 0 additions & 54 deletions jstz_cli/src/accounts/mod.rs

This file was deleted.

26 changes: 2 additions & 24 deletions jstz_cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};

use crate::accounts::account::Account;
use crate::account::account::Account;

fn home() -> PathBuf {
dirs::home_dir()
Expand All @@ -24,31 +24,9 @@ pub struct AccountConfig {
}

impl AccountConfig {
/*
pub fn new() -> Self {
AccountList {
accounts: HashMap::new(),
}
}
*/

pub fn upsert(&mut self, account: Account) {
self.accounts.insert(account.get_alias().clone(), account);
}

/*
pub fn remove(&mut self, alias: &String) -> Option<Account> {
self.accounts.remove(alias)
}
pub fn get(&self, alias: &String) -> Option<&Account> {
self.accounts.get(alias)
}
pub fn get_all(&self) -> &HashMap<String, Account> {
&self.accounts
self.accounts.insert(account.alias.clone(), account);
}
*/
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions jstz_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use clap::Parser;

mod accounts;
mod account;
mod bridge;
mod config;
mod deploy;
Expand All @@ -23,7 +23,7 @@ enum Command {
Bridge(bridge::Command),
/// Commands related to the account management
#[command(subcommand)]
Account(accounts::Command),
Account(account::Command),
/// Deploys a smart function
Deploy {
/// Address used when deploying the contract
Expand Down Expand Up @@ -63,7 +63,7 @@ fn exec(command: Command, cfg: &mut Config) -> Result<()> {
match command {
Command::Sandbox(sandbox_command) => sandbox::exec(cfg, sandbox_command),
Command::Bridge(bridge_command) => bridge::exec(bridge_command, cfg),
Command::Account(account_command) => accounts::exec(account_command, cfg),
Command::Account(account_command) => account::exec(account_command, cfg),
Command::Deploy {
self_address,
function_code,
Expand Down
1 change: 1 addition & 0 deletions jstz_crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ repository.workspace = true
tezos_crypto_rs = { version = "0.5.0", default-features = false }
derive_more = "0.99.17"
serde = "1.0.185"
anyhow = "1.0.75"
2 changes: 2 additions & 0 deletions jstz_crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ pub use error::{Error, Result};
pub mod hash;
pub mod public_key;
pub mod public_key_hash;
pub mod secret_key;
pub mod signature;
pub mod utils;
2 changes: 1 addition & 1 deletion jstz_crypto/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tezos_crypto_rs::hash::PublicKeyBls;

use crate::error::Result;

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum PublicKey {
Bls(PublicKeyBls),
}
Expand Down
28 changes: 28 additions & 0 deletions jstz_crypto/src/secret_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};
use tezos_crypto_rs::hash::SecretKeyBls;

use crate::error::Result;

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum SecretKey {
Bls(SecretKeyBls),
}

impl SecretKey {
pub fn to_base58(&self) -> String {
let SecretKey::Bls(pk) = self;
pk.to_base58_check()
}

pub fn from_base58(data: &str) -> Result<Self> {
let bls = SecretKeyBls::from_base58_check(data)?;

Ok(SecretKey::Bls(bls))
}
}

impl ToString for SecretKey {
fn to_string(&self) -> String {
self.to_base58()
}
}
12 changes: 12 additions & 0 deletions jstz_crypto/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use anyhow::Result;
use tezos_crypto_rs::bls::keypair_from_ikm;

use crate::{hash::Blake2b, public_key::PublicKey, secret_key::SecretKey};

pub fn keypair_from_passphrase(passphrase: &str) -> Result<(SecretKey, PublicKey)> {
let ikm = Blake2b::from(passphrase.as_bytes());

let (sk, pk) = keypair_from_ikm(*ikm.as_array()).unwrap();

Ok((SecretKey::Bls(sk), PublicKey::Bls(pk)))
}

0 comments on commit b1aff83

Please sign in to comment.