Bech32
Bech32Address
和Bech32ContractId
使得地址和合约 ID 可以以bech32
格式使用。它们可以轻松地转换为它们的对应项Address
和ContractId
。
以下是创建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();
**注意:**当从
Address
或ContractId
创建Bech32Address
或Bech32ContractId
时,HRP
(可读部分)默认设置为 "fuel"。