add un/verified signed transaction structs

This commit is contained in:
Sergio Chouhy 2025-07-10 14:53:25 -03:00
parent b49f38ca4b
commit a174eb4b85

View File

@ -1,5 +1,5 @@
use log::info; use log::info;
use secp256k1_zkp::{PedersenCommitment, Tweak}; use secp256k1_zkp::{PedersenCommitment, PublicKey, Scalar, Tweak};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::{digest::FixedOutput, Digest}; use sha2::{digest::FixedOutput, Digest};
@ -57,6 +57,25 @@ pub struct Transaction {
pub state_changes: (serde_json::Value, usize), pub state_changes: (serde_json::Value, usize),
} }
#[derive(Serialize, Deserialize)]
struct TransactionSignature {}
/// A transaction with a signature.
/// Meant to be sent through the network to the sequencer
#[derive(Serialize, Deserialize)]
pub struct UnverifiedSignedTransaction {
transaction: Transaction,
signature: TransactionSignature
}
/// A transaction with a valid signature over its hash.
/// Can only be constructed from an `UnverifiedSignedTransaction`
/// if the signature is valid
pub struct VerifiedSignedTransaction {
transaction: Transaction,
signature: TransactionSignature
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct MintMoneyPublicTx { pub struct MintMoneyPublicTx {
pub acc: [u8; 32], pub acc: [u8; 32],
@ -150,6 +169,9 @@ impl ActionData {
} }
} }
type SignaturePrivateKey = Scalar;
type SignaturePublicKey = PublicKey;
impl Transaction { impl Transaction {
/// Computes and returns the SHA-256 hash of the JSON-serialized representation of `self`. /// Computes and returns the SHA-256 hash of the JSON-serialized representation of `self`.
pub fn hash(&self) -> TreeHashType { pub fn hash(&self) -> TreeHashType {
@ -162,6 +184,13 @@ impl Transaction {
TreeHashType::from(hasher.finalize_fixed()) TreeHashType::from(hasher.finalize_fixed())
} }
pub fn sign(self, _private_key: SignaturePrivateKey) -> UnverifiedSignedTransaction {
let _hash = self.hash();
// Implement actual signature over `hash`
let signature = TransactionSignature {};
UnverifiedSignedTransaction { transaction: self, signature }
}
pub fn log(&self) { pub fn log(&self) {
info!("Transaction hash is {:?}", hex::encode(self.hash())); info!("Transaction hash is {:?}", hex::encode(self.hash()));
info!("Transaction tx_kind is {:?}", self.tx_kind); info!("Transaction tx_kind is {:?}", self.tx_kind);
@ -214,6 +243,14 @@ impl Transaction {
} }
} }
impl UnverifiedSignedTransaction {
pub fn into_verified(self) -> VerifiedSignedTransaction {
let hash = self.transaction.hash();
// Check signature over hash
todo!()
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use secp256k1_zkp::{constants::SECRET_KEY_SIZE, Tweak}; use secp256k1_zkp::{constants::SECRET_KEY_SIZE, Tweak};