multiformats / multiformats/js-multiformats

Proposal: Update MultihashHasher interface so it could support multiple hashing algorithms

Open
#252 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
268
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Goal

As per https://github.com/ipld/js-car/issues/123 we do need a solution for verifying hashes in CAR files without shipping all the MultihashHasher-s with the CAR library.

Proposal

  1. Introduce backwards compatible change to the MultihashHasher by introducing second OPTIONAL code argument to the digest method.
    • If omitted it would default to own code.
    • If different code is passed it would error.
  2. Rename MultihashHasher interface to MultihashHasherCase and repurpose it for single algorithm use cases.
  3. Introduce multi algorithm MultihashHasherVariant interface with the same digest method and a method that returns map of MultihashHasherCase it's comprised of.
  4. Define MultihashHasher as union type discriminated by code field.

Here is the the sketch:

interface MultihashHasherCase<Code extends number = number> {
    code: Code
    digest(bytes: Uint8Array, code?: Code): Digest<Code>
}

interface MultihashHasherVariant<Code extends number = number> {
    // We define `code` as optional which can never have a value. This will allow
    // us to `code` as a discriminant in the union
    code?: never

    cases(): Record<Code, MultihashHasherCase<Code>>

    digest <CodeCase extends Code> (bytes: Uint8Array, code?: CodeCase): Digest<CodeCase>
    
    or <Case extends number> (hasher: MultihashHasher<Case>): MultihashHasherVariant<Code|Case>
}

type MultihashHasher<Code extends number = number> =
    | MultihashHasherCase<Code>
    | MultihashHasherVariant<Code>

Note that above makes current MultihashHasher-s compatible with proposed MultihashHasher type that is to say code that will require new type will accept all existing hashers without changes to them.

  • With such a type in place js-car library would be able to require MultihashHasher parameter in order to be able to verify digests.
  • Added MultihashHasherVariant would allow composing many hashers into one.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the MultihashHasher interface and its usages in the TypeScript source, then review the linked js-car issue for the verification requirement. Compare existing tests and type checks before defining the compatibility and composition behavior described here. Done means the proposed interfaces and union type are implemented without breaking existing hashers, with coverage for single- and multi-algorithm use cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.