Simplify and Refactor Asymmetric Encryption/Decryption #10
@ -11,8 +11,8 @@ use std::str::FromStr;
|
|||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
|
|
||||||
use crate::vault::ethereum::contract_utils::{convert_token_to_rhai, prepare_function_arguments};
|
use crate::vault::ethereum;
|
||||||
use crate::vault::{ethereum, keypair};
|
use crate::vault::keyspace::session_manager as keypair;
|
||||||
|
|
||||||
use crate::vault::symmetric::implementation as symmetric_impl;
|
use crate::vault::symmetric::implementation as symmetric_impl;
|
||||||
// Global Tokio runtime for blocking async operations
|
// Global Tokio runtime for blocking async operations
|
||||||
@ -83,7 +83,7 @@ fn load_key_space(name: &str, password: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_key_space(name: &str, password: &str) -> bool {
|
fn create_key_space(name: &str, password: &str) -> bool {
|
||||||
match keypair::session_manager::create_space(name) {
|
match keypair::create_space(name) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
// Get the current space
|
// Get the current space
|
||||||
match keypair::get_current_space() {
|
match keypair::get_current_space() {
|
||||||
@ -763,7 +763,7 @@ fn call_contract_read(contract_json: &str, function_name: &str, args: rhai::Arra
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Prepare the arguments
|
// Prepare the arguments
|
||||||
let tokens = match prepare_function_arguments(&contract.abi, function_name, &args) {
|
let tokens = match ethereum::prepare_function_arguments(&contract.abi, function_name, &args) {
|
||||||
Ok(tokens) => tokens,
|
Ok(tokens) => tokens,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Error preparing arguments: {}", e);
|
log::error!("Error preparing arguments: {}", e);
|
||||||
@ -793,7 +793,7 @@ fn call_contract_read(contract_json: &str, function_name: &str, args: rhai::Arra
|
|||||||
match rt.block_on(async {
|
match rt.block_on(async {
|
||||||
ethereum::call_read_function(&contract, &provider, function_name, tokens).await
|
ethereum::call_read_function(&contract, &provider, function_name, tokens).await
|
||||||
}) {
|
}) {
|
||||||
Ok(result) => convert_token_to_rhai(&result),
|
Ok(result) => ethereum::convert_token_to_rhai(&result),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to call contract function: {}", e);
|
log::error!("Failed to call contract function: {}", e);
|
||||||
Dynamic::UNIT
|
Dynamic::UNIT
|
||||||
@ -818,7 +818,7 @@ fn call_contract_write(contract_json: &str, function_name: &str, args: rhai::Arr
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Prepare the arguments
|
// Prepare the arguments
|
||||||
let tokens = match prepare_function_arguments(&contract.abi, function_name, &args) {
|
let tokens = match ethereum::prepare_function_arguments(&contract.abi, function_name, &args) {
|
||||||
Ok(tokens) => tokens,
|
Ok(tokens) => tokens,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Error preparing arguments: {}", e);
|
log::error!("Error preparing arguments: {}", e);
|
||||||
|
@ -4,12 +4,12 @@ use ethers::prelude::*;
|
|||||||
use ethers::signers::{LocalWallet, Signer, Wallet};
|
use ethers::signers::{LocalWallet, Signer, Wallet};
|
||||||
use ethers::utils::hex;
|
use ethers::utils::hex;
|
||||||
use k256::ecdsa::SigningKey;
|
use k256::ecdsa::SigningKey;
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use sha2::{Sha256, Digest};
|
|
||||||
|
|
||||||
use crate::vault::error::CryptoError;
|
|
||||||
use crate::vault::keypair::KeyPair;
|
|
||||||
use super::networks::NetworkConfig;
|
use super::networks::NetworkConfig;
|
||||||
|
use crate::vault;
|
||||||
|
use crate::vault::error::CryptoError;
|
||||||
|
|
||||||
/// An Ethereum wallet derived from a keypair.
|
/// An Ethereum wallet derived from a keypair.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -21,7 +21,10 @@ pub struct EthereumWallet {
|
|||||||
|
|
||||||
impl EthereumWallet {
|
impl EthereumWallet {
|
||||||
/// Creates a new Ethereum wallet from a keypair for a specific network.
|
/// Creates a new Ethereum wallet from a keypair for a specific network.
|
||||||
pub fn from_keypair(keypair: &KeyPair, network: NetworkConfig) -> Result<Self, CryptoError> {
|
pub fn from_keypair(
|
||||||
|
keypair: &vault::keyspace::keypair_types::KeyPair,
|
||||||
|
network: NetworkConfig,
|
||||||
|
) -> Result<Self, CryptoError> {
|
||||||
// Get the private key bytes from the keypair
|
// Get the private key bytes from the keypair
|
||||||
let private_key_bytes = keypair.signing_key.to_bytes();
|
let private_key_bytes = keypair.signing_key.to_bytes();
|
||||||
|
|
||||||
@ -44,7 +47,11 @@ impl EthereumWallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new Ethereum wallet from a name and keypair (deterministic derivation) for a specific network.
|
/// Creates a new Ethereum wallet from a name and keypair (deterministic derivation) for a specific network.
|
||||||
pub fn from_name_and_keypair(name: &str, keypair: &KeyPair, network: NetworkConfig) -> Result<Self, CryptoError> {
|
pub fn from_name_and_keypair(
|
||||||
|
name: &str,
|
||||||
|
keypair: &vault::keyspace::keypair_types::KeyPair,
|
||||||
|
network: NetworkConfig,
|
||||||
|
) -> Result<Self, CryptoError> {
|
||||||
// Get the private key bytes from the keypair
|
// Get the private key bytes from the keypair
|
||||||
let private_key_bytes = keypair.signing_key.to_bytes();
|
let private_key_bytes = keypair.signing_key.to_bytes();
|
||||||
|
|
||||||
@ -73,7 +80,10 @@ impl EthereumWallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new Ethereum wallet from a private key for a specific network.
|
/// Creates a new Ethereum wallet from a private key for a specific network.
|
||||||
pub fn from_private_key(private_key: &str, network: NetworkConfig) -> Result<Self, CryptoError> {
|
pub fn from_private_key(
|
||||||
|
private_key: &str,
|
||||||
|
network: NetworkConfig,
|
||||||
|
) -> Result<Self, CryptoError> {
|
||||||
// Remove 0x prefix if present
|
// Remove 0x prefix if present
|
||||||
let private_key_clean = private_key.trim_start_matches("0x");
|
let private_key_clean = private_key.trim_start_matches("0x");
|
||||||
|
|
||||||
@ -99,7 +109,9 @@ impl EthereumWallet {
|
|||||||
|
|
||||||
/// Signs a message with the Ethereum wallet.
|
/// Signs a message with the Ethereum wallet.
|
||||||
pub async fn sign_message(&self, message: &[u8]) -> Result<String, CryptoError> {
|
pub async fn sign_message(&self, message: &[u8]) -> Result<String, CryptoError> {
|
||||||
let signature = self.wallet.sign_message(message)
|
let signature = self
|
||||||
|
.wallet
|
||||||
|
.sign_message(message)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| CryptoError::SignatureFormatError(e.to_string()))?;
|
.map_err(|e| CryptoError::SignatureFormatError(e.to_string()))?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user