/** * @param BufferInterface $hash * @param int|string $nBits * @return bool */ public function check(BufferInterface $hash, $nBits) { $negative = false; $overflow = false; $target = $this->math->writeCompact($nBits, $negative, $overflow); if ($negative || $overflow || $this->math->cmp($target, 0) === 0 || $this->math->cmp($target, $this->getMaxTarget()) > 0) { throw new \RuntimeException('nBits below minimum work'); } if ($this->math->cmp($hash->getInt(), $target) > 0) { throw new \RuntimeException("Hash doesn't match nBits"); } return true; }
/** * Encode a given hex string in base58 * * @param BufferInterface $binary * @return string * @throws \Exception */ public static function encode(BufferInterface $binary) { $size = $binary->getSize(); if ($binary->getBinary() === '') { return ''; } $math = Bitcoin::getMath(); $orig = $binary->getBinary(); $decimal = $binary->getInt(); $return = ''; while ($math->cmp($decimal, 0) > 0) { list($decimal, $rem) = $math->divQr($decimal, 58); $return .= self::$base58chars[$rem]; } $return = strrev($return); //leading zeros for ($i = 0; $i < $size && $orig[$i] === ""; $i++) { $return = '1' . $return; } return $return; }
/** * @param BufferInterface $privateKey * @return bool */ public function validatePrivateKey(BufferInterface $privateKey) { $math = $this->math; $scalar = $privateKey->getInt(); return $math->cmp($scalar, 0) > 0 && $math->cmp($scalar, $this->getGenerator()->getOrder()) < 0; }