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()); }
/** * 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? $binary = $oldseed = $seed->getHex(); // Perform sha256 hash 5 times per iteration for ($i = 0; $i < 5 * 20000; $i++) { // Hash should return binary data $binary = hash('sha256', $binary . $oldseed, true); } $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); // Convert binary data to hex. $str = new Buffer($binary, 32, $ecAdapter->getMath()); return self::fromSecretExponent($str->getInt(), $ecAdapter); }
/** * @param Buffer $entropy * @return array */ public function entropyToWords(Buffer $entropy) { $math = $this->ecAdapter->getMath(); $ENT = $entropy->getSize() * 8; $CS = $ENT / 32; $entBits = $math->baseConvert($entropy->getHex(), 16, 2); $csBits = $this->calculateChecksum($entropy, $CS); $bits = str_pad($entBits . $csBits, $ENT + $CS, '0', STR_PAD_LEFT); $result = []; foreach (str_split($bits, 11) as $bit) { $idx = $math->baseConvert($bit, 2, 10); $result[] = $this->wordList->getWord($idx); } return $result; }
/** * @param Buffer $hash * @return Block */ public function fetchBlock(Buffer $hash) { if ($this->debug) { echo 'db: called fetchBlock (' . $hash->getHex() . '\\n'; } $stmt = $this->dbh->prepare(' SELECT h.hash, h.version, h.prevBlock, h.merkleRoot, h.nBits, h.nNonce, h.nTimestamp FROM blockIndex b JOIN headerIndex h ON b.hash = h.hash WHERE b.hash = :hash '); $stmt->bindValue(':hash', $hash->getHex()); if ($stmt->execute()) { $r = $stmt->fetch(); $stmt->closeCursor(); if ($r) { return new Block(Bitcoin::getMath(), new BlockHeader($r['version'], $r['prevBlock'], $r['merkleRoot'], $r['nTimestamp'], Buffer::int($r['nBits'], 4), $r['nNonce']), $this->fetchBlockTransactions($hash)); } } throw new \RuntimeException('Failed to fetch block'); }
<?php require "../vendor/autoload.php"; use BitWasp\Buffertools\Buffer; use BitWasp\Buffertools\BufferHex; use BitWasp\Buffertools\BufferInt; // Binary data and ASCII can be passed directly to a Buffer $binary = new Buffer('hello world'); echo $binary->getBinary() . PHP_EOL; echo $binary->getHex() . PHP_EOL; // BufferHex and BufferInt convert data to binary $hex = new BufferHex('68656c6c6f20776f726c64'); echo $binary->getBinary() . PHP_EOL; echo $hex->getHex() . PHP_EOL; // All Buffers expose getBinary(), getInt(), getHex() $int = new BufferInt(65); echo $int->getBinary() . PHP_EOL; echo $int->getInt() . PHP_EOL; echo $int->getHex() . PHP_EOL;
/** * @param Buffer $hash */ public function __construct(Buffer $hash) { $this->hash = $hash->getHex(); }
/** * @param Buffer $hash * @return $this */ public function markReceived(Buffer $hash) { unset($this->inFlight[$hash->getHex()]); return $this; }
/** * @param Buffer $buffer * @return string */ public function readBits(Buffer $buffer) { return str_pad($this->getMath()->baseConvert($buffer->getHex(), 16, 2), $this->length * 8, '0', STR_PAD_LEFT); }