コード例 #1
0
 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());
 }
コード例 #2
0
 /**
  * @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');
         }
     }
 }
コード例 #3
0
ファイル: ProofOfWork.php プロジェクト: sbwdlihao/bitcoin-php
 /**
  * @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;
 }
コード例 #4
0
 /**
  * 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());
 }
コード例 #5
0
ファイル: Base58.php プロジェクト: rubensayshi/bitcoin-php
 /**
  * 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;
 }
コード例 #6
0
ファイル: Rfc6979.php プロジェクト: rubensayshi/bitcoin-php
 /**
  * @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);
 }
コード例 #7
0
ファイル: Version.php プロジェクト: tokenly/bitcoin-p2p-php
 public function hasBlockchain()
 {
     $math = Bitcoin::getMath();
     return $math->cmp($math->bitwiseAnd($this->services->getInt(), self::NODE_NETWORK), self::NODE_NETWORK) == 0;
 }
コード例 #8
0
ファイル: EcAdapter.php プロジェクト: sbwdlihao/bitcoin-php
 /**
  * @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;
 }
コード例 #9
0
ファイル: Interpreter.php プロジェクト: sbwdlihao/bitcoin-php
 /**
  * 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;
 }
コード例 #10
0
ファイル: PhpEcc.php プロジェクト: rubensayshi/bitcoin-php
 /**
  * @param Buffer $privateKey
  * @return bool
  */
 public function validatePrivateKey(Buffer $privateKey)
 {
     return $this->checkInt($privateKey->getInt(), $this->getGenerator()->getOrder());
 }
コード例 #11
0
 /**
  * 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.
 }