mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 21:37:56 +00:00
Encryption support for the statement store (#14440)
* Added ECIES encryption * tweaks * fmt * Make clippy happy * Use local keystore * qed
This commit is contained in:
@@ -49,6 +49,8 @@ pub use store_api::{
|
||||
Error, NetworkPriority, Result, StatementSource, StatementStore, SubmitResult,
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod ecies;
|
||||
pub mod runtime_api;
|
||||
#[cfg(feature = "std")]
|
||||
mod store_api;
|
||||
@@ -61,12 +63,17 @@ mod sr25519 {
|
||||
pub type Public = app_sr25519::Public;
|
||||
}
|
||||
|
||||
mod ed25519 {
|
||||
/// Statement-store specific ed25519 crypto primitives.
|
||||
pub mod ed25519 {
|
||||
mod app_ed25519 {
|
||||
use sp_application_crypto::{app_crypto, ed25519, key_types::STATEMENT};
|
||||
app_crypto!(ed25519, STATEMENT);
|
||||
}
|
||||
/// Statement-store specific ed25519 public key.
|
||||
pub type Public = app_ed25519::Public;
|
||||
/// Statement-store specific ed25519 key pair.
|
||||
#[cfg(feature = "std")]
|
||||
pub type Pair = app_ed25519::Pair;
|
||||
}
|
||||
|
||||
mod ecdsa {
|
||||
@@ -507,6 +514,28 @@ impl Statement {
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Encrypt give data with given key and store both in the statements.
|
||||
#[cfg(feature = "std")]
|
||||
pub fn encrypt(
|
||||
&mut self,
|
||||
data: &[u8],
|
||||
key: &sp_core::ed25519::Public,
|
||||
) -> core::result::Result<(), ecies::Error> {
|
||||
let encrypted = ecies::encrypt_ed25519(key, data)?;
|
||||
self.data = Some(encrypted);
|
||||
self.decryption_key = Some((*key).into());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decrypt data (if any) with the given private key.
|
||||
#[cfg(feature = "std")]
|
||||
pub fn decrypt_private(
|
||||
&self,
|
||||
key: &sp_core::ed25519::Pair,
|
||||
) -> core::result::Result<Option<Vec<u8>>, ecies::Error> {
|
||||
self.data.as_ref().map(|d| ecies::decrypt_ed25519(key, d)).transpose()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -615,4 +644,18 @@ mod test {
|
||||
statement.remove_proof();
|
||||
assert_eq!(statement.verify_signature(), SignatureVerificationResult::NoSignature);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encrypt_decrypt() {
|
||||
let mut statement = Statement::new();
|
||||
let (pair, _) = sp_core::ed25519::Pair::generate();
|
||||
let plain = b"test data".to_vec();
|
||||
|
||||
//let sr25519_kp = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
|
||||
statement.encrypt(&plain, &pair.public()).unwrap();
|
||||
assert_ne!(plain.as_slice(), statement.data().unwrap().as_slice());
|
||||
|
||||
let decrypted = statement.decrypt_private(&pair).unwrap();
|
||||
assert_eq!(decrypted, Some(plain));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user