1

I am developing a browser extension using Webpack v5 and incorporating the bitcoinjs-lib and ecpair libraries to create a Bitcoin wallet. However, I am encountering an error when attempting to load the WebAssembly module.

I would greatly appreciate any guidance or suggestions on how to resolve this issue successfully and instantiate the WebAssembly module in my browser extension.

Error Message:

enter image description here

Here are the key details of my setup:

  • I am using Webpack v5 to bundle the application.
  • I have included the bitcoinjs-lib and ecpair libraries as dependencies in my project.
  • Error is occurring when trying to import tiny-secp256k1 and also when passing that instance to ECPairFactory.

enter image description here

My webpack.config.js file:

const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { exec } = require("child_process");
const fs = require("fs");

function copyFolderSync(from, to) {
  fs.mkdirSync(to, { recursive: true });
  fs.readdirSync(from).forEach((element) => {
    if (element !== "manifest.json") {
      if (fs.lstatSync(path.join(from, element)).isFile()) {
        fs.copyFileSync(path.join(from, element), path.join(to, element));
      } else {
        copyFolderSync(path.join(from, element), path.join(to, element));
      }
    }
  });
}

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
          },
        },
      },
      {
        test: /\.wasm$/,
        type: "webassembly/async", // or "webassembly/sync"
      },
    ],
  },
  experiments: {
    asyncWebAssembly: true,
  },
  resolve: {
    extensions: [".js", ".jsx"],
    fallback: {
      buffer: require.resolve("buffer"),
      stream: require.resolve("stream-browserify"),
    },
  },
  entry: {
    background: "./app/background.js",
    popup: "./app/popup.js",
  },
  output: {
    path: path.resolve(__dirname, "dist/chrome"),
    filename: "[name].js",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./app/popup.html",
      filename: "popup.html",
      chunks: ["popup"],
    }),
    new CopyPlugin({
      patterns: [
        {
          from: "app",
          to: "",
          globOptions: {
            ignore: [
              "**/background.js",
              "**/popup.js",
              "**/popup.html",
              "**/build-types",
              "**/manifest",
            ],
          },
        },
        {
          from: "ui",
          to: "ui",
        },
      ],
    }),
    {
      apply: (compiler) => {
        compiler.hooks.afterEmit.tap("AfterEmitPlugin", (compilation) => {
          // Run the JavaScript file after the compilation is done
          exec("node development/build/index.js", (error, stdout, stderr) => {
            if (error) {
              console.error(`exec error: ${error}`);
              return;
            }
            // Copy files from the temp folder to other folders
            fs.readdirSync(path.resolve(__dirname, "dist")).forEach((dest) => {
              copyFolderSync("dist/chrome", `dist/${dest}`);
            });
          });
        });
      },
    },
  ],
};
2
  • 2
    I’m voting to close this question because the error message is quite generic, almost surely not caused by any software related to Bitcoin.
    – Mercedes
    Commented Jun 13, 2023 at 14:39
  • I understand that the error message may appear generic and not directly related to Bitcoin software. However, the error occurs consistently when trying to import the tiny-secp256k1 library, which is essential for Bitcoin-related functionality. I would greatly appreciate your guidance on how to proceed with either creating a Bitcoin wallet or resolving the specific issue I'm facing with the Content Security Policy restriction with Webpack. Any suggestions or insights you can provide would be immensely helpful in moving forward with my project. Thank you for your understanding and support. Commented Jun 13, 2023 at 16:23

1 Answer 1

0

Although my error was different, I recently could not use tiny-secp256k1 in my Nuxt project.

I found out that the library can be easily replaced with @bitcoinerlab/secp256k1 that is more widely supported.

Jonathan Underwood, developer of bitcoinjs-lib and tiny-secp256k1, confirmed that the replacement should be fine.

You can try the same with:

npm install @bitcoinerlab/secp256k1

And:

import ecc from '@bitcoinerlab/secp256k1';
import { ECPairFactory } from 'ecpair';
const ECPair = ECPairFactory(ecc);

I hope this help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.