Skip to content

Commit

Permalink
Merge branch 'main' into events-with-alias-support
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Jun 25, 2024
2 parents 86b2372 + 7984cd0 commit 0c6583f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
13 changes: 11 additions & 2 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub enum Error {
CannotParseContractId(String, DecodeError),
#[error(transparent)]
UtilsError(#[from] get_spec::Error),
#[error(transparent)]
Config(#[from] config::Error),
}

#[async_trait::async_trait]
Expand All @@ -87,8 +89,15 @@ impl NetworkRunnable for Cmd {
let wasm: wasm::Args = wasm.into();
wasm.parse()?.spec
} else {
let contract_id = soroban_spec_tools::utils::contract_id_from_str(&self.contract_id)
.map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e))?;
let network = config.map_or_else(
|| self.network.get(&self.locator).map_err(Error::from),
|c| c.get_network().map_err(Error::from),
)?;

let contract_id = self
.locator
.resolve_contract_id(&self.contract_id, &network.network_passphrase)?;

get_remote_contract_spec(
&contract_id,
&self.locator,
Expand Down
55 changes: 40 additions & 15 deletions cmd/soroban-cli/src/commands/contract/deploy/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Cmd {
/// Whether to ignore safety checks when deploying contracts
pub ignore_checks: bool,
/// The alias that will be used to save the contract's id.
#[arg(long)]
#[arg(long, value_parser = clap::builder::ValueParser::new(alias_validator))]
pub alias: Option<String>,
}

Expand Down Expand Up @@ -114,8 +114,6 @@ pub enum Error {

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
self.validate_alias()?;

let res = self.run_against_rpc_server(None, None).await?.to_envelope();
match res {
TxnEnvelopeResult::TxnEnvelope(tx) => println!("{}", tx.to_xdr_base64(Limits::none())?),
Expand All @@ -135,20 +133,17 @@ impl Cmd {
}
Ok(())
}
}

fn validate_alias(&self) -> Result<(), Error> {
match self.alias.clone() {
Some(alias) => {
let regex = Regex::new(r"^[a-zA-Z0-9_-]{1,30}$").unwrap();
fn alias_validator(alias: &str) -> Result<String, Error> {
let regex = Regex::new(r"^[a-zA-Z0-9_-]{1,30}$").unwrap();

if regex.is_match(&alias) {
Ok(())
} else {
Err(Error::InvalidAliasFormat { alias })
}
}
None => Ok(()),
}
if regex.is_match(alias) {
Ok(alias.into())
} else {
Err(Error::InvalidAliasFormat {
alias: alias.into(),
})
}
}

Expand Down Expand Up @@ -306,4 +301,34 @@ mod tests {

assert!(result.is_ok());
}

#[test]
fn test_alias_validator_with_valid_inputs() {
let valid_inputs = [
"hello",
"123",
"hello123",
"hello_123",
"123_hello",
"123-hello",
"hello-123",
"HeLlo-123",
];

for input in valid_inputs {
let result = alias_validator(input);
assert!(result.is_ok());
assert!(result.unwrap() == input);
}
}

#[test]
fn test_alias_validator_with_invalid_inputs() {
let invalid_inputs = ["", "invalid!", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];

for input in invalid_inputs {
let result = alias_validator(input);
assert!(result.is_err());
}
}
}
8 changes: 5 additions & 3 deletions cmd/soroban-cli/src/commands/contract/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::commands::network::{self, Network};
use crate::commands::{global, NetworkRunnable};
use crate::{
rpc::{self, Client},
utils, Pwd,
Pwd,
};

#[derive(Parser, Debug, Default, Clone)]
Expand Down Expand Up @@ -124,8 +124,10 @@ impl Cmd {
}

fn contract_id(&self) -> Result<[u8; 32], Error> {
utils::contract_id_from_str(&self.contract_id)
.map_err(|e| Error::CannotParseContractId(self.contract_id.clone(), e))
let network = self.network()?;
Ok(self
.locator
.resolve_contract_id(&self.contract_id, &network.network_passphrase)?)
}
}

Expand Down

0 comments on commit 0c6583f

Please sign in to comment.