@@ -2,7 +2,7 @@ import { describe, expect, it } from "@jest/globals";
22import { validateSerializedTree , toBuffer , createLeafNode , validateProof } from "../utils" ;
33import { defaultHash } from "../index" ;
44import { Buffer } from "buffer" ;
5- import { MerkleNode , MerkleProof , SerializedTree } from "../types" ;
5+ import { MerkleNode , MerkleProof , SerializedTree , ProofItem } from "../types" ;
66
77describe ( "Utility Functions" , ( ) => {
88 describe ( "validateSerializedTree" , ( ) => {
@@ -19,39 +19,39 @@ describe("Utility Functions", () => {
1919 leaves : "not an array" ,
2020 tree : [ [ "a1b2c3" ] ]
2121 } ;
22- expect ( ( ) => validateSerializedTree ( invalidData as unknown as SerializedTree ) ) . toThrow ( "Invalid tree structure " ) ;
22+ expect ( ( ) => validateSerializedTree ( invalidData as unknown as SerializedTree ) ) . toThrow ( "Invalid tree data: leaves must be an array " ) ;
2323 } ) ;
2424
2525 it ( "should throw on non-array tree" , ( ) => {
2626 const invalidData = {
2727 leaves : [ "a1b2c3" ] ,
2828 tree : "not an array"
2929 } ;
30- expect ( ( ) => validateSerializedTree ( invalidData as unknown as SerializedTree ) ) . toThrow ( "Invalid tree structure " ) ;
30+ expect ( ( ) => validateSerializedTree ( invalidData as unknown as SerializedTree ) ) . toThrow ( "Invalid tree data: tree must be an array " ) ;
3131 } ) ;
3232
3333 it ( "should throw on invalid hex in leaves" , ( ) => {
3434 const invalidData : SerializedTree = {
3535 leaves : [ "a1b2c3" , "not hex" ] ,
3636 tree : [ [ "a1b2c3" , "d4e5f6" ] , [ "abcdef" ] ]
3737 } ;
38- expect ( ( ) => validateSerializedTree ( invalidData ) ) . toThrow ( "Invalid hex string in leaves array " ) ;
38+ expect ( ( ) => validateSerializedTree ( invalidData ) ) . toThrow ( "Invalid tree data: leaves must be hex strings " ) ;
3939 } ) ;
4040
4141 it ( "should throw on invalid hex in tree" , ( ) => {
4242 const invalidData : SerializedTree = {
4343 leaves : [ "a1b2c3" , "d4e5f6" ] ,
4444 tree : [ [ "a1b2c3" , "d4e5f6" ] , [ "not hex" ] ]
4545 } ;
46- expect ( ( ) => validateSerializedTree ( invalidData ) ) . toThrow ( "Invalid hex string in tree array " ) ;
46+ expect ( ( ) => validateSerializedTree ( invalidData ) ) . toThrow ( "Invalid tree data: tree hashes must be hex strings " ) ;
4747 } ) ;
4848
4949 it ( "should throw on non-array tree level" , ( ) => {
5050 const invalidData = {
5151 leaves : [ "a1b2c3" , "d4e5f6" ] ,
5252 tree : [ [ "a1b2c3" , "d4e5f6" ] , "not an array" ]
5353 } ;
54- expect ( ( ) => validateSerializedTree ( invalidData as unknown as SerializedTree ) ) . toThrow ( "Invalid hex string in tree array " ) ;
54+ expect ( ( ) => validateSerializedTree ( invalidData as unknown as SerializedTree ) ) . toThrow ( "Invalid tree data: tree levels must be arrays " ) ;
5555 } ) ;
5656
5757 it ( "should handle empty arrays" , ( ) => {
@@ -67,7 +67,7 @@ describe("Utility Functions", () => {
6767 leaves : [ "a1b2c3" ] ,
6868 tree : [ [ "a1b2c3" ] , [ "abcdef" ] , "not an array" ]
6969 } ;
70- expect ( ( ) => validateSerializedTree ( malformedData as unknown as SerializedTree ) ) . toThrow ( "Invalid hex string in tree array " ) ;
70+ expect ( ( ) => validateSerializedTree ( malformedData as unknown as SerializedTree ) ) . toThrow ( "Invalid tree data: tree levels must be arrays " ) ;
7171 } ) ;
7272 } ) ;
7373
@@ -106,52 +106,57 @@ describe("Utility Functions", () => {
106106
107107 describe ( "validateProof" , ( ) => {
108108 it ( "should validate correct proof" , ( ) => {
109- const leafHash = defaultHash ( Buffer . from ( "leaf" ) ) ;
110- const siblingHash = defaultHash ( Buffer . from ( "sibling" ) ) ;
111- const rootHash = defaultHash ( Buffer . concat ( [ leafHash , siblingHash ] ) ) ;
112-
113- const proof : MerkleProof [ ] = [ {
114- position : "right" ,
115- hash : siblingHash
116- } ] ;
117-
118- expect ( validateProof ( proof , leafHash , rootHash , defaultHash ) ) . toBe ( true ) ;
119- } ) ;
120-
121- it ( "should reject incorrect proof" , ( ) => {
122- const leafHash = defaultHash ( Buffer . from ( "leaf" ) ) ;
123- const wrongSiblingHash = defaultHash ( Buffer . from ( "wrong" ) ) ;
124- const rootHash = defaultHash ( Buffer . from ( "correct root" ) ) ;
125-
126- const proof : MerkleProof [ ] = [ {
127- position : "right" ,
128- hash : wrongSiblingHash
129- } ] ;
130-
131- expect ( validateProof ( proof , leafHash , rootHash , defaultHash ) ) . toBe ( false ) ;
132- } ) ;
133-
134- it ( "should handle multiple proof steps" , ( ) => {
135- const leafHash = defaultHash ( Buffer . from ( "leaf" ) ) ;
136- const sibling1Hash = defaultHash ( Buffer . from ( "sibling1" ) ) ;
137- const sibling2Hash = defaultHash ( Buffer . from ( "sibling2" ) ) ;
138-
139- // Create a two-level proof
140- const intermediateHash = defaultHash ( Buffer . concat ( [ leafHash , sibling1Hash ] ) ) ;
141- const rootHash = defaultHash ( Buffer . concat ( [ intermediateHash , sibling2Hash ] ) ) ;
142-
143- const proof : MerkleProof [ ] = [
109+ const proof : MerkleProof = [
144110 {
145- position : "right" ,
146- hash : sibling1Hash
147- } ,
111+ sibling : Buffer . from ( "test" ) ,
112+ position : "right"
113+ }
114+ ] ;
115+ expect ( ( ) => validateProof ( proof ) ) . not . toThrow ( ) ;
116+ } ) ;
117+
118+ it ( "should throw on invalid proof structure" , ( ) => {
119+ const invalidProof = [
148120 {
149121 position : "right" ,
150- hash : sibling2Hash
122+ hash : Buffer . from ( "test" )
123+ }
124+ ] as unknown as MerkleProof ;
125+ expect ( ( ) => validateProof ( invalidProof ) ) . toThrow ( ) ;
126+ } ) ;
127+
128+ it ( "should throw on invalid position" , ( ) => {
129+ const invalidProof : MerkleProof = [
130+ {
131+ sibling : Buffer . from ( "test" ) ,
132+ position : "invalid" as "left" | "right"
151133 }
152134 ] ;
135+ expect ( ( ) => validateProof ( invalidProof ) ) . toThrow ( ) ;
136+ } ) ;
153137
154- expect ( validateProof ( proof , leafHash , rootHash , defaultHash ) ) . toBe ( true ) ;
138+ it ( "should throw on invalid sibling" , ( ) => {
139+ const invalidProof : MerkleProof = [
140+ {
141+ sibling : "not a buffer" as unknown as Buffer ,
142+ position : "right"
143+ }
144+ ] ;
145+ expect ( ( ) => validateProof ( invalidProof ) ) . toThrow ( ) ;
146+ } ) ;
147+
148+ it ( "should validate multiple proof items" , ( ) => {
149+ const proof : MerkleProof = [
150+ {
151+ sibling : Buffer . from ( "test1" ) ,
152+ position : "right"
153+ } ,
154+ {
155+ sibling : Buffer . from ( "test2" ) ,
156+ position : "left"
157+ }
158+ ] ;
159+ expect ( ( ) => validateProof ( proof ) ) . not . toThrow ( ) ;
155160 } ) ;
156161 } ) ;
157162} ) ;
0 commit comments