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.
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;
}
Besides conforming to the interface of the AbstractAddress
, the Address
class also defines one property; bech32Address
, which is of the Bech32
type.
/**
* @deprecated
* Type `Bech32Address` is now deprecated, as is this property. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
*/
readonly bech32Address: Bech32Address;
Thanks to the utility functions provided by the AbstractAddress
class, there are several ways to create an Address
instance:
Bech32
Address To create an Address
from a Bech32
address, use the following code snippet:
import { Address } from 'fuels';
const ADDRESS_BECH32 =
'fuel1elnmzsav56dqnp95sx4e2pckq36cvae9ser44m5zlvgtwxw49fmqd7e42e';
const address = new Address(ADDRESS_BECH32);
[!NOTE] Note
Bech32
addresses likefuel1..
are now deprecated. UseB256
addresses instead. (help )
To create an Address
from a public key, use the following code snippet:
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);
To create an Address
from a 256-bit address, use the following code snippet:
import { Address } from 'fuels';
const b256 =
'0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
const address = Address.fromB256(b256);
console.log('b256', address.toB256());
// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6
The Address
class also provides some practical utility functions:
fromString
: Create a new Address
from an ambiguous source that may be a Bech32
or B256
address: import { Address } from 'fuels';
const address = Address.fromRandom();
const addressCloneFromBech = Address.fromString(address.toString());
const addressCloneFromB256 = Address.fromString(address.toB256());
fromDynamicInput
: Create a new Address
when the address source is unknown: 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);
equals:
As you may already notice, the equals
function can compare addresses instances: 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