BlockUtil { public static byte[] getDifficultyTarget(final byte[] bits) { assert bits.length == BITS_MANTISSA_LENGTH + 1 : "Bits length must be 4."; final BigInteger mantissa = new BigInteger(ArrayUtil.arrayCopy(bits, 1, 4)); final BigInteger base = BigInteger.valueOf(BITS_BASE); final int exponent = ((bits[0] & 0xFF) - BITS_MANTISSA_LENGTH); final BigInteger target = mantissa.multiply(base.pow(exponent)); final byte[] targetBytes = target.toByteArray(); return targetBytes; }  static byte[] getDifficultyTarget(final byte[] bits); static byte[] getBitcoinDiffTarget(final byte[] bits); static byte[] getPoolDiffTarget(final byte[] bits); static byte[] getFullBaseDiffTarget(final byte[] bits, final boolean pool); static final long BITS_BASE; static final int BITS_MANTISSA_LENGTH; }
@Test public void getDifficultyTargetTest() { final byte[] min = new byte[] { 0x1D & 0xFF, 0x00 & 0xFF, (byte) (0xFF & 0xFF), (byte) (0xFF & 0xFF)}; final byte[] spec = new byte[] { 0x1B & 0xFF, 0x04 & 0xFF, (byte) (0x04 & 0xFF), (byte) (0xCB & 0xFF)}; final byte[] dev = new byte[] { 0x18 & 0xFF, 0x1B & 0xFF, (byte) (0xC3 & 0xFF), (byte) (0x30 & 0xFF)}; final byte[] minDiff = BlockUtil.getDifficultyTarget(min); final byte[] specDiff = BlockUtil.getDifficultyTarget(spec); final byte[] devDiff = BlockUtil.getDifficultyTarget(dev); Assert.assertEquals(0x00 & 0xFF, minDiff[0] & 0xFF); Assert.assertEquals(0xFF & 0xFF, minDiff[1] & 0xFF); Assert.assertEquals(0xFF & 0xFF, minDiff[2] & 0xFF); Assert.assertEquals(0x00 & 0xFF, minDiff[0] & 0xFF); Assert.assertEquals(min[0] & 0xFF, minDiff.length); Assert.assertEquals(spec[0] & 0xFF, specDiff.length); Assert.assertEquals(dev[0] & 0xFF, devDiff.length); }