Skip to content

Commit

Permalink
fix: remove_admin and update_owner, update_admin are removed (#692)
Browse files Browse the repository at this point in the history
* fix: remove_admin and update_owner is removed, and created a method for update_admin

* fix: transfer admin removed and add_admin updated

* fix: transfer admin removed and add_admin updated

* fix: add_owner_unauthorized removed
  • Loading branch information
gcranju authored Sep 12, 2023
1 parent d38b78e commit b259f4b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 394 deletions.
25 changes: 0 additions & 25 deletions contracts/cosmwasm-vm/cw-mock-ibc-dapp/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,6 @@ impl<'a> CwIbcConnection<'a> {
.add_attribute("admin", new_admin.to_string()))
}

/// The code defines a function to remove an admin and another function to validate an address.
///
/// Arguments:
///
/// * `store`: `store` is a mutable reference to a trait object of type `Storage`. It is used to
/// interact with the contract's storage and modify its state.
/// * `info`: `info` is a parameter of type `MessageInfo` which contains information about the message
/// being executed, such as the sender's address, the amount of coins being sent, and the gas limit. It
/// is used in the `remove_admin` function to ensure that the sender is the owner of the
///
/// Returns:
///
/// The `remove_admin` function returns a `Result<Response, ContractError>` and the `validate_address`
/// function returns a `Result<String, ContractError>`.
pub fn remove_admin(
&self,
store: &mut dyn Storage,
info: MessageInfo,
) -> Result<Response, ContractError> {
self.ensure_owner(store, &info)?;

self.admin().remove(store);
Ok(Response::new().add_attribute("method", "remove_admin"))
}

pub fn validate_address(api: &dyn Api, address: &str) -> Result<Addr, ContractError> {
if !address.chars().all(|x| x.is_alphanumeric()) {
return Err(ContractError::InvalidAddress {
Expand Down
88 changes: 0 additions & 88 deletions contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,100 +49,12 @@ impl<'a> CwIbcConnection<'a> {
info: MessageInfo,
admin: Addr,
) -> Result<Response, ContractError> {
if admin.to_string().is_empty() {
return Err(ContractError::AdminAddressCannotBeNull {});
}

let owner = self
.owner()
.load(store)
.map_err(|_| ContractError::Unauthorized {})?;

if info.sender != owner {
return Err(ContractError::Unauthorized {});
}
self.admin().save(store, &admin)?;
Ok(Response::new()
.add_attribute("method", "add_admin")
.add_attribute("admin", admin.to_string()))
}

/// This function updates the admin address of a contract if the caller is the owner and the new
/// address is valid.
///
/// Arguments:
///
/// * `store`: A mutable reference to a trait object of type `dyn Storage`. This is used to interact
/// with the contract's storage.
/// * `info`: MessageInfo is a struct that contains information about the message being executed, such
/// as the sender's address, the amount of tokens being sent, and the gas limit. It is used to ensure
/// that only authorized parties can execute certain functions and to handle payment transactions.
/// * `new_admin`: A string representing the new address of the admin that will replace the current
/// admin.
///
/// Returns:
///
/// a `Result<Response, ContractError>`. If the function executes successfully, it returns a `Response`
/// object with attributes "action" and "admin". If there is an error, it returns a `ContractError`
/// object with a specific error message.
pub fn update_admin(
&self,
store: &mut dyn Storage,
info: MessageInfo,
new_admin: Addr,
) -> Result<Response, ContractError> {
if new_admin.to_string().is_empty() {
return Err(ContractError::AdminAddressCannotBeNull {});
}

if !new_admin.to_string().chars().all(|x| x.is_alphanumeric()) {
return Err(ContractError::InvalidAddress {
address: new_admin.to_string(),
});
}

self.ensure_admin(store, info.sender)?;

self.admin()
.update(store, |mut current_admin| -> Result<_, ContractError> {
if current_admin == new_admin {
Err(ContractError::AdminAlreadyExist)
} else {
current_admin = new_admin.clone();
Ok(current_admin)
}
})?;

Ok(Response::new()
.add_attribute("action", "update admin")
.add_attribute("admin", new_admin.to_string()))
}

/// The code defines a function to remove an admin and another function to validate an address.
///
/// Arguments:
///
/// * `store`: `store` is a mutable reference to a trait object of type `Storage`. It is used to
/// interact with the contract's storage and modify its state.
/// * `info`: `info` is a parameter of type `MessageInfo` which contains information about the message
/// being executed, such as the sender's address, the amount of coins being sent, and the gas limit. It
/// is used in the `remove_admin` function to ensure that the sender is the owner of the
///
/// Returns:
///
/// The `remove_admin` function returns a `Result<Response, ContractError>` and the `validate_address`
/// function returns a `Result<String, ContractError>`.
pub fn remove_admin(
&self,
store: &mut dyn Storage,
info: MessageInfo,
) -> Result<Response, ContractError> {
self.ensure_owner(store, &info)?;

self.admin().remove(store);
Ok(Response::new().add_attribute("method", "remove_admin"))
}

pub fn validate_address(api: &dyn Api, address: &str) -> Result<Addr, ContractError> {
if !address.chars().all(|x| x.is_alphanumeric()) {
return Err(ContractError::InvalidAddress {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> CwIbcConnection<'a> {
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

CwIbcConnection::validate_address(deps.api, info.sender.as_str())?;
self.init(deps.storage, info, msg)
}

Expand Down Expand Up @@ -96,6 +96,7 @@ impl<'a> CwIbcConnection<'a> {
ExecuteMsg::SetAdmin { address } => {
let validated_address =
CwIbcConnection::validate_address(deps.api, address.as_str())?;
self.ensure_admin(deps.storage, info.clone().sender)?;
self.add_admin(deps.storage, info, validated_address)
}
ExecuteMsg::SendMessage { to, sn, msg } => {
Expand Down
44 changes: 0 additions & 44 deletions contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,48 +56,4 @@ impl<'a> CwIbcConnection<'a> {
.add_attribute("method", "add_owner")
.add_attribute("owner", owner.to_string()))
}

/// This function updates the owner of a contract if the sender is the current owner and the new
/// owner does not already exist.
///
/// Arguments:
///
/// * `store`: `store` is a mutable reference to a trait object `dyn Storage`. This is used to
/// interact with the contract's storage and persist data between contract executions.
/// * `info`: `info` is a `MessageInfo` struct that contains information about the message that
/// triggered the contract execution, such as the sender's address, the amount of tokens
/// transferred, and the message's ID. It is used to verify that the sender is the current owner of
/// the contract before updating the owner
/// * `new_owner`: A String representing the new owner that the current owner is trying to update
/// to.
///
/// Returns:
///
/// a `Result<Response, ContractError>`. If the function executes successfully, it returns a
/// `Response` object with some attributes added to it. If there is an error, it returns a
/// `ContractError`.
pub fn update_owner(
&self,
store: &mut dyn Storage,
info: MessageInfo,
new_owner: Addr,
) -> Result<Response, ContractError> {
self.owner()
.update(store, |mut current_owner| -> Result<_, ContractError> {
if info.sender == current_owner {
if current_owner == new_owner {
Err(ContractError::OwnerAlreadyExist)
} else {
current_owner = new_owner.clone();
Ok(current_owner)
}
} else {
Err(ContractError::Unauthorized {})
}
})?;

Ok(Response::new()
.add_attribute("action", "update owner")
.add_attribute("owner", new_owner.to_string()))
}
}
Loading

0 comments on commit b259f4b

Please sign in to comment.