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

Validate Bech32 addresses #10

Open
wants to merge 1 commit 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
29 changes: 18 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ anyhow = "1.0.81"
async-trait = "0.1.80"
base16ct = "0.2.0"
base64 = "0.22.0"
bech32 = "0.11.0"
clap = { version = "4.5.4", features = ["derive"] }
dotenv = "0.15.0"
itertools = "0.12.1"
Expand Down
31 changes: 31 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
use anyhow::Error;
use bech32::{primitives::decode::CheckedHrpstring, Bech32};

use crate::validations::CovenantValidationContext;

pub mod assets;
pub mod astroport;
pub mod chain;
pub mod neutron;
pub mod path;

pub fn validate_party_address<'a>(ctx: &mut CovenantValidationContext<'a>,
key: &'a str, field : &'a str, address: &str) {
// Decode the Bech32 address
match validate_bech32_address(address) {
Ok(_) => {
// Note: no check is done on the HRP (Human Readable Part) for now
ctx.valid_field(key, field, "valid Bech32 address".to_owned());
},
Err(_) => {
ctx.invalid_field(key, field, "Invalid Bech32 address".to_owned());
}
}
}

fn validate_bech32_address(address: &str) -> Result<(), Error> {
let _ = CheckedHrpstring::new::<Bech32>(address)
.map_err(|e| anyhow::anyhow!(e))?;
Ok(())
}

#[test]
fn test_validate_bech32_address() {
validate_bech32_address("cosmos1ayw8xtxkty5cfzx44z6vxpevmtudg2n3f4etcq").unwrap();
validate_bech32_address("neutron1ayw8xtxkty5cfzx44z6vxpevmtudg2n3d2sfz8").unwrap();
}
8 changes: 6 additions & 2 deletions src/validations/single_party_pol_covenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use super::{CovenantValidationContext, Validate};
use crate::utils::assets::get_chain_asset_info;
use crate::utils::chain::get_chain_info;
use crate::utils::path::{get_path_info, IBCPath};
use crate::utils::validate_party_address;
use crate::validations::neutron::verify_expiration;
use crate::validations::{
astroport::verify_astroport_liquid_pooler_config,
Expand Down Expand Up @@ -168,9 +169,12 @@ impl<'a> Validate<'a> for SinglePartyPolCovenantInstMsg {
);
}

field = "party_receiver_addr";
validate_party_address(ctx, key, field, msg.covenant_party_config.party_receiver_addr.as_str());
field = "addr";
validate_party_address(ctx, key, field, msg.covenant_party_config.addr.as_str());

//TODO: Validate the rest of the covenant party config
// field = "party_receiver_addr";
// field = "addr";
// field = "denom_to_pfm_map";
// field = "fallback_address";

Expand Down
15 changes: 10 additions & 5 deletions src/validations/two_party_pol_covenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{CovenantValidationContext, Validate};
use crate::utils::assets::get_chain_asset_info;
use crate::utils::chain::get_chain_info;
use crate::utils::path::{get_path_info, IBCPath};
use crate::utils::validate_party_address;
use crate::validations::astroport::verify_astroport_liquid_pooler_config;
use crate::validations::neutron::verify_expiration;
use crate::validations::{
Expand Down Expand Up @@ -289,9 +290,10 @@ async fn verify_party_config<'a>(
);
}

//TODO: validate addresses
// field = "party_receiver_addr";
// field = "addr";
field = "party_receiver_addr";
validate_party_address(ctx, key, field, native_party.party_receiver_addr.as_str());
field = "addr";
validate_party_address(ctx, key, field, native_party.addr.as_str());
}
tppc::CovenantPartyConfig::Interchain(interchain_party) => {
let path_info =
Expand Down Expand Up @@ -449,9 +451,12 @@ async fn verify_party_config<'a>(
);
}

field = "party_receiver_addr";
validate_party_address(ctx, key, field, interchain_party.party_receiver_addr.as_str());
field = "addr";
validate_party_address(ctx, key, field, interchain_party.addr.as_str());

//TODO: Validate the rest of the covenant party config
// field = "party_receiver_addr";
// field = "addr";
// field = "denom_to_pfm_map";
// field = "fallback_address";
}
Expand Down