From afb54644683c8da508d250731a6462676cf44747 Mon Sep 17 00:00:00 2001
From: rk <59029880+rrr523@users.noreply.github.com>
Date: Wed, 20 Dec 2023 00:53:45 +0800
Subject: [PATCH] Feat/rs webworker (#429)
* chore: Update example, using reed solomon
* chore: Update Rollup config
* chore: Benchmark
* docs: Update README
* feat: Support WebWorker
---
.changeset/dirty-buttons-care.md | 5 +
examples/nextjs/package.json | 1 +
.../src/components/object/create/index.tsx | 13 +--
packages/reed-solomon/README.md | 2 +-
packages/reed-solomon/benchmark.md | 19 ++++
packages/reed-solomon/examples/singlefile.js | 16 ---
packages/reed-solomon/examples/web.html | 57 ++++++++++
packages/reed-solomon/package.json | 17 ++-
packages/reed-solomon/rollup.config.js | 43 +++++++-
packages/reed-solomon/src/utils.js | 1 +
packages/reed-solomon/src/web.adapter.js | 100 ++++++++++++++++++
packages/reed-solomon/types/index.d.ts | 4 -
packages/reed-solomon/types/web.adapter.d.ts | 14 +++
pnpm-lock.yaml | 3 +
14 files changed, 263 insertions(+), 32 deletions(-)
create mode 100644 .changeset/dirty-buttons-care.md
create mode 100644 packages/reed-solomon/benchmark.md
delete mode 100644 packages/reed-solomon/examples/singlefile.js
create mode 100644 packages/reed-solomon/examples/web.html
create mode 100644 packages/reed-solomon/src/web.adapter.js
create mode 100644 packages/reed-solomon/types/web.adapter.d.ts
diff --git a/.changeset/dirty-buttons-care.md b/.changeset/dirty-buttons-care.md
new file mode 100644
index 00000000..b2cbcb65
--- /dev/null
+++ b/.changeset/dirty-buttons-care.md
@@ -0,0 +1,5 @@
+---
+'@bnb-chain/reed-solomon': patch
+---
+
+feat: Support Web Worker
diff --git a/examples/nextjs/package.json b/examples/nextjs/package.json
index 1fe739d5..a3dd3282 100644
--- a/examples/nextjs/package.json
+++ b/examples/nextjs/package.json
@@ -14,6 +14,7 @@
"@bnb-chain/greenfield-cosmos-types": "0.4.0-alpha.25",
"@bnb-chain/greenfield-js-sdk": "workspace:*",
"@bnb-chain/greenfiled-file-handle": "workspace:*",
+ "@bnb-chain/reed-solomon": "workspace:*",
"@cosmjs/encoding": "^0.32.0",
"@cosmjs/proto-signing": "^0.32.0",
"@cosmjs/stargate": "^0.32.0",
diff --git a/examples/nextjs/src/components/object/create/index.tsx b/examples/nextjs/src/components/object/create/index.tsx
index ee9a8efb..9427665a 100644
--- a/examples/nextjs/src/components/object/create/index.tsx
+++ b/examples/nextjs/src/components/object/create/index.tsx
@@ -1,9 +1,9 @@
import { client } from '@/client';
-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';
+import { ReedSolomon } from '@bnb-chain/reed-solomon';
export const CreateObject = () => {
const { address, connector } = useAccount();
@@ -64,14 +64,11 @@ export const CreateObject = () => {
const multiCal = await checksumWorker.generateCheckSumV2(file);
console.log('multiCal', multiCal);
+ const rs = new ReedSolomon();
const fileBytes = await file.arrayBuffer();
- const hashResult = await (window as any).FileHandle.getCheckSums(
- new Uint8Array(fileBytes),
- );
- const { contentLength, expectCheckSums } = hashResult;
+ const expectCheckSums = rs.encode(new Uint8Array(fileBytes));
console.log('offChainData', offChainData);
- console.log('hashResult', hashResult);
const createObjectTx = await client.object.createObject(
{
@@ -81,8 +78,8 @@ export const CreateObject = () => {
visibility: 'VISIBILITY_TYPE_PRIVATE',
fileType: file.type,
redundancyType: 'REDUNDANCY_EC_TYPE',
- contentLength,
- expectCheckSums: JSON.parse(expectCheckSums),
+ contentLength: fileBytes.byteLength,
+ expectCheckSums: expectCheckSums,
// empty tags
// tags: {
// tags: [],
diff --git a/packages/reed-solomon/README.md b/packages/reed-solomon/README.md
index 43c03890..647d9d0a 100644
--- a/packages/reed-solomon/README.md
+++ b/packages/reed-solomon/README.md
@@ -22,7 +22,7 @@ Use directly in the browser via script tag:
get reed solomon
-
+
+
+
+
+