-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathbitcoin.class.php
executable file
·325 lines (298 loc) · 8.82 KB
/
bitcoin.class.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
/**
* Bitcoin classes
*
* By Mike Gogulski - All rights reversed http://www.unlicense.org/ (public domain)
*
* If you find this library useful, your donation of Bitcoins to address
* 1E3d6EWLgwisXY2CWXDcdQQP2ivRN7e9r9 would be greatly appreciated. Thanks!
*
* PHPDoc is available at http://code.gogulski.com/
*
* @author Mike Gogulski - http://www.nostate.com/ http://www.gogulski.com/
* @author theymos - theymos @ http://bitcoin.org/smf
*/
define("BITCOIN_ADDRESS_VERSION", "00");// this is a hex byte
/**
* Bitcoin utility functions class
*
* @author theymos (functionality)
* @author Mike Gogulski
* http://www.gogulski.com/ http://www.nostate.com/
* (encapsulation, string abstraction, PHPDoc)
*/
class Bitcoin {
/*
* Bitcoin utility functions by theymos
* Via http://www.bitcoin.org/smf/index.php?topic=1844.0
* hex input must be in uppercase, with no leading 0x
*/
private static $hexchars = "0123456789ABCDEF";
private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
/**
* Convert a hex string into a (big) integer
*
* @param string $hex
* @return int
* @access private
*/
private function decodeHex($hex) {
$hex = strtoupper($hex);
$return = "0";
for ($i = 0; $i < strlen($hex); $i++) {
$current = (string) strpos(self::$hexchars, $hex[$i]);
$return = (string) bcmul($return, "16", 0);
$return = (string) bcadd($return, $current, 0);
}
return $return;
}
/**
* Convert an integer into a hex string
*
* @param int $dec
* @return string
* @access private
*/
private function encodeHex($dec) {
$return = "";
while (bccomp($dec, 0) == 1) {
$dv = (string) bcdiv($dec, "16", 0);
$rem = (integer) bcmod($dec, "16");
$dec = $dv;
$return = $return . self::$hexchars[$rem];
}
return strrev($return);
}
/**
* Convert a Base58-encoded integer into the equivalent hex string representation
*
* @param string $base58
* @return string
* @access private
*/
private function decodeBase58($base58) {
$origbase58 = $base58;
$return = "0";
for ($i = 0; $i < strlen($base58); $i++) {
$current = (string) strpos(Bitcoin::$base58chars, $base58[$i]);
$return = (string) bcmul($return, "58", 0);
$return = (string) bcadd($return, $current, 0);
}
$return = self::encodeHex($return);
//leading zeros
for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++) {
$return = "00" . $return;
}
if (strlen($return) % 2 != 0) {
$return = "0" . $return;
}
return $return;
}
/**
* Convert a hex string representation of an integer into the equivalent Base58 representation
*
* @param string $hex
* @return string
* @access private
*/
private function encodeBase58($hex) {
if (strlen($hex) % 2 != 0) {
die("encodeBase58: uneven number of hex characters");
}
$orighex = $hex;
$hex = self::decodeHex($hex);
$return = "";
while (bccomp($hex, 0) == 1) {
$dv = (string) bcdiv($hex, "58", 0);
$rem = (integer) bcmod($hex, "58");
$hex = $dv;
$return = $return . self::$base58chars[$rem];
}
$return = strrev($return);
//leading zeros
for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
$return = "1" . $return;
}
return $return;
}
/**
* Convert a 160-bit Bitcoin hash to a Bitcoin address
*
* @author theymos
* @param string $hash160
* @param string $addressversion
* @return string Bitcoin address
* @access public
*/
public static function hash160ToAddress($hash160, $addressversion = BITCOIN_ADDRESS_VERSION) {
$hash160 = $addressversion . $hash160;
$check = pack("H*", $hash160);
$check = hash("sha256", hash("sha256", $check, true));
$check = substr($check, 0, 8);
$hash160 = strtoupper($hash160 . $check);
return self::encodeBase58($hash160);
}
/**
* Convert a Bitcoin address to a 160-bit Bitcoin hash
*
* @author theymos
* @param string $addr
* @return string Bitcoin hash
* @access public
*/
public static function addressToHash160($addr) {
$addr = self::decodeBase58($addr);
$addr = substr($addr, 2, strlen($addr) - 10);
return $addr;
}
/**
* Determine if a string is a valid Bitcoin address
*
* @author theymos
* @param string $addr String to test
* @param string $addressversion
* @return boolean
* @access public
*/
public static function checkAddress($addr, $addressversion = BITCOIN_ADDRESS_VERSION) {
$addr = self::decodeBase58($addr);
if (strlen($addr) != 50) {
return false;
}
$version = substr($addr, 0, 2);
if (hexdec($version) > hexdec($addressversion)) {
return false;
}
$check = substr($addr, 0, strlen($addr) - 8);
$check = pack("H*", $check);
$check = strtoupper(hash("sha256", hash("sha256", $check, true)));
$check = substr($check, 0, 8);
return $check == substr($addr, strlen($addr) - 8);
}
/**
* Convert the input to its 160-bit Bitcoin hash
*
* @param string $data
* @return string
* @access private
*/
private function hash160($data) {
$data = pack("H*", $data);
return strtoupper(hash("ripemd160", hash("sha256", $data, true)));
}
/**
* Convert a Bitcoin public key to a 160-bit Bitcoin hash
*
* @param string $pubkey
* @return string
* @access public
*/
public static function pubKeyToAddress($pubkey) {
return self::hash160ToAddress(self::hash160($pubkey));
}
/**
* Remove leading "0x" from a hex value if present.
*
* @param string $string
* @return string
* @access public
*/
public static function remove0x($string) {
if (substr($string, 0, 2) == "0x" || substr($string, 0, 2) == "0X") {
$string = substr($string, 2);
}
return $string;
}
}
require_once(INCLUDE_DIR . "/lib/jsonRPCClient.php");
/**
* Bitcoin client class for access to a Bitcoin server via JSON-RPC-HTTP[S]
*
* Implements the methods documented at https://www.bitcoin.org/wiki/doku.php?id=api
*
* @version 0.3.19
* @author Mike Gogulski
* http://www.gogulski.com/ http://www.nostate.com/
*/
class BitcoinClient extends jsonRPCClient {
/**
* Create a jsonrpc_client object to talk to the bitcoin server and return it,
* or false on failure.
*
* @param string $scheme
* "http" or "https"
* @param string $username
* User name to use in connection the Bitcoin server's JSON-RPC interface
* @param string $password
* Server password
* @param string $address
* Server hostname or IP address
* @param mixed $port
* Server port (string or integer)
* @param string $certificate_path
* Path on the local filesystem to server's PEM certificate (ignored if $scheme != "https")
* @param integer $debug_level
* 0 (default) = no debugging;
* 1 = echo JSON-RPC messages received to stdout;
* 2 = log transmitted messages also
* @return jsonrpc_client
* @access public
* @throws BitcoinClientException
*/
public function __construct($scheme, $username, $password, $address = "localhost", $certificate_path = '', $debug = false) {
$scheme = strtolower($scheme);
if ($scheme != "http" && $scheme != "https")
throw new Exception("Scheme must be http or https");
if (empty($username))
throw new Exception("Username must be non-blank");
if (empty($password))
throw new Exception("Password must be non-blank");
if (!empty($certificate_path) && !is_readable($certificate_path))
throw new Exception("Certificate file " . $certificate_path . " is not readable");
$uri = $scheme . "://" . $username . ":" . $password . "@" . $address . "/";
parent::__construct($uri, $debug);
}
/**
* Test if the connection to the Bitcoin JSON-RPC server is working
*
* The check is done by calling the server's getinfo() method and checking
* for a fault.
*
* To turn code compatible with BTC >= 0.16, getmininginfo() method used instead of getinfo()
*
* @return mixed boolean TRUE if successful, or a fault string otherwise
* @access public
* @throws none
*/
public function can_connect() {
try {
$r = $this->getmininginfo();
} catch (Exception $e) {
return $e->getMessage();
}
return true;
}
public function validateaddress($coin_address) {
try {
$aStatus = parent::validateaddress($coin_address);
if (!$aStatus['isvalid']) {
return false;
}
} catch (Exception $e) {
return false;
}
return true;
}
public function is_testnet() {
try {
$r = parent::getinfo();
if ($r['testnet']) {
return true;
}
} catch (Exception $e) {
return false;
}
return false;
}
}