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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::error::Error;

use crate::common::address::Address;
use crate::common::genesis_tx::GenesisTx;
use crate::common::transaction::verify_tx;
use crate::serialization::tx::GenesisSignedTx as ProtoGenesisSignedTx;
use crate::traits::{Decode, Encode, Proto, Transaction, VerifiableTransaction};

use protobuf::Message as ProtoMessage;
use secp256k1::{RecoverableSignature, RecoveryId, Secp256k1};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SignedGenesisTx {
    to: Address,
    amount: u64,
    signature: RecoverableSignature,
    recovery: RecoveryId,
}

impl Transaction<Address, RecoverableSignature, RecoveryId> for SignedGenesisTx {
    fn get_from(&self) -> Option<Address> {
        None
    }
    fn get_to(&self) -> Option<Address> {
        Some(self.to)
    }
    fn get_amount(&self) -> u64 {
        self.amount
    }
    fn get_fee(&self) -> Option<u64> {
        None
    }
    fn get_nonce(&self) -> Option<u32> {
        None
    }
    fn get_signature(&self) -> Option<RecoverableSignature> {
        Some(self.signature)
    }
    fn get_recovery(&self) -> Option<RecoveryId> {
        Some(self.recovery)
    }
}

impl SignedGenesisTx {
    pub fn new(
        to: Address,
        amount: u64,
        signature: RecoverableSignature,
        recovery: RecoveryId,
    ) -> SignedGenesisTx {
        SignedGenesisTx {
            to,
            amount,
            signature,
            recovery,
        }
    }
}

impl Proto for SignedGenesisTx {
    type ProtoType = ProtoGenesisSignedTx;
    fn to_proto(&self) -> Result<Self::ProtoType, Box<Error>> {
        let mut proto_signed_genesis_tx = ProtoGenesisSignedTx::new();
        proto_signed_genesis_tx.set_to(self.to.to_vec());
        proto_signed_genesis_tx.set_amount(self.amount);
        proto_signed_genesis_tx.set_recovery(self.recovery.to_i32() as u32);
        let secp = Secp256k1::without_caps();
        proto_signed_genesis_tx.set_signature(self.signature.serialize_compact(&secp).1.to_vec());
        Ok(proto_signed_genesis_tx)
    }

    fn from_proto(_prototype: &Self::ProtoType) -> Result<Self, Box<Error>> {
        unimplemented!()
    }
}

impl Encode for SignedGenesisTx {
    fn encode(&self) -> Result<Vec<u8>, Box<Error>> {
        let proto_signed_genesis_tx = self.to_proto()?;
        Ok(proto_signed_genesis_tx.write_to_bytes()?)
    }
}

impl Decode for SignedGenesisTx {
    fn decode(buffer: &[u8]) -> Result<SignedGenesisTx, Box<Error>> {
        let secp = Secp256k1::without_caps();
        let mut proto_signed_genesis_tx = ProtoGenesisSignedTx::new();
        proto_signed_genesis_tx.merge_from_bytes(&buffer)?;
        let mut to = [0u8; 20];
        to.clone_from_slice(&proto_signed_genesis_tx.to);
        let recovery = RecoveryId::from_i32(proto_signed_genesis_tx.recovery as i32)?;
        let signature = RecoverableSignature::from_compact(
            &secp,
            &proto_signed_genesis_tx.signature,
            recovery,
        )?;
        Ok(SignedGenesisTx::new(
            to,
            proto_signed_genesis_tx.amount,
            signature,
            recovery,
        ))
    }
}

