-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlibrary.php
More file actions
171 lines (140 loc) · 5.79 KB
/
Copy pathlibrary.php
File metadata and controls
171 lines (140 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* library.php
*
* @author Fexra <fexra@protonmail.com>
*
* Donate TRTLuzAzNs1E1RBFhteX56A5353vyHuSJ5AYYQfoN97PNbcMDvwQo4pUWHs7SYpuD9ThvA7AD3r742kwTmWh5o9WFaB9JXH8evP
*
* Reality is the concensus constructed between your neurons.
*/
class Turtlecoin_Library {
protected $url = null, $is_debug = false, $parameters_structure = 'array';
protected $curl_options = array(
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 8
);
protected $host;
protected $port;
private $httpErrors = array(
400 => '400 Bad Request',
401 => '401 Unauthorized',
403 => '403 Forbidden',
404 => '404 Not Found',
405 => '405 Method Not Allowed',
406 => '406 Not Acceptable',
408 => '408 Request Timeout',
500 => '500 Internal Server Error',
502 => '502 Bad Gateway',
503 => '503 Service Unavailable'
);
public function __construct($pHost, $pPort) {
$this->validate(false === extension_loaded('curl'), 'The curl extension must be loaded to use this class!');
$this->validate(false === extension_loaded('json'), 'The json extension must be loaded to use this class!');
$this->host = $pHost;
$this->port = $pPort;
$this->url = $pHost . ':' . $pPort . '/json_rpc';
}
public function validate($pFailed, $pErrMsg) {
if ($pFailed) {
echo $pErrMsg;
}
}
public function setDebug($pIsDebug) {
$this->is_debug = !empty($pIsDebug);
return $this;
}
public function setCurlOptions($pOptionsArray) {
if (is_array($pOptionsArray)) {
$this->curl_options = $pOptionsArray + $this->curl_options;
} else {
echo 'Invalid options type.';
}
return $this;
}
public function _run($method, $params) {
$result = $this->request($method, $params);
return $result;
}
private function request($pMethod, $pParams) {
static $requestId = 0;
$requestId++;
if(!$pParams) { $pParams = json_decode('{}'); }
$request = json_encode(array('jsonrpc' => '2.0', 'method' => $pMethod, 'params' => $pParams, 'id' => $requestId));
$responseMessage = $this->getResponse($request);
//If debug is enabled
$this->debug('Url: ' . $this->url . "\r\n", false);
$this->debug('Request: <br> <br> ' . $request . "\r\n", false);
$this->debug(' <br>Response: <br> <br> ' . $responseMessage . "\r\n", true);
$responseDecoded = json_decode($responseMessage, true);
//Validate reponse
$this->validate(empty($responseDecoded['id']), 'Invalid response data structure: ' . $responseMessage);
$this->validate($responseDecoded['id'] != $requestId, 'Request id: ' . $requestId . ' is different from Response id: ' . $responseDecoded['id']);
if(isset($responseDecoded['error'])) {
$errorMessage = 'Request have return error: ' . $responseDecoded['error']['message'] . '; ' . "\n" . 'Request: ' . $request . '; ';
if (isset($responseDecoded['error']['data'])) {
$errorMessage .= "\n" . 'Error data: ' . $responseDecoded['error']['data'];
}
$this->validate(!is_null($responseDecoded['error']), $errorMessage);
}
return $responseDecoded['result'];
}
protected function debug($pAdd, $pShow = false) {
static $debug, $startTime;
if(false === $this->is_debug) {
return;
}
$debug .= $pAdd;
$startTime = empty($startTime) ? array_sum(explode(' ', microtime())) : $startTime;
if (true === $pShow and !empty($debug)) {
$endTime = array_sum(explode(' ', microtime()));
$debug .= 'Request time: ' . round($endTime - $startTime, 3) . ' s Memory usage: ' . round(memory_get_usage() / 1024) . " kb\r\n";
echo nl2br($debug);
flush();
$debug = $startTime = null;
}
}
//Curl Request
protected function getResponse($pRequest) {
$ch = curl_init();
if (!$ch) {
throw new RuntimeException('Could\'t initialize a cURL session');
}
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pRequest);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!curl_setopt_array($ch, $this->curl_options)) {
throw new RuntimeException('Error while setting curl options');
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (isset($this->httpErrors[$httpCode])) {
echo 'Response Http Error - ' . $this->httpErrors[$httpCode];
}
if (0 < curl_errno($ch)) {
echo '[ERROR] Failed to connect to turtlecoin-wallet-rpc at ' . $this->host . ' port '. $this->port .'</br>';
}
curl_close($ch);
return $response;
}
//Here is where you can start adding methods supported below:
//
//https://wiki.bytecoin.org/wiki/Bytecoin_RPC_Wallet_JSON_RPC_API
public function getBalance() {
$balance = $this->_run('getBalance');
return $balance;
}
public function getStatus() {
$status = $this->_run('getStatus');
return $status['lastBlockHash'];
}
x public function getPayments($lastBlockHash, $paymentId) {
$payment_param = array('blockCount' => 100, 'blockHash' => $lastBlockHash, 'paymentId' => $paymentId);
$get_payments = $this->_run('getTransactions', $payment_param);
return $get_payments;
}
}