1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Transactions

mod hash;

pub use self::hash::Hash;
use std::slice;
#[cfg(feature = "serde")]
use {
    serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer},
    subtle_encoding::base64,
};

/// Transactions are arbitrary byte arrays whose contents are validated by the
/// underlying Tendermint application.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Transaction(Vec<u8>);

impl Transaction {
    /// Create a new raw transaction from a byte vector
    pub fn new<V>(into_vec: V) -> Transaction
    where
        V: Into<Vec<u8>>,
    {
        Transaction(into_vec.into())
    }

    /// Convert this transaction into a byte vector
    pub fn into_vec(self) -> Vec<u8> {
        self.0.clone()
    }

    /// Borrow the contents of this transaction as a byte slice
    pub fn as_bytes(&self) -> &[u8] {
        self.0.as_slice()
    }
}

impl AsRef<[u8]> for Transaction {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Transaction {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let bytes = base64::decode(String::deserialize(deserializer)?.as_bytes())
            .map_err(|e| D::Error::custom(format!("{}", e)))?;

        Ok(Self::new(bytes))
    }
}

#[cfg(feature = "serde")]
impl Serialize for Transaction {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        String::from_utf8(base64::encode(self.as_bytes()))
            .unwrap()
            .serialize(serializer)
    }
}

/// Transaction data is a wrapper for a list of transactions, where
/// transactions are arbitrary byte arrays.
///
/// <https://github.com/tendermint/tendermint/blob/master/docs/spec/blockchain/blockchain.md#data>
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[derive(Clone, Debug)]
pub struct Data {
    txs: Option<Vec<Transaction>>,
}

impl Data {
    /// Create a new transaction data collection
    pub fn new<I>(into_transactions: I) -> Data
    where
        I: Into<Vec<Transaction>>,
    {
        Data {
            txs: Some(into_transactions.into()),
        }
    }

    /// Convert this collection into a vector
    pub fn into_vec(self) -> Vec<Transaction> {
        self.iter().cloned().collect()
    }

    /// Iterate over the transactions in the collection
    pub fn iter(&self) -> slice::Iter<Transaction> {
        self.as_ref().iter()
    }
}

impl AsRef<[Transaction]> for Data {
    fn as_ref(&self) -> &[Transaction] {
        self.txs.as_ref().map(Vec::as_slice).unwrap_or_else(|| &[])
    }
}