]> git.ardis.lu - block-hash-calculator/commitdiff
Allow up to 64 bit block number and timestamp
authorArdis Lu <ardisglu@gmail.com>
Wed, 14 May 2025 04:58:48 +0000 (21:58 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 14 May 2025 04:58:48 +0000 (21:58 -0700)
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.

calculate.js

index 9870112e3063b4801f3712bcf4d8b263edf3bab4..a0b57f7467851404f5e011c269c5749bf9f0bdb4 100644 (file)
@@ -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
 
This page took 0.046206 seconds and 4 git commands to generate.