public function testSerialize() { $hex = '41414141'; $dec = EccFactory::getAdapter()->hexDec($hex); $bin = pack("H*", $hex); $this->buffer = Buffer::hex($hex); // Check Binary $retBinary = $this->buffer->getBinary(); $this->assertSame($bin, $retBinary); // Check Hex $this->assertSame($hex, $this->buffer->getHex()); // Check Decimal $this->assertSame($dec, $this->buffer->getInt()); }
/** * @param $opCode * @param ScriptStack $mainStack * @throws \BitWasp\Bitcoin\Exceptions\ScriptStackException * @throws \Exception */ private function twoValueCases($opCode, ScriptStack $mainStack) { if ($mainStack->size() < 2) { throw new \Exception('Invalid stack operation (greater than)'); } $num1 = $mainStack->top(-2)->getInt(); // cscriptnum $num2 = $mainStack->top(-1)->getInt(); $opCodes = $this->opCodes; $opName = $opCodes->getOp($opCode); $math = $this->math; $castToBool = $this->castToBool; if ($opName == 'OP_ADD') { $num = $math->add($num1, $num2); } elseif ($opName == 'OP_SUB') { // cscriptnum $num = $math->sub($num1, $num2); } elseif ($opName == 'OP_BOOLAND') { // cscriptnum $num = $math->cmp($num1, $this->_bn0->getInt()) !== 0 && $math->cmp($num2, $this->_bn0->getInt()) !== 0; } elseif ($opName == 'OP_BOOLOR') { $num = $math->cmp($num1, $this->_bn0->getInt()) !== 0 || $math->cmp($num2, $this->_bn0->getInt()) !== 0; } elseif ($opName == 'OP_NUMEQUAL') { // cscriptnum $num = $math->cmp($num1, $num2) == 0; } elseif ($opName == 'OP_NUMEQUALVERIFY') { // cscriptnum $num = $math->cmp($num1, $num2) == 0; } elseif ($opName == 'OP_NUMNOTEQUAL') { $num = $math->cmp($num1, $num2) !== 0; } elseif ($opName == 'OP_LESSTHAN') { // cscriptnum $num = $math->cmp($num1, $num2) < 0; } elseif ($opName == 'OP_GREATERTHAN') { $num = $math->cmp($num1, $num2) > 0; } elseif ($opName == 'OP_LESSTHANOREQUAL') { // cscriptnum $num = $math->cmp($num1, $num2) <= 0; } elseif ($opName == 'OP_GREATERTHANOREQUAL') { $num = $math->cmp($num1, $num2) >= 0; } elseif ($opName == 'OP_MIN') { $num = $math->cmp($num1, $num2) <= 0 ? $num1 : $num2; } else { // is OP_MAX $num = $math->cmp($num1, $num2) >= 0 ? $num1 : $num2; } $mainStack->pop(); $mainStack->pop(); $buffer = Buffer::hex($math->decHex($num)); $mainStack->push($buffer); if ($opCodes->isOp($opCode, 'OP_NUMEQUALVERIFY')) { if ($castToBool($mainStack->top(-1))) { $mainStack->pop(); } else { throw new \Exception('NUM EQUAL VERIFY error'); } } }
/** * @param Buffer $hash * @param int|string $nBits * @return bool */ public function check(Buffer $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; }
/** * Generate a master private key given a * @param Buffer $seed * @param EcAdapterInterface $ecAdapter * @return ElectrumKey */ public static function generateMasterKey(Buffer $seed, EcAdapterInterface $ecAdapter = null) { // Really weird, did electrum actually hash hex string seeds? $seed = $oldseed = $seed->getHex(); // Perform sha256 hash 5 times per iteration for ($i = 0; $i < 5 * 20000; $i++) { // Hash should return binary data $seed = hash('sha256', $seed . $oldseed, true); } // Convert binary data to hex. $str = new Buffer($seed); return self::fromSecretExponent($str->getInt(), $ecAdapter ?: Bitcoin::getEcAdapter()); }
/** * Encode a given hex string in base58 * * @param Buffer $binary * @return string * @throws \Exception */ public static function encode(Buffer $binary) { $size = $binary->getSize(); if ($size == 0) { 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 = $return . self::$base58chars[$rem]; } $return = strrev($return); //leading zeros for ($i = 0; $i < $size && $orig[$i] == ""; $i++) { $return = "1" . $return; } return $return; }
/** * @param EcAdapterInterface $ecAdapter * @param PrivateKeyInterface $privateKey * @param Buffer $messageHash * @param string $algo */ public function __construct(EcAdapterInterface $ecAdapter, PrivateKeyInterface $privateKey, Buffer $messageHash, $algo = 'sha256') { $mdPk = new MdPrivateKey($ecAdapter->getMath(), $ecAdapter->getGenerator(), $privateKey->getSecretMultiplier()); $this->ecAdapter = $ecAdapter; $this->hmac = new HmacRandomNumberGenerator($ecAdapter->getMath(), $mdPk, $messageHash->getInt(), $algo); }
public function hasBlockchain() { $math = Bitcoin::getMath(); return $math->cmp($math->bitwiseAnd($this->services->getInt(), self::NODE_NETWORK), self::NODE_NETWORK) == 0; }
/** * @param Buffer $privateKey * @return bool */ public function validatePrivateKey(Buffer $privateKey) { $math = $this->math; $scalar = $privateKey->getInt(); return $math->cmp($scalar, 0) > 0 && $math->cmp($scalar, $this->getGenerator()->getOrder()) < 0; }
/** * Cast the value to a boolean * * @param $value * @return bool */ public function castToBool(Buffer $value) { if ($value->getSize() === 0) { return true; } // Since we're using buffers, lets try ensuring the contents are not 0. return $this->math->cmp($value->getInt(), 0) > 0; }
/** * @param Buffer $privateKey * @return bool */ public function validatePrivateKey(Buffer $privateKey) { return $this->checkInt($privateKey->getInt(), $this->getGenerator()->getOrder()); }
/** * Cast the value to a boolean * * @param $value * @return bool */ public function castToBool(Buffer $value) { // Since we're using buffers, lets try ensuring the contents are not 0. return $this->ecAdapter->getMath()->cmp($value->getInt(), 0) > 0; // cscriptNum or edge case. }