Skip to content

Instantly share code, notes, and snippets.

@SteenSchutt
Created November 17, 2014 17:31
Show Gist options
  • Save SteenSchutt/58ef9fdc58a0738bf371 to your computer and use it in GitHub Desktop.
Save SteenSchutt/58ef9fdc58a0738bf371 to your computer and use it in GitHub Desktop.
A simple static PHP class which can be used to interface with the omnicha.in API. Wallet support will be implemented later in another class named OmnichainWallet.
<?php
class Omnichain {
private static $baseurl = "https://omnicha.in/api/";
// Returns an array of general information about the network such
// as difficulty, network hashrate, total block count etc.
public static function getInfo() {
return self::call("getinfo");
}
// Returns the balance of a given address as a floating point number.
// Returns 0 on unseen addresses, throws exception on invalid addresses.
public static function getBalance($address) {
$data = array("address" => $address);
$response = self::call("getbalance", $data);
return $response['balance'];
}
// Checks whether or not an address is valid.
// Returns a boolean value.
public static function checkAddress($address) {
$data = array("address" => $address);
$response = self::call("checkaddress", $data);
return $response['isvalid'];
}
// Validate a message signed using an Omnicoin address.
// Returns true if the signature is valid, false if invalid.
public static function verifyMessage($address, $message, $signature) {
$data = array(
"address" => $address,
"message" => $message,
"signature" => $signature,
);
$response = self::call("verifymessage", $data);
return $response['isvalid'];
}
// Fetches the numbers needed to draw charts for the same things as getInfo().
public static function getCharts() {
return self::call("getcharts");
}
// Get a list of the 100 richest addresses, including their amount, value in USD,
// vanity name and the percentage of all coins in circulation owned by it.
public static function getRichList() {
return self::call("getrichlist")['richlist'];
}
// Get statistics about Omnichain's online wallet service, currently the amount of
// registered users and their cumulative balance.
public static function getWalletStatistics() {
return self::call("getwstats");
}
// Call a remote method on the API, decode the response.
// Throws OmnichainException on error.
private static function call($method, $data = null) {
// Build the request URL
$url = self::$baseurl . "?method=" . $method;
if($data !== null) {
$url .= "&" . http_build_query($data);
}
// Set up cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute request
$output = curl_exec($ch);
curl_close($ch);
// Parse response
$response = json_decode($output, true);
// Throw a checked exception on error
if($response['error'] === true) {
throw new OmnichainException($response['error_info']);
}
return $response['response'];
}
}
class OmnichainException extends Exception {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment