question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@ethersproject/random not working properly in service worker

See original GitHub issue

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:

https://github.com/ethers-io/ethers.js/blob/73a46efea32c3f9a4833ed77896a216e3d3752a0/packages/random/src.ts/browser-random.ts#L12-L23

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ricmoocommented, Dec 14, 2021

This is quite possible. I’ll schedule this fix for the next release. 😃

0reactions
ricmoocommented, Jan 11, 2022

Awesome! Glad to hear it. 😃

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript - Service Workers not working correctly
In the fetch event of my service worker I attempt to load index.html , and if this fails (no internet connection) I load...
Read more >
Using Service Workers - Web APIs | MDN
This article provides information on getting started with service workers, including basic architecture, registering a service worker, ...
Read more >
Stuff I wish I'd known sooner about service workers - GitHub Gist
The new service worker isn't fetched by the old one ... This continues to be true after a service worker has been successfully...
Read more >
A guide to Service Workers - pitfalls and best practices
Implementation can be quite tricky though and prone to a lot of issues that could easily slip into production if not tested properly....
Read more >
Service workers - web.dev
A service worker as a middleware proxy, running device-side, between your PWA. When an app requests a resource ... Not all browsers support...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found