gnosisguild / enclave

Enclave is an open-source protocol for Encrypted Execution Environments (E3).
GNU Lesser General Public License v3.0
10 stars 3 forks source link

Create Ciphernode daemon system for starting stopping and interaction #64

Open ryardley opened 1 month ago

ryardley commented 1 month ago

We will need to be able to run the cli with commands for the daemon

Initially we need something like the following:

enclave start
enclave start --daemon 
enclave start -d
enclave stop
enclave stop --id 0x1234...abcd
enclave restart
enclave status

We should however be deliberate about what we need in terms of our api and will need to be flexible so we can workshop output / erganomics


One method to get this working would be with a tcp listener although this might be out of the scope of this initially.

An AI generated example of the kind of thing we could use for interacting with the daemon we should use a proper library like clap to parse the args

use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::thread;

fn handle_client(mut stream: TcpStream) {
    let mut buffer = [0; 512];
    loop {
        match stream.read(&mut buffer) {
            Ok(size) => {
                if size == 0 { return; } // Connection was closed
                let command = String::from_utf8_lossy(&buffer[..size]);
                let response = match command.trim() {
                    "start" => "Starting process...",
                    "stop" => "Stopping process...",
                    "status" => "Process is running",
                    _ => "Unknown command",
                };
                stream.write_all(response.as_bytes()).unwrap();
            },
            Err(_) => return,
        }
    }
}

fn main() -> std::io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:8080")?;
    println!("Daemon listening on port 8080");

    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                thread::spawn(move || {
                    handle_client(stream);
                });
            }
            Err(e) => {
                eprintln!("Error: {}", e);
            }
        }
    }
    Ok(())
}
use std::io::{Read, Write};
use std::net::TcpStream;

fn send_command(command: &str) -> std::io::Result<String> {
    let mut stream = TcpStream::connect("127.0.0.1:8080")?;
    stream.write_all(command.as_bytes())?;

    let mut buffer = String::new();
    stream.read_to_string(&mut buffer)?;
    Ok(buffer)
}

fn main() -> std::io::Result<()> {
    let command = std::env::args().nth(1).unwrap_or_else(|| "status".to_string());
    match send_command(&command) {
        Ok(response) => println!("Daemon response: {}", response),
        Err(e) => eprintln!("Error: {}", e),
    }
    Ok(())
}
ryardley commented 2 days ago
enclave start [-d] [--daemon]

enclave stop

enclave status

enclave logs [--tail N] [--follow]
enclave password set

enclave password rotate
enclave wallet new

enclave wallet set

enclave wallet export ADDRESS [--output FILE]

Of these the ones we need to build now are:

enclave wallet set --from-env "PRIVATE_KEY"
enclave start
enclave aggregator start
enclave password set --from-env "CIPHERNODE_SECRET"