Created
April 21, 2024 05:43
-
-
Save ardislu/c84a312aaa77b981d6779d109e51118d to your computer and use it in GitHub Desktop.
Convert a hex string of 8 bytes into a BigInt.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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