Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename AccountData struct #1116

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [BREAKING] Moved `generated` module from `miden-proving-service-client` crate to `tx_prover::generated` hierarchy (#1102).
- Added an endpoint to the `miden-proving-service` to update the workers (#1107).
- Renamed the protobuf file of the transaction prover to `tx_prover.proto` (#1110).
- [BREAKING] Renamed `AccountData` to `AccountFile` (#1116).

## 0.7.2 (2025-01-28) - `miden-objects` crate only

Expand All @@ -22,7 +23,6 @@
- Added missing doc comments (#1100).
- Fixed setting of supporting types when instantiating `AccountComponent` from templates (#1103).


## 0.7.0 (2025-01-22)

### Highlights
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ use super::{
Account, AuthSecretKey, Word,
};

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

/// Account data contains a complete description of an account, including the [Account] struct as
/// Account file 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, Clone)]
pub struct AccountData {
pub struct AccountFile {
pub account: Account,
pub account_seed: Option<Word>,
pub auth_secret_key: AuthSecretKey,
}

impl AccountData {
impl AccountFile {
pub fn new(account: Account, account_seed: Option<Word>, auth: AuthSecretKey) -> Self {
Self {
account,
Expand All @@ -39,31 +39,31 @@ impl AccountData {
}
}

/// Serialises and writes binary [AccountFile] to specified file
#[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())
}

/// Reads from file and tries to deserialise an [AccountFile]
#[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)?)
Ok(AccountFile::read_from(&mut reader).map_err(|_| io::ErrorKind::InvalidData)?)
}
}

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

impl Serializable for AccountData {
impl Serializable for AccountFile {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let AccountData {
let AccountFile {
account,
account_seed,
auth_secret_key: auth,
Expand All @@ -75,7 +75,7 @@ impl Serializable for AccountData {
}
}

impl Deserializable for AccountData {
impl Deserializable for AccountFile {
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)?;
Expand All @@ -102,14 +102,14 @@ mod tests {
#[cfg(feature = "std")]
use tempfile::tempdir;

use super::AccountData;
use super::AccountFile;
use crate::{
account::{storage, Account, AccountCode, AccountId, AuthSecretKey, Felt, Word},
asset::AssetVault,
testing::account_id::ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN,
};

fn build_account_data() -> AccountData {
fn build_account_file() -> AccountFile {
let id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN).unwrap();
let code = AccountCode::mock();

Expand All @@ -121,37 +121,37 @@ mod tests {
let account_seed = Some(Word::default());
let auth_secret_key = AuthSecretKey::RpoFalcon512(SecretKey::new());

AccountData::new(account, account_seed, auth_secret_key)
AccountFile::new(account, account_seed, auth_secret_key)
}

#[test]
fn test_serde() {
let account_data = build_account_data();
let serialized = account_data.to_bytes();
let deserialized = AccountData::read_from_bytes(&serialized).unwrap();
assert_eq!(deserialized.account, account_data.account);
assert_eq!(deserialized.account_seed, account_data.account_seed);
let account_file = build_account_file();
let serialized = account_file.to_bytes();
let deserialized = AccountFile::read_from_bytes(&serialized).unwrap();
assert_eq!(deserialized.account, account_file.account);
assert_eq!(deserialized.account_seed, account_file.account_seed);
assert_eq!(
deserialized.auth_secret_key.to_bytes(),
account_data.auth_secret_key.to_bytes()
account_file.auth_secret_key.to_bytes()
);
}

#[cfg(feature = "std")]
#[test]
fn test_serde_file() {
let dir = tempdir().unwrap();
let filepath = dir.path().join("account_data.mac");
let filepath = dir.path().join("account_file.mac");

let account_data = build_account_data();
account_data.write(filepath.as_path()).unwrap();
let deserialized = AccountData::read(filepath.as_path()).unwrap();
let account_file = build_account_file();
account_file.write(filepath.as_path()).unwrap();
let deserialized = AccountFile::read(filepath.as_path()).unwrap();

assert_eq!(deserialized.account, account_data.account);
assert_eq!(deserialized.account_seed, account_data.account_seed);
assert_eq!(deserialized.account, account_file.account);
assert_eq!(deserialized.account_seed, account_file.account_seed);
assert_eq!(
deserialized.auth_secret_key.to_bytes(),
account_data.auth_secret_key.to_bytes()
account_file.auth_secret_key.to_bytes()
);
}
}
4 changes: 2 additions & 2 deletions crates/miden-objects/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub use storage::{AccountStorage, AccountStorageHeader, StorageMap, StorageSlot,
mod header;
pub use header::AccountHeader;

mod data;
pub use data::AccountData;
mod file;
pub use file::AccountFile;

// ACCOUNT
// ================================================================================================
Expand Down
Loading