hyperware_process_lib 3.0.0

A library for writing Hyperware processes in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Ethereum signer functionality for Hyperware.
//!
//! This module provides low-level cryptographic signing operations for Ethereum,
//! including private key management, message signing, and transaction signing.
//! It separates the cryptographic concerns from the higher-level wallet functionality.

use crate::eth::EthError;
use hex;
use rand::{thread_rng, RngCore};
use serde::{Deserialize, Serialize};
use sha3::{Digest, Sha3_256};
use thiserror::Error;

use alloy::{
    consensus::{SignableTransaction, TxEip1559, TxEnvelope},
    network::eip2718::Encodable2718,
    network::TxSignerSync,
    primitives::TxKind,
    signers::{local::PrivateKeySigner, SignerSync},
};
use alloy_primitives::{Address as EthAddress, B256, U256};
use std::str::FromStr;

// For encryption/decryption
const SALT_SIZE: usize = 16;
const NONCE_SIZE: usize = 12;
const KEY_SIZE: usize = 32;
const TAG_SIZE: usize = 16;

/// Transaction data structure used for signing transactions
#[derive(Debug, Clone)]
pub struct TransactionData {
    /// The recipient address
    pub to: EthAddress,
    /// The amount to send in wei
    pub value: U256,
    /// Optional transaction data (for contract interactions)
    pub data: Option<Vec<u8>>,
    /// The transaction nonce
    pub nonce: u64,
    /// The maximum gas for the transaction
    pub gas_limit: u64,
    /// The gas price in wei
    pub gas_price: u128,
    /// Optional max priority fee (for EIP-1559 transactions)
    pub max_priority_fee: Option<u128>,
    /// The chain ID for the transaction
    pub chain_id: u64,
}

/// Errors that can occur during signing operations
#[derive(Debug, Error)]
pub enum SignerError {
    #[error("failed to generate random bytes: {0}")]
    RandomGenerationError(String),

    #[error("invalid private key format: {0}")]
    InvalidPrivateKey(String),

    #[error("chain ID mismatch: expected {expected}, got {actual}")]
    ChainIdMismatch { expected: u64, actual: u64 },

    #[error("failed to sign transaction or message: {0}")]
    SigningError(String),

    #[error("ethereum error: {0}")]
    EthError(#[from] EthError),

    #[error("encryption error: {0}")]
    EncryptionError(String),

    #[error("decryption error: {0}")]
    DecryptionError(String),
}

/// The storage format for encrypted signers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptedSignerData {
    /// The encrypted private key data
    pub encrypted_data: Vec<u8>,
    /// The Ethereum address (for quick reference without decryption)
    pub address: String,
    /// The chain ID this signer is for
    pub chain_id: u64,
}

/// The Signer trait defines the interface for all signing implementations
pub trait Signer {
    /// Get the Ethereum address associated with this signer
    fn address(&self) -> EthAddress;

    /// Get the chain ID this signer is configured for
    fn chain_id(&self) -> u64;

    /// Sign a transaction with the private key
    fn sign_transaction(&self, tx_data: &TransactionData) -> Result<Vec<u8>, SignerError>;

    /// Sign a message following Ethereum's personal_sign format
    fn sign_message(&self, message: &[u8]) -> Result<Vec<u8>, SignerError>;

    /// Sign a raw hash without any prefix (for EIP-712 and similar)
    fn sign_hash(&self, hash: &[u8]) -> Result<Vec<u8>, SignerError>;
}

/// Local signer implementation using a private key stored in memory
#[derive(Debug, Clone)]
pub struct LocalSigner {
    /// The underlying private key signer from alloy
    pub inner: PrivateKeySigner,

    /// The Ethereum address derived from the private key
    pub address: EthAddress,

    /// The chain ID this signer is configured for
    pub chain_id: u64,

    /// The private key as a hex string
    pub private_key_hex: String,
}

// Manual implementation of Serialize for LocalSigner
impl Serialize for LocalSigner {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;

        // Serialize only the fields we need
        let mut state = serializer.serialize_struct("LocalSigner", 3)?;
        state.serialize_field("address", &self.address)?;
        state.serialize_field("chain_id", &self.chain_id)?;
        state.serialize_field("private_key_hex", &self.private_key_hex)?;
        state.end()
    }
}

// Manual implementation of Deserialize for LocalSigner
impl<'de> Deserialize<'de> for LocalSigner {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct LocalSignerData {
            #[allow(dead_code)]
            address: EthAddress,
            chain_id: u64,
            private_key_hex: String,
        }

        let data = LocalSignerData::deserialize(deserializer)?;

        // Reconstruct the LocalSigner from the private key
        match LocalSigner::from_private_key(&data.private_key_hex, data.chain_id) {
            Ok(signer) => Ok(signer),
            Err(e) => Err(serde::de::Error::custom(format!(
                "Failed to reconstruct signer: {}",
                e
            ))),
        }
    }
}

impl LocalSigner {
    /// Create a new signer with a randomly generated private key
    pub fn new_random(chain_id: u64) -> Result<Self, SignerError> {
        // Generate a secure random private key directly
        let mut rng = thread_rng();
        let mut private_key_bytes = [0u8; 32];
        rng.fill_bytes(&mut private_key_bytes);

        // Make sure the private key is valid (less than curve order)
        // TODO: This is a simplification
        let max_scalar =
            hex::decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140")
                .map_err(|_| {
                    SignerError::RandomGenerationError("Failed to decode max scalar".to_string())
                })?;

        // Simple check: if our random bytes are >= max_scalar, regenerate
        // This is a simplified approach - production code would use more sophisticated comparison
        if private_key_bytes.as_slice().cmp(max_scalar.as_slice()) != std::cmp::Ordering::Less {
            // Try again with a new random value
            rng.fill_bytes(&mut private_key_bytes);
        }

        // Convert to B256 for the PrivateKeySigner
        let key = B256::from_slice(&private_key_bytes);

        // Store the private key hex string for later use
        let private_key_hex = format!("0x{}", hex::encode(private_key_bytes));

        // Create the PrivateKeySigner
        let inner = match PrivateKeySigner::from_bytes(&key) {
            Ok(signer) => signer,
            Err(e) => return Err(SignerError::InvalidPrivateKey(e.to_string())),
        };

        let address = inner.address();

        Ok(Self {
            inner,
            address,
            chain_id,
            private_key_hex,
        })
    }

    /// Create a signer from a private key in hexadecimal string format
    pub fn from_private_key(private_key: &str, chain_id: u64) -> Result<Self, SignerError> {
        // Remove 0x prefix if present
        let clean_key = private_key.trim_start_matches("0x");

        // Parse hex string into bytes
        if clean_key.len() != 64 {
            return Err(SignerError::InvalidPrivateKey(
                "Private key must be 32 bytes (64 hex characters)".to_string(),
            ));
        }

        let key_bytes =
            hex::decode(clean_key).map_err(|e| SignerError::InvalidPrivateKey(e.to_string()))?;

        Self::from_bytes(&key_bytes, chain_id, format!("0x{}", clean_key))
    }

    /// Create a signer from raw private key bytes
    fn from_bytes(
        bytes: &[u8],
        chain_id: u64,
        private_key_hex: String,
    ) -> Result<Self, SignerError> {
        if bytes.len() != 32 {
            return Err(SignerError::InvalidPrivateKey(
                "Private key must be exactly 32 bytes".to_string(),
            ));
        }

        // Convert to B256 (fixed bytes)
        let key = B256::from_slice(bytes);

        // Create the PrivateKeySigner
        let inner = match PrivateKeySigner::from_bytes(&key) {
            Ok(wallet) => wallet,
            Err(e) => return Err(SignerError::InvalidPrivateKey(e.to_string())),
        };

        let address = inner.address();

        Ok(Self {
            inner,
            address,
            chain_id,
            private_key_hex,
        })
    }

    /// Encrypt this signer using a password
    pub fn encrypt(&self, password: &str) -> Result<EncryptedSignerData, SignerError> {
        // Extract the private key hex (without 0x prefix)
        let clean_key = self.private_key_hex.trim_start_matches("0x");

        // Convert to bytes
        let key_bytes =
            hex::decode(clean_key).map_err(|e| SignerError::EncryptionError(e.to_string()))?;

        // Encrypt the private key
        let encrypted_data =
            encrypt_data(&key_bytes, password).map_err(|e| SignerError::EncryptionError(e))?;

        // Create encrypted data structure
        Ok(EncryptedSignerData {
            encrypted_data,
            address: self.address.to_string(),
            chain_id: self.chain_id,
        })
    }

