Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: deprecate bech32 addresses #3411

Merged
merged 18 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/late-pugs-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/interfaces": patch
"@fuel-ts/address": patch
"@fuel-ts/errors": patch
---

chore: deprecate bech32 addresses
maschad marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ The e2e test can be found at:

The Bech32 address of this wallet is `fuel1x33ajpj0jy5p2wcqqu45e32r75zrwfeh6hwqfv5un670rv4p0mns58enjg`. This address can be funded via the [faucet](https://faucet-testnet.fuel.network/).

> [!NOTE] Note
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))

If you want to run an e2e test locally, you can provide your own wallet address and private key. For obvious security reasons, the private key should not be shared.

These can be overridden by generating an environment file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The `contractId` property from the [`Contract`](../../api/Program/Contract.md) c

The [`Address`](../../api/Address/Address.md) class wraps all methods from the [`AbstractAddress`](../../api/Interfaces/AbstractAddress.md) class and adds a single property: `bech32Address`. This property is a string encoded in [`Bech32`](../types/bech32.md) format, recognizable by the human-readable prefix `fuel` followed by the separator `1`.

> [!NOTE] Note
> `Bech32` addresses like `fuel1..` are now deprecated; please switch to B256 format, for more details see [here](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256).

When you log the `contractId` property of an instantiated Contract using `console.log`, the output appears as follows:

