/** * Performs select query and, if required, hydrates the response to target class. * * @param string $query Query to perform * @param array $params Query parameters * @param bool $single_result If TRUE, only one result (first) is returned * @param null|string $hydrate_class If set, the class will be hydrated from result (using Hydrater) * @param null|string $hydrate_map_id Identifier of hydration map (or null if none) * @param null|string $error_message Message to display, if query fails * * @return mixed Hydrated class, array if no hydration or FALSE if not found. * @throws DataException */ protected function select($query, $params = [], $single_result = false, $hydrate_class = null, $hydrate_map_id = null, $error_message = null) { Console::debug("Performing query: %s", $query); $this->logQueryParameters($params, Console::DEBUG); $stmt = $this->pdo->prepare($query); if (!$stmt || !$stmt->execute($params)) { $this->logDatabaseError($params); throw new DataException($error_message ?: "Unable to perform select query."); } $result = []; while ($values = $stmt->fetch(PDO::FETCH_ASSOC)) { $single = $values; if (is_string($hydrate_class)) { $single = new $hydrate_class(); Hydrator::sharedHydrator()->fromArray($single, $values, $hydrate_map_id); } $result[] = $single; if ($single_result) { // if only one result is requested, // break out after first result read break; } } // if there is some result, return it; // otherwise, return false if (count($result)) { return $single_result ? $result[0] : $result; } return false; }
/** * Method uses XPUB to generate next deterministic address. * * @param string $xpub Master public key (XPUB) * @param int $index Index of the deterministic address * * @return string Generated address for specified index */ public static function generateAddress($xpub, $index = 0) { if (!class_exists('BitWasp\\BitcoinLib\\BIP32')) { // if class BIP32 is not available, trigger an error trigger_error("Library BitWasp/bitcoin-lib-php is required to generate deterministic address", E_USER_ERROR); } Console::debug("Generating address for XPUB '%s' index %d", $xpub, $index); $address = BIP32::build_address($xpub, sprintf('0/%d', $index))[0]; Console::debug("Generated address (index %d): '%s'", $index, $address); return $address; }