@@ -8,13 +8,15 @@ import all from 'it-all'
88import drain from 'it-drain'
99import { CID } from 'multiformats/cid'
1010import * as raw from 'multiformats/codecs/raw'
11+ import { create } from 'multiformats/hashes/digest'
1112import { identity } from 'multiformats/hashes/identity'
13+ import { sha512 } from 'multiformats/hashes/sha2'
1214import Sinon from 'sinon'
1315import { stubInterface } from 'sinon-ts'
1416import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1517import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1618import { getHasher } from '../../src/utils/get-hasher.js'
17- import { NetworkedStorage } from '../../src/utils/networked-storage.js'
19+ import { NetworkedStorage , getCidBlockVerifierFunction } from '../../src/utils/networked-storage.js'
1820import { createBlock } from '../fixtures/create-block.js'
1921import type { NetworkedStorageComponents } from '../../src/utils/networked-storage.js'
2022import type { BlockBroker } from '@helia/interface/blocks'
@@ -212,4 +214,25 @@ describe('networked-storage', () => {
212214 expect ( block ) . to . equalBytes ( blocks [ 0 ] . block )
213215 expect ( slowBroker . retrieve . getCall ( 0 ) ) . to . have . nested . property ( 'args[1].signal.aborted' , true )
214216 } )
217+
218+ it ( 'truncates hash digest when longer than multihash size' , async ( ) => {
219+ const testData = uint8ArrayFromString ( 'hello world test data' )
220+
221+ // Create a full SHA-512 hash
222+ const fullHash = await sha512 . digest ( testData )
223+
224+ // Create a truncated digest (32 bytes instead of 64)
225+ const truncatedDigest = create ( sha512 . code , fullHash . digest . subarray ( 0 , 32 ) )
226+
227+ // Create a CID with the truncated hash
228+ const cid = CID . createV1 ( raw . code , truncatedDigest )
229+
230+ // Get the hasher and create the verifier function
231+ const hasher = await getHasher ( ) ( sha512 . code )
232+ const verifyFn = getCidBlockVerifierFunction ( cid , hasher )
233+
234+ // This should not throw - the verifier should truncate the full SHA-512 digest
235+ // to match the 32-byte truncated digest in the CID
236+ await expect ( verifyFn ( testData ) ) . to . not . be . rejected ( )
237+ } )
215238} )
0 commit comments