@web3-storage/car-block-validator
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -33,3 +33,3 @@ 'use strict'; | ||
])); | ||
async function validateBlock(block) { | ||
function validateBlock(block) { | ||
const hasher = hashMap.get(block.cid.multihash.code); | ||
@@ -39,6 +39,12 @@ if (!hasher) { | ||
} | ||
const bytesHash = await hasher.digest(block.bytes); | ||
if (!uint8arrays.equals(bytesHash.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes'); | ||
const result = hasher.digest(block.bytes); | ||
const compareDigests = h => { | ||
if (!uint8arrays.equals(h.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes'); | ||
} | ||
}; | ||
if (result instanceof Promise) { | ||
return result.then(compareDigests); | ||
} | ||
compareDigests(result); | ||
} | ||
@@ -45,0 +51,0 @@ |
@@ -45,3 +45,3 @@ import { | ||
])); | ||
export async function validateBlock(block) { | ||
export function validateBlock(block) { | ||
const hasher = hashMap.get(block.cid.multihash.code); | ||
@@ -51,6 +51,12 @@ if (!hasher) { | ||
} | ||
const bytesHash = await hasher.digest(block.bytes); | ||
if (!equals(bytesHash.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes'); | ||
const result = hasher.digest(block.bytes); | ||
const compareDigests = h => { | ||
if (!equals(h.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes'); | ||
} | ||
}; | ||
if (result instanceof Promise) { | ||
return result.then(compareDigests); | ||
} | ||
compareDigests(result); | ||
} |
{ | ||
"name": "@web3-storage/car-block-validator", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Validate car block bytes", | ||
@@ -5,0 +5,0 @@ "main": "./cjs/src/index.js", |
@@ -26,2 +26,3 @@ import { sha256, sha512 } from 'multiformats/hashes/sha2' | ||
/** @type {Map<number, import('multiformats/hashes/interface').MultihashHasher>} */ | ||
export const hashMap = new Map( | ||
@@ -39,3 +40,3 @@ [ | ||
*/ | ||
export async function validateBlock (block) { | ||
export function validateBlock (block) { | ||
const hasher = hashMap.get(block.cid.multihash.code) | ||
@@ -46,7 +47,16 @@ if (!hasher) { | ||
const bytesHash = await hasher.digest(block.bytes) | ||
const result = hasher.digest(block.bytes) | ||
if (!equals(bytesHash.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes') | ||
/** @param {import('multiformats/hashes/interface').MultihashDigest} h */ | ||
const compareDigests = h => { | ||
if (!equals(h.digest, block.cid.multihash.digest)) { | ||
throw new Error('CID hash does not match bytes') | ||
} | ||
} | ||
if (result instanceof Promise) { | ||
return result.then(compareDigests) | ||
} | ||
compareDigests(result) | ||
} |
@@ -40,5 +40,5 @@ import test from 'ava' | ||
for await (const block of reader) { | ||
t.throwsAsync(() => validateBlock(block)) | ||
t.throwsAsync(async () => validateBlock(block)) | ||
} | ||
}) | ||
} |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
export function validateBlock(block: Block): Promise<void>; | ||
export function validateBlock(block: Block): Promise<void> | undefined; | ||
/** | ||
@@ -12,3 +12,4 @@ * @typedef {object} Block | ||
*/ | ||
export const hashMap: Map<any, any>; | ||
/** @type {Map<number, import('multiformats/hashes/interface').MultihashHasher>} */ | ||
export const hashMap: Map<number, import('multiformats/hashes/interface').MultihashHasher>; | ||
export type Block = { | ||
@@ -15,0 +16,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24044
247
0