Skip to content

Instantly share code, notes, and snippets.

@teutidos
Created February 14, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teutidos/3372162c559eb64932ed3fb1a89e634c to your computer and use it in GitHub Desktop.
Save teutidos/3372162c559eb64932ed3fb1a89e634c to your computer and use it in GitHub Desktop.
<?php
class Kotomemo {
private $zAddress;
private $rpc;
public function __construct($zAddress) {
$this->zAddress = $zAddress;
$this->rpc = new RPCUtil("192.168.1.10", 8432, 'user', 'password');
}
public function getAllMemo() {
$memo_list = [];
$txs = $this->rpc->command(['method'=> 'z_listreceivedbyaddress', 'params'=> [$this->zAddress]]);
foreach ($txs as $tx) {
$tx_info = $this->rpc->command(['method' => 'gettransaction', 'params'=>[$tx['txid']]]);
$memo_hex = $tx['memo'];
$memo_str = $this->hexToStr($memo_hex);
$memo_list[$tx_info['time']] = ['text' => $memo_str, 'txid' => $tx['txid'], 'amount' => $tx['amount']];
}
krsort($memo_list);
$counter = -1;
foreach($memo_list as $time => $memo) {
$memo_list[$time]['no'] = count($memo_list) - ++$counter;
}
return $memo_list;
}
private function hexToStr($hex) {
$formatted_array = [];
if ($this->isValidHex($hex)) {
$output = explode("\n", hex2bin($hex));
foreach ($output as $line) {
if (!$this->isBlankLine($line)) {
$formatted_array[] =$line;
}
}
$formatted = $formatted_array;
return $formatted;
} else {
return '';
}
}
private function isValidHex($hex) {
foreach (str_split($hex) as $char) {
$ord = ord($char);
if (!(($ord >= 48 && $ord <= 57) || ($ord >= 97 && $ord <= 102))) return false;
}
return true;
}
private function isBlankLine($hex) {
foreach (str_split(bin2hex($hex)) as $char) {
$ord = ord($char);
if ($ord !== 48) return false;
}
return true;
}
}
class RPCUtil {
private $ip;
private $port;
private $rpcuser;
private $rpcpassword;
public function __construct($ip = '127.0.0.1', $port = 8432, $rpcuser = 'rpcuser', $rpcpassword = 'rpcpassword') {
$this->ip = $ip;
$this->port = $port;
$this->rpcuser = $rpcuser;
$this->rpcpassword = $rpcpassword;
}
function command($request) {
$request = json_encode($request);
$kotod = curl_init();
curl_setopt_array($kotod, [
CURLOPT_URL => $this->ip,
CURLOPT_PORT => $this->port,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->rpcuser . ":" . $this->rpcpassword,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
CURLOPT_RETURNTRANSFER => true
]);
$response = json_decode(curl_exec($kotod), true);
return $response['result'];
}
}
class Util {
static public function h($string) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}
}
$kotomemo = new Kotomemo('zkFthoybC41MZzNnP5DwL6mkRvbzZHbBQ44QvfYPeSeTehFXPdec3dTbnafWZUR7SaMVfUy7i68isLXfRu2FfAdEqmvofzc');
$memos = $kotomemo->getAllMemo();
?>
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<link rel="stylesheet" href="./index.css">
<title>kotomemo</title>
</head>
<body>
<div class="container">
<h1 class="display-4">kotomemo</h1>
<hr>
<?php
foreach ($memos as $posted => $memo) {
echo '<div class="card" style="margin-bottom: 0.25rem;">';
echo '<div class="card-body">';
echo '<p class="card-text">';
foreach ($memo['text'] as $memo_line) {
echo Util::h($memo_line) . '<br>';
}
echo '</p>';
echo '<small>#'.$memo['no'].' '.date('Y-m-d H:i:s T', $posted).' '.$memo['amount'].'Koto '.$memo['txid'].'</small>';
echo '</div>';
echo '</div>';
}
?>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment