Skip to content

Commit

Permalink
updated bech32 crate v0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
andreivasilescu24 committed Aug 26, 2024
1 parent 259aaf2 commit 82cd85a
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion framework/scenario/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ base64 = "0.22"
num-bigint = "0.4"
num-traits = "0.2"
hex = "0.4"
bech32 = "0.9"
bech32 = "0.11"
log = "0.4.17"
sha2 = "0.10.6"
serde = "1.0"
Expand Down
9 changes: 4 additions & 5 deletions framework/scenario/src/bech32.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use bech32::{FromBase32, ToBase32, Variant};
use bech32::{Bech32, Hrp};
use multiversx_sc::types::heap::Address;

pub fn decode(bech32_address: &str) -> Address {
let (_, dest_address_bytes_u5, _) = bech32::decode(bech32_address)
let (_hrp, dest_address_bytes) = bech32::decode(bech32_address)
.unwrap_or_else(|err| panic!("bech32 decode error for {bech32_address}: {err}"));
let dest_address_bytes = Vec::<u8>::from_base32(&dest_address_bytes_u5).unwrap();
if dest_address_bytes.len() != 32 {
panic!("Invalid address length after decoding")
}
Expand All @@ -13,6 +12,6 @@ pub fn decode(bech32_address: &str) -> Address {
}

pub fn encode(address: &Address) -> String {
bech32::encode("erd", address.as_bytes().to_base32(), Variant::Bech32)
.expect("bech32 encode error")
let hrp = Hrp::parse("erd").expect("invalid hrp");
bech32::encode::<Bech32>(hrp, address.as_bytes()).expect("bech32 encode error")
}
2 changes: 1 addition & 1 deletion sdk/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ hex = "0.4.3"
base64 = "0.22"
pbkdf2 = { version = "0.12.2", default-features = false }
zeroize = "1.4.2"
bech32 = "0.9"
bech32 = "0.11"
itertools = "0.13.0"
pem = "3.0.2"
log = "0.4.17"
Expand Down
5 changes: 3 additions & 2 deletions sdk/core/src/crypto/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use super::private_key::PrivateKey;
use anyhow::Result;
use bech32::{self, ToBase32, Variant};
use bech32::{self, Bech32, Hrp};
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
Expand All @@ -23,7 +23,8 @@ impl PublicKey {
}

pub fn to_address(&self) -> Result<String> {
let address = bech32::encode("erd", self.0.to_base32(), Variant::Bech32)?;
let hrp = Hrp::parse("erd")?;
let address = bech32::encode::<Bech32>(hrp, &self.0)?;
Ok(address)
}

Expand Down
8 changes: 4 additions & 4 deletions sdk/core/src/data/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::{Debug, Display};

use crate::crypto::public_key::PublicKey;
use anyhow::Result;
use bech32::{FromBase32, ToBase32, Variant};
use bech32::{Bech32, Hrp};
use serde::{
de::{Deserialize, Deserializer},
ser::{Serialize, Serializer},
Expand All @@ -21,8 +21,7 @@ impl Address {
}

pub fn from_bech32_string(bech32: &str) -> Result<Self> {
let (_, data, _) = bech32::decode(bech32)?;
let data = Vec::<u8>::from_base32(&data)?;
let (_hrp, data) = bech32::decode(bech32)?;

let mut bits: [u8; 32] = [0u8; 32];
bits.copy_from_slice(&data);
Expand All @@ -31,7 +30,8 @@ impl Address {
}

pub fn to_bech32_string(&self) -> Result<String> {
let address = bech32::encode("erd", self.0.to_base32(), Variant::Bech32)?;
let hrp = Hrp::parse("erd")?;
let address = bech32::encode::<Bech32>(hrp, &self.0)?;
Ok(address)
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/scenario-format/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ num-bigint = "0.4"
num-traits = "0.2"
hex = "0.4"
sha3 = "0.10.8"
bech32 = "0.9.0"
bech32 = "0.11.0"
5 changes: 2 additions & 3 deletions sdk/scenario-format/src/value_interpreter/functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::value_interpreter::*;
use bech32::FromBase32;
use sha3::{Digest, Keccak256};

pub const SC_ADDRESS_NUM_LEADING_ZEROS: usize = 8;
Expand Down Expand Up @@ -71,6 +70,6 @@ pub(crate) fn sc_address_expression(input: &str, vm_type: &VMIdentifier) -> Vec<
}

pub(crate) fn bech32(input: &str) -> Vec<u8> {
let (_, decoded, _) = bech32::decode(input).expect("bech32 decode error");
Vec::<u8>::from_base32(&decoded).expect("bech32 base64 decode error")
let (_hrp, decoded) = bech32::decode(input).expect("bech32 decode error");
decoded
}
2 changes: 1 addition & 1 deletion tools/mxpy-snippet-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = "0.52.3"
path = "../../framework/base"

[dependencies]
bech32 = "0.9"
bech32 = "0.11"
num-bigint = "0.4"
num-traits = "0.2"
hex = "0.4"
4 changes: 1 addition & 3 deletions tools/mxpy-snippet-generator/src/helper_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bech32::FromBase32;
use multiversx_sc::types::heap::Address;

use crate::constants::*;
Expand Down Expand Up @@ -79,8 +78,7 @@ impl TransactionType {
}

pub fn bech32_to_bytes(bech32_address: &str) -> Address {
let (_, dest_address_bytes_u5, _) = bech32::decode(bech32_address).unwrap();
let dest_address_bytes = Vec::<u8>::from_base32(&dest_address_bytes_u5).unwrap();
let (_hrp, dest_address_bytes) = bech32::decode(bech32_address).unwrap();
if dest_address_bytes.len() != ADDRESS_LEN {
panic!("Invalid address length after decoding")
}
Expand Down

0 comments on commit 82cd85a

Please sign in to comment.