Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Check built Wasm file against that in the reference metadata
  • Loading branch information
HCastano committed Oct 20, 2022
commit 7de595c5d9d79db42a5141fe82042ca1aec1470c
39 changes: 35 additions & 4 deletions crates/cargo-contract/src/cmd/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ impl VerifyCommand {
let build_info = metadata.source.build_info.as_ref().unwrap();
let build_info: BuildInfo =
serde_json::from_value(build_info.clone().into()).unwrap();
dbg!(&build_info);

// 2. Call `cmd::Build` with the given `BuildInfo`
let args = ExecuteArgs {
manifest_path,
manifest_path: manifest_path.clone(),
verbosity: Default::default(),
build_mode: build_info.build_mode,
network: Default::default(),
Expand All @@ -74,9 +73,41 @@ impl VerifyCommand {
output_type: Default::default(),
};

let _build_result = execute(args)?;
let build_result = execute(args)?;

// 3. Read output file, compare with given contract_wasm
todo!()
let reference_wasm = metadata.source.wasm.unwrap().to_string();

let built_wasm_path = build_result.dest_wasm.unwrap();
let fs_wasm = std::fs::read(built_wasm_path)?;
let built_wasm = build_byte_str(&fs_wasm);

if reference_wasm != built_wasm {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should also compare the contract ABI metadata itself? They should be identical too, and if not it might give clues as to what (if anything) is different.

log::debug!(
"Expected Wasm Binary '{}'\n\nGot Wasm Binary `{}`",
&reference_wasm,
&built_wasm
);
anyhow::bail!(
"Failed to verify the authenticity of `{}` contract againt the workspace found at {:?}.",
metadata.contract.name,
manifest_path.as_ref(),
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be super useful to print out the SourceCompiler properties of both reference_wasm and built_wasm in case of non matching wasm.

It's useful to know whether they are the same or different for figuring out why verification failed.

And while we are at it, any other properties we have available in Source e.g. SourceLanguage to catch different versions of ink!.

}

log::info!("Succesfully verified `{}`!", &metadata.contract.name);

Ok(())
}
}

fn build_byte_str(bytes: &[u8]) -> String {
use std::fmt::Write;

let mut str = String::new();
write!(str, "0x").expect("failed writing to string");
for byte in bytes {
write!(str, "{:02x}", byte).expect("failed writing to string");
}
str
}