Cryptocurrency : Monero Browser Mining with CoinHive

Introduction

Bitcoin, Ethereum, Monero... oh my! Cryptocurrencies are much in the news recently and have seemed to catch the public imagination! Advocates praise their advantages and potential to disrupt, while detractors are cautioning against their peril and warning of a financial bubble... which is not surprising since cryptocurrencies combine an intriguing technical foundation with the profit seeking thrill that is innate to human nature.

In the past I have made several forays into CryptoCurrency mining. More recently I have built multiple Ethereum mining rigs (another blog post for another time) and been exposed first hand to the trade off between up front investment and long term return.

One aspect of cryptocurrency mining that really caught my attention was browser mining... Although I was originally exposed to it on less than reputable terms, I see browser mining as a novel alternative mechanism for monetizing your website.

I hate ads. I know they power the internet. I know they enable websites to continue providing quality content. I know they are Google's primary source of revenue. Still. I hate being sold to. I hate the intrusion.

Browser mining offers an intriguing alternative... Instead of asking for your time and attention, browser mining borrows some CPU cycles and runs up a couple extra pennies on your electricity bill.

By allowing your browser to mine cryptocurrency you are paying for the content you are consuming, just in a much less invasion manner than having to be subjected to an ad.

Whether or not browser mining is ultimately a replacement for advertising is unlikely, but it is an intriguing alternative and something I would love to see as a voluntary alternative on websites.

As soon as I learned about the concept, I was eager to try it out. I found it to be surprisingly simple!

CoinHive

Although featured in the nefarious hacks referenced above, CoinHive is a legitimate purveyor of browser mining middle man services.

CoinHive is essentially a mining pool... It divides up mining work into small parts, and sends them to its agents, who complete the work and send back their results. Each agent gets a share of the overall profit from mining. Once enough shares has been accumulated, CoinHive distributes the Agent's profit to a configurable wallet address.

NOTE: CoinHive mines Monero. The reason for this is that unlike other major cryptocurrencies, Monero is both GPU and ASIC resistant. This means that mining with a GPU is not overwhelmingly better than mining with a CPU, and mining cannot be performed with custom hardware (ASICs). This levels the playing field and allows folks leveraging just CPUs to mine competitively.

At the time of this writing, Monero is worth about $100 per unit. A single CPU mining 24/7 is capable of mining about 1 or 2 Monero per year.

Most visitors to a website are not going to be mining Monero 24/7 however, but this is the beauty of the model. There is no difference between 1 CPU mining for 10,000 seconds, or 1,000 CPUs mining for 10 seconds. The more traffic you drive to your site the more your mining profits will increase!

To get started, you need to create a CoinHive account. Follow the instructions on the site.

Monero Wallet

CoinHive is a middle man. It requires a wallet address to send your Agent's profits to. A wallet is just a public/private key pair used to receive and send Monero. You can create a Monero Wallet offline (or online) using a desktop client, or online using one of the Monero wallet websites.

I recommend using the Monero desktop client. It gives you full control of you wallet files, the ability to create backups, and puts the responsibility of security in your hands.

On the other hand, the Monero desktop client requires that you download your own entire copy of the Monero blockchain, and is only accessible from a single computer. If you would rather have more convenience, consider a cloud wallet such as MyMonero.

Once you have created a wallet, enter the public address into your account on CoinHive.

You are now ready to start mining!

Deployment

To test this browser miner, I added it to my personal website / scratch pad: stephenmouring.name.

NOTE: CoinHive allows you to create multiple "sites" with are unique key pairs that identify your mining agent. I created a single site for my purposes, but if you had a large deployment over multiple websites using multiple key pairs would allow you to track revenue from each individual website.

Here is the snippet of HTML I embedded:

<div id="mining">  
    <div class="label">Browser Mining:</div>

    <div id="mining-statistics">
    </div>
</div>  

This just gives me a place to show the hashing rate so I can see how efficient my CPU is and verify that the miner is running. Here is the relevant JavaScript:

<script src="https://coinhive.com/lib/coinhive.min.js"></script>

<script type="text/javascript">

var miner = new CoinHive.Anonymous('<YOUR SITE KEY>');  
miner.start();

setInterval(function() {  
  $('#mining-statistics').html('');
  $('#mining-statistics').append($('<div>', { 'class':'mining-statistic' }).append(miner.getHashesPerSecond() + " H/s"))
}, 2500);

Very simple. The first <script> block remotely loads CoinHive's mining library. This is where all the magic and complexity of the mining happens, but they have created a simple API to wrap that and a simple interface to integrate with.

var miner = new CoinHive.Anonymous('<YOUR SITE KEY>');  
miner.start();  

This is really all there is to it! As soon as this code executes, the miner will start mining.

There rest of the code is for show:

setInterval(function() {  
  $('#mining-statistics').html('');
  $('#mining-statistics').append($('<div>', { 'class':'mining-statistic' }).append(miner.getHashesPerSecond() + " H/s"))
}, 3000);

Every three seconds, I update the div on the page with the current hash rate. This gives me a benchmark for performance and visual indication of activity.

Conclusion

That is all there is! Remarkable simple. An intriguing use of technology. An interesting alternative to advertising? Tell me what you think!

Questions? Comments? Email me at [email protected]!