From 8ddc4d2b692173a02f6e337a752b84878293ef1f Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 20 Jun 2024 14:20:59 +0100 Subject: [PATCH] feat: use wasm piece hasher (#195) We removed this from the client library (and use the JS native version) since there's a miriad of environments that JS runs in that have differing ways to load WASM. However in Node.js it's a captive environment so should load fine and give us a little speedup. --- index.js | 17 +++++++++++++++++ package-lock.json | 4 ++-- package.json | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d4607d0..5dbdeac 100644 --- a/index.js +++ b/index.js @@ -5,11 +5,14 @@ import ora from 'ora' import { CID } from 'multiformats/cid' import { base64 } from 'multiformats/bases/base64' import { identity } from 'multiformats/hashes/identity' +import * as Digest from 'multiformats/hashes/digest' import * as DID from '@ipld/dag-ucan/did' import * as dagJSON from '@ipld/dag-json' import { CarWriter } from '@ipld/car' import { filesFromPaths } from 'files-from-path' +import * as PieceHasher from 'fr32-sha2-256-trunc254-padded-binary-tree-multihash' import * as Account from './account.js' + import { spaceAccess } from '@web3-storage/w3up-client/capability/access' import { AgentData } from '@web3-storage/access' import * as Space from './space.js' @@ -157,6 +160,20 @@ export async function upload(firstPath, opts) { : `Storing ${Math.min(Math.round((totalSent / totalSize) * 100), 100)}%` const root = await uploadFn({ + pieceHasher: { + code: PieceHasher.code, + name: 'fr32-sha2-256-trunc254-padded-binary-tree-multihash', + async digest (input) { + const hasher = PieceHasher.create() + hasher.write(input) + + const bytes = new Uint8Array(hasher.multihashByteLength()) + hasher.digestInto(bytes, 0, true) + hasher.free() + + return Digest.decode(bytes) + } + }, onShardStored: ({ cid, size, piece }) => { totalSent += size if (opts?.verbose) { diff --git a/package-lock.json b/package-lock.json index 5e1bd56..b46a6f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,7 @@ "ansi-escapes": "^6.2.0", "chalk": "^5.3.0", "files-from-path": "^1.0.4", + "fr32-sha2-256-trunc254-padded-binary-tree-multihash": "^3.3.0", "open": "^9.1.0", "ora": "^7.0.1", "pretty-tree": "^1.0.0", @@ -3795,8 +3796,7 @@ "node_modules/fr32-sha2-256-trunc254-padded-binary-tree-multihash": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/fr32-sha2-256-trunc254-padded-binary-tree-multihash/-/fr32-sha2-256-trunc254-padded-binary-tree-multihash-3.3.0.tgz", - "integrity": "sha512-O11VDxPmPvbQj5eac2BJXyieNacyd+RCMhwOzXQQM/NCI25x3c32YWB4/JwgOWPCpKnNXF6lpK/j0lj7GWOnYQ==", - "dev": true + "integrity": "sha512-O11VDxPmPvbQj5eac2BJXyieNacyd+RCMhwOzXQQM/NCI25x3c32YWB4/JwgOWPCpKnNXF6lpK/j0lj7GWOnYQ==" }, "node_modules/fs.realpath": { "version": "1.0.0", diff --git a/package.json b/package.json index 679f21e..e9566fb 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "ansi-escapes": "^6.2.0", "chalk": "^5.3.0", "files-from-path": "^1.0.4", + "fr32-sha2-256-trunc254-padded-binary-tree-multihash": "^3.3.0", "open": "^9.1.0", "ora": "^7.0.1", "pretty-tree": "^1.0.0",