Skip to content

Address

In Sway, the Address type serves as a type-safe wrapper around the primitive b256 type. The SDK takes a different approach and has its own abstraction for the Address type.

AbstractAddress Class

The SDK defines the AbstractAddress class, which provides a set of utility functions for easy manipulation and conversion between address formats.

ts
export abstract class AbstractAddress {
  abstract toJSON(): string;
  abstract toString(): string;
  abstract toAddress(): Bech32Address;
  abstract toB256(): B256Address;
  abstract toHexString(): string;
  abstract toBytes(): Uint8Array;
  abstract equals(other: AbstractAddress): boolean;
}
See code in context

Address Class

Besides conforming to the interface of the AbstractAddress, the Address class also defines one property; bech32Address, which is of the Bech32 type.

ts
readonly bech32Address: Bech32Address;
See code in context

Creating an Address

Thanks to the utility functions provided by the AbstractAddress class, there are several ways to create an Address instance:

From a Bech32 Address

To create an Address from a Bech32 address, use the following code snippet:

ts
import { Address } from 'fuels';

const ADDRESS_BECH32 =
  'fuel1elnmzsav56dqnp95sx4e2pckq36cvae9ser44m5zlvgtwxw49fmqd7e42e';

const address = new Address(ADDRESS_BECH32);
See code in context

From a Public Key

To create an Address from a public key, use the following code snippet:

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

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

const provider = await Provider.create(LOCAL_NETWORK_URL);

const wallet = Wallet.generate({ provider });

const address = Address.fromPublicKey(wallet.publicKey);
See code in context

From a 256-bit Address

To create an Address from a 256-bit address, use the following code snippet:

ts
import { Address } from 'fuels';
const b256 =
  '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';

const address = Address.fromB256(b256);

console.log('b256', address.toB256());
// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6
See code in context

Utility Functions

The Address class also provides some practical utility functions:

  1. fromString: Create a new Address from an ambiguous source that may be a Bech32 or B256 address:
ts
import { Address } from 'fuels';

const address = Address.fromRandom();

const addressCloneFromBech = Address.fromString(address.toString());
const addressCloneFromB256 = Address.fromString(address.toB256());
See code in context
  1. fromDynamicInput: Create a new Address when the address source is unknown:
ts
import { Address } from 'fuels';

const dataFromInput: string =
  '0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e';

// If the input string can't be resolved this will throw an error
const address = Address.fromDynamicInput(dataFromInput);
See code in context
  1. equals: As you may already notice, the equals function can compare addresses instances:
ts
import { Address } from 'fuels';

const address = Address.fromRandom();

const address1 = Address.fromString(address.toString());
const address2 = Address.fromString(address.toB256());

console.log('equals', address1.equals(address2));
// true
See code in context