106 lines
2.7 KiB
Rust
106 lines
2.7 KiB
Rust
//! 宪法收据模块
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
use sha3::{Digest, Sha3_384};
|
||
|
||
/// 宪法收据
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ConstitutionalReceipt {
|
||
/// 交易哈希 (SHA3-384)
|
||
pub transaction_hash: [u8; 48],
|
||
/// 宾法哈希 (SHA3-384)
|
||
pub constitutional_hash: [u8; 48],
|
||
/// 条款掩码
|
||
pub clause_mask: u64,
|
||
/// 执行结果哈希 (SHA3-384)
|
||
pub execution_result_hash: [u8; 48],
|
||
/// 时间戳
|
||
pub timestamp: u64,
|
||
/// 有效期窗口
|
||
pub validity_window: u64,
|
||
/// CEE节点签名列表
|
||
pub signatures: Vec<Vec<u8>>,
|
||
/// 收据ID (SHA3-384)
|
||
pub receipt_id: [u8; 48],
|
||
}
|
||
|
||
impl ConstitutionalReceipt {
|
||
/// 创建新的宪法收据
|
||
pub fn new(
|
||
transaction_hash: [u8; 48],
|
||
constitutional_hash: [u8; 48],
|
||
clause_mask: u64,
|
||
) -> Self {
|
||
let receipt_id = Self::generate_receipt_id(&transaction_hash);
|
||
|
||
Self {
|
||
transaction_hash,
|
||
constitutional_hash,
|
||
clause_mask,
|
||
execution_result_hash: [0u8; 48],
|
||
timestamp: 0,
|
||
validity_window: 300,
|
||
signatures: Vec::new(),
|
||
receipt_id,
|
||
}
|
||
}
|
||
|
||
fn generate_receipt_id(transaction_hash: &[u8; 48]) -> [u8; 48] {
|
||
let mut hasher = Sha3_384::new();
|
||
hasher.update(transaction_hash);
|
||
hasher.update(b"NAC_CR");
|
||
let result = hasher.finalize();
|
||
|
||
let mut id = [0u8; 48];
|
||
id.copy_from_slice(&result);
|
||
id
|
||
}
|
||
|
||
/// 添加CEE签名
|
||
pub fn add_signature(&mut self, signature: Vec<u8>) {
|
||
self.signatures.push(signature);
|
||
}
|
||
|
||
/// 验证收据
|
||
pub fn verify(&self) -> bool {
|
||
let expected_id = Self::generate_receipt_id(&self.transaction_hash);
|
||
if self.receipt_id != expected_id {
|
||
return false;
|
||
}
|
||
!self.signatures.is_empty()
|
||
}
|
||
|
||
/// 检查收据是否在有效期内
|
||
pub fn is_valid(&self) -> bool {
|
||
true // TODO: 实现时间检查
|
||
}
|
||
}
|
||
|
||
/// CEE请求数据
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct CEERequest {
|
||
/// 交易哈希
|
||
pub transaction_hash: String,
|
||
/// 交易载荷(Base64编码,可选)
|
||
pub transaction_payload: Option<String>,
|
||
}
|
||
|
||
impl CEERequest {
|
||
/// 创建新的CEE请求
|
||
pub fn new(transaction_hash: [u8; 48]) -> Self {
|
||
Self {
|
||
transaction_hash: format!("{:?}", transaction_hash),
|
||
transaction_payload: None,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// CEE响应数据
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct CEEResponse {
|
||
/// 宪法收据
|
||
pub receipt: ConstitutionalReceipt,
|
||
/// CEE节点签名
|
||
pub signature: String,
|
||
}
|