Addresses and varying address formats are commonplace when interacting with decentralized applications. Furthermore, different networks may enforce different address formats.
The Fuel Network uses the
Bech32
address format for its interactions, an example of which can be seen below:
const bech32 = ' fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs ' ;
Icon ClipboardText
However, a hexlified
Bits256 (hex) is another common address format; an example can be seen below:
const bits256 = ' 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 ' ;
Icon ClipboardText
At times, these can even be wrapped in a
Struct . Such as an
Asset ID or a
EVM Address :
// #import { EvmAddress };
const address : EvmAddress = {
value : ' 0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6 ' ,
};
Icon ClipboardText
The TS-SDK makes converting between these addresses simple using the
Address helper, which provides various utilities for conversion.
The following
conversion guide will show how to utilize this class to convert between address formats, as well as Sway Standard Types.
This guide demonstrates how to convert between address formats and Sway Standard Types using helper functions. Native types are wrappers for bytes, and you can perform conversions between them by leveraging these functions and classes.
By instantiating an
Address
, we can validate a
Bech32
address and easily convert it to a
b256
:
// #import { Address };
const bech32 = ' fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs ' ;
const addressInstance = Address. fromDynamicInput (bech32);
const b256 = addressInstance. toB256 ();
// 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f
Icon ClipboardText
Or, if you'd prefer to use utility functions directly for validation and conversion, you can use isBech32
and toB256
:
// #import { toB256, isBech32 };
const bech32 = ' fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs ' ;
if ( isBech32 (bech32)) {
b256 = toB256 (bech32);
// 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f
}
Icon ClipboardText
In a similar fashion, we have both class functions on the
Address
and utilities available for
b256
validation and conversion:
// #import { Address };
const b256 = ' 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f ' ;
const addressInstance = Address. fromDynamicInput (b256);
const bech32 = addressInstance.bech32Address;
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
Icon ClipboardText
And by using the isB256
and toBech32
utilities:
// #import { toBech32, isB256 };
const b256 = ' 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f ' ;
if ( isB256 (b256)) {
bech32 = toBech32 (b256);
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
}
Icon ClipboardText
The Contract
id
property has the
AbstractAddress
type. Therefore, it can be converted using the
Address
class functions such as
toAddress
and
toB256
:
// #import { Address, Contract };
const address = Address. fromB256 (
' 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f '
);
const contract = new Contract (address, abi, provider);
const bech32 = contract.id. toAddress ();
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
Icon ClipboardText
Similarly, the Wallet
address
property is also of type
AbstractAddress
and can therefore use the same
Address
class functions for conversion:
// #import { Wallet, Address };
const address = Address. fromB256 (
' 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f '
);
const wallet : WalletLocked = Wallet. fromAddress (address, provider);
const walletAddress = wallet.address. toAddress ();
Icon ClipboardText
Asset IDs are a wrapped
b256 value. The following example shows how to create an
Address
from a
b256
type:
// #import { Address, AssetId };
const b256 = ' 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f ' ;
const address = Address. fromB256 (b256);
const assetId : AssetId = address. toAssetId ();
Icon ClipboardText