diff --git a/common/src/block.rs b/common/src/block.rs index 2e4e9b9..6ad1902 100644 --- a/common/src/block.rs +++ b/common/src/block.rs @@ -1,20 +1,34 @@ use std::io::{Cursor, Read}; +use k256::ecdsa::Signature; use rs_merkle::Hasher; -use crate::merkle_tree_public::hasher::OwnHasher; +use crate::{merkle_tree_public::hasher::OwnHasher, transaction::AuthenticatedTransaction}; use nssa; pub type BlockHash = [u8; 32]; pub type BlockId = u64; +pub type TimeStamp = u64; #[derive(Debug, Clone)] -pub struct Block { +pub struct BlockHeader { pub block_id: BlockId, pub prev_block_id: BlockId, pub prev_block_hash: BlockHash, pub hash: BlockHash, - pub transactions: Vec, + pub timestamp: TimeStamp, + pub signature: Signature, +} + +#[derive(Debug, Clone)] +pub struct BlockBody { + pub transactions: Vec, +} + +#[derive(Debug, Clone)] +pub struct Block { + pub header: BlockHeader, + pub body: BlockBody, } #[derive(Debug, PartialEq, Eq)] @@ -22,7 +36,9 @@ pub struct HashableBlockData { pub block_id: BlockId, pub prev_block_id: BlockId, pub prev_block_hash: BlockHash, - pub transactions: Vec, + pub timestamp: TimeStamp, + pub signature: Signature, + pub transactions: Vec, } impl From for Block { @@ -31,11 +47,17 @@ impl From for Block { let hash = OwnHasher::hash(&data); Self { - block_id: value.block_id, - prev_block_id: value.prev_block_id, - hash, - transactions: value.transactions, - prev_block_hash: value.prev_block_hash, + header: BlockHeader { + block_id: value.block_id, + prev_block_id: value.prev_block_id, + prev_block_hash: value.prev_block_hash, + hash, + timestamp: value.timestamp, + signature: value.signature, + }, + body: BlockBody { + transactions: value.transactions, + }, } } } @@ -46,6 +68,8 @@ impl From for HashableBlockData { block_id: value.block_id, prev_block_id: value.prev_block_id, prev_block_hash: value.prev_block_hash, + timestamp: value.timestamp, + signature: value.signature, transactions: value.transactions, } } diff --git a/common/src/transaction.rs b/common/src/transaction.rs index afc4559..ba2cc97 100644 --- a/common/src/transaction.rs +++ b/common/src/transaction.rs @@ -25,39 +25,49 @@ pub type Tag = u8; #[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] pub enum TxKind { Public, - Private, - Shielded, - Deshielded, + PrivacyPreserving, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] ///General transaction object pub struct TransactionBody { pub tx_kind: TxKind, - ///Tx input data (public part) - pub execution_input: Vec, - ///Tx output data (public_part) - pub execution_output: Vec, - ///Tx input utxo commitments - pub utxo_commitments_spent_hashes: Vec, - ///Tx output utxo commitments - pub utxo_commitments_created_hashes: Vec, - ///Tx output nullifiers - pub nullifier_created_hashes: Vec, - ///Execution proof (private part) - pub execution_proof_private: String, ///Encoded blobs of data - pub encoded_data: Vec<(CipherText, Vec, Tag)>, - ///Transaction senders ephemeral pub key - pub ephemeral_pub_key: Vec, - ///Public (Pedersen) commitment - pub commitment: Vec, - ///tweak - pub tweak: Tweak, - ///secret_r - pub secret_r: [u8; 32], - ///Hex-encoded address of a smart contract account called - pub sc_addr: String, + pub encoded_transaction_data: Vec, +} + +impl From for TransactionBody { + fn from(value: nssa::NSSATransaction) -> Self { + match value { + nssa::NSSATransaction::Public(tx) => { + Self { + tx_kind: TxKind::Public, + encoded_transaction_data: tx.to_bytes(), + } + }, + nssa::NSSATransaction::PrivacyPreserving(tx) => { + Self { + tx_kind: TxKind::PrivacyPreserving, + encoded_transaction_data: tx.to_bytes(), + } + } + } + } +} + +impl TryFrom for nssa::NSSATransaction { + type Error = nssa::error::NssaError; + + fn try_from(value: TransactionBody) -> Result { + match value.tx_kind { + TxKind::Public => { + nssa::PublicTransaction::from_bytes(&value.encoded_transaction_data).map(|tx| tx.into()) + }, + TxKind::PrivacyPreserving => { + nssa::PrivacyPreservingTransaction::from_bytes(&value.encoded_transaction_data).map(|tx| tx.into()) + }, + } + } } #[derive(Debug, Serialize, Deserialize)] @@ -172,41 +182,6 @@ impl TransactionBody { pub fn log(&self) { info!("Transaction hash is {:?}", hex::encode(self.hash())); info!("Transaction tx_kind is {:?}", self.tx_kind); - info!("Transaction execution_input is {:?}", { - if let Ok(action) = serde_json::from_slice::(&self.execution_input) { - action.into_hexed_print() - } else { - "".to_string() - } - }); - info!("Transaction execution_output is {:?}", { - if let Ok(action) = serde_json::from_slice::(&self.execution_output) { - action.into_hexed_print() - } else { - "".to_string() - } - }); - info!( - "Transaction utxo_commitments_spent_hashes is {:?}", - self.utxo_commitments_spent_hashes - .iter() - .map(|val| hex::encode(*val)) - .collect::>() - ); - info!( - "Transaction utxo_commitments_created_hashes is {:?}", - self.utxo_commitments_created_hashes - .iter() - .map(|val| hex::encode(*val)) - .collect::>() - ); - info!( - "Transaction nullifier_created_hashes is {:?}", - self.nullifier_created_hashes - .iter() - .map(|val| hex::encode(*val)) - .collect::>() - ); info!( "Transaction encoded_data is {:?}", self.encoded_data @@ -214,10 +189,6 @@ impl TransactionBody { .map(|val| (hex::encode(val.0.clone()), hex::encode(val.1.clone()))) .collect::>() ); - info!( - "Transaction ephemeral_pub_key is {:?}", - hex::encode(self.ephemeral_pub_key.clone()) - ); } } @@ -309,18 +280,7 @@ mod tests { fn test_transaction_body() -> TransactionBody { TransactionBody { tx_kind: TxKind::Public, - execution_input: vec![1, 2, 3, 4], - execution_output: vec![5, 6, 7, 8], - utxo_commitments_spent_hashes: vec![[9; 32], [10; 32], [11; 32], [12; 32]], - utxo_commitments_created_hashes: vec![[13; 32]], - nullifier_created_hashes: vec![[0; 32], [1; 32], [2; 32], [3; 32]], - execution_proof_private: "loremipsum".to_string(), - encoded_data: vec![(vec![255, 255, 255], vec![254, 254, 254], 1)], - ephemeral_pub_key: vec![5; 32], - commitment: vec![], - tweak: Tweak::from_slice(&[7; SECRET_KEY_SIZE]).unwrap(), - secret_r: [8; 32], - sc_addr: "someAddress".to_string(), + encoded_transaction_data: vec![1,2,3,4], } } diff --git a/nssa/src/lib.rs b/nssa/src/lib.rs index ed88047..74c58b1 100644 --- a/nssa/src/lib.rs +++ b/nssa/src/lib.rs @@ -16,3 +16,21 @@ pub use signature::PrivateKey; pub use signature::PublicKey; pub use signature::Signature; pub use state::V01State; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum NSSATransaction { + Public(PublicTransaction), + PrivacyPreserving(PrivacyPreservingTransaction), +} + +impl From for NSSATransaction { + fn from(value: PublicTransaction) -> Self { + Self::Public(value) + } +} + +impl From for NSSATransaction { + fn from(value: PrivacyPreservingTransaction) -> Self { + Self::PrivacyPreserving(value) + } +} diff --git a/sequencer_core/src/sequencer_store/block_store.rs b/sequencer_core/src/sequencer_store/block_store.rs index 3bd68cb..3c05792 100644 --- a/sequencer_core/src/sequencer_store/block_store.rs +++ b/sequencer_core/src/sequencer_store/block_store.rs @@ -77,18 +77,33 @@ fn block_to_transactions_map(block: &Block) -> HashMap { mod tests { use super::*; + use common::block::{BlockBody, BlockHeader}; + use k256::{ + ecdsa::{signature::SignerMut, Signature, SigningKey}, + FieldBytes, + }; use tempfile::tempdir; #[test] fn test_get_transaction_by_hash() { let temp_dir = tempdir().unwrap(); let path = temp_dir.path(); + + let key_bytes = FieldBytes::from_slice(&[37; 32]); + let mut private_key: SigningKey = SigningKey::from_bytes(key_bytes).unwrap(); + let genesis_block = Block { - block_id: 0, - prev_block_id: 0, - prev_block_hash: [0; 32], - hash: [1; 32], - transactions: vec![], + header: BlockHeader { + block_id: 0, + prev_block_id: 0, + prev_block_hash: [0; 32], + hash: [1; 32], + timestamp: 0, + signature: private_key.sign(&[42; 32]), + }, + body: BlockBody { + transactions: vec![], + }, }; // Start an empty node store let mut node_store =