Mine Bitcoin and Monero in the Browser

By  on  

It's worth noting that Google and ad blockers consider this script as malware, so use with that understanding in mind.

I recently heard this story from Buenos Aires where a Starbucks was caught was caught mining cryptocurrency on customer computers that were attempting to connect to free WiFi.  I also ran a Twitter poll recently asking if people would allow sites to mine cryptocurrency on each page as an alternative to advertising.  Most preferred advertising but it got people thinking about new types of ad models.

All of this got me thinking about how a developer could implement Bitcoin (or Ethereum or Monero, etc.) mining within a webpage using JavaScript.  It's super simple with coin-hive, a utility created for Monero and other cryptocurrency mining!

Note:  I'm definitely not endorsing the idea that you should mine cryptocurrency on a visitor machine without their knowledge.  Doing so is a breach of trust, a drain on their battery, a possible hike on their energy cost, etc.

CoinHive is a service so you'll need to sign up for an account.  You can explore their terms as you'd like.  Next you'll want to get the coin-hive package:

npm install -g coin-hive

Now you can mine on your local machine with a short JavaScript snippet:

const CoinHive = require('coin-hive');
 
(async () => {
  // Create miner
  const miner = await CoinHive('YOUR_COINHIVE_KEY'); // CoinHive's Site Key
 
  // Start miner
  await miner.start();
 
  // Listen on events
  miner.on('found', () => console.log('Found!'));
  miner.on('accepted', () => console.log('Accepted!'));
  miner.on('update', data =>
    console.log(`
    Hashes per second: ${data.hashesPerSecond}
    Total hashes: ${data.totalHashes}
    Accepted hashes: ${data.acceptedHashes}
  `)
  );
 
  // Stop miner
  setTimeout(async () => await miner.stop(), 60000);
})();

This snippet will work both in Node.js and the browser.  coin-hive also has command line support.  The CoinHive web dashboard gives you a nice mertrics of what you've generated and earned.

The process is pretty straight-forward, but again, just because it's easy doesn't mean it's something you should add to your public website without a lot of thought about how you'll handle the privacy concerns.  Also consider you need hefty volume of mining to earn any real money.

Recent Features

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    Drag and Drop MooTools File Uploads

    Honesty hour confession:  file uploading within the web browser sucks.  It just does.  Like the ugly SELECT element, the file input is almost unstylable and looks different on different platforms.  Add to those criticism the fact that we're all used to drag and drop operations...

  • By
    Implement the Google AJAX Search API

    Let's be honest...WordPress' search functionality isn't great. Let's be more honest...no search functionality is better than Google's. Luckily for us, Google provides an awesome method by which we can use their search for our own site: the Google AJAX Search API.

Discussion

  1. It’s been seen that Google are penalising/marking sites with coin miners as having malware on the Google SERPS. So would avoid this if you like your high ranking SERPS.

    • Kirill

      it does not, does not tag. but in the future, most likely, if you do not notify visitors to the site.
      But antiviruses are marked on the coinhive. there are services with which is ok with AV. many write about the affiliate gridcash, but I did not try it myself

  2. Doc

    fwiw, bitcoin is dead. My nephew works for [biggest financial institution in the world] and bitcoin is dead. Cryptocurrency is what they are gearing up for. The world will change in about two years.

  3. Cod

    Doc, bitcoin IS a cryptocurrency. Which cryptocurrency in particular are they gearing up for?

  4. Rafal

    This is similar solution. Even simpler.
    https://toaurochs.com

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!