    /// Decrypt an encrypted signer
    pub fn decrypt(encrypted: &EncryptedSignerData, password: &str) -> Result<Self, SignerError> {
        let decrypted_bytes = decrypt_data(&encrypted.encrypted_data, password)
            .map_err(|e| SignerError::DecryptionError(e))?;

        // Convert bytes back to hex string
        let private_key_hex = format!("0x{}", hex::encode(&decrypted_bytes));

        // Create a new signer with the specified chain ID
        Self::from_bytes(&decrypted_bytes, encrypted.chain_id, private_key_hex)
    }

    /// Export the private key as a hexadecimal string
    pub fn export_private_key(&self) -> String {
        self.private_key_hex.clone()
    }
}

impl Signer for LocalSigner {
    fn address(&self) -> EthAddress {
        self.address
    }

    fn chain_id(&self) -> u64 {
        self.chain_id
    }

    fn sign_transaction(&self, tx_data: &TransactionData) -> Result<Vec<u8>, SignerError> {
        // Verify chain ID matches the signer's chain ID
        if tx_data.chain_id != self.chain_id {
            return Err(SignerError::ChainIdMismatch {
                expected: self.chain_id,
                actual: tx_data.chain_id,
            });
        }

        // Convert hyperware types to alloy types
        let to_str = tx_data.to.to_string();
        let to = alloy_primitives::Address::from_str(&to_str)
            .map_err(|e| SignerError::SigningError(format!("Invalid contract address: {}", e)))?;

        // Create transaction based on chain type
        // Both Ethereum mainnet and Base use EIP-1559 transactions
        let mut tx = TxEip1559 {
            chain_id: tx_data.chain_id,
            nonce: tx_data.nonce,
            to: TxKind::Call(to),
            gas_limit: tx_data.gas_limit,
            max_fee_per_gas: tx_data.gas_price,
            // Use provided priority fee or calculate a reasonable default based on the chain
            max_priority_fee_per_gas: tx_data.max_priority_fee.unwrap_or_else(|| {
                match tx_data.chain_id {
                    // Ethereum mainnet (1)
                    1 => tx_data.gas_price / 10,
                    // Base (8453) - typically accepts lower priority fees
                    8453 => tx_data.gas_price / 5,
                    // Default fallback for other networks
                    _ => tx_data.gas_price / 10,
                }
            }),
            input: tx_data.data.clone().unwrap_or_default().into(),
            value: tx_data.value,
            ..Default::default()
        };

        // Sign the transaction with the wallet
        let sig = match self.inner.sign_transaction_sync(&mut tx) {
            Ok(sig) => sig,
            Err(e) => return Err(SignerError::SigningError(e.to_string())),
        };

        // Create signed transaction envelope
        let signed = TxEnvelope::from(tx.into_signed(sig));

        // Encode the transaction
        let mut buf = vec![];
        signed.encode_2718(&mut buf);

        Ok(buf)
    }

    fn sign_message(&self, message: &[u8]) -> Result<Vec<u8>, SignerError> {
        // Create the Ethereum signed message prefixed hash
        let prefix = format!("\x19Ethereum Signed Message:\n{}", message.len());
        let prefixed_message = [prefix.as_bytes(), message].concat();

        // Hash the message
        let hash = sha3::Keccak256::digest(&prefixed_message);
        let hash_bytes = B256::from_slice(hash.as_slice());

        // Sign the hash
        match self.inner.sign_hash_sync(&hash_bytes) {
            Ok(signature) => Ok(signature.as_bytes().to_vec()),
            Err(e) => Err(SignerError::SigningError(e.to_string())),
        }
    }

