Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bls-signatures = "0.15"
shellexpand = "3"
flate2 = "1.0"
tar = "0.4"
reqwest = { version = "0.12", features = ["blocking"] }
reqwest = { version = "0.12", features = ["blocking", "json"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] }
num_cpus = "1.16"
1 change: 1 addition & 0 deletions src/commands/start/curio/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Enable = true
ListenAddress = "0.0.0.0:4702"

[Subsystems]
GuiAddress = "0.0.0.0:4701"
EnableCommP = true
EnableMoveStorage = true
EnablePDP = true
Expand Down
6 changes: 5 additions & 1 deletion src/commands/start/curio/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ fn build_docker_create_args(
"-p".to_string(),
format!("{}:12301", api_port_alt),
"-p".to_string(),
format!("{}:4701", gui_port),
format!(
"127.0.0.1:{}:{}",
gui_port,
super::constants::CURIO_WEB_RPC_PORT
),
"-p".to_string(),
format!("{}:4702", pdp_port),
]);
Expand Down
1 change: 1 addition & 0 deletions src/commands/start/curio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod pre_execute;
mod rpc;
pub mod storage;
pub mod verification;
mod web_rpc;

use super::step::{SetupContext, Step};
use std::error::Error;
Expand Down
73 changes: 5 additions & 68 deletions src/commands/start/curio/pdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! Handles importing PDP private keys via Curio Web RPC API.

use super::super::step::SetupContext;
use super::constants::{CURIO_WEB_RPC_PORT, PDP_KEY_IMPORT_WAIT_SECS};
use super::constants::PDP_KEY_IMPORT_WAIT_SECS;
use super::web_rpc::WebRpc;
use crate::commands::init::keys::KeyInfo;
use crate::docker::command_logger::run_and_log_command;
use std::error::Error;
use std::fs;
use std::thread;
Expand All @@ -19,15 +19,15 @@ use tracing::info;
pub fn import_pdp_key(context: &SetupContext, sp_index: usize) -> Result<(), Box<dyn Error>> {
info!("Importing PDP private key for PDP SP {}...", sp_index);

let run_id = context.run_id();
let container_name = format!("foc-{}-curio-{}", run_id, sp_index);
let rpc = WebRpc::connect(context, sp_index)?;

// Get PDP_SP_{sp_index} private key and expected eth address
let pdp_sp_name = format!("PDP_SP_{}", sp_index);
let (private_key, expected_eth_address) = get_pdp_sp_credentials(&pdp_sp_name)?;

// Import key via JSON-RPC
let returned_address = import_key_via_rpc(context, &container_name, &private_key)?;
let returned_address = rpc.import_pdp_key(&private_key)?;
thread::sleep(Duration::from_secs(PDP_KEY_IMPORT_WAIT_SECS));

// Verify the returned address matches expected
if returned_address.to_lowercase() != expected_eth_address.to_lowercase() {
Expand Down Expand Up @@ -69,66 +69,3 @@ fn get_pdp_sp_credentials(pdp_sp_name: &str) -> Result<(String, String), Box<dyn

Ok((pdp_sp.private_key.clone(), eth_address))
}

/// Import PDP key via Curio Web RPC API
///
/// Returns the Ethereum address from the response
fn import_key_via_rpc(
context: &SetupContext,
container_name: &str,
private_key: &str,
) -> Result<String, Box<dyn Error>> {
// Construct JSON-RPC payload
let payload = format!(
r#"{{"jsonrpc":"2.0","method":"CurioWeb.ImportPDPKey","params":["{}"],"id":1}}"#,
private_key
);

// Call via curl inside container
let key = format!("curio_pdp_import_key_{}", container_name);
let output = run_and_log_command(
"docker",
&[
"exec",
container_name,
"curl",
"-s",
"-X",
"POST",
"-H",
"Content-Type: application/json",
"-d",
&payload,
&format!("http://localhost:{}/api/webrpc/v0", CURIO_WEB_RPC_PORT),
],
context,
&key,
)?;

if !output.status.success() {
return Err(format!(
"Failed to call ImportPDPKey: {}",
String::from_utf8_lossy(&output.stderr)
)
.into());
}

// Parse JSON response to extract address
let response = String::from_utf8_lossy(&output.stdout);
let json: serde_json::Value = serde_json::from_str(&response)?;

// Check for JSON-RPC error
if let Some(error) = json.get("error") {
return Err(format!("ImportPDPKey RPC error: {}", error).into());
}

// Extract result (the address)
let address = json
.get("result")
.and_then(|r| r.as_str())
.ok_or("Missing or invalid 'result' in response")?;

thread::sleep(Duration::from_secs(PDP_KEY_IMPORT_WAIT_SECS));

Ok(address.to_string())
}
Loading
Loading