Skip to content
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

feat(hasher): add blake3 #395

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"@libp2p/ipni-content-routing": "^1.0.1",
"@loadable/component": "^5.14.1",
"@tableflip/react-inspector": "^2.3.0",
"blake3-multihash": "^0.0.4",
"cytoscape": "^3.18.1",
"cytoscape-dagre": "^2.3.2",
"filesize": "^6.1.0",
Expand Down
21 changes: 18 additions & 3 deletions src/lib/hash-importer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* global globalThis */
import { digest as blake3Digest } from 'blake3-multihash'
import { keccak256 } from 'js-sha3'
import { type Hasher, from } from 'multiformats/hashes/hasher'
import * as sha2 from 'multiformats/hashes/sha2'

type SupportedHashers = typeof sha2.sha256 | typeof sha2.sha512 | Hasher<'keccak-256', 27> | Hasher<'sha1', 17>
type SupportedHashers = typeof sha2.sha256
| typeof sha2.sha512
| Hasher<'blake3-multihash', 30>
| Hasher<'keccak-256', 27>
| Hasher<'sha1', 17>

export default async function getHasherForCode (code: number): Promise<SupportedHashers> {
switch (code) {
case sha2.sha256.code:
Expand All @@ -15,7 +21,7 @@ export default async function getHasherForCode (code: number): Promise<Supported
return from({
name: 'sha1',
code,
encode: async (data: Uint8Array) => {
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const crypto = globalThis.crypto ?? (await import('crypto')).webcrypto
const hashBuffer = await crypto.subtle.digest('SHA-1', data)
return new Uint8Array(hashBuffer)
Expand All @@ -25,10 +31,19 @@ export default async function getHasherForCode (code: number): Promise<Supported
return from({
name: 'keccak-256',
code,
encode: async (data: Uint8Array) => {
encode: async (data: Uint8Array): Promise<Uint8Array> => {
return new Uint8Array(keccak256.arrayBuffer(data))
}
})
case 30: // blake3-multihash
return from({
name: 'blake3-multihash',
code,
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const { digest } = await blake3Digest(data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could pull the import down into this so we only import this library dynamically when we need it, and add as a peer/optional/optionalPeer. That may solve the ipfs-webui issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and Done, however I didn't test the wasm issue.

return digest
}
})

default:
throw new Error(`unknown multihasher code '${code}'`)
Expand Down
Loading