Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link

Icon LinkConverting Between Address Formats and Native 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.

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