From ee36ca2064ec75eaaa7c75639ca852b45518b86e Mon Sep 17 00:00:00 2001 From: Fabien Penso Date: Mon, 30 Oct 2023 22:47:41 +0100 Subject: [PATCH 1/5] Add support for uppercase bech32 --- cosmrs/src/base/account_id.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cosmrs/src/base/account_id.rs b/cosmrs/src/base/account_id.rs index d8260e84..2776ba57 100644 --- a/cosmrs/src/base/account_id.rs +++ b/cosmrs/src/base/account_id.rs @@ -79,6 +79,11 @@ impl FromStr for AccountId { type Err = ErrorReport; fn from_str(s: &str) -> Result { + if s.starts_with(|c: char| c.is_uppercase()) { + let (hrp, bytes) = + bech32::decode_upper(s).wrap_err(format!("invalid uppercase bech32: '{}'", s))?; + return Self::new(&hrp, &bytes); + } let (hrp, bytes) = bech32::decode(s).wrap_err(format!("invalid bech32: '{}'", s))?; Self::new(&hrp, &bytes) } @@ -148,6 +153,14 @@ mod tests { .unwrap(); } + /// See https://en.bitcoin.it/wiki/BIP_0173 -- UPPERCASE is a valid bech32 + #[test] + fn with_uppercase() { + "STARS1JUME25TTJLCAQQJZJJQX9HUMVZE3VCC8QF2KWL" + .parse::() + .unwrap(); + } + #[test] fn to_string() { let account_id = "juno10j9gpw9t4jsz47qgnkvl5n3zlm2fz72k67rxsg" From 97d924dab892a34f9b6d2af43790d28214dd2a76 Mon Sep 17 00:00:00 2001 From: Fabien Penso Date: Mon, 30 Oct 2023 23:22:55 +0100 Subject: [PATCH 2/5] Update cosmrs/src/base/account_id.rs Co-authored-by: Tony Arcieri (iqlusion) --- cosmrs/src/base/account_id.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cosmrs/src/base/account_id.rs b/cosmrs/src/base/account_id.rs index 2776ba57..05e91070 100644 --- a/cosmrs/src/base/account_id.rs +++ b/cosmrs/src/base/account_id.rs @@ -79,12 +79,11 @@ impl FromStr for AccountId { type Err = ErrorReport; fn from_str(s: &str) -> Result { - if s.starts_with(|c: char| c.is_uppercase()) { - let (hrp, bytes) = - bech32::decode_upper(s).wrap_err(format!("invalid uppercase bech32: '{}'", s))?; - return Self::new(&hrp, &bytes); - } - let (hrp, bytes) = bech32::decode(s).wrap_err(format!("invalid bech32: '{}'", s))?; + let (hrp, bytes) = if s.starts_with(|c: char| c.is_uppercase()) { + bech32::decode_upper(s) + } else + bech32::decode(s) + }.wrap_err(format!("invalid uppercase bech32: '{}'", s))?; Self::new(&hrp, &bytes) } } From 5766d6e7ae9d5d95e1a49fc2683ba23c041ac6f5 Mon Sep 17 00:00:00 2001 From: Fabien Penso Date: Tue, 31 Oct 2023 09:28:06 +0100 Subject: [PATCH 3/5] Update cosmrs/src/base/account_id.rs Co-authored-by: Tony Arcieri (iqlusion) --- cosmrs/src/base/account_id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmrs/src/base/account_id.rs b/cosmrs/src/base/account_id.rs index 05e91070..092537c0 100644 --- a/cosmrs/src/base/account_id.rs +++ b/cosmrs/src/base/account_id.rs @@ -81,7 +81,7 @@ impl FromStr for AccountId { fn from_str(s: &str) -> Result { let (hrp, bytes) = if s.starts_with(|c: char| c.is_uppercase()) { bech32::decode_upper(s) - } else + } else { bech32::decode(s) }.wrap_err(format!("invalid uppercase bech32: '{}'", s))?; Self::new(&hrp, &bytes) From efd972a1c31ab5ed6238bb21f90383fe18cf7d58 Mon Sep 17 00:00:00 2001 From: Fabien Penso Date: Tue, 31 Oct 2023 14:28:49 +0100 Subject: [PATCH 4/5] Update cosmrs/src/base/account_id.rs Co-authored-by: Tony Arcieri (iqlusion) --- cosmrs/src/base/account_id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmrs/src/base/account_id.rs b/cosmrs/src/base/account_id.rs index 092537c0..d089bb18 100644 --- a/cosmrs/src/base/account_id.rs +++ b/cosmrs/src/base/account_id.rs @@ -83,7 +83,7 @@ impl FromStr for AccountId { bech32::decode_upper(s) } else { bech32::decode(s) - }.wrap_err(format!("invalid uppercase bech32: '{}'", s))?; + }.wrap_err(format!("invalid bech32: '{}'", s))?; Self::new(&hrp, &bytes) } } From 0c7d30bb29c5c60c8cf820d528e0601afacd4a63 Mon Sep 17 00:00:00 2001 From: "Tony Arcieri (iqlusion)" Date: Tue, 31 Oct 2023 10:46:38 -0600 Subject: [PATCH 5/5] Update cosmrs/src/base/account_id.rs --- cosmrs/src/base/account_id.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cosmrs/src/base/account_id.rs b/cosmrs/src/base/account_id.rs index d089bb18..199284c1 100644 --- a/cosmrs/src/base/account_id.rs +++ b/cosmrs/src/base/account_id.rs @@ -83,7 +83,8 @@ impl FromStr for AccountId { bech32::decode_upper(s) } else { bech32::decode(s) - }.wrap_err(format!("invalid bech32: '{}'", s))?; + } + .wrap_err(format!("invalid bech32: '{}'", s))?; Self::new(&hrp, &bytes) } }