Skip to content

Commit b259f4b

Browse files
authored
fix: remove_admin and update_owner, update_admin are removed (#692)
* 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
1 parent d38b78e commit b259f4b

File tree

6 files changed

+18
-394
lines changed

6 files changed

+18
-394
lines changed

contracts/cosmwasm-vm/cw-mock-ibc-dapp/src/admin.rs

-25
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,6 @@ impl<'a> CwIbcConnection<'a> {
118118
.add_attribute("admin", new_admin.to_string()))
119119
}
120120

121-
/// The code defines a function to remove an admin and another function to validate an address.
122-
///
123-
/// Arguments:
124-
///
125-
/// * `store`: `store` is a mutable reference to a trait object of type `Storage`. It is used to
126-
/// interact with the contract's storage and modify its state.
127-
/// * `info`: `info` is a parameter of type `MessageInfo` which contains information about the message
128-
/// being executed, such as the sender's address, the amount of coins being sent, and the gas limit. It
129-
/// is used in the `remove_admin` function to ensure that the sender is the owner of the
130-
///
131-
/// Returns:
132-
///
133-
/// The `remove_admin` function returns a `Result<Response, ContractError>` and the `validate_address`
134-
/// function returns a `Result<String, ContractError>`.
135-
pub fn remove_admin(
136-
&self,
137-
store: &mut dyn Storage,
138-
info: MessageInfo,
139-
) -> Result<Response, ContractError> {
140-
self.ensure_owner(store, &info)?;
141-
142-
self.admin().remove(store);
143-
Ok(Response::new().add_attribute("method", "remove_admin"))
144-
}
145-
146121
pub fn validate_address(api: &dyn Api, address: &str) -> Result<Addr, ContractError> {
147122
if !address.chars().all(|x| x.is_alphanumeric()) {
148123
return Err(ContractError::InvalidAddress {

contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/admin.rs

-88
Original file line numberDiff line numberDiff line change
@@ -49,100 +49,12 @@ impl<'a> CwIbcConnection<'a> {
4949
info: MessageInfo,
5050
admin: Addr,
5151
) -> Result<Response, ContractError> {
52-
if admin.to_string().is_empty() {
53-
return Err(ContractError::AdminAddressCannotBeNull {});
54-
}
55-
56-
let owner = self
57-
.owner()
58-
.load(store)
59-
.map_err(|_| ContractError::Unauthorized {})?;
60-
61-
if info.sender != owner {
62-
return Err(ContractError::Unauthorized {});
63-
}
6452
self.admin().save(store, &admin)?;
6553
Ok(Response::new()
6654
.add_attribute("method", "add_admin")
6755
.add_attribute("admin", admin.to_string()))
6856
}
6957

70-
/// This function updates the admin address of a contract if the caller is the owner and the new
71-
/// address is valid.
72-
///
73-
/// Arguments:
74-
///
75-
/// * `store`: A mutable reference to a trait object of type `dyn Storage`. This is used to interact
76-
/// with the contract's storage.
77-
/// * `info`: MessageInfo is a struct that contains information about the message being executed, such
78-
/// as the sender's address, the amount of tokens being sent, and the gas limit. It is used to ensure
79-
/// that only authorized parties can execute certain functions and to handle payment transactions.
80-
/// * `new_admin`: A string representing the new address of the admin that will replace the current
81-
/// admin.
82-
///
83-
/// Returns:
84-
///
85-
/// a `Result<Response, ContractError>`. If the function executes successfully, it returns a `Response`
86-
/// object with attributes "action" and "admin". If there is an error, it returns a `ContractError`
87-
/// object with a specific error message.
88-
pub fn update_admin(
89-
&self,
90-
store: &mut dyn Storage,
91-
info: MessageInfo,
92-
new_admin: Addr,
93-
) -> Result<Response, ContractError> {
94-
if new_admin.to_string().is_empty() {
95-
return Err(ContractError::AdminAddressCannotBeNull {});
96-
}
97-
98-
if !new_admin.to_string().chars().all(|x| x.is_alphanumeric()) {
99-
return Err(ContractError::InvalidAddress {
100-
address: new_admin.to_string(),
101-
});
102-
}
103-
104-
self.ensure_admin(store, info.sender)?;
105-
106-
self.admin()
107-
.update(store, |mut current_admin| -> Result<_, ContractError> {
108-
if current_admin == new_admin {
109-
Err(ContractError::AdminAlreadyExist)
110-
} else {
111-
current_admin = new_admin.clone();
112-
Ok(current_admin)
113-
}
114-
})?;
115-
116-
Ok(Response::new()
117-
.add_attribute("action", "update admin")
118-
.add_attribute("admin", new_admin.to_string()))
119-
}
120-
121-
/// The code defines a function to remove an admin and another function to validate an address.
122-
///
123-
/// Arguments:
124-
///
125-
/// * `store`: `store` is a mutable reference to a trait object of type `Storage`. It is used to
126-
/// interact with the contract's storage and modify its state.
127-
/// * `info`: `info` is a parameter of type `MessageInfo` which contains information about the message
128-
/// being executed, such as the sender's address, the amount of coins being sent, and the gas limit. It
129-
/// is used in the `remove_admin` function to ensure that the sender is the owner of the
130-
///
131-
/// Returns:
132-
///
133-
/// The `remove_admin` function returns a `Result<Response, ContractError>` and the `validate_address`
134-
/// function returns a `Result<String, ContractError>`.
135-
pub fn remove_admin(
136-
&self,
137-
store: &mut dyn Storage,
138-
info: MessageInfo,
139-
) -> Result<Response, ContractError> {
140-
self.ensure_owner(store, &info)?;
141-
142-
self.admin().remove(store);
143-
Ok(Response::new().add_attribute("method", "remove_admin"))
144-
}
145-
14658
pub fn validate_address(api: &dyn Api, address: &str) -> Result<Addr, ContractError> {
14759
if !address.chars().all(|x| x.is_alphanumeric()) {
14860
return Err(ContractError::InvalidAddress {

contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/contract.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> CwIbcConnection<'a> {
6060
msg: InstantiateMsg,
6161
) -> Result<Response, ContractError> {
6262
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
63-
63+
CwIbcConnection::validate_address(deps.api, info.sender.as_str())?;
6464
self.init(deps.storage, info, msg)
6565
}
6666

@@ -96,6 +96,7 @@ impl<'a> CwIbcConnection<'a> {
9696
ExecuteMsg::SetAdmin { address } => {
9797
let validated_address =
9898
CwIbcConnection::validate_address(deps.api, address.as_str())?;
99+
self.ensure_admin(deps.storage, info.clone().sender)?;
99100
self.add_admin(deps.storage, info, validated_address)
100101
}
101102
ExecuteMsg::SendMessage { to, sn, msg } => {

contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/owner.rs

-44
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,4 @@ impl<'a> CwIbcConnection<'a> {
5656
.add_attribute("method", "add_owner")
5757
.add_attribute("owner", owner.to_string()))
5858
}
59-
60-
/// This function updates the owner of a contract if the sender is the current owner and the new
61-
/// owner does not already exist.
62-
///
63-
/// Arguments:
64-
///
65-
/// * `store`: `store` is a mutable reference to a trait object `dyn Storage`. This is used to
66-
/// interact with the contract's storage and persist data between contract executions.
67-
/// * `info`: `info` is a `MessageInfo` struct that contains information about the message that
68-
/// triggered the contract execution, such as the sender's address, the amount of tokens
69-
/// transferred, and the message's ID. It is used to verify that the sender is the current owner of
70-
/// the contract before updating the owner
71-
/// * `new_owner`: A String representing the new owner that the current owner is trying to update
72-
/// to.
73-
///
74-
/// Returns:
75-
///
76-
/// a `Result<Response, ContractError>`. If the function executes successfully, it returns a
77-
/// `Response` object with some attributes added to it. If there is an error, it returns a
78-
/// `ContractError`.
79-
pub fn update_owner(
80-
&self,
81-
store: &mut dyn Storage,
82-
info: MessageInfo,
83-
new_owner: Addr,
84-
) -> Result<Response, ContractError> {
85-
self.owner()
86-
.update(store, |mut current_owner| -> Result<_, ContractError> {
87-
if info.sender == current_owner {
88-
if current_owner == new_owner {
89-
Err(ContractError::OwnerAlreadyExist)
90-
} else {
91-
current_owner = new_owner.clone();
92-
Ok(current_owner)
93-
}
94-
} else {
95-
Err(ContractError::Unauthorized {})
96-
}
97-
})?;
98-
99-
Ok(Response::new()
100-
.add_attribute("action", "update owner")
101-
.add_attribute("owner", new_owner.to_string()))
102-
}
10359
}

0 commit comments

Comments
 (0)