ether_link/
lib.rs

1pub mod wallet;
2pub use wallet::Wallet;
3
4pub mod signature;
5pub use signature::EthereumSignature;
6
7//pub mod encryption;
8//pub mod types;
9
10
11//pub use encryption::encrypt_message_with_hex_pubkey;
12//pub use signature::sign_message;
13//pub use types::{EncryptedMessage, SignaturePayload};
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18    use hex;
19
20    #[tokio::test]
21    async fn test_sign_message() {
22        let wallet = Wallet::new();
23        let message = "Hello, world!";
24        let signature = wallet.sign_message(message).await.unwrap_or_else(|e| panic!("Failed to sign message: {}", e));
25        
26        // Parse the signature JSON string
27        let sig_json: serde_json::Value = serde_json::from_str(&signature).unwrap_or_else(|e| panic!("Failed to parse signature JSON: {}", e));
28        println!("Signature JSON: {}", serde_json::to_string_pretty(&sig_json).unwrap_or_else(|e| panic!("Failed to format JSON: {}", e)));
29        
30        // Verify the signature contains all expected fields
31        assert!(sig_json.get("address").is_some());
32        assert!(sig_json.get("signed_message").is_some());
33        assert!(sig_json.get("signature").is_some());
34    }
35
36    #[test]
37    fn test_eth_keypair_generation() {
38        let wallet = Wallet::new();
39
40        // Check that the address is 42 chars: "0x" + 40 hex digits
41        assert_eq!(wallet.address.len(), 42);
42        assert!(wallet.address.starts_with("0x"));
43
44        // Check compressed key format (hex string should be 66 chars - 2 for each byte of the 33 byte key)
45        assert_eq!(wallet.pubkey_compressed.len(), 66);
46        
47        // Convert hex to bytes for byte checking
48        let compressed_bytes = hex::decode(&wallet.pubkey_compressed).unwrap();
49        assert!(
50            compressed_bytes[0] == 0x02 || compressed_bytes[0] == 0x03,
51            "Compressed pubkey should start with 0x02 or 0x03"
52        );
53
54        // Check uncompressed key format (hex string should be 130 chars - 2 for each byte of the 65 byte key)
55        assert_eq!(wallet.pubkey_uncompressed.len(), 130);
56        
57        // Convert hex to bytes for byte checking
58        let uncompressed_bytes = hex::decode(&wallet.pubkey_uncompressed).unwrap();
59        assert_eq!(uncompressed_bytes[0], 0x04, 
60            "Uncompressed pubkey should start with 0x04");
61
62        // Debug output
63        println!("Private Key: {}", wallet.privkey);
64        println!("Compressed Public Key: {}", wallet.pubkey_compressed);
65        println!("Uncompressed Public Key: {}", wallet.pubkey_uncompressed);
66        println!("Ethereum Address: {}", wallet.address);
67    }
68
69    #[tokio::test]
70    async fn test_check_signature_is_valid() {
71        let wallet = Wallet::new();
72        let message = "Hello, world!";
73        let signature_json = wallet.sign_message(&message).await.unwrap_or_else(|e| panic!("Failed to sign message: {}", e));
74        
75        // Parse the signature JSON to extract the components
76        let sig_json: serde_json::Value = serde_json::from_str(&signature_json).unwrap_or_else(|e| panic!("Failed to parse signature JSON: {}", e));
77        let address = sig_json["address"].as_str().unwrap().to_string();
78        let signed_message = sig_json["signed_message"].as_str().unwrap().to_string();
79        let signature = sig_json["signature"].as_str().unwrap().to_string();
80
81        println!("Address: {}", address);
82        println!("Signed Message: {}", signed_message);
83        println!("Signature: {}", signature);
84
85        let eth_signature = EthereumSignature::new(address, signed_message, signature);
86        let is_valid = eth_signature.verify_ethereum_signature().unwrap_or_else(|e| panic!("Failed to verify signature: {}", e));
87        println!("Signature is valid: {}", is_valid);
88        assert!(is_valid, "Signature verification failed");
89    }
90}