Skip to content

Commit

Permalink
Add get_buffer_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
jarry-xiao committed Jan 30, 2023
1 parent 66e7058 commit 0adce9b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
72 changes: 35 additions & 37 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ enum SubCommand {
#[clap(short, long)]
base_image: Option<String>,
},
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")]
Expand All @@ -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<usize>,
buffer_address: Pubkey,
},
}

Expand All @@ -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::<Vec<_>>();
buffer = buffer.iter().map(|x| *x).rev().collect::<Vec<_>>();
}
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::<Vec<_>>();
buffer = buffer.iter().map(|x| *x).rev().collect::<Vec<_>>();
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<u8>) -> String {
let mut hasher = Sha1::new();
let mut buffer = bytes
.into_iter()
.rev()
.skip_while(|&x| x == 0)
.collect::<Vec<_>>();
buffer = buffer.iter().map(|x| *x).rev().collect::<Vec<_>>();
hasher.update(&buffer);
let program_hash = hasher.finalize();
hex::encode(program_hash)
}

pub fn build(filepath: Option<String>, base_image: Option<String>) -> anyhow::Result<()> {
let path = filepath.unwrap_or(
std::env::current_dir()?
Expand Down

0 comments on commit 0adce9b

Please sign in to comment.