2026-08-12 22:55:36 +02:00
|
|
|
//! SHA-256 hashed answer chain. `parent_hashes` is DAG-shaped, but
|
|
|
|
|
//! nothing produces more than one parent yet - the shape is reserved
|
|
|
|
|
//! for follow-up questions posted back into a visitor's chain.
|
2026-07-29 19:38:40 +02:00
|
|
|
#![cfg(feature = "ssr")]
|
|
|
|
|
|
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
|
|
|
|
|
|
/// Hashes one chain node from its question, its parent hash(es), the
|
|
|
|
|
/// submitted responses, and a timestamp. Parents are sorted first so the
|
|
|
|
|
/// hash doesn't depend on the order multiple parents happened to arrive
|
|
|
|
|
/// in.
|
|
|
|
|
pub fn hash_node(
|
|
|
|
|
question_id: &str,
|
|
|
|
|
parent_hashes: &[String],
|
|
|
|
|
responses: &serde_json::Value,
|
|
|
|
|
timestamp_ms: i64,
|
|
|
|
|
) -> String {
|
|
|
|
|
let mut parents = parent_hashes.to_vec();
|
|
|
|
|
parents.sort();
|
|
|
|
|
let payload = serde_json::json!({
|
|
|
|
|
"questionId": question_id,
|
|
|
|
|
"parentHashes": parents,
|
|
|
|
|
"responses": responses,
|
|
|
|
|
"timestamp": timestamp_ms,
|
|
|
|
|
});
|
|
|
|
|
let data = serde_json::to_vec(&payload).expect("json values always serialize");
|
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
|
hasher.update(&data);
|
|
|
|
|
format!("{:x}", hasher.finalize())
|
|
|
|
|
}
|