Skip to content

Commit 27e8808

Browse files
authored
chore: deprecate bech32 addresses (#3411)
1 parent 2cef020 commit 27e8808

File tree

11 files changed

+67
-28
lines changed

11 files changed

+67
-28
lines changed

.changeset/late-pugs-carry.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@fuel-ts/interfaces": patch
3+
"@fuel-ts/address": patch
4+
"@fuel-ts/errors": patch
5+
---
6+
7+
chore: deprecate bech32 addresses

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ The e2e test can be found at:
246246

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

249+
> [!NOTE] Note
250+
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
251+
249252
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.
250253

251254
These can be overridden by generating an environment file:

apps/docs/src/guide/contracts/managing-deployed-contracts.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The `contractId` property from the [`Contract`](../../api/Program/Contract.md) c
88

99
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`.
1010

11+
> [!NOTE] Note
12+
> `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).
13+
1114
When you log the `contractId` property of an instantiated Contract using `console.log`, the output appears as follows:
1215

1316
```console

apps/docs/src/guide/types/address.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ To create an [`Address`](../../api/Address/Address.md) from a `Bech32` address,
2424

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

27+
> [!NOTE] Note
28+
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
29+
2730
### From a Public Key
2831

2932
To create an [`Address`](../../api/Address/Address.md) from a public key, use the following code snippet:

apps/docs/src/guide/types/bech32.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# `Bech32`
22

3+
> [!NOTE] Note
4+
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
5+
36
The SDK uses the `Bech32` type as the core property of the [`Address`](../../api/Address/Address.md) class, specifically through the `bech32Address` property.
47

58
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.

apps/docs/src/guide/utilities/address-conversion.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The Fuel Network uses the [`Bech32`](../types/bech32.md) address format for its
66

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

9+
> [!NOTE] Note
10+
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
11+
912
However, a hexlified [Bits256](../types/bits256.md) (hex) is another common address format; an example can be seen below:
1013

1114
<<< @/../../docs-snippets2/src/types/evm-address/creating-an-evm.ts#snippet-2{ts:line-numbers}

packages/address/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

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

7+
> [!NOTE] Note
8+
> `Bech32` addresses like `fuel1..` are now deprecated. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
9+
710
# Table of contents
811

912
- [Documentation](#documentation)

packages/address/src/address.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,30 @@ import {
3030
*/
3131
export default class Address extends AbstractAddress {
3232
// #region address-2
33+
/**
34+
* @deprecated
35+
* Type `Bech32Address` is now deprecated, as is this property. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
36+
*/
3337
readonly bech32Address: Bech32Address;
3438
// #endregion address-2
3539

3640
/**
37-
* @param address - A Bech32 address
41+
* @param address - A Bech32 address or B256 address
3842
*/
39-
constructor(address: Bech32Address) {
43+
constructor(address: Bech32Address | B256Address) {
4044
super();
41-
this.bech32Address = normalizeBech32(address);
4245

43-
if (!isBech32(this.bech32Address)) {
44-
throw new FuelError(
45-
FuelError.CODES.INVALID_BECH32_ADDRESS,
46-
`Invalid Bech32 Address: ${address}.`
47-
);
46+
if (isB256(address)) {
47+
this.bech32Address = toBech32(address);
48+
} else {
49+
this.bech32Address = normalizeBech32(address as Bech32Address);
50+
51+
if (!isBech32(this.bech32Address)) {
52+
throw new FuelError(
53+
FuelError.CODES.INVALID_BECH32_ADDRESS,
54+
`Invalid Bech32 Address: ${this.bech32Address}.`
55+
);
56+
}
4857
}
4958
}
5059

@@ -60,7 +69,8 @@ export default class Address extends AbstractAddress {
6069

6170
/**
6271
* Returns the `bech32Address` property
63-
*
72+
* @deprecated
73+
* Type `Bech32Address` is now deprecated, as is this method. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
6474
* @returns The `bech32Address` property
6575
*/
6676
toAddress(): Bech32Address {
@@ -69,7 +79,6 @@ export default class Address extends AbstractAddress {
6979

7080
/**
7181
* Converts and returns the `bech32Address` property to a 256 bit hash string
72-
*
7382
* @returns The `bech32Address` property as a 256 bit hash string
7483
*/
7584
toB256(): B256Address {
@@ -78,16 +87,14 @@ export default class Address extends AbstractAddress {
7887

7988
/**
8089
* Converts and returns the `bech32Address` property to a byte array
81-
*
8290
* @returns The `bech32Address` property as a byte array
8391
*/
8492
toBytes(): Uint8Array {
8593
return getBytesFromBech32(this.bech32Address);
8694
}
8795

8896
/**
89-
* Converts
90-
*
97+
* Converts the `bech32Address` property to a 256 bit hash string
9198
* @returns The `bech32Address` property as a 256 bit hash string
9299
*/
93100
toHexString(): B256Address {
@@ -105,16 +112,14 @@ export default class Address extends AbstractAddress {
105112

106113
/**
107114
* Converts and returns the `bech32Address` property as a string
108-
*
109-
* @returns The `bech32Address` property as a string
115+
* @returns The `bech32Address` property as a JSON string
110116
*/
111117
toJSON(): string {
112118
return this.bech32Address;
113119
}
114120

115121
/**
116122
* Clears the first 12 bytes of the `bech32Address` property and returns it as a `EvmAddress`
117-
*
118123
* @returns The `bech32Address` property as an {@link EvmAddress | `EvmAddress`}
119124
*/
120125
toEvmAddress(): EvmAddress {
@@ -126,9 +131,8 @@ export default class Address extends AbstractAddress {
126131
}
127132

128133
/**
129-
* Wraps the `bech32Address` property and returns as an `AssetId`.
130-
*
131-
* @returns The `bech32Address` property as an {@link AssetId | `AssetId`}
134+
* Wraps the B256 property and returns as an `AssetId`.
135+
* @returns The B256 property as an {@link AssetId | `AssetId`}
132136
*/
133137
toAssetId(): AssetId {
134138
return {
@@ -137,17 +141,15 @@ export default class Address extends AbstractAddress {
137141
}
138142

139143
/**
140-
* returns the address `checksum` as a string
141-
*
142-
* @returns The value of `bech32Address` property
144+
* Wraps the B256 address `checksum` and returns it as a string
145+
* @returns The B256 address `checksum` as a string
143146
*/
144147
override valueOf(): string {
145148
return this.toChecksum();
146149
}
147150

148151
/**
149152
* Compares this the `bech32Address` property to another for direct equality
150-
*
151153
* @param other - Another address to compare against
152154
* @returns The equality of the comparison
153155
*/

packages/address/src/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export const FUEL_BECH32_HRP_PREFIX = 'fuel';
2323

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

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

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

8588
/**
8689
* Takes a Bech32 address and returns the byte data
87-
*
90+
* @deprecated
91+
* 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.
8892
* @hidden
8993
*/
9094
export function getBytesFromBech32(address: Bech32Address): Uint8Array {
@@ -93,7 +97,6 @@ export function getBytesFromBech32(address: Bech32Address): Uint8Array {
9397

9498
/**
9599
* Converts a Bech32 address string into B256
96-
*
97100
* @hidden
98101
*/
99102
export function toB256(address: Bech32Address): B256Address {

packages/errors/src/error-codes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ export enum ErrorCode {
2525
WORKSPACE_NOT_DETECTED = 'workspace-not-detected',
2626

2727
// address
28+
/**
29+
* @deprecated
30+
* Type `Bech32Address` is now deprecated, as is this constant. Use `B256` addresses instead. ([help](https://docs.fuel.network/docs/specs/abi/argument-encoding/#b256))
31+
*/
2832
INVALID_BECH32_ADDRESS = 'invalid-bech32-address',
33+
2934
INVALID_EVM_ADDRESS = 'invalid-evm-address',
3035
INVALID_B256_ADDRESS = 'invalid-b256-address',
3136

0 commit comments

Comments
 (0)