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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use algonaut_crypto::HashDigest;
use algonaut_crypto::Signature;
use algonaut_encoding::U8_32Visitor;
use data_encoding::BASE64;
use derive_more::{Add, Display, Sub};
use error::CoreError;
pub use multisig::MultisigSignature;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::Digest;
use static_assertions::_core::ops::{Add, Sub};
use std::convert::TryInto;
use std::fmt::{self, Debug, Formatter};
use std::ops::Mul;

pub use address::Address;
pub use address::MultisigAddress;
pub use multisig::MultisigSubsig;

mod address;
pub mod error;
mod multisig;

pub const MICRO_ALGO_CONVERSION_FACTOR: f64 = 1e6;

/// MicroAlgos are the base unit of currency in Algorand
#[derive(
    Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Serialize, Deserialize, Display, Add, Sub,
)]
pub struct MicroAlgos(pub u64);

impl Add<u64> for MicroAlgos {
    type Output = Self;

    fn add(self, rhs: u64) -> Self::Output {
        MicroAlgos(self.0 + rhs)
    }
}

impl Sub<u64> for MicroAlgos {
    type Output = Self;

    fn sub(self, rhs: u64) -> Self::Output {
        MicroAlgos(self.0 - rhs)
    }
}

// Intentionally not implementing Mul<Rhs=Self>
// If you're multiplying a MicroAlgos by MicroAlgos, something has gone wrong in your math
// That would give you MicroAlgos squared and those don't exist
impl Mul<u64> for MicroAlgos {
    type Output = Self;

    fn mul(self, rhs: u64) -> Self::Output {
        MicroAlgos(self.0 * rhs)
    }
}

/// Round of the Algorand consensus protocol
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize, Display, Add, Sub)]
pub struct Round(pub u64);

impl Add<u64> for Round {
    type Output = Self;

    fn add(self, rhs: u64) -> Self::Output {
        Round(self.0 + rhs)
    }
}

impl Sub<u64> for Round {
    type Output = Self;

    fn sub(self, rhs: u64) -> Self::Output {
        Round(self.0 - rhs)
    }
}

// Intentionally not implementing Mul<Rhs=Self>
// If you're multiplying a Round by a Round, something has gone wrong in your math
// That would give you Rounds squared and those don't exist
impl Mul<u64> for Round {
    type Output = Self;

    fn mul(self, rhs: u64) -> Self::Output {
        Round(self.0 * rhs)
    }
}

impl From<u64> for Round {
    fn from(u: u64) -> Self {
        Self(u)
    }
}

/// Participation public key used in key registration transactions
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct VotePk(pub [u8; 32]);

impl Serialize for VotePk {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(&self.0[..])
    }
}

impl<'de> Deserialize<'de> for VotePk {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(VotePk(deserializer.deserialize_bytes(U8_32Visitor)?))
    }
}

impl Debug for VotePk {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_base64_str())
    }
}

impl VotePk {
    pub fn from_base64_str(base64_str: &str) -> Result<VotePk, CoreError> {
        Ok(VotePk(base64_str_to_u8_array(base64_str)?))
    }

    pub fn to_base64_str(self) -> String {
        BASE64.encode(&self.0)
    }
}

/// VRF public key used in key registration transaction
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct VrfPk(pub [u8; 32]);

impl Serialize for VrfPk {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        serializer.serialize_bytes(&self.0[..])
    }
}

impl<'de> Deserialize<'de> for VrfPk {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(VrfPk(deserializer.deserialize_bytes(U8_32Visitor)?))
    }
}

impl Debug for VrfPk {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_base64_str())
    }
}

impl VrfPk {
    pub fn from_base64_str(base64_str: &str) -> Result<VrfPk, CoreError> {
        Ok(VrfPk(base64_str_to_u8_array(base64_str)?))
    }

