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
#[cfg(feature = "std")]
use std::{
    fs::{self, File},
    io::{self, Read},
    path::Path,
};

use miden_crypto::utils::SliceReader;

use super::{
    super::utils::serde::{
        ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
    },
    Account, Word,
};
use crate::utils::format;

// ACCOUNT DATA
// ================================================================================================

/// Account data contains a complete description of an account, including the [Account] struct as
/// well as account seed and account authentication info.
///
/// The intent of this struct is to provide an easy way to serialize and deserialize all
/// account-related data as a single unit (e.g., to/from files).
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct AccountData {
    pub account: Account,
    pub account_seed: Option<Word>,
    pub auth: AuthData,
}

impl AccountData {
    pub fn new(account: Account, account_seed: Option<Word>, auth: AuthData) -> Self {
        Self { account, account_seed, auth }
    }

    #[cfg(feature = "std")]
    /// Serialises and writes binary AccountData to specified file
    pub fn write(&self, filepath: impl AsRef<Path>) -> io::Result<()> {
        fs::write(filepath, self.to_bytes())
    }

    #[cfg(feature = "std")]
    /// Reads from file and tries to deserialise an AccountData
    pub fn read(filepath: impl AsRef<Path>) -> io::Result<Self> {
        let mut file = File::open(filepath)?;
        let mut buffer = Vec::new();

        file.read_to_end(&mut buffer)?;
        let mut reader = SliceReader::new(&buffer);

        Ok(AccountData::read_from(&mut reader).map_err(|_| io::ErrorKind::InvalidData)?)
    }
}

// SERIALIZATION
// ================================================================================================

impl Serializable for AccountData {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        let AccountData { account, account_seed, auth } = self;

        account.write_into(target);
        account_seed.write_into(target);
        auth.write_into(target);
    }
}

impl Deserializable for AccountData {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let account = Account::read_from(source)?;
        let account_seed = <Option<Word>>::read_from(source)?;
        let auth = AuthData::read_from(source)?;

        Ok(Self::new(account, account_seed, auth))
    }

    fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError> {
        Self::read_from(&mut SliceReader::new(bytes))
    }
}

// AUTH DATA
// ================================================================================================

/// AuthData is a representation of the AuthScheme struct meant to be used
/// for Account serialisation and deserialisation for transport of Account data
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum AuthData {
    RpoFalcon512Seed([u8; 40]),
}

// SERIALIZATION
// ================================================================================================

impl Serializable for AuthData {
    fn write_into<W: ByteWriter>(&self, target: &mut W) {
        match self {
            AuthData::RpoFalcon512Seed(seed) => {
                0_u8.write_into(target);
                seed.write_into(target);
            },
        }
    }
}

impl Deserializable for AuthData {
    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
        let scheme = u8::read_from(source)?;
        match scheme {
            0 => {
                let seed = <[u8; 40]>::read_from(source)?;
                Ok(AuthData::RpoFalcon512Seed(seed))
            },
            value => Err(DeserializationError::InvalidValue(format!("Invalid value: {}", value))),
        }
    }

    fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError> {
        Self::read_from(&mut SliceReader::new(bytes))
    }
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
    use assembly::{ast::ModuleAst, Assembler};
    use miden_crypto::utils::{Deserializable, Serializable};
    use storage::AccountStorage;
    use tempfile::tempdir;

    use super::{AccountData, AuthData};
    use crate::{
        accounts::{
            storage, Account, AccountCode, AccountId, Felt, Word,
            ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN,
        },
        assets::AssetVault,
    };

    fn build_account_data() -> AccountData {
        // create account id
        let id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN).unwrap();

        // build account code
        let source = "
            export.foo
                push.1 push.2 mul
            end

            export.bar
                push.1 push.2 add
            end
        ";
        let module = ModuleAst::parse(source).unwrap();
        let assembler = Assembler::default();
        let code = AccountCode::new(module, &assembler).unwrap();

        // create account and auth
        let vault = AssetVault::new(&[]).unwrap();
        let storage = AccountStorage::new(vec![]).unwrap();
        let nonce = Felt::new(0);
        let account = Account::new(id, vault, storage, code, nonce);
        let account_seed = Some(Word::default());
        let auth_seed = [0u8; 40];
        let auth = AuthData::RpoFalcon512Seed(auth_seed);

        // create AccountData
        AccountData::new(account, account_seed, auth)
    }

    #[test]
    fn account_data_correctly_serialises_and_deserialises() {
        // create AccountData
        let account_data = build_account_data();

        // serialize and deserialize the code; make sure deserialized version matches the original
        let bytes = account_data.to_bytes();
        let account_data_2 = AccountData::read_from_bytes(&bytes).unwrap();
        assert_eq!(account_data, account_data_2);
    }

    #[test]
    fn account_data_is_correctly_writen_and_read_to_and_from_file() {
        // setup temp directory
        let dir = tempdir().unwrap();
        let filepath = dir.path().join("account_data.mac");

        // create AccountData
        let account_data = build_account_data();

        // write AccountData to file
        account_data.write(filepath.as_path()).unwrap();

        // read AccountData from file
        let account_data_2 = AccountData::read(filepath.as_path()).unwrap();

        // make sure deserialized version matches the original
        assert_eq!(account_data, account_data_2)
    }
}