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: add blake2b-256 hasher #436

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 5 additions & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {

// storybook deps
'@storybook/addon-actions',
'@storybook/addon-coverage',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-links',
Expand All @@ -36,7 +37,10 @@ module.exports = {
'@storybook/types',

// problem with deps
'@typescript-eslint/eslint-plugin'
'@typescript-eslint/eslint-plugin',

// scripts
'wait-on',
],
productionIgnorePatterns: [
'.aegir.js',
Expand Down
1 change: 1 addition & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config: StorybookConfig = {
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/addon-interactions',
'@storybook/addon-coverage'
],
framework: {
name: '@storybook/react-vite',
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ see https://github.com/ipfs/ipld-explorer-components/pull/360#discussion_r120625
To add another hasher, you will need to update all locations containing the comment `// #WhenAddingNewHasher`:

1. Add a dependency on the hasher to this package (if it's not already in multiformats or other package)
1. Add the hasher in the switch statement in [./src/lib/get-codec-for-cid.ts](./src/lib/get-codec-for-cid.ts)
Copy link
Member Author

Choose a reason for hiding this comment

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

this step is not needed when adding a hasher

1. Update [./src/lib/hash-importer.ts](./src/lib/hash-importer.ts)
- Update `SupportedHashers` to include your hasher type
- Update `getHasherForCode` to return your hasher
Expand Down
35,888 changes: 20,561 additions & 15,327 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@
]
},
"eslintConfig": {
"settings": { "react": {"version": "16.3"}},
"settings": {
"react": {
"version": "16.3"
}
},
"extends": [
"plugin:react/recommended",
"plugin:jsx-a11y/recommended",
Expand Down Expand Up @@ -147,14 +151,16 @@
"prepare": "npm run build",
"dev": "vite",
"start": "cross-env NODE_ENV=production babel src -d dist --copy-files --no-copy-ignored --extensions '.ts,.tsx,.js,.jsx' --watch",
"test": "run-s test:unit test:consumer",
"test": "run-s test:unit test:storybook:ci test:consumer",
"test:node": "run-s test:unit",
"test:unit": "vitest run --environment=node",
"storybook": "storybook dev -p 6006",
"storybook": "run-s storybook:build storybook:serve",
"storybook:build": "storybook build",
"storybook:serve": "http-server storybook-static --port 6006",
"test:consumer": "run-s test:consumer:webui test:consumer:ipld.io",
"test:consumer:webui": "aegir test-dependant https://github.com/ipfs/ipfs-webui",
"test:consumer:ipld.io": "aegir test-dependant https://github.com/ipld/explore.ipld.io"
"test:consumer:ipld.io": "aegir test-dependant https://github.com/ipld/explore.ipld.io",
"test:storybook:ci": "concurrently -k -s first -n \"SB,TEST\" -c \"magenta,blue\" \"storybook build --quiet && http-server storybook-static --port 6006 --silent\" \"wait-on 'tcp:127.0.0.1:6006' && test-storybook --coverage\""
},
"dependencies": {
"@helia/block-brokers": "^3.0.3",
Expand Down Expand Up @@ -189,6 +195,7 @@
"@babel/preset-typescript": "^7.23.3",
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
"@rollup/plugin-node-resolve": "^15.2.3",
"@storybook/addon-coverage": "^1.0.4",
"@storybook/addon-essentials": "^7.0.18",
"@storybook/addon-interactions": "^7.0.18",
"@storybook/addon-links": "^7.0.18",
Expand All @@ -202,6 +209,7 @@
"@storybook/react": "^7.0.18",
"@storybook/react-dom-shim": "^7.6.12",
"@storybook/react-vite": "^7.0.18",
"@storybook/test-runner": "^0.19.1",
"@storybook/types": "^7.6.12",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^12.1.5",
Expand All @@ -211,10 +219,12 @@
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.0.0",
"aegir": "^42.2.11",
"concurrently": "^8.2.2",
"cross-env": "^6.0.0",
"eslint-config-ipfs": "^6.0.2",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-react": "^7.34.1",
"http-server": "^14.1.1",
"i18next": "^23.8.2",
"i18next-browser-languagedetector": "7.2.0",
"i18next-http-backend": "2.4.3",
Expand All @@ -237,7 +247,8 @@
"typescript": "^5.0.4",
"vite": "^4.3.7",
"vite-plugin-svgr": "^3.2.0",
"vitest": "^0.31.3"
"vitest": "^0.31.3",
"wait-on": "^8.0.0"
},
"peerDependencies": {
"@loadable/component": "^5.14.1",
Expand Down
31 changes: 28 additions & 3 deletions src/lib/hash-importer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global globalThis */
import * as sha3 from '@multiformats/sha3'
import { createBLAKE3 } from 'hash-wasm'
import { createBLAKE3, createBLAKE2b } from 'hash-wasm'
import { type Hasher, from } from 'multiformats/hashes/hasher'
import * as sha2 from 'multiformats/hashes/sha2'

Expand All @@ -9,11 +9,12 @@ export type SupportedHashers = typeof sha2.sha256 |
typeof sha2.sha512 |
Hasher<'keccak-256', 27> |
Hasher<'sha1', 17> |
Hasher<'blake2b-512', 45632> |
Hasher<'blake2b-256', 0xb220> |
Hasher<'blake2b-512', 0xb240> |
Hasher<'sha3-512', 20> |
Hasher<'blake3', 30>

export async function getHashersForCodes (code: number, ...codes: number[]): Promise<SupportedHashers[]> {
export async function getHashersForCodes (...codes: number[]): Promise<SupportedHashers[]> {
return Promise.all(codes.map(getHasherForCode))
}

Expand Down Expand Up @@ -61,6 +62,30 @@ export async function getHasherForCode (code: number): Promise<SupportedHashers>
return blake3Hasher.digest('binary')
}
}))
case 0xb220: // blake2b-256
return getBoundHasher(from({
name: 'blake2b-256',
code,
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const blake2bHasher = await createBLAKE2b(256)
blake2bHasher.init()
blake2bHasher.update(data)
return blake2bHasher.digest('binary')
}
}))
case 0xb240: // blake2b-512
return getBoundHasher(from({
name: 'blake2b-512',
code,
encode: async (data: Uint8Array): Promise<Uint8Array> => {
const blake2bHasher = await createBLAKE2b(512)
blake2bHasher.init()
blake2bHasher.update(data)
return blake2bHasher.digest('binary')
}
})
)

