Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(interchain-token-service): verify input validation #208

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
30 changes: 19 additions & 11 deletions contracts/stellar-interchain-token-service/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ impl InterchainTokenServiceInterface for InterchainTokenService {

caller.require_auth();

ensure!(initial_supply >= 0, ContractError::InvalidSupply);
Copy link
Member

Choose a reason for hiding this comment

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

no minter and 0 initial supply is a weird case that we can check for too


let initial_minter = if initial_supply > 0 {
Some(env.current_contract_address())
} else if let Some(ref minter) = minter {
Expand Down Expand Up @@ -542,12 +544,13 @@ impl InterchainTokenService {
/// Retrieves the configuration value for the specified token ID.
///
/// # Arguments
/// - `env`: Reference to the environment.
/// - `token_id`: A 32-byte unique identifier for the token.
///
/// # Returns
/// - `Ok(TokenIdConfigValue)`: The configuration value if it exists.
/// - `Err(ContractError::InvalidTokenId)`: If the token ID does not exist in storage.
///
/// # Errors
/// - `ContractError::InvalidTokenId`: If the token ID does not exist in storage.
fn token_id_config(
env: &Env,
token_id: BytesN<32>,
Expand All @@ -561,12 +564,13 @@ impl InterchainTokenService {
/// Retrieves the configuration value for the specified token ID and extends its TTL.
///
/// # Arguments
/// - `env`: Reference to the environment.
/// - `token_id`: A 32-byte unique identifier for the token.
///
/// # Returns
/// - `Ok(TokenIdConfigValue)`: The configuration value if it exists.
/// - `Err(ContractError::InvalidTokenId)`: If the token ID does not exist in storage.
///
/// # Errors
/// - `ContractError::InvalidTokenId`: If the token ID does not exist in storage.
fn token_id_config_with_extended_ttl(
env: &Env,
token_id: BytesN<32>,
Expand All @@ -583,23 +587,27 @@ impl InterchainTokenService {

/// Deploys a remote token on a specified destination chain.
///
/// This function authorizes the caller, retrieves the token's metadata,
/// validates the metadata, and emits an event indicating the start of the
/// token deployment process. It also constructs and sends the deployment
/// message to the remote chain.
/// This function retrieves and validates the token's metadata
/// and emits an event indicating the start of the token deployment process.
/// It also constructs and sends the deployment message to the remote chain.
///
/// # Arguments
/// * `env` - Reference to the environment object.
/// * `caller` - Address of the caller initiating the deployment.
/// * `deploy_salt` - Unique salt used for token deployment.
/// * `destination_chain` - The name of the destination chain where the token will be deployed.
/// * `gas_token` - The token used to pay for gas during the deployment.
///
/// # Returns
/// Returns the token ID of the deployed token on the remote chain, or an error if the deployment fails.
/// - `Ok(BytesN<32>)`: Returns the token ID.
///
/// # Errors
/// Returns `ContractError` if the deployment fails, the token ID is invalid, or token metadata is invalid.
/// - `ContractError::InvalidDestinationChain`: If the `destination_chain` is the current chain.
/// - `ContractError::InvalidTokenId`: If the token ID is invalid.
/// - Errors propagated from `token_metadata`.
Copy link
Contributor

@ahramy ahramy Jan 24, 2025

Choose a reason for hiding this comment

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

nit: Is this necessary? If so, we need to update similar docstrings for other functions for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's consistent with the rest of the ITS docstring

/// - Any error propagated from `pay_gas_and_call_contract`.
///
/// # Authorization
/// - The `caller` must authenticate.
fn deploy_remote_token(
env: &Env,
caller: Address,
Expand Down
1 change: 1 addition & 0 deletions contracts/stellar-interchain-token-service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum ContractError {
InvalidTokenDecimals = 27,
TokenAlreadyRegistered = 28,
ContractPaused = 29,
InvalidSupply = 30,
}

impl_not_approved_error!(ContractError);
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,22 @@ fn deploy_interchain_token_fails_with_invalid_auth() {
client.deploy_interchain_token(&sender, &salt, &token_metadata, &initial_supply, &minter)
);
}

#[test]
fn deploy_interchain_token_fails_with_negative_supply() {
let (env, client, _, _, _) = setup_env();

let (sender, salt, token_metadata) = dummy_token_params(&env);
let invalid_supply = -1;

assert_contract_err!(
client.mock_all_auths().try_deploy_interchain_token(
&sender,
&salt,
&token_metadata,
&invalid_supply,
&None
),
ContractError::InvalidSupply
);
}
Loading