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

remove ability to override authenticators #18

Merged
merged 3 commits into from
Jan 29, 2024
Merged
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
4 changes: 2 additions & 2 deletions account/src/auth/sign_arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn wrap_message(msg_bytes: &[u8], signer: Addr) -> Vec<u8> {
#[cfg(test)]
mod tests {
use crate::auth::sign_arb::wrap_message;
use crate::auth::util;
use crate::auth::Authenticator::Secp256K1;
use crate::auth::{util, Authenticator};
use crate::contract::instantiate;
use crate::msg::InstantiateMsg;
use base64::{engine::general_purpose, Engine as _};
Expand Down Expand Up @@ -122,6 +122,6 @@ mod tests {
signature: Binary::from(signature_bytes),
};

let res = instantiate(deps.as_mut(), env.clone(), info, instantiate_msg).unwrap();
instantiate(deps.as_mut(), env.clone(), info, instantiate_msg).unwrap();
}
}
3 changes: 3 additions & 0 deletions account/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub enum ContractError {

#[error("invalid token")]
InvalidToken,

#[error("cannot override existing authenticator at index {index}")]
OverridingIndex { index: u8 },
}

pub type ContractResult<T> = Result<T, ContractError>;
32 changes: 24 additions & 8 deletions account/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cosmwasm_std::{Addr, Binary, Deps, DepsMut, Env, Event, MessageInfo, Order, Response};

use crate::auth::{AddAuthenticator, Authenticator};
use crate::error::ContractError::OverridingIndex;
use crate::{
auth,
error::{ContractError, ContractResult},
Expand Down Expand Up @@ -118,7 +119,7 @@ pub fn add_auth_method(
)? {
Err(ContractError::InvalidSignature)
} else {
AUTHENTICATORS.save(deps.storage, id, &auth)?;
save_authenticator(deps, id, &auth)?;
Ok(())
}
}
Expand All @@ -137,7 +138,7 @@ pub fn add_auth_method(
)? {
Err(ContractError::InvalidSignature)
} else {
AUTHENTICATORS.save(deps.storage, id, &auth)?;
save_authenticator(deps, id, &auth)?;
Ok(())
}
}
Expand All @@ -156,7 +157,7 @@ pub fn add_auth_method(
)? {
Err(ContractError::InvalidSignature)
} else {
AUTHENTICATORS.save(deps.storage, id, &auth)?;
save_authenticator(deps, id, &auth)?;
Ok(())
}
}
Expand All @@ -176,7 +177,7 @@ pub fn add_auth_method(
)? {
Err(ContractError::InvalidSignature)
} else {
AUTHENTICATORS.save(deps.storage, id, &auth)?;
save_authenticator(deps, id, &auth)?;
Ok(())
}
}
Expand All @@ -192,6 +193,19 @@ pub fn add_auth_method(
)
}

pub fn save_authenticator(
deps: DepsMut,
id: u8,
authenticator: &Authenticator,
) -> ContractResult<()> {
if AUTHENTICATORS.has(deps.storage, id) {
return Err(OverridingIndex { index: id });
}

AUTHENTICATORS.save(deps.storage, id, authenticator)?;
Ok(())
}

pub fn remove_auth_method(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -237,7 +251,7 @@ mod tests {

#[test]
fn test_before_tx() {
let authId = 0;
let auth_id = 0;
let mut deps = mock_dependencies();
let env = mock_env();

Expand All @@ -251,13 +265,15 @@ mod tests {
let sig_arr = general_purpose::STANDARD.decode(signature).unwrap();

// The index of the first authenticator is 0.
let credIndex = vec![0u8];
let cred_index = vec![0u8];

let mut new_vec = Vec::new();
new_vec.extend_from_slice(&credIndex);
new_vec.extend_from_slice(&cred_index);
new_vec.extend_from_slice(&sig_arr);

AUTHENTICATORS.save(deps.as_mut().storage, authId, &auth);
AUTHENTICATORS
.save(deps.as_mut().storage, auth_id, &auth)
.unwrap();

let sig_bytes = Binary::from(new_vec);
let tx_bytes = Binary::from(general_purpose::STANDARD.decode("Cp0BCpoBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEnoKP3hpb24xbTZ2aDIwcHM3NW0ybjZxeHdwandmOGZzM2t4dzc1enN5M3YycnllaGQ5c3BtbnUwcTlyc2g0NnljeRIreGlvbjFlMmZ1d2UzdWhxOHpkOW5ra2s4NzZuYXdyd2R1bGd2NDYwdnpnNxoKCgV1eGlvbhIBMRJTCksKQwodL2Fic3RyYWN0YWNjb3VudC52MS5OaWxQdWJLZXkSIgog3pl1PDD1NqnoBnBk5J0wjYzvUFAkWKGTN2lgHc+PAUcSBAoCCAESBBDgpxIaFHhpb24tbG9jYWwtdGVzdG5ldC0xIAg=").unwrap());
Expand Down
Loading