-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbtc.rpc.php
214 lines (209 loc) · 6.96 KB
/
btc.rpc.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
class EnryEWT_BTC
{
private $username;
private $password;
private $proto;
private $host;
private $port;
private $url;
private $CACertificate;
public $status;
public $error;
public $raw_response;
public $response;
private $id = 0;
private $ewt_address = '';//钱包账号
private $ewt_propertyid = 1;//BTC:31,BTC:1...
/**
* @param string $username
* @param string $password
* @param string $host
* @param int $port
* @param string $proto
* @param string $url
*/
public function __construct($username, $password, $host = 'localhost', $port = 8332, $url = null)
{
$this->username = $username;
$this->password = $password;
$this->host = $host;
$this->port = $port;
$this->url = $url;
// Set some defaults
$this->proto = 'http';
$this->CACertificate = null;
}
/**
* BTC产生地址
* @return
*/
public function get_NewAddress()
{
$ret = $this->getnewaddress();
if(!$this->isError($ret))
{
return json_encode(array('code'=>"200",'message'=>'onSucc','data'=>$ret));
}
return json_encode(array('code'=>"500",'message'=>'onFail','data'=>$ret));
}
/**
* BTC查询余额
* @return
*/
public function get_Balance($ewt_address,$ewt_propertyid)
{
$ret = $this->omni_getbalance($ewt_address,$ewt_propertyid);
if(!$this->isError($ret))
{
return json_encode(array('code'=>"200",'message'=>'onSucc','data'=>$ret));
}
return json_encode(array('code'=>"500",'message'=>'onFail','data'=>$ret));
}
/**
* BTC转帐
* @param toaddress
* @param amount
* @return
*/
public function get_Sendto($ewt_address,$toaddress,$ewt_propertyid,$amount)
{
$ret = $this->omni_send($ewt_address,$toaddress,$ewt_propertyid,$amount);
if(!$this->isError($ret))
{
return json_encode(array('code'=>"200",'message'=>'onSucc','data'=>$ret));
}
return json_encode(array('code'=>"500",'message'=>'onFail','data'=>$ret));
}
/**
* 验证地址的有效性
* @param address
* @return
*/
public function vailed_Address($address)
{
$ret = $this->validateaddress($address);
if(!$this->isError($ret))
{
return json_encode(array('code'=>"200",'message'=>'onSucc','data'=>$ret));
}
return json_encode(array('code'=>"500",'message'=>'onFail','data'=>$ret));
}
/**
* 交易确认
* @param txid
* @param txt
* @return
*/
public function parse_Trade($txid)
{
$ret = $this->omni_gettransaction($txid);
if(!$this->isError($ret))
{
if(!strpos($ret,$this->ewt_propertyid))
{
return json_encode(array('code'=>"500",'message'=>'onFail','data'=>'非BTC交易'.$ret));
}
return json_encode(array('code'=>"200",'message'=>'onSucc','data'=>$ret));
}
return json_encode(array('code'=>"500",'message'=>'onFail','data'=>$ret));
}
public function isError($body)
{
if(strpos($body,'error')||$body==false||$body==null)
{
return false;
}
return true;
}
/**
* @param string|null $certificate
*/
public function setSSL($certificate = null)
{
$this->proto = 'https'; // force HTTPS
$this->CACertificate = $certificate;
}
public function __call($method, $params)
{
$this->status = null;
$this->error = null;
$this->raw_response = null;
$this->response = null;
// If no parameters are passed, this will be an empty array
$params = array_values($params);
// The ID should be unique for each call
$this->id++;
// Build the request, it's ok that params might have any empty array
$request = json_encode(array(
'method' => $method,
'params' => $params,
'id' => $this->id
));
// Build the cURL session
$curl = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$this->url}");
$options = array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->username . ':' . $this->password,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request
);
// This prevents users from getting the following warning when open_basedir is set:
// Warning: curl_setopt() [function.curl-setopt]:
// CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
if (ini_get('open_basedir')) {
unset($options[CURLOPT_FOLLOWLOCATION]);
}
if ($this->proto == 'https') {
// If the CA Certificate was specified we change CURL to look for it
if (!empty($this->CACertificate)) {
$options[CURLOPT_CAINFO] = $this->CACertificate;
$options[CURLOPT_CAPATH] = DIRNAME($this->CACertificate);
} else {
// If not we need to assume the SSL cannot be verified
// so we set this flag to FALSE to allow the connection
$options[CURLOPT_SSL_VERIFYPEER] = false;
}
}
curl_setopt_array($curl, $options);
// Execute the request and decode to an array
$this->raw_response = curl_exec($curl);
$this->response = json_decode($this->raw_response, true);
// If the status is not 200, something is wrong
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// If there was no error, this will be an empty string
$curl_error = curl_error($curl);
curl_close($curl);
if (!empty($curl_error)) {
$this->error = $curl_error;
}
if ($this->response['error']) {
// If bitcoind returned an error, put that in $this->error
$this->error = $this->response['error']['message'];
} elseif ($this->status != 200) {
// If bitcoind didn't return a nice error message, we need to make our own
switch ($this->status) {
case 400:
$this->error = 'HTTP_BAD_REQUEST';
break;
case 401:
$this->error = 'HTTP_UNAUTHORIZED';
break;
case 403:
$this->error = 'HTTP_FORBIDDEN';
break;
case 404:
$this->error = 'HTTP_NOT_FOUND';
break;
}
}
if ($this->error) {
return false;
}
return $this->response['result'];
}
}