impl VerifiableTransaction for SignedGenesisTx {
    fn verify(&self) -> Result<(), Box<Error>> {
        let genesis_tx = GenesisTx::new(self.to, self.amount);
        let encoding = genesis_tx.encode()?;
        verify_tx(encoding, self.to, self.signature)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::ValidAddress;
    use rand::{thread_rng, Rng};

    #[test]
    fn it_creates_a_signed_genesis_transaction() {
        let to: Address =
            Address::from_string(&"HLjHZYkjRNkjH3zPmXoU8FDEJ3ALDkuA".to_string()).unwrap();
        let amount = 100;
        let signature_bytes = [
            155, 15, 206, 7, 232, 20, 132, 186, 33, 220, 220, 31, 36, 100, 48, 103, 61, 198, 40,
            155, 48, 189, 196, 64, 162, 132, 254, 252, 160, 242, 136, 253, 42, 105, 138, 104, 227,
            162, 198, 254, 59, 114, 252, 62, 3, 211, 77, 93, 196, 72, 221, 18, 128, 112, 143, 185,
            199, 178, 56, 0, 141, 232, 12, 201,
        ];
        let secp = Secp256k1::without_caps();
        let recovery = RecoveryId::from_i32(0).unwrap();
        let signature =
            RecoverableSignature::from_compact(&secp, &signature_bytes, recovery).unwrap();
        let signed_genesis_tx = SignedGenesisTx::new(to, amount, signature, recovery);
        assert_eq!(to, signed_genesis_tx.to);
        assert_eq!(amount, signed_genesis_tx.amount);
        assert_eq!(signature, signed_genesis_tx.signature);
        assert_eq!(recovery, signed_genesis_tx.recovery);
    }

    #[test]
    fn it_verifies_a_signed_genesis_transaction() {
        let to: Address =
            Address::from_string(&"HLjHZYkjRNkjH3zPmXoU8FDEJ3ALDkuA".to_string()).unwrap();
        let amount = 100;
        let signature_bytes = [
            155, 15, 206, 7, 232, 20, 132, 186, 33, 220, 220, 31, 36, 100, 48, 103, 61, 198, 40,
            155, 48, 189, 196, 64, 162, 132, 254, 252, 160, 242, 136, 253, 42, 105, 138, 104, 227,
            162, 198, 254, 59, 114, 252, 62, 3, 211, 77, 93, 196, 72, 221, 18, 128, 112, 143, 185,
            199, 178, 56, 0, 141, 232, 12, 201,
        ];
        let secp = Secp256k1::without_caps();
        let recovery = RecoveryId::from_i32(0).unwrap();
        let signature =
            RecoverableSignature::from_compact(&secp, &signature_bytes, recovery).unwrap();
        let signed_genesis_tx = SignedGenesisTx::new(to, amount, signature, recovery);
        signed_genesis_tx.verify().unwrap();
    }

    #[test]
    fn it_rejects_a_forged_genesis_transaction() {
        let to: Address =
            Address::from_string(&"HLjHZYkjRNkjH3zPmXoU8FDEJ3ALDkuA".to_string()).unwrap();
        let amount = 200;
        let signature_bytes = [
            155, 15, 206, 7, 232, 20, 132, 186, 33, 220, 220, 31, 36, 100, 48, 103, 61, 198, 40,
            155, 48, 189, 196, 64, 162, 132, 254, 252, 160, 242, 136, 253, 42, 105, 138, 104, 227,
            162, 198, 254, 59, 114, 252, 62, 3, 211, 77, 93, 196, 72, 221, 18, 128, 112, 143, 185,
            199, 178, 56, 0, 141, 232, 12, 201,
        ];
        let secp = Secp256k1::without_caps();
        let recovery = RecoveryId::from_i32(0).unwrap();
        let signature =
            RecoverableSignature::from_compact(&secp, &signature_bytes, recovery).unwrap();
        let signed_genesis_tx = SignedGenesisTx::new(to, amount, signature, recovery);
        match signed_genesis_tx.verify() {
            Ok(_) => panic!("Invalid signature was reported as verified"),
            Err(_) => {}
        }
    }

    #[test]
    fn it_decodes_a_signed_genesis_transaction() {
        let to: Address =
            Address::from_string(&"HLjHZYkjRNkjH3zPmXoU8FDEJ3ALDkuA".to_string()).unwrap();
        let amount = 100;
        let signature_bytes = [
            155, 15, 206, 7, 232, 20, 132, 186, 33, 220, 220, 31, 36, 100, 48, 103, 61, 198, 40,
            155, 48, 189, 196, 64, 162, 132, 254, 252, 160, 242, 136, 253, 42, 105, 138, 104, 227,
            162, 198, 254, 59, 114, 252, 62, 3, 211, 77, 93, 196, 72, 221, 18, 128, 112, 143, 185,
            199, 178, 56, 0, 141, 232, 12, 201,
        ];
        let secp = Secp256k1::without_caps();
        let recovery = RecoveryId::from_i32(0).unwrap();
        let signature =
            RecoverableSignature::from_compact(&secp, &signature_bytes, recovery).unwrap();
        let signed_genesis_tx = SignedGenesisTx::new(to, amount, signature, recovery);
        let encoding = signed_genesis_tx.encode().unwrap();
        let decoded_signed_genesis_tx = SignedGenesisTx::decode(&encoding).unwrap();
        assert_eq!(signed_genesis_tx, decoded_signed_genesis_tx);
    }

    #[test]
    #[should_panic]
    fn it_fails_to_decode_random_bad_bytes() {
        let mut random_bytes = [0u8; 256];
        thread_rng().fill(&mut random_bytes);
        SignedGenesisTx::decode(&random_bytes.to_vec()).unwrap();
    }
}