aum_core/
transaction.rs

1use std::{
2    fmt::{Debug, Display},
3    hash::Hash,
4};
5use thiserror::Error;
6
7pub trait TransactionId:
8    Clone + Debug + Display + Send + Sync + 'static + Eq + Sized + Hash
9{
10}
11
12pub trait Transaction: Clone + Send + Sync + 'static {
13    type Hash: crate::hash::Hash;
14    type Address: crate::address::Address;
15    type TransactionId: TransactionId;
16    type TransactionParameters;
17
18    fn new(from: Self::Address, to: Self::Address, parameters: Self::TransactionParameters)
19    -> Self;
20    fn transaction_id(&self) -> Result<Self::TransactionId, TransactionError>;
21    fn hash(&self) -> Self::Hash;
22    fn from_bytes(bytes: &[u8]) -> Result<Self, TransactionError>;
23    fn to_bytes(&self) -> Vec<u8>;
24}
25
26pub trait SignedTransaction:
27    Clone + Debug + Send + Sync + 'static + Eq + Ord + Hash + TransactionSignature
28{
29    fn signature(&self) -> Vec<u8>;
30    fn from_bytes(bytes: &[u8]) -> Result<Self, TransactionError>;
31    fn to_bytes(&self) -> Vec<u8>;
32}
33pub trait TransactionSignature: Clone + Debug + Send + Sync + 'static + Eq + Ord + Hash {
34    type Transaction: Transaction;
35    fn from_transaction(transaction: &Self::Transaction) -> Self;
36    fn to_transaction(&self) -> Self::Transaction;
37}
38#[derive(Debug, Error)]
39pub enum TransactionError {
40    #[error("Invalid transaction ID")]
41    InvalidTransactionId,
42    #[error("Invalid transaction bytes")]
43    InvalidBytes,
44
45    #[error("{0}")]
46    Custom(#[from] Box<dyn std::error::Error + Send + Sync>),
47}