From afd4197fce7a2104a4014f272f03a52690365d28 Mon Sep 17 00:00:00 2001 From: "Dr. Maxim Orlovsky" Date: Tue, 22 Nov 2022 14:38:08 +0100 Subject: [PATCH] WIP on encryption with command-line utility --- Cargo.lock | 1 + Cargo.toml | 3 ++- bech32/src/lib.rs | 2 +- src/bin/lnpbp.rs | 25 ++++++++++++++++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5b3507f..47abf52f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -561,6 +561,7 @@ dependencies = [ "lnpbp_chain", "lnpbp_elgamal", "lnpbp_identity", + "secp256k1 0.24.1", "serde", "serde_json", "serde_with", diff --git a/Cargo.toml b/Cargo.toml index dd85bb9a..0cef5a9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ required-features = ["cli"] [dependencies] amplify = { version = "3.13.0", features = ["stringly_conversions", "std"] } strict_encoding = "2.0.0-alpha.2" +secp256k1 = { version = "0.24.1", features = ["global-context"], optional = true } lnpbp_bech32 = { version = "0.9.0-alpha.1", path = "bech32" } lnpbp_chain = { version = "0.9.0-alpha.1", path = "chain" } lnpbp_elgamal = { version = "0.9.0", path = "elgamal", optional = true } @@ -42,7 +43,7 @@ colorize = { version = "0.1.0", optional = true } # Used by cli only [features] default = ["zip"] all = ["serde", "elgamal", "identity", "zip", "cli"] -cli = ["clap", "serde", "identity", "base64-compat", "base58", "serde_yaml", "serde_json", "amplify/hex", "colorize"] +cli = ["clap", "serde", "identity", "base64-compat", "base58", "serde_yaml", "serde_json", "amplify/hex", "colorize", "secp256k1"] serde = ["serde_crate", "serde_with", "amplify/serde", "lnpbp_bech32/serde", "lnpbp_chain/serde"] identity = ["lnpbp_identity"] diff --git a/bech32/src/lib.rs b/bech32/src/lib.rs index 5a9851bd..ae40f37c 100644 --- a/bech32/src/lib.rs +++ b/bech32/src/lib.rs @@ -107,7 +107,7 @@ impl From for Error { } } -/// Type for wrapping Vec data in cases you need to do a convenient +/// Type for wrapping `Vec` data in cases you need to do a convenient /// enum variant display derives with `#[display(inner)]` #[cfg_attr( feature = "serde", diff --git a/src/bin/lnpbp.rs b/src/bin/lnpbp.rs index 4a2671a5..a031dc05 100644 --- a/src/bin/lnpbp.rs +++ b/src/bin/lnpbp.rs @@ -34,6 +34,7 @@ use lnpbp::{bech32, id}; use lnpbp_identity::{ EcAlgo, IdentityCert, IdentitySigner, SigCert, VerifyError, }; +use secp256k1::SECP256K1; use serde::Serialize; use strict_encoding::{StrictDecode, StrictEncode}; @@ -427,7 +428,29 @@ fn main() -> Result<(), Error> { sig.verify(&cert, data)?; println!("{}", "Signature is valid".green()); } - Command::Identity(_) => todo!("elgamal encryption support"), + Command::Identity(IdentityCommand::Encrypt { + armor, + identity_file, + cert, + message, + src_file, + dst_file, + }) => { + let fd = fs::File::open(identity_file)?; + let id = IdentitySigner::strict_decode(fd)?; + let mut input = file_str_or_stdin(src_file, message)?; + let mut data = vec![]; + input.read_to_end(&mut data)?; + lnpbp_elgamal::encrypt(SECP256K1, &data, cert.pubkey())?; + } + Command::Identity(IdentityCommand::Decrypt { + armor, + identity_file, + cert, + message, + src_file, + dst_file, + }) => {} Command::Convert { data, from,