Skip to content

Address

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:

ts
const bech32 = 'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs';
See code in context

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

ts
const bits256 = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
See code in context

At times, these can even be wrapped in a Struct. Such as an Asset ID or a EVM Address:

ts
import type { EvmAddress } from 'fuels';

const address: EvmAddress = {
  value: '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6',
};
See code in context

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.

Address 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.

From Bech32 to b256

By instantiating an Address, we can validate a Bech32 address and easily convert it to a b256:

ts
import { Address } from 'fuels';

const bech32 = 'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs';
const addressInstance = Address.fromDynamicInput(bech32);
const b256 = addressInstance.toB256();
// 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f
See code in context

Or, if you'd prefer to use utility functions directly for validation and conversion, you can use isBech32 and toB256:

ts
import { isBech32, toB256 } from 'fuels';

const bech32 = 'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs';

if (isBech32(bech32)) {
  b256 = toB256(bech32);
  // 0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f
}
See code in context

From b256 to Bech32

In a similar fashion, we have both class functions on the Address and utilities available for b256 validation and conversion:

ts
import { Address } from 'fuels';

const b256 = '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';
const addressInstance = Address.fromDynamicInput(b256);
const bech32 = addressInstance.bech32Address;
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
See code in context

And by using the isB256 and toBech32 utilities:

ts
import { toBech32, isB256 } from 'fuels';

const b256 = '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';

if (isB256(b256)) {
  bech32 = toBech32(b256);
  // fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
}
See code in context

Converting 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:

ts
import { Contract, Address } from 'fuels';

const address = Address.fromB256(
  '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f'
);

const contract = new Contract(address, abi, provider);

const bech32 = contract.id.toAddress();
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
See code in context

Converting a Wallet Address

Similarly, the Wallet address property is also of type AbstractAddress and can therefore use the same Address class functions for conversion:

ts
import { Wallet, Address } from 'fuels';

const address = Address.fromB256(
  '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f'
);
const wallet: WalletLocked = Wallet.fromAddress(address, provider);
const walletAddress = wallet.address.toAddress();
See code in context

Converting an Asset ID

Asset IDs are a wrapped b256 value. The following example shows how to create an Address from a b256 type:

ts
import type { AssetId } from 'fuels';
import { Address } from 'fuels';

const b256 = '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';
const address = Address.fromB256(b256);
const assetId: AssetId = address.toAssetId();
See code in context