Skip to content

Commit 8e30e27

Browse files
committed
AccountId uses Etherem 0x encoding
1 parent e8997dd commit 8e30e27

4 files changed

Lines changed: 157 additions & 42 deletions

File tree

Cargo.lock

Lines changed: 128 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/std/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ hex = { workspace = true }
2222
bech32 = { version = "0.9.1", default-features = false }
2323
chrono = { version = "0.4.19", default-features = false, features = ["std"] }
2424
derivative = "2.2.0"
25+
26+
# TODO: move this up to top level Cargo if it works out
27+
alloy-primitives = { version = "0.8.2", default-features = false }

packages/std/src/account_id.rs

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ use std::fmt::{Debug, Display, Formatter};
22
use std::ops::Deref;
33

44
use ::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+
68
use cosmwasm_std::{Addr, StdResult};
79
use cw_storage_plus::{Key, KeyDeserialize, Prefixer, PrimaryKey};
810
use 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)]
3740
pub 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

5861
impl 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,"));

packages/std/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod time;
99
mod tx;
1010
mod utils;
1111

12-
pub use account_id::{must_id, AccountId, AccountIdError, DEFAULT_BECH32_PREFIX};
12+
pub use account_id::{must_id, AccountId, AccountIdError};
1313
use cosmwasm_std::Binary;
1414
pub use encode::{CoinEncode, HexEncode};
1515
pub use gas::{GasError, GasMeter, GasResult};

0 commit comments

Comments
 (0)