    fn sign_hash(&self, hash: &[u8]) -> Result<Vec<u8>, SignerError> {
        // Sign a raw hash without any prefix
        if hash.len() != 32 {
            return Err(SignerError::SigningError(
                "Hash must be exactly 32 bytes".to_string(),
            ));
        }

        let hash_bytes = B256::from_slice(hash);

        // Sign the hash directly
        match self.inner.sign_hash_sync(&hash_bytes) {
            Ok(signature) => Ok(signature.as_bytes().to_vec()),
            Err(e) => Err(SignerError::SigningError(e.to_string())),
        }
    }
}

/// Encrypt a private key using a password
pub fn encrypt_data(data: &[u8], password: &str) -> Result<Vec<u8>, String> {
    let mut rng = thread_rng();

    // Generate salt
    let mut salt = [0u8; SALT_SIZE];
    rng.fill_bytes(&mut salt);

    // Generate nonce
    let mut nonce = [0u8; NONCE_SIZE];
    rng.fill_bytes(&mut nonce);

    // Derive key using SHA3
    let key = derive_key(password.as_bytes(), &salt);

    // Encrypt data using XOR
    let encrypted_data = encrypt_with_key(data, &key, &nonce);

    // Generate authentication tag
    let tag = compute_tag(&salt, &nonce, &encrypted_data, &key);

    // Combine all components
    Ok([
        salt.as_ref(),
        nonce.as_ref(),
        encrypted_data.as_ref(),
        tag.as_ref(),
    ]
    .concat())
}

/// Decrypt an encrypted private key
pub fn decrypt_data(encrypted_data: &[u8], password: &str) -> Result<Vec<u8>, String> {
    // Check if data is long enough to contain all components
    if encrypted_data.len() < SALT_SIZE + NONCE_SIZE + TAG_SIZE {
        return Err("Encrypted data is too short".into());
    }

    // Extract components
    let salt = &encrypted_data[..SALT_SIZE];
    let nonce = &encrypted_data[SALT_SIZE..SALT_SIZE + NONCE_SIZE];
    let tag = &encrypted_data[encrypted_data.len() - TAG_SIZE..];
    let ciphertext = &encrypted_data[SALT_SIZE + NONCE_SIZE..encrypted_data.len() - TAG_SIZE];

    // Derive key
    let key = derive_key(password.as_bytes(), salt);

    // Verify the authentication tag
    let expected_tag = compute_tag(salt, nonce, ciphertext, &key);
    if tag != expected_tag {
        return Err("Decryption failed: Authentication tag mismatch".into());
    }

    // Decrypt data
    let plaintext = decrypt_with_key(ciphertext, &key, nonce);

    Ok(plaintext)
}

/// Derive a key from a password and salt using SHA3
fn derive_key(password: &[u8], salt: &[u8]) -> [u8; KEY_SIZE] {
    // Initial hash
    let mut hasher = Sha3_256::new();
    hasher.update(salt);
    hasher.update(password);
    let mut key = hasher.finalize().into();

    // Multiple iterations for stronger key derivation
    for _ in 0..10000 {
        let mut hasher = Sha3_256::new();
        hasher.update(key);
        hasher.update(salt);
        key = hasher.finalize().into();
    }

    key
}

/// Encrypt data with a key and nonce using XOR
fn encrypt_with_key(data: &[u8], key: &[u8; KEY_SIZE], nonce: &[u8]) -> Vec<u8> {
    let mut result = Vec::with_capacity(data.len());

    for (i, &byte) in data.iter().enumerate() {
        // Create a unique keystream byte for each position
        let key_byte = key[i % key.len()];
        let nonce_byte = nonce[i % nonce.len()];
        let keystream = key_byte ^ nonce_byte ^ (i as u8);

        // XOR with data
        result.push(byte ^ keystream);
    }

    result
}

/// Decrypt data (same as encrypt since XOR is symmetric)
fn decrypt_with_key(data: &[u8], key: &[u8; KEY_SIZE], nonce: &[u8]) -> Vec<u8> {
    encrypt_with_key(data, key, nonce)
}

/// Compute an authentication tag using SHA3
fn compute_tag(salt: &[u8], nonce: &[u8], data: &[u8], key: &[u8]) -> Vec<u8> {
    let mut hasher = Sha3_256::new();
    hasher.update(salt);
    hasher.update(nonce);
    hasher.update(data);
    hasher.update(key);

    let hash = hasher.finalize();
    hash[..TAG_SIZE].to_vec()
}