```console
Expand Down
3 changes: 3 additions & 0 deletions apps/docs/src/guide/types/address.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ To create an [`Address`](../../api/Address/Address.md) from a `Bech32` address,

<<< @/../../docs-snippets2/src/types/address/creating-an-address.ts#full{ts:line-numbers}

> [!NOTE] Note
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))

### From a Public Key

To create an [`Address`](../../api/Address/Address.md) from a public key, use the following code snippet:
Expand Down
3 changes: 3 additions & 0 deletions apps/docs/src/guide/types/bech32.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# `Bech32`

> [!NOTE] Note
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))

The SDK uses the `Bech32` type as the core property of the [`Address`](../../api/Address/Address.md) class, specifically through the `bech32Address` property.

Originally designed for Bitcoin, the `Bech32` format offers numerous advantages such as enhanced error detection, simplified integrations, and improved compatibility with future upgrades. Given these benefits, the [`Address`](../../api/Address/Address.md) class is constructed around the `Bech32` type.
Expand Down
3 changes: 3 additions & 0 deletions apps/docs/src/guide/utilities/address-conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The Fuel Network uses the [`Bech32`](../types/bech32.md) address format for its

<<< @/../../docs-snippets2/src/types/bech32.ts#addresses-1{ts:line-numbers}

> [!NOTE] Note
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))

However, a hexlified [Bits256](../types/bits256.md) (hex) is another common address format; an example can be seen below:

<<< @/../../docs-snippets2/src/types/evm-address/creating-an-evm.ts#snippet-2{ts:line-numbers}
Expand Down
3 changes: 3 additions & 0 deletions packages/address/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

This module contains the utilities for encoding and decoding address and contract ids between Bech32 and other address formats.

> [!NOTE] Note
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))

# Table of contents

- [Documentation](#documentation)
Expand Down
34 changes: 17 additions & 17 deletions packages/address/src/address.ts
maschad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,30 @@
*/
export default class Address extends AbstractAddress {
// #region address-2
/**
* @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;
// #endregion address-2

/**
* @param address - A Bech32 address
maschad marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor(address: Bech32Address) {
constructor(address: Bech32Address | B256Address) {
super();
this.bech32Address = normalizeBech32(address);

if (!isBech32(this.bech32Address)) {
if (isBech32(address)) {
this.bech32Address = normalizeBech32(address as Bech32Address);
} else if (isB256(address)) {
this.bech32Address = toBech32(address);
} else {
throw new FuelError(
FuelError.CODES.INVALID_BECH32_ADDRESS,
`Invalid Bech32 Address: ${address}.`
);
}
}

Check failure on line 56 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / browser

packages/address/src/address.test.ts > Address class > instantiate an Address class

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:56:2 ❯ packages/address/src/address.test.ts:240:19 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

Check failure on line 56 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / browser

packages/address/src/address.test.ts > Address class > instance equality

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:56:2 ❯ packages/address/src/address.test.ts:251:20 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

/**
* Takes an B256 Address and returns back an checksum address.
Expand All @@ -56,11 +63,12 @@
*/
toChecksum(): ChecksumAddress {
return Address.toChecksum(this.toB256());
}

Check failure on line 66 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / node@18

packages/address/src/address.test.ts > Address class > instantiate an Address class

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:66:3 ❯ packages/address/src/address.test.ts:240:20 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

Check failure on line 66 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / node@18

packages/address/src/address.test.ts > Address class > instance equality

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:66:3 ❯ packages/address/src/address.test.ts:251:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

Check failure on line 66 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / node@20

packages/address/src/address.test.ts > Address class > instantiate an Address class

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:66:3 ❯ packages/address/src/address.test.ts:240:20 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

Check failure on line 66 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / node@20

packages/address/src/address.test.ts > Address class > instance equality

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:66:3 ❯ packages/address/src/address.test.ts:251:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

Check failure on line 66 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / node@22

packages/address/src/address.test.ts > Address class > instantiate an Address class

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:66:3 ❯ packages/address/src/address.test.ts:240:20 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

Check failure on line 66 in packages/address/src/address.ts

View workflow job for this annotation

GitHub Actions / node@22

packages/address/src/address.test.ts > Address class > instance equality

FuelError: Invalid Bech32 Address: FUEL1A7R2L2TFDNCDCCU9UTZQ0FHPTXS3Q080KL32UP3KLVEA8JE2NE9QRQNT6N. ❯ new Address packages/address/src/address.ts:66:3 ❯ packages/address/src/address.test.ts:251:21 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { VERSIONS: { FORC: '0.66.4', FUEL_CORE: '0.40.0', FUELS: '0.97.0' }, metadata: {}, rawError: null, code: 'invalid-bech32-address', toObject: 'Function<toObject>' }

/**
* Returns the `bech32Address` property
*
* @deprecated
* Type `Bech32Address` is now deprecated, as is this method. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
* @returns The `bech32Address` property
*/
toAddress(): Bech32Address {
Expand All @@ -69,7 +77,6 @@

/**
* Converts and returns the `bech32Address` property to a 256 bit hash string
*
* @returns The `bech32Address` property as a 256 bit hash string
*/
toB256(): B256Address {
Expand All @@ -78,16 +85,14 @@

/**
* Converts and returns the `bech32Address` property to a byte array
*
* @returns The `bech32Address` property as a byte array
*/
toBytes(): Uint8Array {
return getBytesFromBech32(this.bech32Address);
}

/**
* Converts
*
* Converts the `bech32Address` property to a 256 bit hash string
* @returns The `bech32Address` property as a 256 bit hash string
*/
toHexString(): B256Address {
Expand All @@ -105,16 +110,14 @@

/**
* Converts and returns the `bech32Address` property as a string
*
* @returns The `bech32Address` property as a string
* @returns The `bech32Address` property as a JSON string
*/
toJSON(): string {
return this.bech32Address;
}

/**
* Clears the first 12 bytes of the `bech32Address` property and returns it as a `EvmAddress`
*
* @returns The `bech32Address` property as an {@link EvmAddress | `EvmAddress`}
*/
toEvmAddress(): EvmAddress {
Expand All @@ -126,9 +129,8 @@
}

/**
* Wraps the `bech32Address` property and returns as an `AssetId`.
*
* @returns The `bech32Address` property as an {@link AssetId | `AssetId`}
* Wraps the B256 property and returns as an `AssetId`.
* @returns The B256 property as an {@link AssetId | `AssetId`}
*/
toAssetId(): AssetId {
return {
Expand All @@ -137,8 +139,7 @@
}

/**
* returns the address `checksum` as a string
*
* Returns the value of the `bech32Address` property
maschad marked this conversation as resolved.
Show resolved Hide resolved
* @returns The value of `bech32Address` property
*/
override valueOf(): string {
Expand All @@ -147,7 +148,6 @@

/**
* Compares this the `bech32Address` property to another for direct equality
*
* @param other - Another address to compare against
* @returns The equality of the comparison
*/
Expand Down
13 changes: 8 additions & 5 deletions packages/address/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const FUEL_BECH32_HRP_PREFIX = 'fuel';

/**
* Decodes a Bech32 address string into Decoded
*
* @deprecated
* Type `Bech32Address` is now deprecated, as is this function. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
* @hidden
*/
export function fromBech32(address: Bech32Address): Decoded {
Expand All @@ -32,7 +33,8 @@ export function fromBech32(address: Bech32Address): Decoded {

/**
* Converts a B256 address string into Bech32
*
* @deprecated
* Type `Bech32Address` is now deprecated, as is this function. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
* @hidden
*/
export function toBech32(address: B256Address): Bech32Address {
Expand All @@ -44,7 +46,8 @@ export function toBech32(address: B256Address): Bech32Address {

/**
* Determines if a given string is Bech32 format
*
* @deprecated
* Type `Bech32Address` is now deprecated, as is this function. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
* @hidden
*/
export function isBech32(address: BytesLike): boolean {
Expand Down Expand Up @@ -84,7 +87,8 @@ export function isEvmAddress(address: string): boolean {

/**
* Takes a Bech32 address and returns the byte data
*
* @deprecated
* The `bech32Address` is now deprecated. Please migrate to B256 format (see https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256 for more details) as this will be the standard going forward.
* @hidden
*/
export function getBytesFromBech32(address: Bech32Address): Uint8Array {
Expand All @@ -93,7 +97,6 @@ export function getBytesFromBech32(address: Bech32Address): Uint8Array {

/**
* Converts a Bech32 address string into B256
*
* @hidden
*/
export function toB256(address: Bech32Address): B256Address {
Expand Down
5 changes: 5 additions & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export enum ErrorCode {
WORKSPACE_NOT_DETECTED = 'workspace-not-detected',

// address
/**
* @deprecated
* Type `Bech32Address` is now deprecated, as is this constant. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
*/
INVALID_BECH32_ADDRESS = 'invalid-bech32-address',

INVALID_EVM_ADDRESS = 'invalid-evm-address',
INVALID_B256_ADDRESS = 'invalid-b256-address',

Expand Down
4 changes: 4 additions & 0 deletions packages/interfaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
*/

// #region bech32-1
/**
* @deprecated
* Type `Bech32Address` is now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
*/
export type Bech32Address = `fuel${string}`;
// #endregion bech32-1
export type B256Address = string;
Expand Down
Loading