shove-js
Version:
SDK for shove.bet
42 lines (37 loc) • 1.56 kB
text/typescript
import { toUtf8Bytes } from "ethers";
import { keccak256 } from "ethers";
/**
* Converts a bigint value to a hexadecimal string representation with leading zeros.
* @param value The bigint value to convert.
* @param length The desired length of the hexadecimal string (default is 64 characters).
* @returns A hexadecimal string representation of the bigint value.
*/
export function bigintToHex(value: bigint, length: number = 40): string {
// Convert bigint to hex and remove the '0x' prefix
const hexString = value.toString(16);
// Pad the hex string with leading zeros to the specified length
return "0x" + hexString.padStart(length, "0");
}
/**
* Pads a hex string with leading zeros to reach the target length (in bytes)
* @param hexString - The hex string to pad (with or without 0x prefix)
* @param byteLength - The target length in bytes (default: 32 for 256-bit values)
* @returns The padded hex string with 0x prefix
*/
export function padHexString(
hexString: string,
byteLength: number = 32,
): string {
const hex = hexString.startsWith("0x") ? hexString.slice(2) : hexString;
const targetLength = byteLength * 2;
const padded = hex.padStart(targetLength, "0");
return "0x" + padded;
}
/**
* Converts an event signature to its keccak256 hash (topic0)
* @param signature - The event signature (e.g., "Deposit(address,address,uint256,address)")
* @returns The keccak256 hash of the signature with 0x prefix
*/
export function getEventTopic(signature: string): string {
return keccak256(toUtf8Bytes(signature));
}