From b2f69d230416129a84ad8237ccc13d088992f74b Mon Sep 17 00:00:00 2001 From: Olivier Desenfans Date: Tue, 23 Jan 2024 13:04:26 +0100 Subject: [PATCH] Feature: deserialize Cairo PIE from bytes Added a `from_bytes` class method to build a `CairoPie` method in addition to the existing `from_file` method. --- vm/src/vm/runners/cairo_pie.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/vm/src/vm/runners/cairo_pie.rs b/vm/src/vm/runners/cairo_pie.rs index caa35e91ee..e2f744a3b7 100644 --- a/vm/src/vm/runners/cairo_pie.rs +++ b/vm/src/vm/runners/cairo_pie.rs @@ -218,7 +218,7 @@ impl CairoPie { pub fn from_zip_archive( mut zip: zip::ZipArchive, - ) -> Result { + ) -> Result { let metadata: CairoPieMetadata = parse_zip_file(zip.by_name("metadata.json")?)?; let execution_resources: ExecutionResources = parse_zip_file(zip.by_name("execution_resources.json")?)?; @@ -235,7 +235,7 @@ impl CairoPie { }; let memory = read_memory_file(zip.by_name("memory.bin")?, addr_size, felt_bytes)?; - Ok(CairoPie { + Ok(Self { metadata, memory, execution_resources, @@ -245,11 +245,19 @@ impl CairoPie { } #[cfg(feature = "std")] - pub fn from_file(path: &Path) -> Result { + pub fn from_bytes(bytes: &[u8]) -> Result { + let reader = std::io::Cursor::new(bytes); + let zip_archive = zip::ZipArchive::new(reader)?; + + Self::from_zip_archive(zip_archive) + } + + #[cfg(feature = "std")] + pub fn from_file(path: &Path) -> Result { let file = std::fs::File::open(path)?; let zip = zip::ZipArchive::new(file)?; - CairoPie::from_zip_archive(zip) + Self::from_zip_archive(zip) } }