Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ predicates = { workspace = true }
walkdir = "2.5.0"
mockito = "1.5.0"
serial_test = "3.0.0"
wasm-encoder = "0.235.0"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

wasm-encoder is already under [dev-dependencies] — it's at line 145, inside the [dev-dependencies] block that starts at line 138. It's only referenced by the #[cfg(test)] module in interface.rs, so it doesn't add a runtime/build dependency for non-test builds. No change needed here.

66 changes: 57 additions & 9 deletions cmd/soroban-cli/src/commands/contract/info/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,7 @@ impl Cmd {
let Fetched { contract, .. } = fetch(&self.common, &print).await?;

let (base64, spec) = match contract {
shared::Contract::Wasm { wasm_bytes } => {
let spec = Spec::new(&wasm_bytes)?;

if spec.env_meta_base64.is_none() {
return Err(NoInterfacePresent());
}

(spec.spec_base64.unwrap(), spec.spec)
}
shared::Contract::Wasm { wasm_bytes } => spec_from_wasm(&wasm_bytes)?,
shared::Contract::StellarAssetContract => {
Spec::spec_to_base64(stellar_asset_spec::xdr())?
}
Expand All @@ -85,3 +77,59 @@ impl Cmd {
Ok(())
}
}

fn spec_from_wasm(wasm_bytes: &[u8]) -> Result<(String, Vec<crate::xdr::ScSpecEntry>), Error> {
let spec = Spec::new(wasm_bytes)?;

if spec.env_meta_base64.is_none() {
return Err(NoInterfacePresent());
}

// `contractenvmetav0` and `contractspecv0` are independent custom
// sections, so the presence of env meta does not imply a spec is present.
let Some(base64) = spec.spec_base64 else {
return Err(NoInterfacePresent());
};

Ok((base64, spec.spec))
}

#[cfg(test)]
mod tests {
use super::*;
use std::borrow::Cow;

/// A minimal WASM module containing only the given custom sections.
fn wasm_with_custom_sections(sections: &[(&str, &[u8])]) -> Vec<u8> {
let mut module = wasm_encoder::Module::new();
for (name, data) in sections {
module.section(&wasm_encoder::CustomSection {
name: Cow::Borrowed(name),
data: Cow::Borrowed(data),
});
}
module.finish()
}

#[test]
fn env_meta_without_spec_returns_no_interface_error() {
// Regression test for https://github.com/stellar/stellar-cli/issues/2429:
// `contractenvmetav0` and `contractspecv0` are independent custom
// sections, so a WASM can carry env meta without a spec. This used to
// panic on `spec.spec_base64.unwrap()`.
let wasm = wasm_with_custom_sections(&[("contractenvmetav0", &[])]);

let result = spec_from_wasm(&wasm);

assert!(matches!(result, Err(Error::NoInterfacePresent())));

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Checked this against the enum definition — NoInterfacePresent is a zero-field tuple variant (declared NoInterfacePresent() at line 41), not a unit variant. For a zero-field tuple variant, Err(Error::NoInterfacePresent()) is the correct pattern. The two suggested alternatives don't compile here: Error::NoInterfacePresent fails with E0532 (expected unit variant, found tuple variant) and Error::NoInterfacePresent(_) fails with E0023 (pattern has 1 field, variant has 0). CI compiles and passes on the current form, so I'll keep matches!(result, Err(Error::NoInterfacePresent())).

}

#[test]
fn missing_env_meta_returns_no_interface_error() {
let wasm = wasm_with_custom_sections(&[]);

let result = spec_from_wasm(&wasm);

assert!(matches!(result, Err(Error::NoInterfacePresent())));

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same as the assertion above: NoInterfacePresent is a zero-field tuple variant (defined NoInterfacePresent() at line 41), so Err(Error::NoInterfacePresent()) is the correct pattern. Error::NoInterfacePresent would fail with E0532 and Error::NoInterfacePresent(_) with E0023 — neither compiles. Keeping the current form; CI confirms it builds and passes.

}
}