Skip to content

Commit fc3f06f

Browse files
authored
Merge pull request #5 from dermetfan/state
state management
2 parents 7c37a03 + cbccc0c commit fc3f06f

File tree

5 files changed

+269
-73
lines changed

5 files changed

+269
-73
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ result*
44
*.skey
55
*.vkey
66
*.mnemonic
7+
challenges.json
8+
state*

nix/nixosModules.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@
5757
(lib.cli.toGNUCommandLineShell {} (removeAttrs cfg.settings ["mnemonic-file"]))
5858
(lib.cli.toGNUCommandLine {} (
5959
lib.optionalAttrs (cfg.settings.mnemonic-file != null) { mnemonic-file = ''"$CREDENTIALS_DIRECTORY"/mnemonic''; }
60+
// lib.optionalAttrs (cfg.settings.data-dir or null == null) { data-dir = ''"$STATE_DIRECTORY"''; }
6061
))
6162
];
6263

6364
enableStrictShellChecks = true;
6465

6566
serviceConfig = {
6667
Type = "exec";
68+
StateDirectory = name;
6769
DynamicUser = true;
6870
LoadCredential = lib.optional (cfg.settings.mnemonic-file != null) "mnemonic:${cfg.settings.mnemonic-file}";
71+
Restart = "always";
6972
};
7073
};
7174
};

src/api.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// shadowharvester/src/api.rs
22

3-
use serde::Deserialize;
3+
use serde::{Deserialize, Serialize};
44
use reqwest;
55
use reqwest::blocking;
66
use serde_json;
@@ -23,7 +23,7 @@ pub struct RegistrationReceipt {
2323
pub registration_receipt: serde_json::Value,
2424
}
2525

26-
#[derive(Debug, Deserialize, Clone)]
26+
#[derive(Debug, Deserialize, Serialize, Clone)]
2727
pub struct ChallengeData {
2828
pub challenge_id: String,
2929
pub difficulty: String,
@@ -51,14 +51,14 @@ pub struct ChallengeResponse { // Made struct public for use in main.rs
5151
pub next_challenge_starts_at: Option<String>,
5252
}
5353

54-
#[derive(Debug, Deserialize)]
55-
struct SolutionReceipt {
54+
#[derive(Debug, Deserialize, PartialEq)]
55+
pub struct SolutionReceipt {
5656
#[serde(rename = "crypto_receipt")]
5757
pub crypto_receipt: serde_json::Value,
5858
}
5959

60-
#[derive(Debug, Deserialize)]
61-
struct DonateResponse {
60+
#[derive(Debug, Deserialize, PartialEq)]
61+
pub struct DonateResponse {
6262
pub status: String,
6363
#[serde(rename = "donation_id")]
6464
pub donation_id: String,
@@ -228,7 +228,7 @@ pub fn submit_solution(
228228
address: &str,
229229
challenge_id: &str,
230230
nonce: &str,
231-
) -> Result<(), String> {
231+
) -> Result<serde_json::Value, String> {
232232
let url = format!(
233233
"{}/solution/{}/{}/{}",
234234
api_url,
@@ -248,8 +248,8 @@ pub fn submit_solution(
248248

249249
if status.is_success() {
250250
// Successful submission
251-
let _: SolutionReceipt = response.json().map_err(|e| format!("Failed to parse successful receipt JSON: {}", e))?;
252-
Ok(())
251+
let receipt: SolutionReceipt = response.json().map_err(|e| format!("Failed to parse successful receipt JSON: {}", e))?;
252+
Ok(receipt.crypto_receipt)
253253
} else {
254254
// Submission failed (4xx or 5xx)
255255
let body_text = response.text().unwrap_or_else(|_| format!("Could not read response body for status {}", status));
@@ -277,7 +277,7 @@ pub fn donate_to(
277277
original_address: &str,
278278
destination_address: &str,
279279
donation_signature: &str,
280-
) -> Result<(), String> {
280+
) -> Result<String, String> {
281281

282282
let url = format!(
283283
"{}/donate_to/{}/{}/{}",
@@ -301,7 +301,7 @@ pub fn donate_to(
301301
if status.is_success() {
302302
let donation_response: DonateResponse = response.json().map_err(|e| format!("Failed to parse successful donation JSON: {}", e))?;
303303
println!("✅ Donation successful. Donation ID: {}", donation_response.donation_id);
304-
Ok(())
304+
Ok(donation_response.donation_id)
305305
} else {
306306
let body_text = response.text().unwrap_or_else(|_| format!("Could not read response body for status {}", status));
307307
let api_error: Result<ApiErrorResponse, _> = serde_json::from_str(&body_text);

src/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ pub struct Cli {
4848
#[arg(long)]
4949
pub challenge: Option<String>,
5050

51+
/// Where to store state (like the mnemonic starting index) and receipts
52+
#[arg(long, default_value = ".")]
53+
pub data_dir: Option<String>,
5154
}
5255

5356

0 commit comments

Comments
 (0)