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
const ADDRESS_BECH32 = 'fuel1elnmzsav56dqnp95sx4e2pckq36cvae9ser44m5zlvgtwxw49fmqd7e42e';
const ADDRESS_CHECKSUM = '0xcfE7B143aCa69A0984b481aB950716047586772586475aee82fB10B719D52A76';

const address = new Address(ADDRESS_BECH32);

expect(address.toString()).toEqual(ADDRESS_CHECKSUM);
expect(address.bech32Address).toEqual(ADDRESS_BECH32);
See code in context

From a Public Key

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

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

const address = Address.fromPublicKey(wallet.publicKey);

expect(address).toEqual(wallet.address);
See code in context

From a 256-bit Address

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

ts
const b256 = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6';

const address = Address.fromB256(b256);

expect(address.toB256()).toEqual(b256);
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
const address = Address.fromRandom();

const addressCloneFromBech = Address.fromString(address.toString());
const addressCloneFromB256 = Address.fromString(address.toB256());

expect(addressCloneFromBech.equals(addressCloneFromB256));
See code in context
  1. fromDynamicInput: Create a new Address when the address source is unknown:
ts
const dataFromInput: string =
  '0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e';

// if the input string can't be resolved this will throw an error
const someAddress = Address.fromDynamicInput(dataFromInput);

expect(someAddress).toBeTruthy();
See code in context
  1. equals: As you may already notice, the equals function can compare addresses instances:
ts
const address = Address.fromRandom();

const addressCloneFromBech = Address.fromString(address.toString());
const addressCloneFromB256 = Address.fromString(address.toB256());

expect(address.equals(addressCloneFromBech)).toBeTruthy();
expect(address.equals(addressCloneFromB256)).toBeTruthy();
See code in context