From 13c7748b86070f2edb841afb00c4863934e5235a Mon Sep 17 00:00:00 2001 From: justin <328965+justinbarry@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:37:51 -0700 Subject: [PATCH 1/3] Add test to exercise `before_tx` code --- account/src/execute.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/account/src/execute.rs b/account/src/execute.rs index 78d6c06..d17999b 100644 --- a/account/src/execute.rs +++ b/account/src/execute.rs @@ -181,3 +181,43 @@ pub fn assert_self(sender: &Addr, contract: &Addr) -> ContractResult<()> { Ok(()) } + +#[cfg(test)] +mod tests { + use base64::{engine::general_purpose, Engine as _}; + use cosmwasm_std::testing::mock_dependencies; + use cosmwasm_std::Binary; + + use crate::auth::Authenticator; + use crate::execute::before_tx; + use crate::state::AUTHENTICATORS; + + #[test] + fn test_before_tx() { + let authId = 0; + let mut deps = mock_dependencies(); + + let pubkey = "Ayrlj6q3WWs91p45LVKwI8JyfMYNmWMrcDinLNEdWYE4"; + let pubkey_bytes = general_purpose::STANDARD.decode(pubkey).unwrap(); + let auth = Authenticator::Secp256K1 { + pubkey: Binary::from(pubkey_bytes), + }; + + let signature = "UDerMpp4QzGxjuu3uTmqoOdPrmRnwiOf6BOlL5xG2pAEx+gS8DV3HwBzrb+QRIVyKVc3D7RYMOAlRFRkpVANDA=="; + let sig_arr = general_purpose::STANDARD.decode(signature).unwrap(); + + // The index of the first authenticator is 0. + let credIndex = vec![0u8]; + + let mut new_vec = Vec::new(); + new_vec.extend_from_slice(&credIndex); + new_vec.extend_from_slice(&sig_arr); + + AUTHENTICATORS.save(deps.as_mut().storage, authId, &auth); + + let sig_bytes = Binary::from(new_vec); + let tx_bytes = Binary::from(general_purpose::STANDARD.decode("Cp0BCpoBChwvY29zbW9zLmJhbmsudjFiZXRhMS5Nc2dTZW5kEnoKP3hpb24xbTZ2aDIwcHM3NW0ybjZxeHdwandmOGZzM2t4dzc1enN5M3YycnllaGQ5c3BtbnUwcTlyc2g0NnljeRIreGlvbjFlMmZ1d2UzdWhxOHpkOW5ra2s4NzZuYXdyd2R1bGd2NDYwdnpnNxoKCgV1eGlvbhIBMRJTCksKQwodL2Fic3RyYWN0YWNjb3VudC52MS5OaWxQdWJLZXkSIgog3pl1PDD1NqnoBnBk5J0wjYzvUFAkWKGTN2lgHc+PAUcSBAoCCAESBBDgpxIaFHhpb24tbG9jYWwtdGVzdG5ldC0xIAg=").unwrap()); + + before_tx(deps.as_ref(), &tx_bytes, Some(&sig_bytes), false).unwrap(); + } +} From d66c799fa792ebe37a554ac09e76279a4179ceb1 Mon Sep 17 00:00:00 2001 From: justin <328965+justinbarry@users.noreply.github.com> Date: Mon, 16 Oct 2023 10:40:44 -0700 Subject: [PATCH 2/3] Remove print statement --- account/src/auth/sign_arb.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/account/src/auth/sign_arb.rs b/account/src/auth/sign_arb.rs index d661294..fd3ac63 100644 --- a/account/src/auth/sign_arb.rs +++ b/account/src/auth/sign_arb.rs @@ -123,6 +123,5 @@ mod tests { }; let res = instantiate(deps.as_mut(), env.clone(), info, instantiate_msg).unwrap(); - println!("response: {:?}", res); } } From a3ffc3a9f8e8dc8e8369640d138913915df95c5b Mon Sep 17 00:00:00 2001 From: justin <328965+justinbarry@users.noreply.github.com> Date: Mon, 16 Oct 2023 13:33:36 -0700 Subject: [PATCH 3/3] Only hash signarb message after wrapping it --- account/src/auth.rs | 2 +- account/src/auth/sign_arb.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/account/src/auth.rs b/account/src/auth.rs index 954d318..8bc24c1 100644 --- a/account/src/auth.rs +++ b/account/src/auth.rs @@ -54,7 +54,7 @@ impl Authenticator { // are signing with signArbitrary (common for cosmos wallets) let verification = sign_arb::verify( api, - tx_bytes_hash.as_slice(), + tx_bytes.as_slice(), sig_bytes.as_slice(), pubkey.as_slice(), )?; diff --git a/account/src/auth/sign_arb.rs b/account/src/auth/sign_arb.rs index fd3ac63..0fb998c 100644 --- a/account/src/auth/sign_arb.rs +++ b/account/src/auth/sign_arb.rs @@ -97,7 +97,7 @@ mod tests { let info = mock_info("sender", &[]); // This is the local faucet address to simplify reuse env.contract.address = Addr::unchecked( - "xion14apeydfljtmvv8vdj97u3mtmlednfhz6dr5scfs2p6xd0gdlxutqvfagkh".to_string(), + "xion1cyyld62ly828e2xnp0c0ckpyz68wwfs26tjpscmqlaum2jcj8zdstlxvya".to_string(), ); let pubkey = "Ayrlj6q3WWs91p45LVKwI8JyfMYNmWMrcDinLNEdWYE4"; @@ -111,7 +111,7 @@ mod tests { signer.as_str() ); - let signature = "ywxOndY+x+AzT77KBVptdCarKG6YyPBVRkpm188P8Sh9SOQ4sIIFK5ZMzN8XLqClTTIsXT14FeeRhuDaL+fMYA=="; + let signature = "AKgG8slCFM78fE9tZzmf+L6yQskPQI0acUg3PBv/kNIO0i19i/RNaJtfFJ8A8MyHmg7Ate5imbwuzsP6mfbEaA=="; let signature_bytes = general_purpose::STANDARD.decode(signature).unwrap(); let instantiate_msg = InstantiateMsg {