default:
throw new Error(`unknown multihasher code '${code}'`)
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib/init-helia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default async function initHelia (kuboGatewayOptions: KuboGatewayOptions)

routers,
// #WhenAddingNewHasher
hashers: await getHashersForCodes(17, 18, 19, 20, 27, 30)
hashers: await getHashersForCodes(17, 18, 19, 20, 27, 30, 45600, 45632)
})

// add helia-only examples
Expand All @@ -45,7 +45,8 @@ export default async function initHelia (kuboGatewayOptions: KuboGatewayOptions)
addDagNodeToHelia(helia, 'dag-cbor', { hello: 'world' }, 27), // bafyrwigbexamue2ba3hmtai7hwlcmd6ekiqsduyf5avv7oz6ln3radvjde
addDagNodeToHelia(helia, 'json', { hello: 'world' }, 20), // bagaaifcavabu6fzheerrmtxbbwv7jjhc3kaldmm7lbnvfopyrthcvod4m6ygpj3unrcggkzhvcwv5wnhc5ufkgzlsji7agnmofovc2g4a3ui7ja
addDagNodeToHelia(helia, 'json', { hello: 'world' }, 30), // bagaaihraf4oq2kddg6o5ewlu6aol6xab75xkwbgzx2dlot7cdun7iirve23a
addDagNodeToHelia(helia, 'raw', (new TextEncoder()).encode('hello'), 30) // bafkr4ihkr4ld3m4gqkjf4reryxsy2s5tkbxprqkow6fin2iiyvreuzzab4
addDagNodeToHelia(helia, 'raw', (new TextEncoder()).encode('hello'), 30), // bafkr4ihkr4ld3m4gqkjf4reryxsy2s5tkbxprqkow6fin2iiyvreuzzab4
addDagNodeToHelia(helia, 'dag-pb', { Data: (new TextEncoder()).encode('hello'), Links: [] }, 0xb220) // bafykbzacec3ssfzln7bfcn54t5voa4onlcx63kkx3reucaiwc7eaffmla7gci
Copy link
Member Author

Choose a reason for hiding this comment

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

we don't need this here, but I added it for something to test that was available

])

return helia
Expand Down
Loading