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

@ethersproject/random not working properly in service worker #2405

Closed
Elinia opened this issue Dec 14, 2021 · 5 comments
Closed

@ethersproject/random not working properly in service worker #2405

Elinia opened this issue Dec 14, 2021 · 5 comments
Labels
enhancement New feature or improvement. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@Elinia
Copy link

Elinia commented Dec 14, 2021

Describe the bug

I'm using ethers.js on a wallet (chrome-extension) project.

Everything worked well in when testing in web pages. But when I move the code to background.js (run as a service worker), it starts to complain about "no secure random source avaialble" (<-- a typo here!).

Actually I solved this issue in my project by adding following code into the beginning of the service worker code:

self.global = { crypto: self.crypto };

So I believe we can be avoid of this issue by adding another condition here:

let anyGlobal: any = null;
try {
anyGlobal = (window as any);
if (anyGlobal == null) { throw new Error("try next"); }
} catch (error) {
try {
anyGlobal = (global as any);
if (anyGlobal == null) { throw new Error("try next"); }
} catch (error) {
anyGlobal = { };
}
}

Hope I'm understanding the error correctly.

Reproduction steps
Run any code that uses randomBytes in @ethersproject/random under service worker environment.

Environment:
Win10 Desktop
Chrome v96
Chrome Extension Manifest V3 background service worker

Search Terms
getRandomValues UNSUPPORTED_OPERATION service worker crypto no secure random source avaialble

@Elinia Elinia added the investigate Under investigation and may be a bug. label Dec 14, 2021
@ricmoo ricmoo added the on-deck This Enhancement or Bug is currently being worked on. label Dec 14, 2021
@ricmoo
Copy link
Member

ricmoo commented Dec 14, 2021

This is quite possible. I'll schedule this fix for the next release. :)

@ricmoo
Copy link
Member

ricmoo commented Jan 7, 2022

This should be fixed in 5.5.3. Can you try it out and let me know if you still have any problems?

Thanks! :)

@ricmoo ricmoo added enhancement New feature or improvement. fixed/complete This Bug is fixed or Enhancement is complete and published. and removed investigate Under investigation and may be a bug. on-deck This Enhancement or Bug is currently being worked on. labels Jan 7, 2022
@Elinia
Copy link
Author

Elinia commented Jan 7, 2022

This should be fixed in 5.5.3. Can you try it out and let me know if you still have any problems?

Thanks! :)

Happy to hear that! I'll get the time to try the new version out about next Tuesday.

@Elinia
Copy link
Author

Elinia commented Jan 11, 2022

@ricmoo
Tried 5.5.3 and it did fix the issue. Thank you very much!

@Elinia Elinia closed this as completed Jan 11, 2022
@ricmoo
Copy link
Member

ricmoo commented Jan 11, 2022

Awesome! Glad to hear it. :)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

2 participants