Socket
Socket
Sign inDemoInstall

bitcoin-diff

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitcoin-diff

Bitcoin difficulty decoding tools


Version published
Maintainers
1
Created
Source

bitcoin-diff - Difficulty decoding tools

Installation

npm install bitcoin-diff
// or
yarn install bitcoin-diff

Usage

const btcDiff = require('bitcoin-diff')

// Convert difficulty to full target
btcDiff.diffToTarget(diff);

// Convert difficulty to 64-bit MSB of the target
btcDiff.diffToTarget64(diff);

// Convert hash/target to difficulty
btcDiff.hashToDiff(hash);

// Convert nBits to 64-bit MSB of the resulting target
btcDiff.bitsToDiff(bits);

// Convert difficulty to nBits
btcDiff.diffToBits(diff);

Source

What are bits, difficulty, and target?

Bits, difficulty, and target are three different ways of expressing the same thing - namely the amount of work involved in mining. Bits is a compact format used in the block header. It only uses 4 bytes, which saves a lot of space compared to the 32 byte target. Difficulty is the inverse ratio of a given target to the target in the very first Bitcoin block. Difficulty in the Bitcoin source code is a double precision number, which means that it is accurate to 15 significant digits. The Bitcoin source code has functions for converting between bits, target and difficulty.

Bits is a form of floating point notation. The first byte of the bits value is the exponent (called the size during the bits -> target and target -> bits conversions, and shift during the bits -> difficulty conversion) and the final 3 bytes of the bits value are the mantissa (called the word during the bits -> target conversion and compact during the target -> bits conversion). There is no reason for the different naming conventions - the Bitcoin source code is just inconsistent. Setting bit 00800000 in the bits value makes the target negative.

Target is used as the threshold during mining. It is a 32 byte (ie. 256 bit) number.

Difficulty is a human-readable indication of the amount of work involved in mining, relative to the work required to mine the very first Bitcoin block. So for example, a difficulty of 2 means that it will take twice as much work, on average, to mine a block as it took to mine the first block. Difficulty is calculated as:

difficulty = first_block_target / current_target

The first block target was 00000000ffff0000000000000000000000000000000000000000000000000000.

As the difficulty increases, the target decreases. This makes sense, since it takes more hashpower to mine a block hash below a lower target. According to the Bitcoin source code, the difficulty can never be negative, so converting a negative bits value to difficulty and back will give a different result to the original value.

With this library you can convert between bits, target and difficulty.

FAQs

Package last updated on 21 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc