Bytes32
In Sway and the FuelVM, Bytes32
represents hashes. They hold a 256-bit (32-byte) value. Bytes32
is a wrapper on a 32-sized slice of u8
: pub struct Bytes32([u8; 32]);
.
These are the main ways of creating a Bytes32
:
use fuels::tx::Bytes32;
use std::str::FromStr;
// Zeroed Bytes32
let b256 = Bytes32::zeroed();
// Grab the inner `[u8; 32]` from
// `Bytes32` by dereferencing (i.e. `*`) it.
assert_eq!([0u8; 32], *b256);
// From a `[u8; 32]`.
let my_slice = [1u8; 32];
let b256 = Bytes32::new(my_slice);
assert_eq!([1u8; 32], *b256);
// From a hex string.
let hex_string = "0x0000000000000000000000000000000000000000000000000000000000000000";
let b256 = Bytes32::from_str(hex_string).expect("failed to create Bytes32 from string");
assert_eq!([0u8; 32], *b256);
However, there are more ways to achieve that and Bytes32
implements many more useful traits, see the fuel-types documentation.
Note: In Fuel, there's a special type called
b256
, which is similar toBytes32
; also used to represent hashes, and it holds a 256-bit value. In Rust, through the SDK, this is represented asBits256(value)
wherevalue
is a[u8; 32]
. If your contract method takes ab256
as input, all you need to do is pass aBits256([u8; 32])
when calling it from the SDK.