Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

fix/nodejs-crypto-scrypt-undefined-error #281

Merged
merged 2 commits into from
Mar 5, 2021
Merged

Conversation

teye
Copy link
Contributor

@teye teye commented Mar 5, 2021

Problem

crypto.scryptSync is undefined on frontend framework

Affected version: 2.2.0
Affected symptoms: cannot decrypt keystore files using scrypt algorithm

There was a change of scrypt usage from scrypt.js to using NodeJS's crypto.scryptSync. Commit: fa9b561. This change was to resolve some building logging errors reported by scrypt.js and to take advantage of the new NodeJs crypto packages.

https://github.com/Zilliqa/Zilliqa-JavaScript-Library/blob/dev/packages/zilliqa-js-crypto/src/keystore.ts#L63

However, the crypto package does not seem to be accessible by frontend frameworks and also our webpack bundle (zilliqa.min.js). Whenever users tried to execute zilliqa.wallet.addByKeyStore or zilliqa.wallet.export("address", "password", 'scrypt');, they encountered a crypto.scryptSync not a function error.

image
image (1)

Unit tests are not able to catch this error, as the crypto package is working fine in the standard nodeJS manner, e.g. as follows:

const { Zilliqa } = require('@zilliqa-js/zilliqa');
const zilliqa = new Zilliqa('https://dev-api.zilliqa.com');

async function test() {
    const keystoreAddress = await zilliqa.wallet.addByKeystore(keystore, "strong_password");
    const exportedKeystore = await zilliqa.wallet.export("address", "strong_password", 'scrypt');
}

test()
node myfile.js // no errors reported

Solution

This PR replaces the builti-in NodeJS crypto.scryptSync with a different javascript implementation; scrypt-js. https://github.com/ricmoo/scrypt-js.

How to test
The error only affects for keystore files that are created using the scrypt algorithm. The webpack examples, examples/webpack/html/load-keystore-demo.js is a demonstration that adds an account by keystore and export it.

yarn
yarn bootstrap
yarn test
cp ./dist/zilliqa.min.js ./examples/webpack/html/

double-click /examples/webpack/html/index.html
open developer console
should see the "keystore" address and "exported keystore" output the console

@teye teye requested a review from AmritKumar as a code owner March 5, 2021 04:48
@teye teye self-assigned this Mar 5, 2021
@teye teye requested review from bb111189 and renlulu as code owners March 5, 2021 04:48
@teye teye requested a review from micovi March 5, 2021 04:48
@bb111189 bb111189 merged commit 3bc32a6 into dev Mar 5, 2021
@bb111189 bb111189 deleted the fix/nodejs-crypto-scrypt branch August 18, 2021 05:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants