lib.rs
Overview
This file serves as a core utility and module aggregator within its scope, providing essential data structures, utility functions, and module exports to support key operations in the system. It primarily handles cryptographic key representations, base64 decoding for identifiers, and reading key data from JSON files. The file also re-exports modules related to business process resolving, message routing, and external message processing, centralizing access to these functionalities.
Modules and Re-Exports
bp_resolver(public): Contains logic related to business process resolution. It exportsMockBPResolverfor use outside this module.defaults(private): Defines default constants such as URL paths and ports.message_router(public): Manages routing of messages within the system.process_ext_messages(public): Handles processing of external messages.
The file re-exports key items for convenient external use:
MockBPResolverfrombp_resolverDEFAULT_NODE_URL_PATH,DEFAULT_NODE_URL_PORT, andDEFAULT_URL_PATHfromdefaults
These re-exports enable other parts of the system to access these constants and types without importing the internal module paths directly.
KeyPair Struct
#[derive(Clone, Deserialize)]
pub struct KeyPair {
pub public: String,
pub secret: String,
}
Description
KeyPair represents a cryptographic key pair consisting of a public key and a secret key, both stored as strings.
Fields
public: The public key component as aString.secret: The secret (private) key component as aString.
Traits
Derives
Cloneto allow duplication of key pairs.Derives
Deserializeto enable deserialization from formats such as JSON usingserde.
Usage Example
let keypair_json = r#"{
"public": "public_key_string",
"secret": "secret_key_string"
}"#;
let keypair: KeyPair = serde_json::from_str(keypair_json).unwrap();
println!("Public Key: {}", keypair.public);
Implementation Notes
There is a
todocomment highlighting the need to prevent printing the secret key into logs, indicating a security consideration.The struct is designed to be compatible with JSON deserialization for easy loading from configuration or key files.
base64_id_decode Function
pub fn base64_id_decode(base64_str: &serde_json::Value) -> Result<String, String>
Description
Decodes a base64-encoded string wrapped in a JSON value into a hexadecimal string representation.
Parameters
base64_str: A reference to aserde_json::Valueexpected to contain a base64-encoded string.
Returns
Ok(String): Hexadecimal encoded string obtained after decoding the base64 input.Err(String): Error message if the input JSON is invalid or decoding fails.
Implementation Details
Extracts the string from the JSON value using
as_str().Uses the external
tvm_types::base64_decodefunction for decoding base64 bytes.Converts the decoded bytes to a hex string using
hex::encode.
Usage Example
let json_val = serde_json::json!("c29tZS1iYXNlNjQtc3RyaW5n");
match base64_id_decode(&json_val) {
Ok(hex_str) => println!("Decoded hex: {}", hex_str),
Err(err) => eprintln!("Error decoding: {}", err),
}
read_keys_from_file Function
pub fn read_keys_from_file(path: &str) -> Result<KeyPair, Box<dyn std::error::Error>>
Description
Reads a JSON file from the specified path and deserializes its content into a KeyPair struct.
Parameters
path: A string slice representing the file path to read the keys from.
Returns
Ok(KeyPair): The deserialized key pair read from the file.Err(Box<dyn std::error::Error>): Any I/O or deserialization error wrapped in a boxed dynamic error type.
Implementation Details
Opens the file at the given path.
Wraps the file in a buffered reader for efficient reading.
Uses
serde_json::from_readerto deserialize the JSON content intoKeyPair.
Usage Example
match read_keys_from_file("keys.json") {
Ok(keys) => println!("Public key: {}", keys.public),
Err(e) => eprintln!("Failed to read keys: {}", e),
}
Interaction with Other Parts of the System
The file depends on external crates such as
serdefor JSON deserialization andtvm_typesfor base64 decoding.The re-exported modules (
bp_resolver,message_router,process_ext_messages) suggest that this file acts as a central access point for message processing, routing, and business process resolution components.The
KeyPairstruct and associated utility functions are likely used by components that require cryptographic key handling, such as authentication modules or message signing/verification systems.Constants re-exported from
defaultsare probably used for configuring network communication endpoints and related settings in other parts of the application.
Diagram
flowchart TD
lib.rs --> bp_resolver[Module: bp_resolver]
lib.rs --> message_router[Module: message_router]
lib.rs --> process_ext_messages[Module: process_ext_messages]
lib.rs --> defaults[Module: defaults]
subgraph KeyPair Struct
KP[KeyPair]
end
subgraph Utility Functions
B64[base64_id_decode]
READ[read_keys_from_file]
end
lib.rs --> KP
lib.rs --> B64
lib.rs --> READ
bp_resolver -->|exports| MockBPResolver
defaults -->|exports| DEFAULT_NODE_URL_PATH
defaults -->|exports| DEFAULT_NODE_URL_PORT
defaults -->|exports| DEFAULT_URL_PATH
This flowchart illustrates the module structure and key components exposed by this file, showing the relationship between modules, key data structures, and utility functions.