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

docs: favour B256 and B512 type references #3599

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .changeset/wet-radios-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

docs: favour `B256` and `B512` type references
8 changes: 4 additions & 4 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,12 @@ export default defineConfig({
link: '/guide/types/asset-id',
},
{
text: 'Bits256',
link: '/guide/types/bits256',
text: 'B256',
link: '/guide/types/b256',
},
{
text: 'Bits512',
link: '/guide/types/bits512',
text: 'B512',
link: '/guide/types/b512',
},
{
text: 'Bytes',
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/.vitepress/plugins/utils/extractImports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('extractImports', () => {
'// #import { AssetId };',
'',
'const assetId: AssetId = {',
' value: Bits256,',
' value: B256,',
'};',
];
const filepath = '/some/file/asset-id.test.ts';
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('extractImports', () => {
' // #import { AssetId, Address };',
'',
' const assetId: AssetId = {',
' value: Bits256,',
' value: B256,',
' };',
' const address: Address = new Address();',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ To interact with a deployed contract using the SDK without redeploying it, you o

The `contractId` property from the [`Contract`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_program.Contract.html) class is an instance of the [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) class.

The [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) class also provides a set of utility functions for easy manipulation and conversion between address formats along with one property; `b256Address`, which is a string encoded in [`B256`](../types/bits256.md) format.
The [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) class also provides a set of utility functions for easy manipulation and conversion between address formats along with one property; `b256Address`, which is a string encoded in [`B256`](../types/b256.md) format.

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

Expand All @@ -22,10 +22,10 @@ If you have already an instantiated and deployed contract in hands you can creat

<<< @./snippets/managing-deployed-contracts.ts#with-contractId{ts:line-numbers}

The previous example assumes that you have a [`Contract`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_program.Contract.html) instance at hand. However, some Fuel tools and Sway use the [`b256`](../types/bits256.md) type format, a hex-encoded string-like type, for contract IDs.
The previous example assumes that you have a [`Contract`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_program.Contract.html) instance at hand. However, some Fuel tools and Sway use the [`B256`](../types/b256.md) type format, a hex-encoded string-like type, for contract IDs.

You might have this format instead, for example, if you have deployed your contract with `forc deploy`.

The process of instantiating a [`Contract`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_program.Contract.html) remains the same when using a contract ID of type `b256`:
The process of instantiating a [`Contract`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_program.Contract.html) remains the same when using a contract ID of type `B256`:

<<< @./snippets/managing-deployed-contracts.ts#with-b256{ts:line-numbers}
2 changes: 1 addition & 1 deletion apps/docs/src/guide/contracts/minted-token-asset-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The asset ID of a token on the Fuel network is determined by two factors:
- The ID of the contract that minted the token,
- A sub-identifier (Sub ID)

> Both of which are [bits256](../types/bits256.md) strings.
> Both of which are [B256](../types/b256.md) strings.

The process involves applying a SHA-256 hash algorithm to the combination of the Contract ID and the Sub ID, to derive an Asset ID - as explained [here](https://docs.fuel.network/docs/specs/identifiers/asset/#asset-id).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const deployer = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider);
const deployContract = await TokenFactory.deploy(deployer);
const { contract } = await deployContract.waitForResult();

// Any valid bits256 string can be used as a sub ID
// Any valid B256 string can be used as a sub ID
const subID =
'0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745';
const mintAmount = bn(1000);
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/guide/encoding/working-with-bytes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ A fixed-length string's size is known at compile time due to the argument declar

<<< @./snippets/working-with-bytes.ts#working-with-bytes-3{ts:line-numbers}

### `b256` / `b512`
### `B256` / `B512`

These are fixed-length byte arrays, with `b256` containing 256 bits and `b512` containing 512 bits. You can use them for address and signature formats.
These are fixed-length byte arrays, with `B256` containing 256 bits and `B512` containing 512 bits. You can use them for address and signature formats.

<<< @./snippets/working-with-bytes.ts#working-with-bytes-4{ts:line-numbers}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Consider the following predicate:

<<< @/../../docs/sway/simple-predicate/src/main.sw#send-and-spend-funds-from-predicates-1{rust:line-numbers}

This predicate accepts an address of type `b256` and compares it with a hard-coded address of the same type. If both addresses are equal, the predicate returns true, otherwise it will return false.
This predicate accepts an address of type `B256` and compares it with a hard-coded address of the same type. If both addresses are equal, the predicate returns true, otherwise it will return false.

## Interacting with the Predicate Using SDK

Let's use the above predicate to validate our transaction.

Once you've compiled the predicate (`forc build`), you'll obtain two important artifacts: the JSON ABI and the predicate's binary code. These are needed to instantiate a new predicate.

This is where we also pass in the predicate's data. Note that the `main` function in our predicate example requires a parameter called `input_address` of type `b256`. We will pass this parameter to the `Predicate` constructor along with the bytecode and the JSON ABI.
This is where we also pass in the predicate's data. Note that the `main` function in our predicate example requires a parameter called `input_address` of type `B256`. We will pass this parameter to the `Predicate` constructor along with the bytecode and the JSON ABI.

<<< @./snippets/cookbook/transferring-assets.ts#send-and-spend-funds-from-predicates-2{ts:line-numbers}

Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/guide/types/address.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Address

In Sway, the [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) 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](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) type.
In Sway, the [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) 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](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) type.

## Address Class

The [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) 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`](./bits256.md) type.
The [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) 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`](./b256.md) type.

<<< @/../../../packages/address/src/address.ts#address-2{ts:line-numbers}

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/guide/types/asset-id.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Asset ID

An Asset ID can be represented using the `AssetId` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `Bits256` value.
An Asset ID can be represented using the `AssetId` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `B256` value.

<<< @./snippets/asset-id/intro.ts#full{ts:line-numbers}

Expand Down
21 changes: 21 additions & 0 deletions apps/docs/src/guide/types/b256.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# `B256`

The type `B256` in Fuel represents hashes and holds a 256-bit (32-bytes) value. The TypeScript SDK represents `B256` as a hexlified string value for portability and provides utilities to convert to `Uint8Array` when the [raw bytes](./bytes32.md) are required.

## Generating random `B256` values

To generate a random `B256` value, you can use the `getRandomB256()` function:

<<< @./snippets/b256/generating-random-b256.ts#full{ts:line-numbers}

### Converting between `B256` and `Uint8Array`

To convert between a `B256` hexlified string and a `Uint8Array`, you can use the `arrayify` and `hexlify` functions:

<<< @./snippets/b256/converting-between-b256-uint8.ts#full{ts:line-numbers}

## Support from `Address` Class

A `B256` value is also supported as part of the [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) class, providing seamless integration with other components of your application. To create an [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) instance from a b256 value, use the `Address.fromB256()` method:

<<< @./snippets/b256/support-from-address-class.ts#full{ts:line-numbers}
25 changes: 25 additions & 0 deletions apps/docs/src/guide/types/b512.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# B512

In Sway, the `B512` type is commonly used to handle public keys and signatures. This guide will explain how the `B512` type is defined in Sway, how to visualize a `B512` value using the SDK, and how to interact with a contract function that accepts a `B512` parameter.

The `B512` type in Sway is a wrapper around two `B256` types, allowing for the representation of 64-byte values. It is defined as a struct:

<<< @/../../docs/sway/bytecode-input/src/main.sw#b512-1{rs:line-numbers}

## `B512` in the SDK

In the SDK, you can visualize a `B512` value by examining a wallet's public key:

<<< @./snippets/b512/b512-in-the-sdk.ts#snippet-1{ts:line-numbers}

## Example: Echoing a `B512` Value in a Contract Function

Let's consider a contract function that accepts a `B512` parameter and returns the same value:

<<< @/../../docs/sway/echo-values/src/main.sw#b512-3{rust:line-numbers}

To call this function and validate the returned value, follow these steps:

<<< @./snippets/b512/echoing-a-b512.ts#snippet-1{ts:line-numbers}

In this example, we generate a wallet, use its public key as the `B512` input, call the `echo_b512` contract function, and expect the returned value to match the original input.
21 changes: 0 additions & 21 deletions apps/docs/src/guide/types/bits256.md

This file was deleted.

25 changes: 0 additions & 25 deletions apps/docs/src/guide/types/bits512.md

This file was deleted.

2 changes: 1 addition & 1 deletion apps/docs/src/guide/types/bytes32.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ You can use the `hexlify` function to convert a byte array to a hex string, and

## Working with b256 in Fuel

In Fuel, there is a special type called b256, which is similar to `bytes32`. Like `bytes32`, `b256` is also used to represent hashes and holds a 256-bit value. You can learn more about working with `b256` values in the [Bits256 documentation](./bits256.md).
In Fuel, there is a special type called b256, which is similar to `bytes32`. Like `bytes32`, `B256` is also used to represent hashes and holds a 256-bit value. You can learn more about working with `B256` values in the [B256 documentation](./b256.md).
4 changes: 2 additions & 2 deletions apps/docs/src/guide/types/evm-address.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# `EvmAddress`

An Ethereum Virtual Machine (EVM) Address can be represented using the `EvmAddress` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `Bits256` value.
An Ethereum Virtual Machine (EVM) Address can be represented using the `EvmAddress` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `B256` value.

<<< @./snippets/evm-address/intro.ts#snippet-1{ts:line-numbers}

## Creating an EVM Address

An EVM Address only has 20 bytes therefore the first 12 bytes of the `Bits256` value are set to 0. Within the SDK, an `Address` can be instantiated and converted to a wrapped and Sway compatible EVM Address using the `toEvmAddress()` function:
An EVM Address only has 20 bytes therefore the first 12 bytes of the `B256` value are set to 0. Within the SDK, an `Address` can be instantiated and converted to a wrapped and Sway compatible EVM Address using the `toEvmAddress()` function:

<<< @./snippets/evm-address/creating-an-evm.ts#snippet-1{ts:line-numbers}

Expand Down
4 changes: 2 additions & 2 deletions apps/docs/src/guide/types/snippets/asset-id/intro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import type { AssetId } from 'fuels';
import { getRandomB256 } from 'fuels';

const bits256 = getRandomB256();
const b256 = getRandomB256();

const assetId: AssetId = {
bits: bits256,
bits: b256,
};
// #endregion full
console.log(assetId, 'assetId');
4 changes: 2 additions & 2 deletions apps/docs/src/guide/types/snippets/evm-address/intro.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// #region snippet-1
import type { B256AddressEvm, EvmAddress } from 'fuels';

const bits256: B256AddressEvm =
const b256: B256AddressEvm =
'0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6';

const evmAddress: EvmAddress = {
bits: bits256,
bits: b256,
};
// #endregion snippet-1

Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/guide/types/structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Please note that TypeScript does not have native support for `u8` and `u64` type

Additionally, TypeScript does not support specifying string length, so just use `string` for the `name`.

In a similar way, since the type `b256` on the SDK is just an hexlified string, we use `string` as well.
In a similar way, since the type `B256` on the SDK is just an hexlified string, we use `string` as well.
6 changes: 3 additions & 3 deletions apps/docs/src/guide/utilities/address-conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Addresses and varying address formats are commonplace when interacting with decentralized applications. Furthermore, different networks may enforce different address formats.

The Fuel Network uses the [`Bits256`](../types/bits256.md) address format for its interactions, an example of which can be seen below:
The Fuel Network uses the [`B256`](../types/b256.md) address format for its interactions, an example of which can be seen below:

<<< @/../../docs/src/guide/types/snippets/b256.ts#addresses-1{ts:line-numbers}

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

<<< @/../../docs/src/guide/types/snippets/evm-address/creating-an-evm.ts#snippet-2{ts:line-numbers}
apps/
Expand Down Expand Up @@ -37,6 +37,6 @@ Similarly, the Wallet `address` property is also of type [`Address`](https://fue

## Converting an Asset ID

[Asset IDs](../types/asset-id.md) are a wrapped [b256](../types/bits256.md) value. The following example shows how to create an [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) from a `b256` type:
[Asset IDs](../types/asset-id.md) are a wrapped [`B256`](../types/b256.md) value. The following example shows how to create an [`Address`](https://fuels-ts-docs-api.vercel.app/classes/_fuel_ts_address.Address.html) from a `B256` type:

<<< @./snippets/address-conversion/asset-id.ts#conversion-4{ts:line-numbers}
2 changes: 1 addition & 1 deletion apps/docs/sway/bytecode-input/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abi MyContract {

// #region b512-1
pub struct B512 {
/// The two `b256`s that make up the `B512`.
/// The two `B256`s that make up the `B512`.
bits: [b256; 2],
}
// #endregion b512-1
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/sway/echo-values/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ impl EchoValues for Contract {
}
// #endregion tuples-2

// #region bits512-3
// #region b512-3
fn echo_b512(input: B512) -> B512 {
input
}
// #endregion bits512-3
// #endregion b512-3
fn echo_u64(value: u64) -> u64 {
value
}
Expand Down
Loading