Skip to content

Bits512

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:

rust
pub struct B512 {
  bytes: [b256; 2],
}
See code in context

b512 in the SDK

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

ts
import { Wallet } from 'fuels';

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

console.log(walllet.publicKey);

// 0x97e3a666e4cd34b6b3cf778ef5ec617de4439b68f7a629245442a1fece7713094a1cb0aa7ad0ac253ca1ea47d4618f9090b2a881e829e091fb2c426763e94cca
See code in context

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:

rust
fn echo_b512(input: B512) -> B512 {
    input
}
See code in context

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

ts
const b512 = wallet.publicKey;

const { value } = await contract.functions.echo_b512(b512).simulate();

expect(value).toEqual(b512);
See code in context

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.