Skip to content

Commit 68e97de

Browse files
committed
fix: support truncated hashes when verifying
1 parent 1c282b4 commit 68e97de

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/utils/src/utils/networked-storage.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,13 @@ export const getCidBlockVerifierFunction = (cid: CID, hasher: MultihashHasher):
411411
hash = res
412412
}
413413

414+
if (hash.digest.length > cid.multihash.size) {
415+
hash = {
416+
...hash,
417+
digest: hash.digest.subarray(0, cid.multihash.size)
418+
}
419+
}
420+
414421
if (!uint8ArrayEquals(hash.digest, cid.multihash.digest)) {
415422
// if a hash mismatch occurs for a TrustlessGatewayBlockBroker, we should try another gateway
416423
throw new InvalidMultihashError('Hash of downloaded block did not match multihash from passed CID')

packages/utils/test/utils/networked-storage.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import all from 'it-all'
88
import drain from 'it-drain'
99
import { CID } from 'multiformats/cid'
1010
import * as raw from 'multiformats/codecs/raw'
11+
import { create } from 'multiformats/hashes/digest'
1112
import { identity } from 'multiformats/hashes/identity'
13+
import { sha512 } from 'multiformats/hashes/sha2'
1214
import Sinon from 'sinon'
1315
import { stubInterface } from 'sinon-ts'
1416
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1517
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1618
import { 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'
1820
import { createBlock } from '../fixtures/create-block.js'
1921
import type { NetworkedStorageComponents } from '../../src/utils/networked-storage.js'
2022
import 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

Comments
 (0)