Skip to content

Commit

Permalink
cosmrs: update AccountId validation to match Cosmos SDK (#197)
Browse files Browse the repository at this point in the history
This Cosmos SDK change allows variable-sized addresses:

cosmos/cosmos-sdk#8363

This commit changes the validation logic to support addresses
whose lengths are `1..=255`.
  • Loading branch information
jstuczyn authored Apr 19, 2022
1 parent c0c9277 commit b9c9caa
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cosmrs/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use serde::{de, de::Error as _, ser, Deserialize, Serialize};
use std::{fmt, str::FromStr};
use subtle_encoding::bech32;

/// Maximum allowed length (in bytes) for an address.
pub const MAX_ADDRESS_LENGTH: usize = 255;

/// Account identifiers
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct AccountId {
Expand Down Expand Up @@ -72,16 +75,16 @@ impl FromStr for AccountId {
fn from_str(s: &str) -> Result<Self> {
let (hrp, bytes) = bech32::decode(s).wrap_err("failed to decode bech32")?;

if bytes.len() == tendermint::account::LENGTH {
if matches!(bytes.len(), 1..=MAX_ADDRESS_LENGTH) {
Ok(Self {
bech32: s.to_owned(),
hrp_length: hrp.len(),
})
} else {
Err(Error::AccountId { id: s.to_owned() }).wrap_err_with(|| {
format!(
"account ID should be at least {} bytes long, but was {} bytes long",
tendermint::account::LENGTH,
"account ID should be at most {} bytes long, but was {} bytes long",
MAX_ADDRESS_LENGTH,
bytes.len()
)
})
Expand Down

0 comments on commit b9c9caa

Please sign in to comment.