/** * WitnessAddress constructor. * @param int $witnessVersion * @param BufferInterface $hash */ public function __construct($witnessVersion, BufferInterface $hash) { if (!is_int($witnessVersion)) { throw new \RuntimeException('Witness version must be an integer'); } if ($hash->getSize() !== 32) { throw new \RuntimeException('Hash for P2WSH address must be 32 bytes'); } $this->witnessVersion = $witnessVersion; $this->hash = $hash->getHex(); }
/** * Generate a master private key given a * @param BufferInterface $seed * @param EcAdapterInterface $ecAdapter * @return ElectrumKey */ public static function generateMasterKey(BufferInterface $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 BufferInterface $entropy * @return array */ public function entropyToWords(BufferInterface $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 BufferInterface $hash */ public function __construct(BufferInterface $hash) { $this->hash = $hash->getHex(); }
/** * Here, we return max 2000 headers following $hash. * Useful for helping other nodes sync. * @param BufferInterface $hash * @return BlockHeaderInterface[] */ public function fetchNextHeaders(BufferInterface $hash) { $stmt = $this->dbh->prepare(' SELECT child.version, child.prevBlock, child.merkleRoot, child.nTimestamp, child.nBits, child.nNonce, child.height FROM headerIndex AS child, headerIndex AS parent WHERE child.rgt < parent.rgt AND parent.hash = :hash LIMIT 2000 '); $stmt->bindValue(':hash', $hash->getBinary()); if ($stmt->execute()) { $results = array(); foreach ($stmt->fetchAll() as $row) { $results[] = new BlockHeader($row['version'], new Buffer($row['prevBlock'], 32), new Buffer($row['merkleRoot'], 32), $row['nTimestamp'], $row['nBits'], $row['nNonce']); } $stmt->closeCursor(); return $results; } throw new \RuntimeException('Failed to fetch next headers ' . $hash->getHex()); }