Socket
Socket
Sign inDemoInstall

coin-hive

Package Overview
Dependencies
100
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.2 to 1.1.0

LICENSE

6

package.json
{
"name": "coin-hive",
"version": "1.0.2",
"version": "1.1.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "mocha test --timeout 60000"
"test": "mocha test --timeout 600000"
},
"author": "",
"license": "MIT",

@@ -19,2 +18,3 @@ "dependencies": {

"devDependencies": {
"expect": "^21.1.0",
"mocha": "^3.5.3"

@@ -21,0 +21,0 @@ },

@@ -26,6 +26,6 @@ # Coin-Hive [![Build Status](https://travis-ci.org/cazala/coin-hive.svg?branch=master)](https://travis-ci.org/cazala/coin-hive)

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}
miner.on('update', data => console.log(`
Hashes per second: ${data.hashesPerSecond}
Total hashes: ${data.totalHashes}
Accepted hashes: ${data.acceptedHashes}
`));

@@ -44,2 +44,52 @@

## API
- `CoinHive(siteKey)`: Returns a promise of a `Miner` instance.
- `miner.start()`: Connect to the pool and start mining. Returns a promise that will resolve once the miner is started.
- `miner.stop()`: Stop mining and disconnect from the pool. Returns a promise that will resolve once the miner is stopped.
- `miner.on(event, callback)`: Specify a callback for an event. The event types are:
- `open`: The connection to our mining pool was opened. Usually happens shortly after miner.start() was called.
- `authed`: The miner successfully authed with the mining pool and the siteKey was verified. Usually happens right after open.
- `close`: The connection to the pool was closed. Usually happens when miner.stop() was called.
- `error`: An error occured. In case of a connection error, the miner will automatically try to reconnect to the pool.
- `job`: A new mining job was received from the pool.
- `found`: A hash meeting the pool's difficulty (currently 256) was found and will be send to the pool.
- `accepted`: A hash that was sent to the pool was accepted.
- `miner.rpc(methodName, argsArray)`: This method allows to interact with the Coin-Hive miner instance. It returns a Promise that resolves the the value of the remote method that was called. The miner intance API can be [found here](https://coin-hive.com/documentation/miner#miner-is-running). Here's an example:
```js
var miner = await CoinHive('SITE_KEY');
await miner.rpc('isRunning'); // false
await miner.start();
await miner.rpc('isRunning'); // true
await miner.rpc('getThrottle'); // 0
await miner.rpc('setThrottle', [0.5]);
await miner.rpc('getThrottle'); // 0.5
```
## ENVIRONMENT VARIABLES
All the following environment variables can be used to configure the miner from the outside:
- `SITE_KEY`: Coin-Hive's Site Key
- `INTERVAL`: The interval on which the miner reports an update
- `PORT`: The port that will be used to launch the server, and where puppeteer will point to
- `HOST`: The host that will be used to launch the server, and where puppeteer will point to
- `PUPPETEER_URL`: In case you don't want to point puppeteer to the local server, you can use this to make it point somewhere else where the miner is served (ie: `PUPPETEER_URL=http://coin-hive.herokuapp.com`)
## Requisites

@@ -49,1 +99,4 @@

## Disclaimer
I have nothing to do with `coin-hive.com`

@@ -1,23 +0,30 @@

var server = require('./server');
var puppeteer = require('./puppeteer');
var defaults = require('../config/defaults');
const server = require('./server');
const puppeteer = require('./puppeteer');
const defaults = require('../config/defaults');
module.exports = async function getRunner(siteKey = defaults.SITE_KEY) {
var port = process.env.PORT || defaults.PORT;
var host = process.env.HOST || defaults.HOST;
var miner = await new Promise((resolve, reject) => {
server().listen(port, host, async (err) => {
if (err) {
return reject(err);
module.exports = async function getRunner(
siteKey = defaults.SITE_KEY,
interval = defaults.INTERVAL,
port = defaults.PORT,
host = defaults.HOST
) {
const miner = await new Promise((resolve, reject) => {
var minerServer = server().listen(
process.env.SERVER_PORT || process.env.PORT || port,
process.env.SERVER_HOST || process.env.HOST || host,
async (err) => {
if (err) {
return reject(err);
}
return resolve(
puppeteer({
siteKey,
interval,
port,
host,
server: minerServer
})
);
}
return resolve(
puppeteer({
siteKey,
port,
host,
interval: defaults.INTERVAL
}));
});
);
});

@@ -24,0 +31,0 @@ await miner.init();

@@ -1,29 +0,11 @@

var EventEmitter = require('events');
var puppeteer = require('puppeteer');
var defaults = require('../config/defaults');
const EventEmitter = require('events');
const puppeteer = require('puppeteer');
const defaults = require('../config/defaults');
// Browser
var browser = null;
var getBrowser = async () => {
if (browser) {
return browser;
}
browser = await puppeteer.launch({ args: ['--no-sandbox'] });
return browser;
}
// Page
var page = null;
var getPage = async () => {
if (page) {
return page;
}
page = (await getBrowser()).newPage();
return page;
}
class Puppeteer extends EventEmitter {
constructor(siteKey, interval, port, host) {
constructor(siteKey, interval, port, host, server) {
super();
this.inited = false;
this.dead = false;
this.siteKey = siteKey;

@@ -33,11 +15,36 @@ this.interval = interval;

this.port = port;
this.server = server;
this.browser = null;
this.page = null;
}
async init() {
async getBrowser() {
if (this.browser) {
return this.browser;
}
this.browser = await puppeteer.launch({ args: ['--no-sandbox'] });
return this.browser;
}
async getPage() {
if (this.page) {
return this.page;
}
this.page = await (await this.getBrowser()).newPage();
return this.page;
}
const page = await getPage();
await page.goto(`http://${this.host}:${this.port}`);
async init() {
if (this.dead) {
throw new Error('This miner has been killed');
}
if (this.inited) {
return this.page;
}
const page = await this.getPage();
const url = process.env.PUPPETEER_URL || `http://${this.host}:${this.port}`;
await page.goto(url);
await page.exposeFunction('found', () => this.emit('found'));

@@ -48,3 +55,4 @@ await page.exposeFunction('accepted', () => this.emit('accepted'));

this.page = page;
this.inited = true;
return this.page;

@@ -55,3 +63,3 @@ }

await this.init();
this.page.evaluate(() => window.start());
return this.page.evaluate(() => window.start());
}

@@ -61,4 +69,26 @@

await this.init();
this.page.evaluate(() => window.stop());
return this.page.evaluate(() => window.stop());
}
async kill() {
try {
await this.stop();
} catch (e) { console.log('Error stopping miner', e) }
try {
const browser = await this.getBrowser();
await browser.close();
} catch (e) { console.log('Error closing browser', e) }
try {
if (this.server) {
this.server.close();
console.log('server closed')
}
} catch (e) { console.log('Error closing server', e) }
this.dead = true;
}
async rpc(method, args) {
await this.init();
return this.page.evaluate((method, args) => window.miner[method].apply(window.miner, args), method, args)
}
}

@@ -68,8 +98,9 @@

var siteKey = options.siteKey || defaults.SITE_KEY;
var interval = options.interval || defaults.INTERVAL;
var port = options.port || defaults.PORT;
var host = options.host || defaults.HOST;
const siteKey = process.env.SITE_KEY || options.siteKey || defaults.SITE_KEY;
const interval = process.env.INTERVAL || options.interval || defaults.INTERVAL;
const port = process.env.PUPPETEER_PORT || process.env.PORT || options.port || defaults.PORT;
const host = process.env.PUPPETEER_HOST || process.env.HOST || options.host || defaults.HOST;
const server = options.server || null;
return new Puppeteer(siteKey, interval, port, host);
return new Puppeteer(siteKey, interval, port, host, server);
}

@@ -0,3 +1,6 @@

const expect = require('expect');
const CoinHive = require('../src');
describe('Coin-Hive', () => {
describe('Coin-Hive', async () => {
it('should mine', async () => {

@@ -7,5 +10,5 @@ var miner = await CoinHive();

return new Promise(resolve => {
miner.on('update', (data) => {
miner.on('update', async (data) => {
if (data.acceptedHashes > 0) {
miner.stop();
await miner.kill();
resolve();

@@ -15,3 +18,18 @@ }

});
})
});
it('should do RPC', async () => {
var miner = await CoinHive();
let isRunning = await miner.rpc('isRunning');
expect(isRunning).toBe(false);
await miner.start();
isRunning = await miner.rpc('isRunning');
expect(isRunning).toBe(true);
let threads = await miner.rpc('getNumThreads');
expect(typeof threads).toBe('number');
await miner.rpc('setNumThreads', [2]);
threads = await miner.rpc('getNumThreads');
expect(threads).toBe(2);
await miner.kill();
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc