lib.rs

Overview

This file provides foundational utility functions and data structures for key management, address normalization, and time calculations within the system. It acts as a core utility module that supports authentication and contract interactions by offering:

The file also contains unit tests that validate the address normalization logic.


Modules


Structs

KeyPair

#[derive(Clone, Debug, Deserialize)]
pub struct KeyPair {
    pub public: String,
    pub secret: String,
}

Represents a cryptographic key pair with:

This struct derives Clone, Debug, and Deserialize traits, allowing it to be cloned, printed for debugging, and deserialized from formats like JSON.

Usage example:

let keypair = KeyPair {
    public: "public_key_string".to_string(),
    secret: "secret_key_string".to_string(),
};

Functions

now_plus_n_secs

fn now_plus_n_secs(ttl: u64) -> u128

Computes a future timestamp in milliseconds since UNIX epoch by adding ttl seconds to the current system time.

Algorithm details:

Usage example:

let future_time_ms = now_plus_n_secs(3600); // 1 hour from now

normalize_address

fn normalize_address(s: &str) -> Option<String>

Normalizes a blockchain address string by extracting a 64-character hexadecimal key from the input.

Implementation details:

Usage example:

let input = "0:8e8dad0462a4d5c528e18251846f24bc5c04cd1871115fb1e9b00c9741f60800";
let normalized = normalize_address(input);
assert_eq!(normalized, Some("8e8dad0462a4d5c528e18251846f24bc5c04cd1871115fb1e9b00c9741f60800".to_string()));

read_keys_from_file

pub fn read_keys_from_file(path: &str) -> Result<KeyPair, Box<dyn std::error::Error>>

Reads and deserializes a JSON file containing a KeyPair.

Implementation details:

Usage example:

match read_keys_from_file("keys.json") {
    Ok(keypair) => println!("Public key: {}", keypair.public),
    Err(e) => eprintln!("Failed to read keys: {}", e),
}

Testing

The file includes comprehensive unit tests for normalize_address covering cases such as:

These tests ensure robust parsing and normalization of address inputs.


Interaction with Other Parts of the System

This file acts as a utility and foundational layer, supporting key management and address validation across the system's authentication and blockchain-related components.


Mermaid Diagram: File Structure and Function Relationships

flowchart TD
A[lib.rs] --> B[KeyPair Struct]
A --> C["now_plus_n_secs()"]
A --> D["normalize_address()"]
A --> E["read_keys_from_file()"]
A --> F[auth module]
A --> G[owner_wallet module]
A --> H[root_contracts module]
C --> I[SystemTime.now]
D --> J[Address String Parsing]
E --> K[File IO]
E --> L[serde_json Deserialization]

This diagram illustrates the main components and their relationships within lib.rs, showing how utility functions and modules are interconnected.