bitcoinjs / tiny-secp256k1

A tiny secp256k1 native/JS wrapper
MIT License
90 stars 55 forks source link

`WebAssembly.Compile` is disallowed on the main thread, if the buffer size is larger than 4KB. #74

Open motorina0 opened 2 years ago

motorina0 commented 2 years ago

Currently the WebAssembly part is loaded synchronously:

const mod = new WebAssembly.Module(binary);
const instance = new WebAssembly.Instance(mod, imports);

This results in the following error: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread.

However the async version of the methods cannot be run on the top-level file AFAIK. Any ideas how to fix this?

junderw commented 2 years ago

Can you provide detailed repro steps?

junderw commented 2 years ago

I think this is a browser error... but that file is not used in browsers.

wasm_loader.browser.ts is for browsers.

Either:

  1. It's an error in whatever bundler you used.
  2. Maybe there's some weird edge case in NodeJS... in which case I need better repro steps.
motorina0 commented 2 years ago
  1. It's an error in whatever bundler you used.

It is an Electron app that uses the lib, it might be the bundler. I will have a more in detail look. Thank you for the hint.

pxr64 commented 1 year ago

Facing the same issue.

I ended up creating a fork and updating the browser loader. src_ts /wasm_loader.browser.ts

// Suppress TS2792: Cannot find module './secp256k1.wasm'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import wasm from "./secp256k1.wasm";

import * as rand from "./rand.js";
import * as validate_error from "./validate_error.js";
const imports = {
    "./rand.js": rand,
    "./validate_error.js": validate_error,
};
const mod = await WebAssembly.compile(Buffer.from(wasm.split(',')[1], 'base64'));
const instance = await WebAssembly.instantiate(mod, imports);

export default instance.exports;
Taha-daboussi commented 1 year ago

hey @motorina0 , I know it's been long time since this issue was opened , just checking if you was able to solve this error