Back to Fuels Ts

Address

apps/docs/src/guide/utilities/address-conversion.md

0.103.02.5 KB
Original Source

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 B256 address format for its interactions, an example of which can be seen below:

<<< @/../../docs/src/guide/types/snippets/b256.ts#addresses-1{ts:line-numbers}

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

<<< @/../../docs/src/guide/types/snippets/evm-address/creating-an-evm.ts#snippet-2{ts:line-numbers} apps/

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

<<< @/../../docs/src/guide/types/snippets//evm-address/using-an-evm-address-1.ts#snippet-2{ts:line-numbers}

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.

Converting a Contract ID

The Contract id property is an instance of the Address class. Therefore, it can be converted using the Address class functions such as toAddress and toB256:

<<< @./snippets/address-conversion/contract.ts#conversion-2{ts:line-numbers}

Converting a Wallet Address

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

<<< @./snippets/address-conversion/wallet.ts#conversion-3{ts:line-numbers}

Converting an Asset ID

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

<<< @./snippets/address-conversion/asset-id.ts#conversion-4{ts:line-numbers}