@@ -2,20 +2,23 @@ use std::fmt::{Debug, Display, Formatter};
22use std:: ops:: Deref ;
33
44use :: cosmwasm_schema:: serde;
5- use bech32:: { self , Error as Bech32Error , FromBase32 , ToBase32 , Variant } ;
5+ // use bech32::{self, Error as Bech32Error, FromBase32, ToBase32, Variant};
6+ use alloy_primitives:: { Address , AddressError } ;
7+
68use cosmwasm_std:: { Addr , StdResult } ;
79use cw_storage_plus:: { Key , KeyDeserialize , Prefixer , PrimaryKey } ;
810use thiserror:: Error ;
911
10- pub const ENV_BECH32_PREFIX : Option < & ' static str > = std:: option_env!( "SLAY_BECH32" ) ;
11- pub const DEFAULT_BECH32_PREFIX : & str = "slay3r" ;
12+ // pub const ENV_BECH32_PREFIX: Option<&'static str> = std::option_env!("SLAY_BECH32");
13+ // pub const DEFAULT_BECH32_PREFIX: &str = "slay3r";
1214
1315/// Valid lengths of decoded addresses
14- pub const VALID_ADDR_LENGTH : [ usize ; 2 ] = [ 20usize , 32usize ] ;
16+ pub const VALID_ADDR_LENGTH : [ usize ; 1 ] = [ 20usize ] ;
17+ // pub const VALID_ADDR_LENGTH: [usize; 2] = [20usize, 32usize];
1518
16- fn bech32_prefix ( ) -> & ' static str {
17- ENV_BECH32_PREFIX . unwrap_or ( DEFAULT_BECH32_PREFIX )
18- }
19+ // fn bech32_prefix() -> &'static str {
20+ // ENV_BECH32_PREFIX.unwrap_or(DEFAULT_BECH32_PREFIX)
21+ // }
1922
2023// Note: this is expanded cw_serde macro minus the Debug implementation, as we want to use Display there
2124#[ derive( :: std:: clone:: Clone , :: std:: cmp:: PartialEq , :: cosmwasm_schema:: schemars:: JsonSchema ) ]
@@ -36,8 +39,8 @@ impl Deref for AccountId {
3639#[ derive( Error , Debug , PartialEq , Eq ) ]
3740pub enum AccountIdError {
3841 /// FIXME: normalize this, so we don't have possibly non-deterministic errors from different crate versions
39- #[ error( "Bech32 : {0}" ) ]
40- Bech32 ( String ) ,
42+ #[ error( "Address : {0}" ) ]
43+ Address ( String ) ,
4144
4245 #[ error( "Invalid variant: bech32m" ) ]
4346 InvalidVariant ,
@@ -49,15 +52,16 @@ pub enum AccountIdError {
4952 InvalidLength ( usize ) ,
5053}
5154
52- impl From < Bech32Error > for AccountIdError {
53- fn from ( value : Bech32Error ) -> Self {
54- AccountIdError :: Bech32 ( value. to_string ( ) )
55+ impl From < AddressError > for AccountIdError {
56+ fn from ( value : AddressError ) -> Self {
57+ AccountIdError :: Address ( value. to_string ( ) )
5558 }
5659}
5760
5861impl Display for AccountId {
5962 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
60- bech32:: encode_to_fmt ( f, bech32_prefix ( ) , self . 0 . to_base32 ( ) , Variant :: Bech32 ) . unwrap ( )
63+ let addr = Address ( self . 0 . as_slice ( ) . try_into ( ) . unwrap ( ) ) ;
64+ write ! ( f, "{:?}" , addr)
6165 }
6266}
6367
@@ -135,23 +139,10 @@ impl AccountId {
135139 }
136140 }
137141
142+ // This requires checksumed....
138143 pub fn parse_string ( encoded : & str ) -> Result < Self , AccountIdError > {
139- let ( hrp, data, variant) = bech32:: decode ( encoded) ?;
140- // no bech32m
141- if variant != Variant :: Bech32 {
142- return Err ( AccountIdError :: InvalidVariant ) ;
143- }
144- // make sure the proper chain prefix
145- let prefix = bech32_prefix ( ) ;
146- if hrp != prefix {
147- return Err ( AccountIdError :: InvalidPrefix ( hrp, prefix) ) ;
148- }
149- let addr = Vec :: < u8 > :: from_base32 ( & data) . unwrap ( ) ;
150- // we only support 20 and 32 bytes for the binary version, enforce this for sanity check
151- if !VALID_ADDR_LENGTH . contains ( & addr. len ( ) ) {
152- return Err ( AccountIdError :: InvalidLength ( addr. len ( ) ) ) ;
153- }
154- Ok ( AccountId ( addr) )
144+ let addr = Address :: parse_checksummed ( encoded, None ) ?;
145+ Ok ( AccountId ( addr. 0 . to_vec ( ) ) )
155146 }
156147
157148 // only for use in test
@@ -212,24 +203,17 @@ mod tests {
212203
213204 #[ test]
214205 fn test_creation ( ) {
215- // properly parses and encoded proper size
206+ // we can encode and decode valid addresses
216207 let id = AccountId :: new ( & [ 42u8 ; 20 ] ) . unwrap ( ) ;
217- assert ! ( id. to_string( ) . starts_with( "slay3r1 " ) ) ;
208+ assert ! ( id. to_string( ) . starts_with( "0x " ) ) ;
218209 let reparse = AccountId :: parse_string ( & id. to_string ( ) ) . unwrap ( ) ;
219210 assert_eq ! ( id, reparse) ;
220211
221- // we can encode and decode valid addresses
222- let id = AccountId :: new ( & [ 69u8 ; 32 ] ) . unwrap ( ) ;
223- assert ! ( id. to_string( ) . starts_with( "slay3r1" ) ) ;
224- let reparse = AccountId :: parse_string ( & id. to_string ( ) ) . unwrap ( ) ;
225- assert_eq ! ( id, reparse) ;
212+ // enforces valid size (we reject 32 bytes)
213+ let _ = AccountId :: new ( & [ 69u8 ; 32 ] ) . unwrap_err ( ) ;
226214
227215 // incorrect raw input fails
228216 let _ = AccountId :: new ( & [ 69u8 ; 15 ] ) . unwrap_err ( ) ;
229-
230- // incorrect bedh32 input fails
231- let bad_addr = id. to_string ( ) . replace ( "q" , "k" ) ;
232- let _ = AccountId :: parse_string ( & bad_addr) . unwrap_err ( ) ;
233217 }
234218
235219 #[ test]
@@ -238,7 +222,7 @@ mod tests {
238222 let id = AccountId :: new ( & raw ) . unwrap ( ) ;
239223
240224 let as_string = to_json_binary ( & id. to_string ( ) ) . unwrap ( ) ;
241- assert ! ( as_string. starts_with( br#""slay3r1 "# ) ) ;
225+ assert ! ( as_string. starts_with( br#""0x "# ) ) ;
242226
243227 let as_raw = to_json_binary ( & raw ) . unwrap ( ) ;
244228 assert ! ( as_raw. starts_with( b"[42,42," ) ) ;
0 commit comments