Skip to content
Newer
Older
100644 961 lines (893 sloc) 33 KB
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
1 <?php
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
2 /**
3 * Bitcoin classes
4 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
5 * By Mike Gogulski - All rights reversed http://www.unlicense.org/ (public domain)
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
6 *
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
7 * If you find this library useful, your donation of Bitcoins to address
8 * 1E3d6EWLgwisXY2CWXDcdQQP2ivRN7e9r9 would be greatly appreciated. Thanks!
9 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
10 * PHPDoc is available at http://code.gogulski.com/
11 *
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
12 * @author Mike Gogulski - http://www.nostate.com/ http://www.gogulski.com/
13 * @author theymos - theymos @ http://bitcoin.org/smf
14 */
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
15
16 define("BITCOIN_ADDRESS_VERSION", "00");// this is a hex byte
17 /**
18 * Bitcoin utility functions class
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
19 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
20 * @author theymos (functionality)
5e7482b @mikegogulski phpdoc
authored Nov 27, 2010
21 * @author Mike Gogulski
22 * http://www.gogulski.com/ http://www.nostate.com/
23 * (encapsulation, string abstraction, PHPDoc)
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
24 */
25 class Bitcoin {
26
27 /*
28 * Bitcoin utility functions by theymos
29 * Via http://www.bitcoin.org/smf/index.php?topic=1844.0
30 * hex input must be in uppercase, with no leading 0x
31 */
32 private static $hexchars = "0123456789ABCDEF";
33 private static $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
34
35 /**
36 * Convert a hex string into a (big) integer
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
37 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
38 * @param string $hex
39 * @return int
40 * @access private
41 */
42 private function decodeHex($hex) {
43 $hex = strtoupper($hex);
44 $return = "0";
45 for ($i = 0; $i < strlen($hex); $i++) {
46 $current = (string) strpos(self::$hexchars, $hex[$i]);
47 $return = (string) bcmul($return, "16", 0);
48 $return = (string) bcadd($return, $current, 0);
49 }
50 return $return;
51 }
52
53 /**
54 * Convert an integer into a hex string
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
55 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
56 * @param int $dec
57 * @return string
58 * @access private
59 */
60 private function encodeHex($dec) {
61 $return = "";
62 while (bccomp($dec, 0) == 1) {
63 $dv = (string) bcdiv($dec, "16", 0);
64 $rem = (integer) bcmod($dec, "16");
65 $dec = $dv;
66 $return = $return . self::$hexchars[$rem];
67 }
68 return strrev($return);
69 }
70
71 /**
72 * Convert a Base58-encoded integer into the equivalent hex string representation
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
73 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
74 * @param string $base58
75 * @return string
76 * @access private
77 */
78 private function decodeBase58($base58) {
79 $origbase58 = $base58;
80
9652e76 @psyvisions Check for invalid characters when base58-decoding.
psyvisions authored Feb 21, 2013
81 //only valid chars allowed
82 if (preg_match('/[^1-9A-HJ-NP-Za-km-z]/', $base58)) {
83 return "";
84 }
85
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
86 $return = "0";
87 for ($i = 0; $i < strlen($base58); $i++) {
88 $current = (string) strpos(Bitcoin::$base58chars, $base58[$i]);
89 $return = (string) bcmul($return, "58", 0);
90 $return = (string) bcadd($return, $current, 0);
91 }
92
93 $return = self::encodeHex($return);
94
95 //leading zeros
96 for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++) {
97 $return = "00" . $return;
98 }
99
100 if (strlen($return) % 2 != 0) {
101 $return = "0" . $return;
102 }
103
104 return $return;
105 }
106
107 /**
108 * Convert a hex string representation of an integer into the equivalent Base58 representation
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
109 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
110 * @param string $hex
111 * @return string
112 * @access private
113 */
114 private function encodeBase58($hex) {
115 if (strlen($hex) % 2 != 0) {
116 die("encodeBase58: uneven number of hex characters");
117 }
118 $orighex = $hex;
119
120 $hex = self::decodeHex($hex);
121 $return = "";
122 while (bccomp($hex, 0) == 1) {
123 $dv = (string) bcdiv($hex, "58", 0);
124 $rem = (integer) bcmod($hex, "58");
125 $hex = $dv;
126 $return = $return . self::$base58chars[$rem];
127 }
128 $return = strrev($return);
129
130 //leading zeros
131 for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
132 $return = "1" . $return;
133 }
134
135 return $return;
136 }
137
138 /**
139 * Convert a 160-bit Bitcoin hash to a Bitcoin address
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
140 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
141 * @author theymos
142 * @param string $hash160
143 * @param string $addressversion
144 * @return string Bitcoin address
145 * @access public
146 */
147 public static function hash160ToAddress($hash160, $addressversion = BITCOIN_ADDRESS_VERSION) {
148 $hash160 = $addressversion . $hash160;
149 $check = pack("H*", $hash160);
150 $check = hash("sha256", hash("sha256", $check, true));
151 $check = substr($check, 0, 8);
152 $hash160 = strtoupper($hash160 . $check);
153 return self::encodeBase58($hash160);
154 }
155
156 /**
157 * Convert a Bitcoin address to a 160-bit Bitcoin hash
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
158 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
159 * @author theymos
160 * @param string $addr
161 * @return string Bitcoin hash
162 * @access public
163 */
164 public static function addressToHash160($addr) {
165 $addr = self::decodeBase58($addr);
166 $addr = substr($addr, 2, strlen($addr) - 10);
167 return $addr;
168 }
169
170 /**
171 * Determine if a string is a valid Bitcoin address
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
172 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
173 * @author theymos
174 * @param string $addr String to test
175 * @param string $addressversion
176 * @return boolean
177 * @access public
178 */
179 public static function checkAddress($addr, $addressversion = BITCOIN_ADDRESS_VERSION) {
180 $addr = self::decodeBase58($addr);
181 if (strlen($addr) != 50) {
182 return false;
183 }
184 $version = substr($addr, 0, 2);
185 if (hexdec($version) > hexdec($addressversion)) {
186 return false;
187 }
188 $check = substr($addr, 0, strlen($addr) - 8);
189 $check = pack("H*", $check);
190 $check = strtoupper(hash("sha256", hash("sha256", $check, true)));
191 $check = substr($check, 0, 8);
192 return $check == substr($addr, strlen($addr) - 8);
193 }
194
195 /**
196 * Convert the input to its 160-bit Bitcoin hash
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
197 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
198 * @param string $data
199 * @return string
200 * @access private
201 */
202 private function hash160($data) {
203 $data = pack("H*", $data);
204 return strtoupper(hash("ripemd160", hash("sha256", $data, true)));
205 }
206
207 /**
208 * Convert a Bitcoin public key to a 160-bit Bitcoin hash
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
209 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
210 * @param string $pubkey
211 * @return string
212 * @access public
213 */
214 public static function pubKeyToAddress($pubkey) {
215 return self::hash160ToAddress($this->hash160($pubkey));
216 }
217
218 /**
219 * Remove leading "0x" from a hex value if present.
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
220 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
221 * @param string $string
222 * @return string
223 * @access public
224 */
225 public static function remove0x($string) {
226 if (substr($string, 0, 2) == "0x" || substr($string, 0, 2) == "0X") {
227 $string = substr($string, 2);
228 }
229 return $string;
230 }
231 }
232
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
233 /**
234 * Exception class for BitcoinClient
235 *
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
236 * @author Mike Gogulski
5e7482b @mikegogulski phpdoc
authored Nov 27, 2010
237 * http://www.gogulski.com/ http://www.nostate.com/
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
238 */
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
239 class BitcoinClientException extends ErrorException {
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
240 // Redefine the exception so message isn't optional
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
241 public function __construct($message, $code = 0, $severity = E_USER_NOTICE, Exception $previous = null) {
242 parent::__construct($message, $code, $severity, $previous);
243 }
244
245 public function __toString() {
246 return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
247 }
248 }
249
250 require_once(dirname(__FILE__) . "/includes/xmlrpc.inc");
251 require_once(dirname(__FILE__) . "/includes/jsonrpc.inc");
252
253 /**
254 * Bitcoin client class for access to a Bitcoin server via JSON-RPC-HTTP[S]
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
255 *
256 * Implements the methods documented at https://www.bitcoin.org/wiki/doku.php?id=api
257 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
258 * @version 0.3.19
5e7482b @mikegogulski phpdoc
authored Nov 27, 2010
259 * @author Mike Gogulski
260 * http://www.gogulski.com/ http://www.nostate.com/
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
261 */
262 class BitcoinClient extends jsonrpc_client {
263
264 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
265 * Create a jsonrpc_client object to talk to the bitcoin server and return it,
266 * or false on failure.
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
267 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
268 * @param string $scheme
269 * "http" or "https"
270 * @param string $username
271 * User name to use in connection the Bitcoin server's JSON-RPC interface
272 * @param string $password
273 * Server password
274 * @param string $address
275 * Server hostname or IP address
276 * @param mixed $port
277 * Server port (string or integer)
278 * @param string $certificate_path
279 * Path on the local filesystem to server's PEM certificate (ignored if $scheme != "https")
280 * @param integer $debug_level
281 * 0 (default) = no debugging;
282 * 1 = echo JSON-RPC messages received to stdout;
283 * 2 = log transmitted messages also
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
284 * @return jsonrpc_client
285 * @access public
286 * @throws BitcoinClientException
287 */
288 public function __construct($scheme, $username, $password, $address = "localhost", $port = 8332, $certificate_path = '', $debug_level = 0) {
289 $scheme = strtolower($scheme);
290 if ($scheme != "http" && $scheme != "https")
291 throw new BitcoinClientException("Scheme must be http or https");
292 if (empty($username))
293 throw new BitcoinClientException("Username must be non-blank");
294 if (empty($password))
295 throw new BitcoinClientException("Password must be non-blank");
296 $port = (string) $port;
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
297 if (!$port || empty($port) || !is_numeric($port) || $port < 1 || $port > 65535 || floatval($port) != intval($port))
298 throw new BitcoinClientException("Port must be an integer and between 1 and 65535");
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
299 if (!empty($certificate_path) && !is_readable($certificate_path))
300 throw new BitcoinClientException("Certificate file " . $certificate_path . " is not readable");
301 $uri = $scheme . "://" . $username . ":" . $password . "@" . $address . ":" . $port . "/";
302 parent::__construct($uri);
303 $this->setDebug($debug_level);
304 $this->setSSLVerifyHost(0);
305 if ($scheme == "https")
306 if (!empty($certificate_path))
307 $this->setCaCertificate($certificate_path);
308 else
309 $this->setSSLVerifyPeer(false);
310 }
311
312 /**
313 * Test if the connection to the Bitcoin JSON-RPC server is working
314 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
315 * The check is done by calling the server's getinfo() method and checking
316 * for a fault.
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
317 *
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
318 * @return mixed boolean TRUE if successful, or a fault string otherwise
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
319 * @access public
320 * @throws none
321 */
322 public function can_connect() {
323 try {
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
324 $r = $this->getinfo();
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
325 } catch (BitcoinClientException $e) {
326 return $e->getMessage();
327 }
328 return true;
329 }
330
331 /**
332 * Convert a Bitcoin server query argument to a jsonrpcval
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
333 *
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
334 * @param mixed $argument
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
335 * @return jsonrpcval
336 * @throws none
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
337 * @todo Make this method private.
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
338 */
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
339 public function query_arg_to_parameter($argument) {
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
340 $type = "";// "string" is encoded as this default type value in xmlrpc.inc
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
341 if (is_numeric($argument)) {
342 if (intval($argument) != floatval($argument)) {
343 $argument = floatval($argument);
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
344 $type = "double";
f98d125 @mikegogulski fix up query argument handling, new tests
authored Nov 24, 2010
345 } else {
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
346 $argument = intval($argument);
f98d125 @mikegogulski fix up query argument handling, new tests
authored Nov 24, 2010
347 $type = "int";
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
348 }
349 }
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
350 if (is_bool($argument))
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
351 $type = "boolean";
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
352 if (is_int($argument))
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
353 $type = "int";
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
354 if (is_float($argument))
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
355 $type = "double";
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
356 if (is_array($argument))
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
357 $type = "array";
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
358 return new jsonrpcval($argument, $type);
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
359 }
360
361 /**
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
362 * Send a JSON-RPC message and optional parameter arguments to the server.
363 *
364 * Use the API functions if possible. This method remains public to support
365 * changes being made to the API before this libarary can be updated.
191a6dd @mikegogulski fix up phpdoc
authored Nov 25, 2010
366 *
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
367 * @param string $message
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
368 * @param mixed $args, ...
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
369 * @return mixed
370 * @throws BitcoinClientException
371 * @see xmlrpc.inc:php_xmlrpc_decode()
372 */
373 public function query($message) {
374 if (!$message || empty($message))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
375 throw new BitcoinClientException("Bitcoin client query requires a message");
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
376 $msg = new jsonrpcmsg($message);
377 if (func_num_args() > 1) {
8bf1f50 @mikegogulski fix query() with arguments
authored Nov 24, 2010
378 for ($i = 1; $i < func_num_args(); $i++) {
379 $msg->addParam(self::query_arg_to_parameter(func_get_arg($i)));
82fc51b @mikegogulski initial commit
authored Nov 23, 2010
380 }
381 }
382 $response = $this->send($msg);
383 if ($response->faultCode()) {
384 throw new BitcoinClientException($response->faultString());
385 }
386 return php_xmlrpc_decode($response->value());
387 }
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
388
389 /*
390 * The following functions implement the Bitcoin RPC API as documented at https://www.bitcoin.org/wiki/doku.php?id=api
391 */
392
393 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
394 * Safely copies wallet.dat to destination, which can be a directory or
395 * a path with filename.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
396 *
397 * @param string $destination
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
398 * @return mixed Nothing, or an error array
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
399 * @throws BitcoinClientException
400 */
401 public function backupwallet($destination) {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
402 if (!$destination || empty($destination))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
403 throw new BitcoinClientException("backupwallet requires a destination");
404 return $this->query("backupwallet", $destination);
405 }
406
407 /**
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
408 * Returns the server's available balance, or the balance for $account with
409 * at least $minconf confirmations.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
410 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
411 * @param string $account Account to check. If not provided, the server's
412 * total available balance is returned.
413 * @param integer $minconf If specified, only transactions with at least
414 * $minconf confirmations will be included in the returned total.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
415 * @return float Bitcoin balance
416 * @throws BitcoinClientException
417 */
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
418 public function getbalance($account = NULL, $minconf = 1) {
419 if (!is_numeric($minconf) || $minconf < 0)
420 throw new BitcoinClientException('getbalance requires a numeric minconf >= 0');
d3e269e @sam2kb Fix getbalance method. Allow empty string (default account).
sam2kb authored Jun 1, 2013
421 if ($account === NULL)
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
422 return $this->query("getbalance");
423 return $this->query("getbalance", $account, $minconf);
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
424 }
425
426 /**
427 * Returns the number of blocks in the longest block chain.
428 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
429 * @return integer Current block count
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
430 * @throws BitcoinClientException
431 */
432 public function getblockcount() {
433 return $this->query("getblockcount");
434 }
435
436 /**
437 * Returns the block number of the latest block in the longest block chain.
438 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
439 * @return integer Block number
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
440 * @throws BitcoinClientException
441 */
442 public function getblocknumber() {
443 return $this->query("getblocknumber");
444 }
445
446 /**
447 * Returns the number of connections to other nodes.
448 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
449 * @return integer Connection count
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
450 * @throws BitcoinClientException
451 */
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
452 public function getconnectioncount() {
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
453 return $this->query("getconnectioncount");
454 }
455
456 /**
457 * Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
458 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
459 * @return float Difficulty
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
460 * @throws BitcoinClientException
461 */
462 public function getdifficulty() {
463 return $this->query("getdifficulty");
464 }
465
466 /**
467 * Returns boolean true if server is trying to generate bitcoins, false otherwise.
468 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
469 * @return boolean Generation status
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
470 * @throws BitcoinClientException
471 */
472 public function getgenerate() {
473 return $this->query("getgenerate");
474 }
475
476 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
477 * Tell Bitcoin server to generate Bitcoins or not, and how many processors
478 * to use.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
479 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
480 * @param boolean $generate
481 * @param integer $maxproc
482 * Limit generation to $maxproc processors, unlimited if -1
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
483 * @return mixed Nothing if successful, error array if not
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
484 * @throws BitcoinClientException
485 */
486 public function setgenerate($generate = TRUE, $maxproc = -1) {
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
487 if (!is_numeric($maxproc) || $maxproc < -1)
488 throw new BitcoinClientException('setgenerate: $maxproc must be numeric and >= -1');
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
489 return $this->query("setgenerate", $generate, $maxproc);
490 }
491
492 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
493 * Returns an array containing server information.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
494 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
495 * @return array Server information
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
496 * @throws BitcoinClientException
497 */
498 public function getinfo() {
499 return $this->query("getinfo");
500 }
501
502 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
503 * Returns the account associated with the given address.
504 *
505 * @param string $address
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
506 * @return string Account
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
507 * @throws BitcoinClientException
508 * @since 0.3.17
509 */
510 public function getaccount($address) {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
511 if (!$address || empty($address))
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
512 throw new BitcoinClientException("getaccount requires an address");
513 return $this->query("getaccount", $address);
514 }
515
516 /**
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
517 * Returns the label associated with the given address.
518 *
519 * @param string $address
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
520 * @return string Label
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
521 * @throws BitcoinClientException
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
522 * @deprecated Since 0.3.17
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
523 */
524 public function getlabel($address) {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
525 if (!$address || empty($address))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
526 throw new BitcoinClientException("getlabel requires an address");
527 return $this->query("getlabel", $address);
528 }
529
530 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
531 * Sets the account associated with the given address.
532 * $account may be omitted to remove an account from an address.
533 *
534 * @param string $address
535 * @param string $account
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
536 * @return NULL
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
537 * @throws BitcoinClientException
538 * @since 0.3.17
539 */
540 public function setaccount($address, $account = "") {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
541 if (!$address || empty($address))
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
542 throw new BitcoinClientException("setaccount requires an address");
543 return $this->query("setaccount", $address, $account);
544 }
545
546 /**
547 * Sets the label associated with the given address.
548 * $label may be omitted to remove a label from an address.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
549 *
550 * @param string $address
551 * @param string $label
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
552 * @return NULL
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
553 * @throws BitcoinClientException
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
554 * @deprecated Since 0.3.17
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
555 */
556 public function setlabel($address, $label = "") {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
557 if (!$address || empty($address))
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
558 throw new BitcoinClientException("setlabel requires an address");
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
559 return $this->query("setlabel", $address, $label);
560 }
561
562 /**
563 * Returns a new bitcoin address for receiving payments.
564 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
565 * If $account is specified (recommended), it is added to the address book so
566 * payments received with the address will be credited to $account.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
567 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
568 * @param string $account Label to apply to the new address
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
569 * @return string Bitcoin address
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
570 * @throws BitcoinClientException
571 */
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
572 public function getnewaddress($account = NULL) {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
573 if (!$account || empty($account))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
574 return $this->query("getnewaddress");
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
575 return $this->query("getnewaddress", $account);
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
576 }
577
578 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
579 * Returns the total amount received by $address in transactions with at least
580 * $minconf confirmations.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
581 *
582 * @param string $address
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
583 * Bitcoin address
584 * @param integer $minconf
585 * Minimum number of confirmations for transactions to be counted
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
586 * @return float Bitcoin total
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
587 * @throws BitcoinClientException
588 */
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
589 public function getreceivedbyaddress($address, $minconf = 1) {
590 if (!is_numeric($minconf) || $minconf < 0)
591 throw new BitcoinClientException('getreceivedbyaddress requires a numeric minconf >= 0');
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
592 if (!$address || empty($address))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
593 throw new BitcoinClientException("getreceivedbyaddress requires an address");
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
594 return $this->query("getreceivedbyaddress", $address, $minconf);
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
595 }
596
597 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
598 * Returns the total amount received by addresses associated with $account
599 * in transactions with at least $minconf confirmations.
600 *
601 * @param string $account
602 * @param integer $minconf
603 * Minimum number of confirmations for transactions to be counted
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
604 * @return float Bitcoin total
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
605 * @throws BitcoinClientException
606 * @since 0.3.17
607 */
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
608 public function getreceivedbyaccount($account, $minconf = 1) {
609 if (!is_numeric($minconf) || $minconf < 0)
610 throw new BitcoinClientException('getreceivedbyaccount requires a numeric minconf >= 0');
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
611 if (!$account || empty($account))
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
612 throw new BitcoinClientException("getreceivedbyaccount requires an account");
613 return $this->query("getreceivedbyaccount", $account, $minconf);
614 }
615
616 /**
617 * Returns the total amount received by addresses with $label in
618 * transactions with at least $minconf confirmations.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
619 *
620 * @param string $label
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
621 * @param integer $minconf
622 * Minimum number of confirmations for transactions to be counted
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
623 * @return float Bitcoin total
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
624 * @throws BitcoinClientException
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
625 * @deprecated Since 0.3.17
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
626 */
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
627 public function getreceivedbylabel($label, $minconf = 1) {
628 if (!is_numeric($minconf) || $minconf < 0)
629 throw new BitcoinClientException('getreceivedbylabel requires a numeric minconf >= 0');
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
630 if (!$label || empty($label))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
631 throw new BitcoinClientException("getreceivedbylabel requires a label");
632 return $this->query("getreceivedbylabel", $label, $minconf);
633 }
634
635 /**
636 * Return a list of server RPC commands or help for $command, if specified.
637 *
638 * @param string $command
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
639 * @return string Help text
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
640 * @throws BitcoinClientException
641 */
642 public function help($command = NULL) {
643 if (!$command || empty($command))
644 return $this->query("help");
645 return $this->query("help", $command);
646 }
647
648 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
649 * Return an array of arrays showing how many Bitcoins have been received by
650 * each address in the server's wallet.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
651 *
652 * @param integer $minconf Minimum number of confirmations before payments are included.
653 * @param boolean $includeempty Whether to include addresses that haven't received any payments.
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
654 * @return array An array of arrays. The elements are:
655 * "address" => receiving address
656 * "account" => the account of the receiving address
657 * "amount" => total amount received by the address
658 * "confirmations" => number of confirmations of the most recent transaction included
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
659 * @throws BitcoinClientException
660 */
661 public function listreceivedbyaddress($minconf = 1, $includeempty = FALSE) {
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
662 if (!is_numeric($minconf) || $minconf < 0)
663 throw new BitcoinClientException('listreceivedbyaddress requires a numeric minconf >= 0');
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
664 return $this->query("listreceivedbyaddress", $minconf, $includeempty);
665 }
666
667 /**
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
668 * Return an array of arrays showing how many Bitcoins have been received by
669 * each account in the server's wallet.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
670 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
671 * @param integer $minconf
672 * Minimum number of confirmations before payments are included.
673 * @param boolean $includeempty
674 * Whether to include addresses that haven't received any payments.
675 * @return array An array of arrays. The elements are:
676 * "account" => the label of the receiving address
677 * "amount" => total amount received by the address
678 * "confirmations" => number of confirmations of the most recent transaction included
679 * @throws BitcoinClientException
680 * @since 0.3.17
681 */
682 public function listreceivedbyaccount($minconf = 1, $includeempty = FALSE) {
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
683 if (!is_numeric($minconf) || $minconf < 0)
684 throw new BitcoinClientException('listreceivedbyaccount requires a numeric minconf >= 0');
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
685 return $this->query("listreceivedbyaccount", $minconf, $includeempty);
686 }
687
688 /**
689 * Return an array of arrays showing how many Bitcoins have been received by
690 * each label in the server's wallet.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
691 *
692 * @param integer $minconf Minimum number of confirmations before payments are included.
693 * @param boolean $includeempty Whether to include addresses that haven't received any payments.
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
694 * @return array An array of arrays. The elements are:
695 * "label" => the label of the receiving address
696 * "amount" => total amount received by the address
697 * "confirmations" => number of confirmations of the most recent transaction included
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
698 * @throws BitcoinClientException
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
699 * @deprecated Since 0.3.17
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
700 */
701 public function listreceivedbylabel($minconf = 1, $includeempty = FALSE) {
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
702 if (!is_numeric($minconf) || $minconf < 0)
703 throw new BitcoinClientException('listreceivedbylabel requires a numeric minconf >= 0');
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
704 return $this->query("listreceivedbylabel", $minconf, $includeempty);
705 }
706
707 /**
708 * Send amount from the server's available balance.
709 *
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
710 * $amount is a real and is rounded to the nearest 0.01. Returns string "sent" on success.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
711 *
712 * @param string $address Destination Bitcoin address or IP address
713 * @param float $amount Amount to send. Will be rounded to the nearest 0.01.
714 * @param string $comment
715 * @param string $comment_to
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
716 * @return string Hexadecimal transaction ID on success.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
717 * @throws BitcoinClientException
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
718 * @todo Document the comment arguments better.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
719 */
720 public function sendtoaddress($address, $amount, $comment = NULL, $comment_to = NULL) {
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
721 if (!$address || empty($address))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
722 throw new BitcoinClientException("sendtoaddress requires a destination address");
a11b1d1 @mikegogulski Unit tests done up to 0.3.16 API, bugfixes in
authored Dec 1, 2010
723 if (!$amount || empty($amount))
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
724 throw new BitcoinClientException("sendtoaddress requires an amount to send");
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
725 if (!is_numeric($amount) || $amount <= 0)
726 throw new BitcoinClientException("sendtoaddress requires the amount sent to be a number > 0");
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
727 $amount = floatval($amount);
728 if (!$comment && !$comment_to)
729 return $this->query("sendtoaddress", $address, $amount);
730 if (!$comment_to)
731 return $this->query("sendtoaddress", $address, $amount, $comment);
732 return $this->query("sendtoaddress", $address, $amount, $comment, $comment_to);
733 }
734
735 /**
736 * Stop the Bitcoin server.
737 *
738 * @throws BitcoinClientException
739 */
740 public function stop() {
741 return $this->query("stop");
742 }
743
744 /**
745 * Check that $address looks like a proper Bitcoin address.
746 *
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
747 * @param string $address String to test for validity as a Bitcoin address
748 * @return array An array containing:
0f823eb @mikegogulski updates for 0.3.17, "object"->"array", better phpdoc. tests still lac…
authored Nov 28, 2010
749 * "isvalid" => true or false
750 * "ismine" => true if the address is in the server's wallet
751 * "address" => bitcoinaddress
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
752 * Note: ismine and address are only returned if the address is valid.
c5e5dcb @mikegogulski Implemented 21 API functions, related changes
authored Nov 27, 2010
753 * @throws BitcoinClientException
754 */
755 public function validateaddress($address) {
756 if (!$address || empty($address))
757 throw new BitcoinClientException("validateaddress requires a Bitcoin address");
758 return $this->query("validateaddress", $address);
759 }
760
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
761 /**
762 * Return information about a specific transaction.
763 *
764 * @param string $txid 64-digit hexadecimal transaction ID
765 * @return array An error array, or an array containing:
766 * "amount" => float Transaction amount
767 * "fee" => float Transaction fee
768 * "confirmations" => integer Network confirmations of this transaction
769 * "txid" => string The transaction ID
770 * "message" => string Transaction "comment" message
771 * "to" => string Transaction "to" message
772 * @throws BitcoinClientException
773 * @since 0.3.18
774 */
775 public function gettransaction($txid) {
776 if (!$txid || empty($txid) || strlen($txid) != 64 || !preg_match('/^[0-9a-fA-F]+$/', $txid))
777 throw new BitcoinClientException("gettransaction requires a valid hexadecimal transaction ID");
05b0628 @dereuromark fix
dereuromark authored Jul 19, 2011
778 return $this->query("gettransaction", $txid);
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
779 }
780
781 /**
782 * Move bitcoins between accounts.
783 *
784 * @param string $fromaccount
785 * Account to move from. If given as an empty string ("") or NULL, bitcoins will
786 * be moved from the wallet balance to the target account.
787 * @param string $toaccount
788 * Account to move to
789 * @param float $amount
790 * Amount to move
791 * @param integer $minconf
792 * Minimum number of confirmations on bitcoins being moved
793 * @param string $comment
794 * Transaction comment
795 * @throws BitcoinClientException
796 * @since 0.3.18
797 */
798 public function move($fromaccount = "", $toaccount, $amount, $minconf = 1, $comment = NULL) {
0096193 @sam2kb Fix move method. Allow moving coins to default account (empty string).
sam2kb authored Jun 1, 2013
799 if (!$fromaccount) $fromaccount = "";
800 if (!$toaccount) $toaccount = "";
801
802 if (!$amount || !is_numeric($amount) || $amount <= 0)
5d425e1 @mikegogulski 0.3.19; still need more unit tests
authored Jan 20, 2011
803 throw new BitcoinClientException("move requires a from account, to account and numeric amount > 0");
804 if (!is_numeric($minconf) || $minconf < 0)
805 throw new BitcoinClientException('move requires a numeric $minconf >= 0');
806 if (!$comment || empty($comment))
807 return $this->query("move", $fromaccount, $toaccount, $amount, $minconf);
808 return $this->query("move", $fromaccount, $toaccount, $amount, $minconf, $comment);
809 }
810
811 /**
812 * Send $amount from $account's balance to $toaddress. This method will fail
813 * if there is less than $amount bitcoins with $minconf confirmations in the
814 * account's balance (unless $account is the empty-string-named default
815 * account; it behaves like the sendtoaddress method). Returns transaction
816 * ID on success.
817 *
818 * @param string $account Account to send from
819 * @param string $toaddress Bitcoin address to send to
820 * @param float $amount Amount to send
821 * @param integer $minconf Minimum number of confirmations on bitcoins being sent
822 * @param string $comment
823 * @param string $comment_to
824 * @return string Hexadecimal transaction ID
825 * @throws BitcoinClientException
826 * @since 0.3.18
827 */
828 public function sendfrom($account, $toaddress, $amount, $minconf = 1, $comment = NULL, $comment_to = NULL) {
829 if (!$account || !$toaddress || empty($toaddress) || !$amount || !is_numeric($amount) || $amount <= 0)
830 throw new BitcoinClientException("sendfrom requires a from account, to account and numeric amount > 0");
831 if (!is_numeric($minconf) || $minconf < 0)
832 throw new BitcoinClientException('sendfrom requires a numeric $minconf >= 0');
833 if (!$comment && !$comment_to)
834 return $this->query("sendfrom", $account, $toaddress, $amount, $minconf);
835 if (!$comment_to)
836 return $this->query("sendfrom", $account, $toaddress, $amount, $minconf, $comment);
837 $this->query("sendfrom", $account, $toaddress, $amount, $minconf, $comment, $comment_to);
838 }
839
840 /**
841 * Return formatted hash data to work on, or try to solve specified block.
842 *
843 * If $data is provided, tries to solve the block and returns true if successful.
844 * If $data is not provided, returns formatted hash data to work on.
845 *
846 * @param string $data Block data
847 * @return mixed
848 * boolean TRUE if $data provided and block solving successful
849 * array otherwise, containing:
850 * "midstate" => string, precomputed hash state after hashing the first half of the data
851 * "data" => string, block data
852 * "hash1" => string, formatted hash buffer for second hash
853 * "target" => string, little endian hash target
854 * @throws BitcoinClientException
855 * @since 0.3.18
856 */
857 public function getwork($data = NULL) {
858 if (!$data)
859 return $this->query("getwork");
860 return $this->query("getwork", $data);
861 }
862
863 /**
864 * Return the current bitcoin address for receiving payments to $account.
865 * The account and address will be created if $account doesn't exist.
866 *
867 * @param string $account Account name
868 * @return string Bitcoin address for $account
869 * @throws BitcoinClientException
870 * @since 0.3.18
871 */
872 public function getaccountaddress($account) {
873 if (!$account || empty($account))
874 throw new BitcoinClientException("getaccountaddress requires an account");
875 return $this->query("getaccountaddress", $account);
876 }
877
878 /**
879 * Return a recent hashes per second performance measurement.
880 *
881 * @return integer Hashes per second
882 * @throws BitcoinClientException
883 */
884 public function gethashespersec() {
885 return $this->query("gethashespersec");
886 }
887
888 /**
889 * Returns the list of addresses associated with the given account.
890 *
891 * @param string $account
892 * @return array
893 * A simple array of Bitcoin addresses associated with $account, empty
894 * if the account doesn't exist.
895 * @throws BitcoinClientException
896 */
897 public function getaddressesbyaccount($account) {
898 if (!$account || empty($account))
899 throw new BitcoinClientException("getaddressesbyaccount requires an account");
900 return $this->query("getaddressesbyaccount", $account);
901 }
902
d3705db @psyvisions Update src/bitcoin.inc
psyvisions authored Feb 21, 2013
903 /**
182235f @sam2kb Add listtransactions and listaccounts methods
sam2kb authored Jun 1, 2013
904 * Returns the list of transactions associated with the given account.
905 *
c10cb07 @sam2kb Accept both empty string "" and wildcard "*" values in listtransactio…
sam2kb authored Jun 16, 2013
906 * @param string $account The account to get transactions from. Accepts empty string "" and wildcard "*" values
49702c1 @sam2kb Add a "from" param to "listtransactions" method
sam2kb authored Jun 16, 2013
907 * @param integer $count The number of transactions to return.
908 * @param integer $from The start number of transactions.
182235f @sam2kb Add listtransactions and listaccounts methods
sam2kb authored Jun 1, 2013
909 * @return array
910 * "account" => account of transaction
911 * "address" => address of transaction
912 * "category" => 'send' or 'recieve'
913 * "amount" => Amount sent/recieved
914 * "fee" => Only on sent transactions, transaction fee taken
915 * "confirmations" => Confirmations
916 * "txid" => Transaction ID
917 * "time" => Time of transaction
918 * * @throws BitcoinClientException
919 */
49702c1 @sam2kb Add a "from" param to "listtransactions" method
sam2kb authored Jun 16, 2013
920 public function listtransactions($account, $count = 10, $from = 0) {
c10cb07 @sam2kb Accept both empty string "" and wildcard "*" values in listtransactio…
sam2kb authored Jun 16, 2013
921 if (!$account) $account = "";
922
49702c1 @sam2kb Add a "from" param to "listtransactions" method
sam2kb authored Jun 16, 2013
923 if (!is_numeric($count) || $count < 0)
924 throw new BitcoinClientException('listtransactions requires a numeric count >= 0');
925 if (!is_numeric($from) || $from < 0)
926 throw new BitcoinClientException('listtransactions requires a numeric from >= 0');
927 return $this->query("listtransactions", $account, $count, $from);
d85ab9a @sam2kb minor / doc
sam2kb authored Jun 16, 2013
928 }
929
182235f @sam2kb Add listtransactions and listaccounts methods
sam2kb authored Jun 1, 2013
930 /**
931 * Returns the list of accounts.
d85ab9a @sam2kb minor / doc
sam2kb authored Jun 16, 2013
932 *
182235f @sam2kb Add listtransactions and listaccounts methods
sam2kb authored Jun 1, 2013
933 */
934 public function listaccounts($minconf = 1) {
935 return $this->query("listaccounts", $minconf);
936 }
937
938 /**
d3705db @psyvisions Update src/bitcoin.inc
psyvisions authored Feb 21, 2013
939 * Returns Transaction id (txid)
940 *
941 * @param string $fromAccount Account to send from
942 * @param array $sendTo Key=address Value=amount
943 * @param integer $minconf
944 * @param string $comment
945 * @return string Hexadecimal transaction ID on success.
946 * @throws BitcoinClientException
947 * @since 0.3.21
948 * @author codler<github>
949 */
950 public function sendmany($fromAccount, $sendTo, $minconf = 1, $comment=NULL) {
951 if (!$fromAccount || empty($fromAccount))
952 throw new BitcoinClientException("sendmany requires an account");
953 if (!is_numeric($minconf) || $minconf < 0)
954 throw new BitcoinClientException('sendmany requires a numeric minconf >= 0');
d85ab9a @sam2kb minor / doc
sam2kb authored Jun 16, 2013
955
d3705db @psyvisions Update src/bitcoin.inc
psyvisions authored Feb 21, 2013
956 if (!$comment)
957 return $this->query("sendmany", $fromAccount, $sendTo, $minconf);
958 return $this->query("sendmany", $fromAccount, $sendTo, $minconf, $comment);
959 }
960
182235f @sam2kb Add listtransactions and listaccounts methods
sam2kb authored Jun 1, 2013
961 }
Something went wrong with that request. Please try again.