⌘K

Icon LinkBech32

Bech32AddressBech32ContractId使得地址和合约 ID 可以以bech32格式使用。它们可以轻松地转换为它们的对应项AddressContractId

以下是创建Bech32Address的主要方法,但请注意,同样适用于Bech32ContractId

use fuels::types::{bech32::Bech32Address, Address, Bytes32};
 
// New from HRP string and a hash
let hrp = "fuel";
let my_slice = [1u8; 32];
let _bech32_address = Bech32Address::new(hrp, my_slice);
 
// Note that you can also pass a hash stored as Bytes32 to new:
let my_hash = Bytes32::new([1u8; 32]);
let _bech32_address = Bech32Address::new(hrp, my_hash);
 
// From a string.
let address = "fuel1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqsx2mt2";
let bech32_address = Bech32Address::from_str(address)?;
assert_eq!([0u8; 32], *bech32_address.hash());
 
// From Address
let plain_address = Address::new([0u8; 32]);
let bech32_address = Bech32Address::from(plain_address);
assert_eq!([0u8; 32], *bech32_address.hash());
 
// Convert to Address
let _plain_address: Address = bech32_address.into();
 
Icon InfoCircle

**注意:**当从AddressContractId创建Bech32AddressBech32ContractId时,HRP(可读部分)默认设置为 "fuel"