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 tofalse
.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 ofIBlockWitness
objects containing witness details for the specified block height(s).
Object Definitions
IBlockWitness
ObjectProperty | Type | Description |
---|---|---|
blockNumber | bigint | string | The block number associated with the witnesses. |
witnesses | IBlockWitnessAPI[] | An array of witnesses for the specified block. |
IBlockWitnessAPI
ObjectProperty | Type | Description |
---|---|---|
trusted | boolean | Whether the witness is trusted. |
signature | string | The cryptographic signature of the witness. |
identity | string | (Optional) The identity of the witness. |
opnetPubKey | string | (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
andpage
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: