Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
73 changes: 73 additions & 0 deletions cmd/soroban-cli/src/commands/contract/info/wasm_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use clap::{arg, command, Command, ArgMatches};
use soroban_rpc::client::Client;
use soroban_xdr::{ScAddress, ScVal, DecodeError};
use stellar_xdr::XdrCodec;
use crate::config::Config;
use crate::error::Result;
use clap::Parser;
use stellar_xdr::curr as xdr;

use crate::commands::{config::network, global};
use crate::config::locator;
use crate::rpc;

#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,

#[command(flatten)]
pub network: network::Args,

/// Contract ID to get the wasm hash for
#[arg(long = "contract-id")]
pub contract_id: stellar_strkey::Contract,
Copy link
Member

Choose a reason for hiding this comment

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

This should be a string, that is then used with alias resolution. Worth looking at other commands that accept a contract ID to see how this works.

}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),

#[error(transparent)]
Network(#[from] network::Error),

#[error(transparent)]
Rpc(#[from] rpc::Error),
}

impl Cmd {
pub async fn run(&self, _global_args: &global::Args) -> Result<(), Error> {
let network = self.network.get(&self.config_locator)?;
let client = network.get_client()?;

// Get the contract instance ledger entry
let key = xdr::LedgerKey::ContractData(xdr::LedgerKeyContractData {
contract: self.contract_id.clone().into(),
key: xdr::ScVal::LedgerKeyContractInstance,
durability: xdr::ContractDataDurability::Persistent,
});

let entry = client.get_ledger_entry(&key).await?;

// Extract the wasm hash from the contract instance
if let xdr::LedgerEntryData::ContractData(data) = entry.data {
if let xdr::ScVal::ContractInstance(instance) = data.val {
if let xdr::ContractExecutable::Wasm(hash) = instance.executable {
println!("{}", hex::encode(hash.0));
return Ok(());
}
}
}

Err(rpc::Error::InvalidResponse("Contract instance not found".into()).into())
Comment on lines +57 to +64
Copy link
Member

@leighmcculloch leighmcculloch Sep 2, 2025

Choose a reason for hiding this comment

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

This code should handle the other contract executable types, and emit the name of the executable if not a wasm-hash. See my other comment here: #1878 (comment).

}
}

pub fn subcommand() -> Command {
command!("wasm-hash")
.about("Retrieve the wasm hash of a contract")
.arg(arg!(--network <NETWORK> "The network to connect to"))
.arg(arg!(--contract-id <CONTRACT_ID> "The contract ID"))
}
8 changes: 8 additions & 0 deletions cmd/soroban-cli/src/commands/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod optimize;
pub mod read;
pub mod restore;
pub mod upload;
pub mod wasm_hash;

use crate::{commands::global, print::Print};

Expand Down Expand Up @@ -91,6 +92,9 @@ pub enum Cmd {
///
/// If no keys are specificed the contract itself is restored.
Restore(restore::Cmd),

/// Get the wasm hash of a deployed contract
WasmHash(wasm_hash::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -142,6 +146,9 @@ pub enum Error {

#[error(transparent)]
Restore(#[from] restore::Error),

#[error(transparent)]
WasmHash(#[from] wasm_hash::Error),
}

impl Cmd {
Expand Down Expand Up @@ -169,6 +176,7 @@ impl Cmd {
Cmd::Fetch(fetch) => fetch.run().await?,
Cmd::Read(read) => read.run().await?,
Cmd::Restore(restore) => restore.run().await?,
Cmd::WasmHash(cmd) => cmd.run(global_args).await?,
}
Ok(())
}
Expand Down
Loading