    pub fn to_base64_str(self) -> String {
        BASE64.encode(&self.0)
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
pub struct CompiledTeal(pub Vec<u8>);

impl CompiledTeal {
    pub fn bytes_to_sign(&self) -> Vec<u8> {
        let mut prefix_encoded_tx = b"Program".to_vec();
        prefix_encoded_tx.extend_from_slice(&self.0);
        prefix_encoded_tx
    }

    pub fn hash(&self) -> HashDigest {
        HashDigest(sha2::Sha512_256::digest(&self.bytes_to_sign()).into())
    }
}

impl From<HashDigest> for Address {
    fn from(digest: HashDigest) -> Self {
        Address(digest.0)
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum LogicSignature {
    ContractAccount,
    DelegatedSig(Signature),
    DelegatedMultiSig(MultisigSignature),
}

pub trait ToMsgPack: Serialize {
    fn to_msg_pack(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
        rmp_serde::to_vec_named(&self)
    }
}

fn base64_str_to_u8_array<const N: usize>(base64_str: &str) -> Result<[u8; N], CoreError> {
    BASE64
        .decode(base64_str.as_bytes())?
        .try_into()
        .map_err(|v| CoreError::General(format!("Couldn't convert vec: {:?} into u8 array", v)))
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct SuggestedTransactionParams {
    pub genesis_id: String,
    pub genesis_hash: HashDigest,
    pub consensus_version: String,
    pub fee_per_byte: MicroAlgos,
    pub min_fee: MicroAlgos,
    pub first_valid: Round,
    pub last_valid: Round,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TransactionTypeEnum {
    Payment,
    KeyRegistration,
    AssetConfiguration,
    AssetTransfer,
    AssetFreeze,
    ApplicationCall,
    StateProof,
}

impl TransactionTypeEnum {
    pub fn to_api_str(&self) -> &str {
        match self {
            TransactionTypeEnum::Payment => "pay",
            TransactionTypeEnum::KeyRegistration => "keyreg",
            TransactionTypeEnum::AssetConfiguration => "acfg",
            TransactionTypeEnum::AssetTransfer => "axfer",
            TransactionTypeEnum::AssetFreeze => "afrz",
            TransactionTypeEnum::ApplicationCall => "appl",
            TransactionTypeEnum::StateProof => "stpf",
        }
    }

    pub fn from_api_str(s: &str) -> Result<Self, CoreError> {
        match s {
            "pay" => Ok(TransactionTypeEnum::Payment),
            "keyreg" => Ok(TransactionTypeEnum::KeyRegistration),
            "acfg" => Ok(TransactionTypeEnum::AssetConfiguration),
            "axfer" => Ok(TransactionTypeEnum::AssetTransfer),
            "afrz" => Ok(TransactionTypeEnum::AssetFreeze),
            "appl" => Ok(TransactionTypeEnum::ApplicationCall),
            "stpf" => Ok(TransactionTypeEnum::StateProof),
            _ => Err(CoreError::General(format!(
                "Couldn't convert tx type str: `{s}` to tx type"
            ))),
        }
    }
}

/// Returns the address corresponding to an application's escrow account.
pub fn to_app_address(app_id: u64) -> Address {
    let bytes = app_id.to_be_bytes();
    let all_bytes = ["appID".as_bytes(), &bytes].concat();
    let hash = sha2::Sha512_256::digest(all_bytes);
    Address(hash.into())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn computes_program_address_correctly() {
        let program = CompiledTeal(vec![
            4, 54, 26, 0, 128, 3, 1, 0, 255, 18, 54, 26, 1, 23, 129, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 1, 18, 16, 54, 26, 2, 128, 32, 98, 162, 25, 173, 185, 140, 183, 76, 228,
            114, 235, 172, 245, 191, 248, 121, 232, 54, 170, 229, 161, 91, 215, 180, 73, 219, 245,
            120, 155, 252, 59, 92, 18, 16,
        ]);

        let digest = program.hash();

        assert_eq!(
            HashDigest([
                45, 117, 175, 55, 21, 23, 57, 110, 158, 143, 60, 222, 234, 143, 168, 69, 75, 239,
                131, 112, 96, 73, 79, 174, 120, 245, 181, 40, 236, 158, 233, 234,
            ]),
            digest
        );

        // Note that this address is also the "address style hash" string we get from the API in ApiCompiledTeal
        assert_eq!(
            "FV226NYVC44W5HUPHTPOVD5IIVF67A3QMBEU7LTY6W2SR3E65HVOQ7JV44",
            Address::new(digest.0).to_string()
        );
    }
}