@web3-storage/car-block-validator
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -33,6 +33,16 @@ 'use strict'; | ||
])); | ||
class UnsupportedHashError extends Error { | ||
constructor(code) { | ||
super(`multihash code ${ code } is not supported`); | ||
} | ||
} | ||
class HashMismatchError extends Error { | ||
constructor() { | ||
super('CID hash does not match bytes'); | ||
} | ||
} | ||
function validateBlock(block) { | ||
const hasher = hashMap.get(block.cid.multihash.code); | ||
if (!hasher) { | ||
throw new Error(`multihash code ${ block.cid.multihash.code } is not supported`); | ||
throw new UnsupportedHashError(block.cid.multihash.code); | ||
} | ||
@@ -42,3 +52,3 @@ const result = hasher.digest(block.bytes); | ||
if (!uint8arrays.equals(h.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes'); | ||
throw new HashMismatchError(); | ||
} | ||
@@ -52,3 +62,5 @@ }; | ||
exports.HashMismatchError = HashMismatchError; | ||
exports.UnsupportedHashError = UnsupportedHashError; | ||
exports.hashMap = hashMap; | ||
exports.validateBlock = validateBlock; |
@@ -45,6 +45,16 @@ import { | ||
])); | ||
export class UnsupportedHashError extends Error { | ||
constructor(code) { | ||
super(`multihash code ${ code } is not supported`); | ||
} | ||
} | ||
export class HashMismatchError extends Error { | ||
constructor() { | ||
super('CID hash does not match bytes'); | ||
} | ||
} | ||
export function validateBlock(block) { | ||
const hasher = hashMap.get(block.cid.multihash.code); | ||
if (!hasher) { | ||
throw new Error(`multihash code ${ block.cid.multihash.code } is not supported`); | ||
throw new UnsupportedHashError(block.cid.multihash.code); | ||
} | ||
@@ -54,3 +64,3 @@ const result = hasher.digest(block.bytes); | ||
if (!equals(h.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes'); | ||
throw new HashMismatchError(); | ||
} | ||
@@ -57,0 +67,0 @@ }; |
{ | ||
"name": "@web3-storage/car-block-validator", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Validate car block bytes", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/src/index.js", |
@@ -35,2 +35,17 @@ import { sha256, sha512 } from 'multiformats/hashes/sha2' | ||
/** The multihash hasher is not supported by this library. */ | ||
export class UnsupportedHashError extends Error { | ||
/** @param {number} code */ | ||
constructor (code) { | ||
super(`multihash code ${code} is not supported`) | ||
} | ||
} | ||
/** The bytes did not hash to the same value as the passed multihash. */ | ||
export class HashMismatchError extends Error { | ||
constructor () { | ||
super('CID hash does not match bytes') | ||
} | ||
} | ||
/** | ||
@@ -43,3 +58,3 @@ * Validates IPLD block bytes. | ||
if (!hasher) { | ||
throw new Error(`multihash code ${block.cid.multihash.code} is not supported`) | ||
throw new UnsupportedHashError(block.cid.multihash.code) | ||
} | ||
@@ -52,3 +67,3 @@ | ||
if (!equals(h.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes') | ||
throw new HashMismatchError() | ||
} | ||
@@ -55,0 +70,0 @@ } |
@@ -5,4 +5,5 @@ import test from 'ava' | ||
import { CarWriter, CarBlockIterator } from '@ipld/car' | ||
import { blake2b512 } from '@multiformats/blake2/blake2b' | ||
import { validateBlock, hashMap } from '../src/index.js' | ||
import { validateBlock, hashMap, HashMismatchError, UnsupportedHashError } from '../src/index.js' | ||
@@ -41,5 +42,22 @@ const bytes = pb.encode({ Data: new Uint8Array([1, 2, 3]), Links: [] }) | ||
for await (const block of reader) { | ||
t.throwsAsync(async () => validateBlock(block)) | ||
const err = await t.throwsAsync(async () => validateBlock(block)) | ||
t.true(err instanceof HashMismatchError) | ||
} | ||
}) | ||
} | ||
test('throws when validating blocks with unsupported hashers', async (t) => { | ||
const hash = await blake2b512.digest(bytes) | ||
const cid = CID.create(1, pb.code, hash) | ||
const { writer, out } = CarWriter.create([cid]) | ||
writer.put({ cid, bytes }) | ||
writer.close() | ||
const reader = await CarBlockIterator.fromIterable(out) | ||
for await (const block of reader) { | ||
const err = await t.throwsAsync(async () => validateBlock(block)) | ||
t.true(err instanceof UnsupportedHashError) | ||
} | ||
}) |
@@ -13,2 +13,11 @@ /** | ||
export const hashMap: Map<number, import('multiformats/hashes/interface').MultihashHasher>; | ||
/** The multihash hasher is not supported by this library. */ | ||
export class UnsupportedHashError extends Error { | ||
/** @param {number} code */ | ||
constructor(code: number); | ||
} | ||
/** The bytes did not hash to the same value as the passed multihash. */ | ||
export class HashMismatchError extends Error { | ||
constructor(); | ||
} | ||
export type Block = { | ||
@@ -15,0 +24,0 @@ cid: import('multiformats/cid').CID; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25977
305