diff --git a/README.md b/README.md index 99a8e82..d1e9be1 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Check it out here: https://solana.fm/address/2ZrriTQSVekoj414Ynysd48jyn4AX6ZF4TT Run the following command: ``` -verifier-cli verify -e examples/hello_world/target/deploy/hello_world.so -i ellipsislabs/hello_world_verifiable_build:latest -p 2ZrriTQSVekoj414Ynysd48jyn4AX6ZF4TTJRqHfbJfn +verifier-cli verify-from-image -e examples/hello_world/target/deploy/hello_world.so -i ellipsislabs/hello_world_verifiable_build:latest -p 2ZrriTQSVekoj414Ynysd48jyn4AX6ZF4TTJRqHfbJfn ``` This the output: diff --git a/src/main.rs b/src/main.rs index 6bfbcb1..52bee43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,9 +24,9 @@ enum SubCommand { #[clap(short, long)] base_image: Option, }, - Verify { + VerifyFromImage { #[clap(short, long)] - executable_path: String, + executable_path_in_image: String, #[clap(short, long)] image: String, #[clap(short, long, default_value = "https://api.mainnet-beta.solana.com")] @@ -37,16 +37,18 @@ enum SubCommand { GetExecutableHash { #[clap(short, long)] filepath: String, - #[clap(short, long, default_value = "false")] - strip: bool, }, GetProgramHash { #[clap(short, long, default_value = "https://api.mainnet-beta.solana.com")] url: String, #[clap(short, long)] program_id: Pubkey, + }, + GetBufferHash { + #[clap(short, long, default_value = "https://api.mainnet-beta.solana.com")] + url: String, #[clap(short, long)] - length: Option, + buffer_address: Pubkey, }, } @@ -57,63 +59,59 @@ fn main() -> anyhow::Result<()> { filepath, base_image, } => build(filepath, base_image), - SubCommand::Verify { - executable_path, + SubCommand::VerifyFromImage { + executable_path_in_image: executable_path, image, url: network, program_id, } => verify(executable_path, image, network, program_id), - SubCommand::GetExecutableHash { filepath, strip } => { + SubCommand::GetExecutableHash { filepath } => { let mut f = std::fs::File::open(&filepath)?; let metadata = std::fs::metadata(&filepath)?; let mut buffer = vec![0; metadata.len() as usize]; f.read(&mut buffer)?; - if strip { - buffer = buffer - .into_iter() - .rev() - .skip_while(|&x| x == 0) - .collect::>(); - buffer = buffer.iter().map(|x| *x).rev().collect::>(); - } - let mut hasher = Sha1::new(); - hasher.update(&buffer); - let program_hash = hasher.finalize(); + let program_hash = get_binary_hash(buffer); println!("{}", hex::encode(program_hash)); Ok(()) } - SubCommand::GetProgramHash { + SubCommand::GetBufferHash { url, - program_id, - length, + buffer_address, } => { + let client = RpcClient::new(url); + let offset = UpgradeableLoaderState::size_of_buffer_metadata(); + let account_data = client.get_account_data(&buffer_address)?[offset..].to_vec(); + let program_hash = get_binary_hash(account_data); + println!("{}", hex::encode(program_hash)); + Ok(()) + } + SubCommand::GetProgramHash { url, program_id } => { let client = RpcClient::new(url); let program_buffer = Pubkey::find_program_address(&[program_id.as_ref()], &bpf_loader_upgradeable::id()) .0; let offset = UpgradeableLoaderState::size_of_programdata_metadata(); - let account_data = client.get_account_data(&program_buffer)?[offset..].to_vec(); - let buffer = if let Some(l) = length { - account_data[..l].to_vec() - } else { - let mut buffer = account_data - .into_iter() - .rev() - .skip_while(|&x| x == 0) - .collect::>(); - buffer = buffer.iter().map(|x| *x).rev().collect::>(); - buffer - }; - let mut hasher = Sha1::new(); - hasher.update(&buffer); - let program_hash = hasher.finalize(); + let program_hash = get_binary_hash(account_data); println!("{}", hex::encode(program_hash)); Ok(()) } } } +pub fn get_binary_hash(bytes: Vec) -> String { + let mut hasher = Sha1::new(); + let mut buffer = bytes + .into_iter() + .rev() + .skip_while(|&x| x == 0) + .collect::>(); + buffer = buffer.iter().map(|x| *x).rev().collect::>(); + hasher.update(&buffer); + let program_hash = hasher.finalize(); + hex::encode(program_hash) +} + pub fn build(filepath: Option, base_image: Option) -> anyhow::Result<()> { let path = filepath.unwrap_or( std::env::current_dir()?