Giter VIP home page Giter VIP logo

bitcoin-miner's Introduction

bitcoin-miner Build Status Coverage Status Build status XO code style

Super-slow yet very educative Bitcoin miner (BTC/BCH) in Javascript

Install

$ npm install bitcoin-miner

Usage

const BTCMiner = require('bitcoin-miner');
// View this block in Block Explorer:  https://insight.bitpay.com/block/00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9
// Get it in JSON format: https://insight.bitpay.com/api/block/00000000000000000020cf2bdc6563fb25c424af588d5fb7223461e72715e4a9
const block = {
	version: 536870912,
	previousblockhash: '00000000000000000061abcd4f51d81ddba5498cff67fed44b287de0990b7266',
	merkleroot: '871148c57dad60c0cde483233b099daa3e6492a91c13b337a5413a4c4f842978',
	time: 1515252561,
	bits: '180091c1'
};
let nonce = 45291990 // initial nonce

const miner = new BTCMiner(block);

// Calculate the target based on current difficulty for this block (block.bits)
const target = miner.getTarget();
console.log('The target for this block is:');
console.log(target.toString('hex'));

let hash;
let found = false;

console.log('\n[Start Mining with initial nonce:', nonce, ']');
while (nonce < (45291990+10000) && !found) { // check the next 1000 nonces starting from 45291990
	hash = miner.getHash(nonce);
	found = miner.checkHash(hash);
	console.log(hash.toString('hex'), nonce, found ? '<- nonce FOUND!!' : '');
	if (found) {
		miner.verifyNonce(block, nonce);
	}
	nonce++;
}

Example Output

Terminal Output

API

getTarget()

Returns the target Buffer for that block based on it's bits (difficulty).

getHash(nonce)

Returns the sha256sha256 hash Buffer for that block's nonce.

checkHash(hash)

Returns a Boolean with true if the hash is lower than the target and viceversa.

checkHash(block, hash)

Print colored verification of the hash against the target on the console (the code shows another way to build the block header in javascript).

Related

License

MIT © Carlos Guerrero

bitcoin-miner's People

Contributors

guerrerocarlos avatar isghe avatar unitof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bitcoin-miner's Issues

Hashing random blocks failures and hard errors

I'm running a script that selects random historical blocks. For each random block I am passing the correct nonce. Sometimes the hash is correct, sometimes it is not correct and other times there is an error. Here are repeatable samples using the example code...

CORRECT HASH:

    //
    //Target Block: 0000000099e42d2610bde471f80267d6e3393f8b653a45c3cb409ae870d4cdb1
    //Correct nonce will be: 1655861781
    //
    
    const block = {
            version: 1
            ,previousblockhash: '000000004b163c55c375213d53647f16d89fc6918b60afeaedc5bef848f813b5'
            ,merkleroot: 'b1ea695ce6cdd3acfdd4f59d247590a857ea02d3b7f99cff12a596f0bde62d49'
            ,time: 1259804330
            ,bits: '1d00ffff'
    }
    0000000099e42d2610bde471f80267d6e3393f8b653a45c3cb409ae870d4cdb1 1655861781 <-SUCCESS

INCORRECT HASH:

    //
    //Target Block: 000000000000000000937fa8b710d4acbe9dea7eb8f252b880f37d4439ccce19
    //Correct nonce will be: 2005385666
    //
    
    const block = {
            version: 536870914
            ,previousblockhash: '0000000000000000011449a27a730a8d03011dcd1eaec65c1b7f1c04649cd150'
            ,merkleroot: 'b80ece38e5be35fb7bceff6fb6754d876945ec917081aeab51dc9a91a6f36ba1'
            ,time: 1501985653
            ,bits: '18014735'
    }
    d8f689fb65f1d798c4827ba81d8ec740f37dd2d5158948b3636d0015d59c6393 2005385666 <-FAIL

ERROR (seems to be related to nonce value):

//
  //Target Block: 000000000000000002c098e283d0f186e3f0d591839c875572f8d0d02e08f35a
  //Correct nonce will be: 2382794207
  //
  
  const block = {
          version: 536870912
          ,previousblockhash: '000000000000000002c91a74eff899976e9738554f51c5673dbb4f4de3764b1f'
          ,merkleroot: '1ff8c9595b935f4566b0ef28a4304c06e98d4f3074c838a1b87453b23e9728da'
          ,time: 1485910721
          ,bits: '1802cc47'
  }
  TypeError: "value" argument is out of bounds
      at checkInt (buffer.js:1185:11)
      at Buffer.writeInt32LE (buffer.js:1375:5)
      at Miner.getHash (C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin-miner\index.js:56:28)
      at miner (C:\Users\corbi\OneDrive\Documents\NodeJS\Miner\index.js:67:21)
      at C:\Users\corbi\OneDrive\Documents\NodeJS\Miner\index.js:50:11
      at C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin\lib\index.js:37:8
      at C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin\lib\jsonrpc.js:138:13
      at Array.forEach (<anonymous>)
      at IncomingMessage.<anonymous> (C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin\lib\jsonrpc.js:127:15)
      at emitNone (events.js:111:20)
  buffer.js:405
      throw new TypeError('Arguments must be Buffers or Uint8Arrays');
      ^
  
  TypeError: Arguments must be Buffers or Uint8Arrays
      at Function.compare (buffer.js:405:11)
      at Miner.checkHash (C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin-miner\index.js:93:17)
      at miner (C:\Users\corbi\OneDrive\Documents\NodeJS\Miner\index.js:68:16)
      at C:\Users\corbi\OneDrive\Documents\NodeJS\Miner\index.js:50:11
      at C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin\lib\index.js:37:8
      at C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin\lib\jsonrpc.js:138:13
      at Array.forEach (<anonymous>)
      at IncomingMessage.<anonymous> (C:\Users\corbi\OneDrive\Documents\NodeJS\node_modules\bitcoin\lib\jsonrpc.js:127:15)
      at emitNone (events.js:111:20)
      at IncomingMessage.emit (events.js:208:7)

Nonce out of Range

When the given nonce is >2,147,483,647 (max integer) the timeBitsNonceBuffer.writeInt32LE(nonce, 8) is value is out of range.

Since you can not mix little endians 32-bit and 64-bit - another workaround is required.

The miners will iterate through all 32-bit values, so they have 4,294,967,296 tries.

Any idea to fix timeBitsNonceBuffer.writeInt32LE(nonce, 8) that max range is allowed to 4,294,967,296.

The implemented algorithm is still working fine - I validated the latest 20 blocks correctly (where the nonce was < 2,147,483,647).

verification

Carlos tengo una consulta, en el metodo verify, cuando el nonce es válido, como se lo comunicas a la red?
gracias
Saludos,
Alan.-

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.