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.
The Address
class also provides a set of utility functions for easy manipulation and conversion between address formats along with one property; b256Address
, which is of the B256
type.
readonly b256Address: B256Address;
There are several ways to create an Address
instance:
To create an Address
from a 256-bit address, use the following code snippet:
import { Address } from 'fuels';
const b256 =
'0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
const address = new Address(b256);
console.log('b256', address.toB256());
// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6
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 = new Provider(LOCAL_NETWORK_URL);
const wallet = Wallet.generate({ provider });
const address = new Address(wallet.publicKey);
To create an Address
from an EVM address, use the following code snippet:
import { Address } from 'fuels';
const evmAddress = '0x675b68aa4d9c2d3bb3f0397048e62e6b7192079c';
const address = new Address(evmAddress);
To create an Address
from an existing Address
instance, use the following code snippet:
import { Address } from 'fuels';
const address = Address.fromRandom();
const addressClone = new Address(address);
equals
As you may already notice, the equals
function can compare addresses instances:
import { Address } from 'fuels';
const address = Address.fromRandom();
const address1 = new Address(address.toString());
const address2 = new Address(address.toB256());
console.log('equals', address1.equals(address2));
// true
toChecksum
To convert an address to a checksum address, use the toChecksum
function:
import { Address } from 'fuels';
const b256 =
'0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';
const address = new Address(b256);
console.log('checksum', address.toChecksum());
// true