Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decrypt feature #679

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
844 changes: 516 additions & 328 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ thiserror = { version = "1.0" }
tokio = { version = "1.34" }
tracing = { version = "0.1" }
# hack-ink
subcryptor = { version = "0.10" }
subhasher = { version = "0.10" }
submetadatan = { version = "0.10" }
subrpcer = { version = "0.10" }
subruntimer = { version = "0.10" }
subspector = { version = "0.10" }
substorager = { version = "0.10" }
subversioner = { version = "0.10" }
subcryptor = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
subhasher = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
submetadatan = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
subrpcer = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
subruntimer = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
subspector = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
substorager = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
subversioner = { version = "0.10", git = "https://github.com/hack-ink/substrate-minimal" }
# subalfred
cmd-impl = { version = "0.9", path = "bin/subalfred/src/command/impl" }
subalfred-core = { version = "0.9", path = "lib/core" }
subalfred-util = { version = "0.9", path = "lib/util" }

# [patch.crates-io]
# hack-ink
# subcryptor = { path = "../substrate-minimal/subcryptor" }
# subhasher = { path = "../substrate-minimal/subhasher" }
# submetadatan = { path = "../substrate-minimal/submetadatan" }
# subrpcer = { path = "../substrate-minimal/subrpcer" }
# subruntimer = { path = "../substrate-minimal/subruntimer" }
# subspector = { path = "../substrate-minimal/subspector" }
# substorager = { path = "../substrate-minimal/substorager" }
# subversioner = { path = "../substrate-minimal/subversioner" }
# [patch.'https://github.com/hack-ink/substrate-minimal']
# # hack-ink
# subcryptor = { path = "../substrate-minimal/subcryptor" }
# subhasher = { path = "../substrate-minimal/subhasher" }
# submetadatan = { path = "../substrate-minimal/submetadatan" }
# subrpcer = { path = "../substrate-minimal/subrpcer" }
# subruntimer = { path = "../substrate-minimal/subruntimer" }
# subspector = { path = "../substrate-minimal/subspector" }
# substorager = { path = "../substrate-minimal/substorager" }
# subversioner = { path = "../substrate-minimal/subversioner" }

[profile.ci-dev]
incremental = false
Expand Down
7 changes: 4 additions & 3 deletions bin/subalfred/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ anyhow = { version = "1.0" }
array-bytes = { workspace = true }
clap = { workspace = true, features = ["cargo", "derive", "wrap_help"] }
color-eyre = { version = "0.6" }
rpassword = { version = "7.3" }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tracing-subscriber = { version = "0.3" }
unescaper = { version = "0.1" }
# hack-ink
subhasher = { workspace = true }
subrpcer = { workspace = true }
substorager = { workspace = true }
subhasher = { workspace = true }
subrpcer = { workspace = true }
substorager = { workspace = true }
# subalfred
cmd-impl = { workspace = true }
subalfred-core = { workspace = true, features = ["clap"] }
4 changes: 4 additions & 0 deletions bin/subalfred/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use check::CheckCmd;
mod convert;
use convert::ConvertCmd;

mod decrypt;
use decrypt::DecryptCmd;

mod get;
use get::GetCmd;

Expand Down Expand Up @@ -37,6 +40,7 @@ pub(crate) enum Cmd {
Check,
#[command(subcommand)]
Convert,
Decrypt,
#[command(subcommand)]
Get,
Hash,
Expand Down
33 changes: 33 additions & 0 deletions bin/subalfred/src/command/decrypt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// std
use std::path::PathBuf;
// crates.io
use clap::Args;
// subalfred
use crate::prelude::*;
use subalfred_core::{key::KeystoreJson, system};

/// Calculate the storage key of the storage item.
#[derive(Debug, Args)]
pub(crate) struct DecryptCmd {
/// Path to the JSON keystore file.
#[arg(value_name = "PATH")]
path: PathBuf,
}
impl DecryptCmd {
pub(crate) fn run(&self) -> Result<()> {
let Self { path } = self;
let keystore = system::read_file_to_struct::<_, KeystoreJson>(path)?;

loop {
let password = rpassword::prompt_password("Keystore password:")?;

if let Ok(secret_key) = keystore.decrypt(&password) {
println!("{}", array_bytes::bytes2hex("0x", secret_key));

return Ok(());
} else {
println!("Wrong password!");
}
}
}
}
3 changes: 3 additions & 0 deletions lib/core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub enum Error {
#[error(transparent)]
Quick(#[from] Quick),

#[error(transparent)]
Subcryptor(#[from] subcryptor::Error),

#[error(transparent)]
Cargo(#[from] Cargo),
#[error(transparent)]
Expand Down
45 changes: 45 additions & 0 deletions lib/core/src/key/keystore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// crates.io
use serde::Deserialize;
// subalfred
use crate::prelude::*;
use subcryptor::constant::SECRET_KEY_LEN;

/// JSON keystore structure.
#[allow(missing_docs)]
#[derive(Debug, Deserialize)]
pub struct KeystoreJson {
pub encoded: String,
pub encoding: Encoding,
}
impl KeystoreJson {
/// Decrypt the keystore.
pub fn decrypt(&self, passphrase: &str) -> Result<[u8; SECRET_KEY_LEN]> {
Ok(subcryptor::decrypt_keystore(
passphrase.as_bytes(),
&subcryptor::base64_decode(&self.encoded)?,
&self.encoding.r#type,
)?)
}
}
#[test]
fn decrypt_should_work() {
let alice_keystore = r#"{"encoded":"paO3SvbXf/ktnJwuxB5Jn3L5Yxrv9JPhOLA4dplOFu8AgAAAAQAAAAgAAACuIZoAe4sfEiV55sUsZaGq9cCwddaqYdBE7rMSfZfYd7kjgw5YUIo7QB6e+PVY+q6Du8uRSxOOjR5++LTEavlppjbkEce38/MrsDmrKccUqxu+hjv8Rt/RK77btcSuWDm6CA5bgX3y2lKeYqJEidMbP68dSKEN8uOoEdOuCdAFCH1BHCKPhOcu80RNcQ9puZDqZdLoPxc7QDlF9LS5","encoding":{"content":["pkcs8","sr25519"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"address":"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","meta":{"genesisHash":"","name":"Alice","whenCreated":1701855108366}}"#;
let alice_secret_key = "0x98319d4ff8a9508c4bb0cf0b5a78d760a0b2082c02775e6e82370816fedfff48925a225d97aa00682d6a59b95b18780c10d7032336e88f3442b42361f4a66011";

assert_eq!(
array_bytes::bytes2hex(
"0x",
serde_json::from_str::<KeystoreJson>(alice_keystore)
.unwrap()
.decrypt("456123")
.unwrap()
),
alice_secret_key
);
}
/// JSON keystore encoding structure.
#[allow(missing_docs)]
#[derive(Debug, Deserialize)]
pub struct Encoding {
pub r#type: Vec<String>,
}
5 changes: 4 additions & 1 deletion lib/core/src/key/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Subalfred core key library.
//! Subalfred's core key library.

mod keystore;
pub use keystore::*;

// std
use std::{
Expand Down
Loading