Skip to main content

Getting Block Witnesses

The getBlockWitness method allows you to retrieve witness data for a specified block height. This method is useful for validating block integrity and examining the cryptographic signatures associated with a block.


Method

getBlockWitness(
height?: BigNumberish,
trusted?: boolean,
limit?: number,
page?: number
): Promise<BlockWitnesses>;
  • Parameters:

    • height?: BigNumberish: (Optional) The height of the block to fetch witnesses for. Defaults to the latest block if not provided.
    • trusted?: boolean: (Optional) Whether to include only trusted witnesses. Defaults to false.
    • limit?: number: (Optional) The maximum number of witnesses to fetch per page.
    • page?: number: (Optional) The page number for paginated witness data.
  • Returns:

    • Promise<BlockWitnesses>: An array of IBlockWitness objects containing witness details for the specified block height(s).

Object Definitions

IBlockWitness Object
PropertyTypeDescription
blockNumberbigint | stringThe block number associated with the witnesses.
witnessesIBlockWitnessAPI[]An array of witnesses for the specified block.
IBlockWitnessAPI Object
PropertyTypeDescription
trustedbooleanWhether the witness is trusted.
signaturestringThe cryptographic signature of the witness.
identitystring(Optional) The identity of the witness.
opnetPubKeystring(Optional) The OP_NET public key of the witness.

Example Usage

Fetching Witnesses for a Block

const blockHeight = BigInt(100); // Replace with desired block height
const witnesses = await provider.getBlockWitness(blockHeight);

witnesses.forEach((blockWitness) => {
console.log("Block Number:", blockWitness.blockNumber);

blockWitness.witnesses.forEach((witness) => {
console.log("Trusted:", witness.trusted);
console.log("Signature:", witness.signature);
console.log("Identity:", witness.identity ?? "N/A");
console.log("OP_NET Public Key:", witness.opnetPubKey ?? "N/A");
});
});

Paginated Witness Fetching

const paginatedWitnesses = await provider.getBlockWitness(
blockHeight,
true,
10,
1
);
console.log("Paginated Witnesses:", paginatedWitnesses);

Best Practices

  • Use the trusted parameter to filter witnesses based on their trust status.
  • For blocks with many witnesses, use the limit and page parameters to manage the response size.
  • If the height parameter is omitted, the latest block's witnesses will be fetched.

What’s Next?

After retrieving block witnesses, you can: