pub struct EnigmaUtils { /* private fields */ }Expand description
Use to encrypt/decrypt messages related to the compute module.
Implementations§
Source§impl EnigmaUtils
impl EnigmaUtils
Sourcepub fn new(seed: Option<[u8; 32]>, chain_id: &str) -> Result<Self, Error>
pub fn new(seed: Option<[u8; 32]>, chain_id: &str) -> Result<Self, Error>
Creates a new EnigmaUtils instance with a seed and chain ID.
The chain_id is used to determine the appropriate IO public key.
If no seed is provided, a random seed will be generated.
§Examples
use secretrs::utils::EnigmaUtils;
let utils = EnigmaUtils::new(None, "secret-4").expect("Failed to create EnigmaUtils");Sourcepub fn from_io_key(
seed: Option<[u8; 32]>,
consensus_io_pubkey: [u8; 32],
) -> Self
pub fn from_io_key( seed: Option<[u8; 32]>, consensus_io_pubkey: [u8; 32], ) -> Self
Creates a new EnigmaUtils instance with a seed and IO public key.
The consensus_io_pubkey is manually provided instead of being derived from a chain ID.
If no seed is provided, a random seed will be generated.
§Examples
let mut registration = RegistrationQueryClient::connect("GRPC_URL").await?;
let enclave_key_bytes = registration.tx_key(()).await?.into_inner().key;
let mut io_key = [0u8; 32];
io_key.copy_from_slice(&enclave_key_bytes[0..32]);
let utils = EnigmaUtils::from_io_key(None, io_key);Sourcepub fn encrypt<M: Serialize>(
&self,
contract_code_hash: &str,
msg: &M,
) -> Result<SecretMsg, Error>
pub fn encrypt<M: Serialize>( &self, contract_code_hash: &str, msg: &M, ) -> Result<SecretMsg, Error>
Encrypts a message for a specific contract using the associated code hash.
The message must implement serde::Serialize. A 64-character hexadecimal string representing the code hash is prepended to the message before encryption. The resulting SecretMsg is a concatenation of nonce, public key, and ciphertext.
§Examples
use secretrs::utils::EnigmaUtils;
let utils = EnigmaUtils::new(None, "secret-4").expect("Failed to create EnigmaUtils");
let contract_code_hash = "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
let msg = QueryMsg::HelloWorld;
let encrypted_msg = utils.encrypt(contract_code_hash, &msg).expect("Encryption failed");
let (nonce, pubkey, ciphertext) = encrypted_msg.into_parts();Sourcepub fn decrypt(
&self,
nonce: &[u8; 32],
ciphertext: &[u8],
) -> Result<Vec<u8>, Error>
pub fn decrypt( &self, nonce: &[u8; 32], ciphertext: &[u8], ) -> Result<Vec<u8>, Error>
Decrypts a message returned by the enclave.
This function decrypts responses using the same nonce that was used to encrypt the request.
§Examples
let decrypted_bytes = utils.decrypt(&nonce, &response.data).expect("Decryption failed");
let decrypted_b64_string = String::from_utf8(decrypted_bytes).expect("Decoding error");
let decoded_bytes = BASE64_STANDARD.decode(decrypted_b64_string).expect("Decoding error");
let data = String::from_utf8(decoded_bytes).expect("Decoding error");Sourcepub fn get_pubkey(&self) -> [u8; 32]
pub fn get_pubkey(&self) -> [u8; 32]
Get the public key of the derived key pair.
Trait Implementations§
Source§impl Clone for EnigmaUtils
impl Clone for EnigmaUtils
Source§fn clone(&self) -> EnigmaUtils
fn clone(&self) -> EnigmaUtils
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for EnigmaUtils
impl Debug for EnigmaUtils
Source§impl SecretUtils for EnigmaUtils
impl SecretUtils for EnigmaUtils
Source§fn encrypt<'life0, 'life1, 'life2, 'async_trait, M>(
&'life0 self,
contract_code_hash: &'life1 str,
msg: &'life2 M,
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'async_trait>>
fn encrypt<'life0, 'life1, 'life2, 'async_trait, M>( &'life0 self, contract_code_hash: &'life1 str, msg: &'life2 M, ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'async_trait>>
Source§fn decrypt<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
nonce: &'life1 [u8; 32],
ciphertext: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn decrypt<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
nonce: &'life1 [u8; 32],
ciphertext: &'life2 [u8],
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Auto Trait Implementations§
impl Freeze for EnigmaUtils
impl RefUnwindSafe for EnigmaUtils
impl Send for EnigmaUtils
impl Sync for EnigmaUtils
impl Unpin for EnigmaUtils
impl UnsafeUnpin for EnigmaUtils
impl UnwindSafe for EnigmaUtils
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request