Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependencies with js-sha256 is risky #1

Open
neeboo opened this issue Oct 30, 2023 · 0 comments
Open

Dependencies with js-sha256 is risky #1

neeboo opened this issue Oct 30, 2023 · 0 comments

Comments

@neeboo
Copy link

neeboo commented Oct 30, 2023

Hi, there is a bug on some specific browser version with sha256 calculation error.

emn178/js-sha256#40

I found that the bitcore-lib depends on bip-schnorr then js-sha256

We should be careful and should changes it to @noble/hashes/sha256

And for Unisat Extension, for quick patch, you should use fix-modules to replace bip-schnorr package

const fixSha256 = () => {
  const file = './node_modules/bip-schnorr/src/convert.js';
  let fileData = fs.readFileSync(file).toString();
  fileData = fileData.replace(
    fileData,
    `
    const BigInteger = require('bigi');
    const Buffer = require('safe-buffer').Buffer;
    const binding = require('@noble/hashes/sha256');

    const { sha256 } = binding;

    
    function bufferToInt(buffer) {
      return BigInteger.fromBuffer(buffer);
    }

    function intToBuffer(bigInteger) {
      return bigInteger.toBuffer(32);
    }

    function hash(buffer) {
      return Buffer.from(sha256.create().update(buffer).digest(), 'hex');
    }

    module.exports = {
      bufferToInt,
      intToBuffer,
      hash,
    };
    `
  );
  fs.writeFileSync(file, fileData);
};

then

const run = async () => {
  let success = true;
  try {
  ...
    fixSha256();
  } catch (e) {
    console.error('error:', e.message);
    success = false;
  } finally {
    console.log('Fix modules result: ', success ? 'success' : 'failed');
  }
};

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant