From b9c9caa3719c3791c5afd016e0c4aa05b1bf1fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 19 Apr 2022 20:09:51 +0200 Subject: [PATCH] cosmrs: update AccountId validation to match Cosmos SDK (#197) This Cosmos SDK change allows variable-sized addresses: https://github.com/cosmos/cosmos-sdk/pull/8363 This commit changes the validation logic to support addresses whose lengths are `1..=255`. --- cosmrs/src/base.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cosmrs/src/base.rs b/cosmrs/src/base.rs index 0930326f..bd3819b3 100644 --- a/cosmrs/src/base.rs +++ b/cosmrs/src/base.rs @@ -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 { @@ -72,7 +75,7 @@ impl FromStr for AccountId { fn from_str(s: &str) -> Result { 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(), @@ -80,8 +83,8 @@ impl FromStr for AccountId { } 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() ) })