Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Address Conversion

Icon LinkAddress

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';

However, a hexlified Bits256 (hex) is another common address format; an example can be seen below:

const bits256 = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';

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',
};

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.

Icon LinkAddress Conversion

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.

Icon LinkFrom Bech32 to b256

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

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 LinkFrom b256 to Bech32

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
 

And by using the isB256 and toBech32 utilities:

// #import { toBech32, isB256 };
 
const b256 = '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';
 
if (isB256(b256)) {
	bech32 = toBech32(b256);
	// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
}

Icon LinkConverting a Contract ID

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 LinkConverting a Wallet Address

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 LinkConverting an Asset ID

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();