Bit-Wasp/bitcoin-lib-php

bits -> difficulty?

alexanderkent opened this issue · 2 comments

This is cool, any decent way to go from bits to difficulty in PHP?

def calculate_target(nBits):
    # cf. CBigNum::SetCompact in bignum.h
    shift = 8 * (((nBits >> 24) & 0xff) - 3)
    print shift
    bits = nBits & 0x7fffff
    print bits
    sign = -1 if (nBits & 0x800000) else 1
    print sign
    r = sign * (bits << shift if shift >= 0 else bits >> -shift)
afk11 commented

I think there's a fudged example in src/Chain/Difficulty. Haven't gotten to dealing with scenario where a compact num can be negative.

There is a Difficulty::getdifficulty() that takes a buffer::hex('hex for your nbits, ie, 1d00ffff')

afk11 commented

Woops, thought you had posted on another repo. See Bit-Wasp/bitcoin-php.