Skip to main content

EnigmaUtils

Struct EnigmaUtils 

Source
pub struct EnigmaUtils { /* private fields */ }
Expand description

Use to encrypt/decrypt messages related to the compute module.

Implementations§

Source§

impl EnigmaUtils

Source

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");
Source

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);
Source

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();
Source

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");
Source

pub fn get_seed(&self) -> [u8; 32]

Get the seed used to derive the key pair.

Source

pub fn get_pubkey(&self) -> [u8; 32]

Get the public key of the derived key pair.

Source

pub fn get_tx_encryption_key(&self, nonce: &[u8; 32]) -> [u8; 32]

Get the encryption key for a specific tx, based on the nonce.

Trait Implementations§

Source§

impl Clone for EnigmaUtils

Source§

fn clone(&self) -> EnigmaUtils

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EnigmaUtils

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>>
where M: 'async_trait + Serialize + Sync, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Encrypts a message for a specific contract using the associated code hash.
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,

Decrypts a message returned by the enclave.
Source§

fn get_pubkey<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = [u8; 32]> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get the public key of the derived key pair.
Source§

fn get_tx_encryption_key<'life0, 'life1, 'async_trait>( &'life0 self, nonce: &'life1 [u8; 32], ) -> Pin<Box<dyn Future<Output = [u8; 32]> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Get the encryption key for a specific tx, based on the nonce.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more