1- import { Block , TransactionResponse , TransactionReceipt , keccak256 , solidityPacked } from 'ethers' ;
1+ import { TransactionReceipt , keccak256 , solidityPacked } from 'ethers' ;
22
3- import { MerkleProofEntry , TransactionMerkleProof } from '..' ;
3+ import { abiEncode , EncodingVersion , TransactionWithRaw } from '../encoding ' ;
44
5- import { abiEncode , EncodingVersion , TransactionWithRaw } from '../../encoding' ;
5+ export class TransactionMerkleProof {
6+ public root : string ;
7+ public siblings : MerkleProofEntry [ ] ;
8+
9+ constructor ( root : string , siblings : MerkleProofEntry [ ] ) {
10+ this . root = root ;
11+ this . siblings = siblings ;
12+ }
13+ }
14+
15+ export class MerkleProofEntry {
16+ public hash : string ;
17+ public isLeft : boolean ;
18+
19+ constructor ( hash : string , isLeft : boolean ) {
20+ this . hash = hash ;
21+ this . isLeft = isLeft ;
22+ }
23+ }
624
725/**
826 * Hashes two child nodes to produce their parent node in the Merkle tree.
@@ -11,7 +29,7 @@ import { abiEncode, EncodingVersion, TransactionWithRaw } from '../../encoding';
1129 * @param right A 32-byte hex string representing the right child hash
1230 * @returns A 32-byte hex string representing the parent node hash
1331 */
14- function hashInner ( left : string , right : string ) : string {
32+ export function hashInner ( left : string , right : string ) : string {
1533 // Use solidityPacked to exactly match Solidity's abi.encodePacked(uint8(0x01), bytes32, bytes32)
1634 return keccak256 ( solidityPacked ( [ 'uint8' , 'bytes32' , 'bytes32' ] , [ 0x01 , left , right ] ) ) ;
1735}
@@ -22,7 +40,7 @@ function hashInner(left: string, right: string): string {
2240 * @param leaf A 32-byte hex string representing the data to be hashed as a leaf
2341 * @returns A 32-byte hex string representing the leaf node hash
2442 */
25- function hashLeaf ( leaf : string ) : string {
43+ export function hashLeaf ( leaf : string ) : string {
2644 // Use solidityPacked to exactly match Solidity's abi.encodePacked(uint8(0x00), bytes)
2745 return keccak256 ( solidityPacked ( [ 'uint8' , 'bytes' ] , [ 0x00 , leaf ] ) ) ;
2846}
@@ -87,12 +105,13 @@ export function computeMerkleRootOfBlock(
87105 * @returns The computed digest as a hex string
88106 */
89107export function computeDigestOf ( blockNumber : number , merkleRoot : string , prevDigest : string | null ) : string {
90- const prevDigestNonNull = prevDigest || ZERO_HASH ;
91- const digest = keccak256 (
92- solidityPacked ( [ 'uint64' , 'bytes32' , 'bytes32' ] , [ blockNumber , merkleRoot , prevDigestNonNull ] ) ,
93- ) ;
108+ // If prevDigest is provided, include it in the packed encoding
109+ if ( prevDigest ) {
110+ return keccak256 ( solidityPacked ( [ 'uint64' , 'bytes32' , 'bytes32' ] , [ blockNumber , merkleRoot , prevDigest ] ) ) ;
111+ }
94112
95- return digest ;
113+ // Otherwise, only use blockNumber and merkleRoot
114+ return keccak256 ( solidityPacked ( [ 'uint64' , 'bytes32' ] , [ blockNumber , merkleRoot ] ) ) ;
96115}
97116
98117export class KeccakMerkleTree {
0 commit comments