From: Ardis Lu Date: Wed, 14 May 2025 04:58:48 +0000 (-0700) Subject: Allow up to 64 bit block number and timestamp X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=16e70de;p=block-hash-calculator Allow up to 64 bit block number and timestamp Currently no example cases where these values exceed 32 bits but might as well. Currently limited to 64 bits as that is the largest size setter available to DataView. --- diff --git a/calculate.js b/calculate.js index 9870112..a0b57f7 100644 --- a/calculate.js +++ b/calculate.js @@ -12,8 +12,8 @@ function arr(hex) { // Number to Uint8Array (big-endian) function arrBE(n) { - const v = new DataView(new ArrayBuffer(4)); - v.setUint32(0, Number(n), false); + const v = new DataView(new ArrayBuffer(8)); + v.setBigUint64(0, BigInt(n), false); return new Uint8Array(v.buffer); } @@ -141,8 +141,8 @@ function calculateZkSync(block) { // Since each value is equal to or less than 32 bytes, abi.encode just gives each value a 32-byte slot (big-endian) const data = new Uint8Array(32 * 4); - data.set(arrBE(block.number), 32 - 4); - data.set(arrBE(block.timestamp), 32 * 2 - 4); + data.set(arrBE(block.number), 32 - 8); + data.set(arrBE(block.timestamp), 32 * 2 - 8); data.set(arr(block.parentHash), 32 * 2); // block.parentHash data.set(blockTxsRollingHash(block.transactions), 32 * 3); // blockTxsRollingHash