Skip to content

Instantly share code, notes, and snippets.

@ardislu
Created April 21, 2024 05:43
Show Gist options
  • Save ardislu/c84a312aaa77b981d6779d109e51118d to your computer and use it in GitHub Desktop.
Save ardislu/c84a312aaa77b981d6779d109e51118d to your computer and use it in GitHub Desktop.
Convert a hex string of 8 bytes into a BigInt.
// Converts a hex string of 8 bytes into a BigInt.
// Useful for converting Ethereum's Beacon Deposit Contract get_deposit_count().
// https://etherscan.io/address/0x00000000219ab540356cBB839Cbe05303d7705Fa
function littleEndian64ToBigInt(bytes) {
bytes = bytes.replace('0x', '').replaceAll(/\s/g, '');
return new DataView(Uint8Array.from(bytes.matchAll(/.{2}/g), b => parseInt(b, 16)).buffer).getBigUint64(0, true);
}
// Example:
// littleEndian64ToBigInt('0x0519160000000000')
// 1448197n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment