Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
update bech32 dep
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Mar 4, 2024
1 parent efde9ca commit 8a22c92
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license-file = "LICENSE"
[dependencies]
anyhow = "1.0"
base64 = "0.22"
bech32 = "0.9"
bech32 = "0.11"
bip32 = { version = "0.5", features = ["secp256k1", "bip39"] }
chrono = "0.4"
clap = { version = "4.5", features = ["derive"] }
Expand Down
11 changes: 5 additions & 6 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ripemd::Ripemd160;
use sha2::{Digest, Sha256};
use sha3::Keccak256;

use bech32::{ToBase32, Variant};
use bech32::{Bech32, Hrp};

// https://iancoleman.io/bip39

Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn from_pk_bytes_to_address(pub_key: &[u8], addr_type: &AddressType) -> Resu
AddressType::Cosmos => cosmos_key_derive(pub_key),
AddressType::Ethereum => ethereum_key_derive(pub_key),
};
let address = bech32::encode("cosmos", data.to_base32(), Variant::Bech32)?;
let address = bech32::encode::<Bech32>(Hrp::parse("cosmos")?, &data)?;
println!("Cosmos address: {}", &address);
Ok(address)
}
Expand Down Expand Up @@ -134,9 +134,8 @@ pub fn mnemonic_to_cosmos_addr(mnemonic: &Mnemonic, hrp: &str, coin_type: &u64)
let seed = mnemonic.to_seed("");
let priv_key = XPrv::derive_from_path(seed, &derivation_path)?;
let pub_key = priv_key.public_key();
Ok(bech32::encode(
hrp,
cosmos_key_derive(pub_key.to_bytes().as_ref()).to_base32(),
Variant::Bech32,
Ok(bech32::encode::<Bech32>(
Hrp::parse(hrp)?,
&cosmos_key_derive(pub_key.to_bytes().as_ref()),
)?)
}
6 changes: 3 additions & 3 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub async fn get_rpc_endpoint_chain_info(rpc_endpoint: &str) -> Result<crate::ch
.map(|x| x.base_vesting_account.unwrap().base_account.unwrap())
})?
.address;
let (prefix, _, _) = bech32::decode(&address)?;
let (prefix, _) = bech32::decode(&address)?;

let page_request = PageRequest {
limit: u64::MAX,
Expand All @@ -214,7 +214,7 @@ pub async fn get_rpc_endpoint_chain_info(rpc_endpoint: &str) -> Result<crate::ch
Result::Ok(if denoms.len() == 1 {
crate::chain::Chain {
chain_id,
prefix,
prefix: prefix.to_string(),
fee: 0,
denom: denoms[0].into(),
}
Expand All @@ -232,7 +232,7 @@ pub async fn get_rpc_endpoint_chain_info(rpc_endpoint: &str) -> Result<crate::ch

crate::chain::Chain {
chain_id,
prefix,
prefix: prefix.to_string(),
fee: 0,
denom,
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Result;
use base64::prelude::{Engine as _, BASE64_STANDARD};
use bech32::Variant;
use bech32::{Bech32, Hrp};
use cosmos_sdk_proto::prost_wkt_types::MessageSerde;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -48,8 +48,8 @@ where
}

pub fn bech32(address: &str, prefix: &str) -> Result<String> {
let (_, bytes, _) = bech32::decode(address)?;
Ok(bech32::encode(prefix, bytes, Variant::Bech32)?)
let (_, bytes) = bech32::decode(address)?;
Ok(bech32::encode::<Bech32>(Hrp::parse(prefix)?, &bytes)?)
}

pub fn parse_dec_amount(st: &str, precision: usize) -> Result<u128> {
Expand Down

0 comments on commit 8a22c92

Please sign in to comment.