lssa/storage/src/transaction.rs

87 lines
2.7 KiB
Rust
Raw Normal View History

2024-10-10 14:09:31 +03:00
use serde::{Deserialize, Serialize};
2024-12-22 16:14:52 +02:00
use sha2::{digest::FixedOutput, Digest};
2024-10-10 14:09:31 +03:00
use crate::merkle_tree_public::TreeHashType;
2024-10-10 14:09:31 +03:00
2024-12-22 16:14:52 +02:00
use elliptic_curve::{
consts::{B0, B1},
generic_array::GenericArray,
};
use sha2::digest::typenum::{UInt, UTerm};
pub type CipherText = Vec<u8>;
pub type Nonce = GenericArray<u8, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>>;
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub enum TxKind {
Public,
Private,
Shielded,
Deshielded,
}
2024-10-10 14:09:31 +03:00
#[derive(Debug, Serialize, Deserialize, Clone)]
///General transaction object
pub struct Transaction {
pub hash: TreeHashType,
pub tx_kind: TxKind,
///Tx input data (public part)
pub execution_input: Vec<u8>,
///Tx output data (public_part)
pub execution_output: Vec<u8>,
2024-12-22 16:14:52 +02:00
///Tx input utxo commitments
pub utxo_commitments_spent_hashes: Vec<TreeHashType>,
///Tx output utxo commitments
pub utxo_commitments_created_hashes: Vec<TreeHashType>,
///Tx output nullifiers
pub nullifier_created_hashes: Vec<TreeHashType>,
///Execution proof (private part)
pub execution_proof_private: String,
2024-12-22 16:14:52 +02:00
///Encoded blobs of data
pub encoded_data: Vec<(CipherText, Vec<u8>)>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
///General transaction object
pub struct TransactionPayload {
pub tx_kind: TxKind,
///Tx input data (public part)
pub execution_input: Vec<u8>,
///Tx output data (public_part)
pub execution_output: Vec<u8>,
///Tx input utxo commitments
pub utxo_commitments_spent_hashes: Vec<TreeHashType>,
///Tx output utxo commitments
pub utxo_commitments_created_hashes: Vec<TreeHashType>,
///Tx output nullifiers
pub nullifier_created_hashes: Vec<TreeHashType>,
///Execution proof (private part)
pub execution_proof_private: String,
///Encoded blobs of data
pub encoded_data: Vec<(CipherText, Vec<u8>)>,
}
impl From<TransactionPayload> for Transaction {
fn from(value: TransactionPayload) -> Self {
let raw_data = serde_json::to_vec(&value).unwrap();
let mut hasher = sha2::Sha256::new();
hasher.update(&raw_data);
let hash = <TreeHashType>::from(hasher.finalize_fixed());
Self {
hash,
tx_kind: value.tx_kind,
execution_input: value.execution_input,
execution_output: value.execution_output,
utxo_commitments_spent_hashes: value.utxo_commitments_spent_hashes,
utxo_commitments_created_hashes: value.utxo_commitments_created_hashes,
nullifier_created_hashes: value.nullifier_created_hashes,
execution_proof_private: value.execution_proof_private,
encoded_data: value.encoded_data,
}
}
2024-10-10 14:09:31 +03:00
}