Bit-Wasp / bitcoin-lib-php

PHP libraries implementing bitcoin key functions, as well as BIP32 and electrum.
The Unlicense
136 stars 86 forks source link

bits -> difficulty? #98

Closed alexanderkent closed 9 years ago

alexanderkent commented 9 years ago

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 9 years ago

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 9 years ago

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