-
Notifications
You must be signed in to change notification settings - Fork 55
feat: support truncating digests #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
64a3d68
feat: support truncated hashes
achingbrain b55d072
chore: simplify
achingbrain d42373b
chore: add comment
achingbrain 161d781
chore: make max hash optional
achingbrain 2a32fe7
chore: remove redundant option
achingbrain 01ae0bb
chore: add jsdoc
achingbrain cba078d
fix: allow truncating identity hashes
achingbrain 71db8d2
chore: update interfaces
achingbrain f250f45
chore: linting again
achingbrain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,43 @@ import type { MultihashHasher } from './interface.js' | |
|
||
type Await<T> = Promise<T> | T | ||
|
||
export function from <Name extends string, Code extends number> ({ name, code, encode }: { name: Name, code: Code, encode(input: Uint8Array): Await<Uint8Array> }): Hasher<Name, Code> { | ||
return new Hasher(name, code, encode) | ||
const DEFAULT_MIN_DIGEST_LENGTH = 20 | ||
|
||
export interface HasherInit <Name extends string, Code extends number> { | ||
name: Name | ||
code: Code | ||
encode(input: Uint8Array): Await<Uint8Array> | ||
|
||
/** | ||
* The minimum length a hash is allowed to be truncated to in bytes | ||
* | ||
* @default 20 | ||
*/ | ||
minDigestLength?: number | ||
|
||
/** | ||
* The maximum length a hash is allowed to be truncated to in bytes. If not | ||
* specified it will be inferred from the length of the digest. | ||
*/ | ||
maxDigestLength?: number | ||
} | ||
|
||
export function from <Name extends string, Code extends number> ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit<Name, Code>): Hasher<Name, Code> { | ||
return new Hasher(name, code, encode, minDigestLength, maxDigestLength) | ||
} | ||
|
||
export interface DigestOptions { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add the unit type in the JS doc so it's clear There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a doc comment, let me know if you think it's clear. |
||
/** | ||
* Truncate the returned digest to this number of bytes. | ||
* | ||
* This may cause the digest method to throw/reject if the passed value is | ||
* greater than the digest length or below a threshold under which the risk of | ||
* hash collisions is significant. | ||
* | ||
* The actual value of this threshold can depend on the hashing algorithm in | ||
* use. | ||
*/ | ||
truncate?: number | ||
} | ||
|
||
/** | ||
|
@@ -15,23 +50,55 @@ export class Hasher<Name extends string, Code extends number> implements Multiha | |
readonly name: Name | ||
readonly code: Code | ||
readonly encode: (input: Uint8Array) => Await<Uint8Array> | ||
readonly minDigestLength: number | ||
readonly maxDigestLength?: number | ||
|
||
constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>) { | ||
constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>, minDigestLength?: number, maxDigestLength?: number) { | ||
this.name = name | ||
this.code = code | ||
this.encode = encode | ||
this.minDigestLength = minDigestLength ?? DEFAULT_MIN_DIGEST_LENGTH | ||
this.maxDigestLength = maxDigestLength | ||
} | ||
|
||
digest (input: Uint8Array): Await<Digest.Digest<Code, number>> { | ||
digest (input: Uint8Array, options?: DigestOptions): Await<Digest.Digest<Code, number>> { | ||
if (options?.truncate != null) { | ||
if (options.truncate < this.minDigestLength) { | ||
throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`) | ||
} | ||
|
||
if (this.maxDigestLength != null && options.truncate > this.maxDigestLength) { | ||
throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`) | ||
} | ||
} | ||
|
||
if (input instanceof Uint8Array) { | ||
const result = this.encode(input) | ||
return result instanceof Uint8Array | ||
? Digest.create(this.code, result) | ||
/* c8 ignore next 1 */ | ||
: result.then(digest => Digest.create(this.code, digest)) | ||
|
||
if (result instanceof Uint8Array) { | ||
return createDigest(result, this.code, options?.truncate) | ||
} | ||
|
||
return result.then(digest => createDigest(digest, this.code, options?.truncate)) | ||
} else { | ||
throw Error('Unknown type, must be binary type') | ||
/* c8 ignore next 1 */ | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create a Digest from the passed uint8array and code, optionally truncating it | ||
* first. | ||
*/ | ||
function createDigest <Code extends number> (digest: Uint8Array, code: Code, truncate?: number): Digest.Digest<Code, number> { | ||
if (truncate != null && truncate !== digest.byteLength) { | ||
if (truncate > digest.byteLength) { | ||
throw new Error(`Invalid truncate option, must be less than or equal to ${digest.byteLength}`) | ||
} | ||
|
||
digest = digest.subarray(0, truncate) | ||
} | ||
|
||
return Digest.create(code, digest) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without any other customization will this break small identity multihashes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't do, unless you
identity.digest(fromString('test'), { truncate: 3 })
, this limit doesn't get touched unless you ask to use the newtruncate
feature so this change should be entirely backward compatible and non-breakingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out the types wouldn't allow passing options to
identity.digest
as it doesn't implement the same interface as things returned fromfrom
(see comment) - I've fixed this up here, still non-breaking.