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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use strkey::StrKey;
use keypair::KeyPair;
use crypto;
use crypto::DecoratedSignature;
use network::Network;
use memo::Memo;
use operation::Operation;
use time_bounds::TimeBounds;
use error::Result;
use xdr::ToXdr;


pub type Fee = u32;

pub struct Transaction {
    source: StrKey,
    fee: Fee,
    memo: Memo,
    sequence: u64,
    time_bounds: Option<TimeBounds>,
    operations: Vec<Operation>,
}


pub struct TransactionEnvelope {
    transaction: Transaction,
    payload: TransactionSignaturePayload,
    signatures: Vec<DecoratedSignature>,
}

pub struct TransactionSignaturePayload {
    network: Network,
    transaction: TaggedTransactionBuffer,
}

#[derive(Clone)]
pub enum EnvelopeType {
    // Scp = 1,
    Tx = 2, // Auth = 3,
}

pub struct TaggedTransactionBuffer {
    tag: EnvelopeType,
    buf: Vec<u8>,
}


impl Transaction {
    pub fn new(source: StrKey,
               fee: Fee,
               memo: Memo,
               sequence: u64,
               time_bounds: Option<TimeBounds>,
               operations: Vec<Operation>)
               -> Transaction {
        Transaction {
            source: source,
            fee: fee,
            memo: memo,
            sequence: sequence,
            time_bounds: time_bounds,
            operations: operations,
        }
    }

    pub fn source(&self) -> &StrKey {
        &self.source
    }

    pub fn fee(&self) -> &Fee {
        &self.fee
    }

    pub fn memo(&self) -> &Memo {
        &self.memo
    }

    pub fn seqnum(&self) -> u64 {
        self.sequence
    }

    pub fn time_bounds(&self) -> &Option<TimeBounds> {
        &self.time_bounds
    }

    pub fn operations(&self) -> &Vec<Operation> {
        &self.operations
    }

    pub fn sign(self, key: &KeyPair) -> Result<TransactionEnvelope> {
        let network = Network::public_network();
        self.sign_with_network(&key, network)
    }

    pub fn sign_with_network(self, key: &KeyPair, network: Network) -> Result<TransactionEnvelope> {
        let mut signed_tx = TransactionEnvelope::new(self, network)?;
        signed_tx.sign(&key)?;
        Ok(signed_tx)
    }
}

impl TransactionEnvelope {
    pub fn new(tx: Transaction, network: Network) -> Result<TransactionEnvelope> {
        let payload = TransactionSignaturePayload::new(&tx, network)?;
        Ok(TransactionEnvelope {
            transaction: tx,
            payload: payload,
            signatures: Vec::new(),
        })
    }

    pub fn transaction(&self) -> &Transaction {
        &self.transaction
    }

    pub fn signatures(&self) -> &Vec<DecoratedSignature> {
        &self.signatures
    }

    pub fn sign(&mut self, keypair: &KeyPair) -> Result<()> {
        let message = self.payload.hash()?;
        let signature = keypair.sign_decorated(&message)?;
        self.signatures.push(signature);
        Ok(())
    }
}

impl TransactionSignaturePayload {
    pub fn new(tx: &Transaction, network: Network) -> Result<Self> {
        Ok(TransactionSignaturePayload {
            network: network,
            transaction: TaggedTransactionBuffer::new(&tx)?,
        })
    }

    pub fn signature_base(&self) -> Result<Vec<u8>> {
        let mut buf = self.network.network_id();
        self.transaction.to_writer(&mut buf)?;
        Ok(buf)
    }

    pub fn hash(&self) -> Result<Vec<u8>> {
        let buf = self.signature_base()?;
        Ok(crypto::hash(&buf))
    }
}

impl TaggedTransactionBuffer {
    pub fn new(tx: &Transaction) -> Result<TaggedTransactionBuffer> {
        let buf = tx.to_xdr()?;
        Ok(TaggedTransactionBuffer {
            tag: EnvelopeType::Tx,
            buf: buf,
        })
    }

    pub fn tag(&self) -> &EnvelopeType {
        &self.tag
    }

    pub fn transaction_buffer(&self) -> &Vec<u8> {
        &self.buf
    }
}


#[cfg(test)]
mod tests {
    use TransactionBuilder;
    use Account;
    use super::TransactionSignaturePayload;
    use Network;

    #[test]
    fn test_payload_hash() {
        let mut account =
            Account::from_account_id("GCLDNMHZTEY6PUYQBYOVERBBZ2W3RLMYOSZWHAMY5R4YW2N6MM4LFA72",
                                     999)
                .unwrap();

        let tx = TransactionBuilder::new(&mut account).build().unwrap();
        let payload = TransactionSignaturePayload::new(&tx, Network::test_network()).unwrap();
        let hash = payload.hash().unwrap();
        let expected_hash = vec![0xe3, 0x15, 0x4f, 0x21, 0x29, 0x7a, 0x7a, 0x18, 0x77, 0x99, 0xc5,
                                 0x9b, 0x44, 0x5b, 0xb7, 0xfc, 0x28, 0x10, 0x80, 0xfd, 0xa, 0x37,
                                 0xcb, 0x10, 0x44, 0x73, 0x65, 0x56, 0x3f, 0x29, 0x45, 0xe9];
        assert_eq!(hash, expected_hash);
    }
}