sign($crypto->skein256('aced')); $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => implode("\r\n", [ 'Accept: application/json', 'X-Contractless-Address: ' . $wallet['address'], 'X-Contractless-Public-Key: ' . bin2hex($wallet['public_key']), 'X-Contractless-Signature: ' . bin2hex($signature), ]), 'ignore_errors' => true, 'timeout' => 20, ], ]); $body = file_get_contents($baseUrl . $route, false, $context); if ($body === false) { throw new RuntimeException('The API request could not be completed.'); } $responseHeaders = $http_response_header ?? []; $statusLine = (string) ($responseHeaders[0] ?? ''); if (preg_match('/\s(\d{3})\s/', $statusLine, $match) !== 1) { throw new RuntimeException('The API returned an invalid HTTP response.'); } $status = (int) $match[1]; echo "HTTP $status\n"; try { $decoded = json_decode($body, true, flags: JSON_THROW_ON_ERROR); echo json_encode( $decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, ) . PHP_EOL; } catch (JsonException) { echo $body . PHP_EOL; } if ($status === 503) { fwrite( STDERR, "The API returned 503. Testing each configured RPC endpoint directly:\n", ); $config = ApplicationFactory::configuration($projectRoot); $proof = new HandshakeProof($wallet['public_key'], $signature); foreach ($config->rpcNodes as $name => $node) { try { $client = new Client( new StreamTransport( host: $node['host'], port: $node['port'], timeout: $config->rpcTimeout, ), $crypto, $proof, ); $reply = $client->blockHeight(); if (strlen($reply) !== 4) { throw new RuntimeException( "Block-height reply contained " . strlen($reply) . ' bytes.', ); } $height = unpack('Vheight', $reply); fwrite( STDERR, sprintf( "[%s] %s:%d succeeded at height %u\n", $name, $node['host'], $node['port'], (int) ($height['height'] ?? 0), ), ); } catch (Throwable $endpointError) { fwrite( STDERR, sprintf( "[%s] %s:%d failed: %s: %s\n", $name, $node['host'], $node['port'], $endpointError::class, $endpointError->getMessage(), ), ); } } } exit($status >= 200 && $status < 300 ? 0 : 1); } catch (Throwable $error) { fwrite(STDERR, 'API test failed: ' . $error->getMessage() . PHP_EOL); exit(1); }