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 b256Address =
  '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
See code in context

apps/

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

ts
const evmAddress: EvmAddress = {
  bits: '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 type { B256Address, Bech32Address } from 'fuels';
import { Address } from 'fuels';

const bech32: Bech32Address =
  'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs';

const address: Address = Address.fromDynamicInput(bech32);

const b256: B256Address = address.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 type { B256Address, Bech32Address } from 'fuels';
import { isBech32, toB256 } from 'fuels';

const bech32: Bech32Address =
  'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs';

const b256: B256Address | null = isBech32(bech32) ? toB256(bech32) : null;
// 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 type { B256Address, Bech32Address } from 'fuels';
import { Address } from 'fuels';

const b256: B256Address =
  '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';

const address: Address = Address.fromDynamicInput(b256);

const bech32: Bech32Address = address.bech32Address;
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
See code in context

And by using the isB256 and toBech32 utilities:

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

const b256 =
  '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f';
const bech32 = isB256(b256) ? toBech32(b256) : null;
// 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 type { Bech32Address } from 'fuels';
import { Address, Provider, Contract } from 'fuels';

import { LOCAL_NETWORK_URL } from '../../env';
import { Counter } from '../../typegend/contracts';

const provider = await Provider.create(LOCAL_NETWORK_URL);

const contractAbi = Counter.abi;
const contractAddress = Address.fromB256(
  '0x6d309766c0f1c6f103d147b287fabecaedd31beb180d45cf1bf7d88397aecc6f'
);

const contract = new Contract(contractAddress, contractAbi, provider);

const bech32: Bech32Address = 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 type { Bech32Address, WalletLocked } from 'fuels';
import { Address, Provider, Wallet } from 'fuels';

import { LOCAL_NETWORK_URL } from '../../env';

const provider = await Provider.create(LOCAL_NETWORK_URL);

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

const wallet: WalletLocked = Wallet.fromAddress(address, provider);

const bech32: Bech32Address = wallet.address.toAddress();
// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs
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, B256Address } from 'fuels';
import { Address } from 'fuels';

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