Skip to content

Commit

Permalink
fix: Multi cal hash (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
rrr523 authored Dec 1, 2023
1 parent 23aa4eb commit b97b305
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-llamas-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bnb-chain/greenfiled-file-handle': patch
---

fix: Multi cal hash
5 changes: 5 additions & 0 deletions examples/nextjs/src/components/object/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ACCOUNT_PRIVATEKEY } from '@/config/env';
import { getOffchainAuthKeys } from '@/utils/offchainAuth';
import { ChangeEvent, useState } from 'react';
import { useAccount } from 'wagmi';
import { getCheckSumsWorker } from '@bnb-chain/greenfiled-file-handle';

export const CreateObject = () => {
const { address, connector } = useAccount();
Expand Down Expand Up @@ -59,6 +60,10 @@ export const CreateObject = () => {
return;
}

const checksumWorker = getCheckSumsWorker();
const multiCal = await checksumWorker.generateCheckSumV2(file);
console.log('multiCal', multiCal);

const fileBytes = await file.arrayBuffer();
const hashResult = await (window as any).FileHandle.getCheckSums(
new Uint8Array(fileBytes),
Expand Down
4 changes: 2 additions & 2 deletions packages/file-handle/src/browser/worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getChecksumApi() {

const workerScript = `
importScripts('https://unpkg.com/[email protected]/dist/sha256.umd.min.js')
importScripts('http://unpkg.com/@bnb-chain/greenfiled-file-handle/dist/browser/esm/wasm_exec-worker.js');
importScripts('https://unpkg.com/@bnb-chain/greenfiled-file-handle/dist/browser/esm/wasm_exec-worker.js');
function getDecodeBase64Length(data) {
let bufferLength = Math.floor(data.length * 0.75);
const len = data.length;
Expand Down Expand Up @@ -114,7 +114,7 @@ export function getChecksumApi() {
const init = async () => {
const go = new Go();
const result = await WebAssembly.instantiateStreaming(
fetch('https://unpkg.com/@bnb-chain/greenfiled-file-handle/dist/browser/esm/index.js'),
fetch('https://unpkg.com/@bnb-chain/greenfiled-file-handle/dist/node/file-handle-worker.wasm'),
go.importObject,
);
if (result) {
Expand Down
6 changes: 3 additions & 3 deletions packages/file-handle/types/expose.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export function getCheckSums(
redundancyVal: number;
}>;

export async function generateCheckSumV2(file: File): Promise<THashResult>;

export function getCheckSumsWorker(): generateCheckSumV2;
export function getCheckSumsWorker(): {
generateCheckSumV2(file: File): Promise<THashResult>;
};

0 comments on commit b97b305

Please sign in to comment.