-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.phpcoinlib.php
More file actions
1298 lines (1071 loc) · 40.4 KB
/
Copy pathclass.phpcoinlib.php
File metadata and controls
1298 lines (1071 loc) · 40.4 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* PHPcoinlib - A class for convenient interfacing with Bitcoin and other cryptocurrency daemons.
*
* @author Eli Lahr <hifieli2@gmail.com>
* @version 1.1 public
* @created Dec 11 2013
*
*/
class PHPcoinlib {
public $conf = array(
// Essentials
'rpchost' => '127.0.0.1', // IP or hostname where your bitcoind is installed and listening. use 127.0.0.1 or localhost if applicable. See 'rpcallowip=' in bitcoind.conf
'rpcport' => 8332, // port number that is listening for connections. See 'rpcport=' in bitcoind.conf.
'rpcuser' => '', // See 'rpcuser=' in bitcoind.conf.
'rpcpass' => '', // See 'rpcpassword=' in bitcoind.conf.
// Options
'rcptimeout' => 6, // How long to wait for a response.
'rpcssl' => false, // See the following in bitcoind.conf: -rpcssl -rpcsslcertificatechainfile=<file.cert> -rpcsslprivatekeyfile=<file.pem>
'stop_on_http_err' => false, // Wether or not to stop on HTTP statuses other than 200. Not really recommended, but can be useful for some debugging situations.
'throw_exceptions' => true, // true = Throw Exceptions for errors; false = return strings on errors
'curreny_name' => 'Bitcoin' // The name of the cryptocurrency network we are connecting to. This is esoteric (a label), and has no impact on functionality.
'curreny_symbol' => 'BTC' // The symbol of the cryptocurrency network we are connecting to. This is esoteric (a label), and has no impact on functionality.
// Security
'rpcbannedcommands' => array( // Commands that are to be ignored!
'sendfrom',
'sendmany',
'sendtoaddress',
'setgenerate',
'stop',
)
);
// Class Variables
public $rpcid = 1;
public $response = null;
public $response_json = '';
public $response_raw = null;
private $project_name = 'PHPcoinlib';
private $project_version = '0.0.1a';
public function __construct($user = null, $password = null, $host = null, $port = null, $rpcssl = false, $tout = null) {
$this->setConnection($user, $password, $host, $port, $rpcssl, $tout);
}
public function setConnection($user = '', $password = '', $host = '127.0.0.1', $port = 8332, $rpcssl = false, $tout = 6) {
$this->setConf('rpcuser', $user);
$this->setConf('rpcpass', $password);
$this->setConf('rpchost', $host);
$this->setConf('rpcport', $port);
$this->setConf('rpcssl', $rpcssl);
$this->setConf('rcptimeout', $tout);
}
public function setCurrency($curreny_name = 'Bitcoin', $curreny_symbol = 'BTC') {
$this->setConf('curreny_name', $curreny_name);
$this->setConf('curreny_symbol', $curreny_symbol);
}
public function setConf($key, $value = null) {
if (isset($this->conf[$key])) {
$this->conf[$key] = $value;
return true;
} else {
return false;
}
}
public function getConf($key) {
if (isset($this->conf[$key])) {
return $this->conf[$key];
} else {
return null;
}
}
///////////////////////////////////////////////////////////////////////////
// Coin Methods
///////////////////////////////////////////////////////////////////////////
// addmultisigaddress <nrequired> <'["key","key"]'> [account]
// Add a nrequired-to-sign multisignature address to the wallet"
// each key is a Bitcoin address or hex-encoded public key
// If [account] is specified, assign address to [account].
public function AddMultiSigAddress($nrequired, $keys, $account = '') {
$keys = (!is_string($keys)) ? json_encode($keys) : $keys;
$this->RawRequest('addmultisigaddress', array($nrequired, $keys, $account));
return $this->response;
}
// addnode <node> <add|remove|onetry>
// Attempts add or remove <node> from the addnode list or try a connection to <node> once.
public function AddNode($node, $aro = 'add') {
$this->RawRequest('addnode', array($node, $aro));
return $this->response;
}
// backupwallet <destination>
// Safely copies wallet.dat to destination, which can be a directory or a path with filename.
public function BackupWallet($destination) {
$this->RawRequest('backupwallet', array($destination));
return $this->response;
}
// createmultisig <nrequired> <'["key","key"]'>
// Creates a multi-signature address and returns a json object
// with keys:
// address : bitcoin address
// redeemScript : hex-encoded redemption script
public function CreateMultiSig($nrequired, $keys) {
$keys = (!is_string($keys)) ? json_encode($keys) : $keys;
$this->RawRequest('createmultisig', array($nrequired, $keys));
return $this->response;
}
// createrawtransaction [{"txid":txid,"vout":n},...] {address:amount,...}
// Create a transaction spending given inputs
// (array of objects containing transaction id and output number),
// sending to given address(es).
// Returns hex-encoded raw transaction.
// Note that the transaction's inputs are not signed, and
// it is not stored in the wallet or transmitted to the network.
public function CreateRawTransaction($inputs, $addr_amount) {
$inputs = (!is_string($inputs)) ? json_encode($inputs) : $inputs;
$addr_amount = (!is_string($addr_amount)) ? json_encode($addr_amount) : $addr_amount;
$this->RawRequest('createrawtransaction', array($inputs, $addr_amount));
return $this->response;
}
// decoderawtransaction <hex string>
// Return a JSON object representing the serialized, hex-encoded transaction.
public function DecodeRawTransaction($hex) {
$this->RawRequest('decoderawtransaction', array($hex));
return $this->response;
}
// decodescript <hex string>
// Decode a hex-encoded script.
public function DecodeScript($hex) {
$this->RawRequest('decodescript', array($hex));
return $this->response;
}
// dumpprivkey <bitcoinaddress>
// Reveals the private key corresponding to <bitcoinaddress>.
public function DumpPrivKey($bitcoinaddress) {
$this->RawRequest('dumpprivkey', array($bitcoinaddress));
return $this->response;
}
// dumpwallet <filename>
// Dumps all wallet keys in a human-readable format.
public function DumpWallet($filename) {
$this->RawRequest('dumpwallet', array($filename));
return $this->response;
}
// encryptwallet <passphrase>
// Encrypts the wallet with <passphrase>.
public function EncryptWallet($passphrase) {
$this->RawRequest('encryptwallet', array($passphrase));
return $this->response;
}
// getaccount <bitcoinaddress>
// Returns the account associated with the given address.
public function GetAccount($bitcoinaddress) {
$this->RawRequest('getaccount', array($bitcoinaddress));
return $this->response;
}
// getaccountaddress <account>
// Returns the current Bitcoin address for receiving payments to this account.
public function GetAccountAddress($account) {
$this->RawRequest('getaccountaddress', array($account));
return $this->response;
}
// getaddednodeinfo
// Returns information about the given added node, or all added nodes
// (note that onetry addnodes are not listed here)
// If dns is false, only a list of added nodes will be provided,
// otherwise connected information will also be available.
public function GetAddedNodeInfo() {
$this->RawRequest('getaddednodeinfo');
return $this->response;
}
// getaddressesbyaccount <account>
// Returns the list of addresses for the given account.
public function GetAddressesByAccount($account) {
$this->RawRequest('getaddressesbyaccount', array($account));
return $this->response;
}
// getbalance [account] [minconf=1]
// If [account] is not specified, returns the server's total available balance.
// If [account] is specified, returns the balance in the account.
public function GetBalance($account = null, $minconf = null) {
if (empty($account) && empty($minconf)) {
$this->RawRequest('getbalance');
} else {
if (!empty($account) && !empty($minconf)) {
$this->RawRequest('getbalance', array($account, $minconf));
} else {
if (!empty($account) && empty($minconf)) {
$this->RawRequest('getbalance', array($account, 1));
} else {
$this->RawRequest('getbalance', array('', $minconf));
}
}
}
return $this->response;
}
// getbestblockhash
// Returns the hash of the best (tip) block in the longest block chain.
public function GetBestBlockHash() {
$this->RawRequest('getbestblockhash');
return $this->response;
}
// getblock <hash> [verbose=true]
// If verbose is false, returns a string that is serialized, hex-encoded data for block <hash>.
// If verbose is true, returns an Object with information about block <hash>.
public function GetBlock($hash, $verbose = true) {
$this->RawRequest('getblock', array($hash, $verbose));
return $this->response;
}
// getblockcount
// Returns the number of blocks in the longest block chain.
public function GetBlockCount() {
$this->RawRequest('getblockcount');
return $this->response;
}
// getblockhash <index>
// Returns hash of block in best-block-chain at <index>.
public function GetBlockHash($index) {
$this->RawRequest('getblockhash', array($index));
return $this->response;
}
// getblocktemplate [params]
// Returns data needed to construct a block to work on:
// "version" : block version
// "previousblockhash" : hash of current highest block
// "transactions" : contents of non-coinbase transactions that should be included in the next block
// "coinbaseaux" : data that should be included in coinbase
// "coinbasevalue" : maximum allowable input to coinbase transaction, including the generation award and transaction fees
// "target" : hash target
// "mintime" : minimum timestamp appropriate for next block
// "curtime" : current timestamp
// "mutable" : list of ways the block template may be changed
// "noncerange" : range of valid nonces
// "sigoplimit" : limit of sigops in blocks
// "sizelimit" : limit of block size
// "bits" : compressed target of next block
// "height" : height of the next block
// See https://en.bitcoin.it/wiki/BIP_0022 for full specification.
public function GetBlockTemplate($params) {
$params = (!is_string($params)) ? json_encode($params) : $params;
$this->RawRequest('getblocktemplate', array($params));
return $this->response;
}
// getconnectioncount
// Returns the number of connections to other nodes.
public function GetConnectionCount() {
$this->RawRequest('getconnectioncount');
return $this->response;
}
// getdifficulty
// Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
public function GetDifficulty() {
$this->RawRequest('getdifficulty');
return $this->response;
}
public function GetDiff() {
//not to be confused with GetDifficulty
$this->RawRequest('getdifficulty');
//return $this->response;
$MiningInfo = $this->response;
if (is_array($MiningInfo)) {
return $MiningInfo['proof-of-work'];
} else {
return $MiningInfo;
}
}
// getgenerate
// Returns true or false.
public function GetGenerate() {
$this->RawRequest('getgenerate');
return $this->response;
}
// gethashespersec
// Returns a recent hashes per second performance measurement while generating.
/**
*
*
*
* @return <type> Return_Description
*/
public function GetHashesPerSec() {
$this->RawRequest('gethashespersec');
return $this->response;
}
// getinfo
// Returns an object containing various state info.
public function GetInfo() {
$this->RawRequest('getinfo');
return $this->response;
}
// getmininginfo
// Returns an object containing mining-related information.
public function GetMiningInfo() {
$this->RawRequest('getmininginfo');
return $this->response;
}
// getnewaddress [account]
// Returns a new Bitcoin address for receiving payments. If [account] is specified (recommended), it is added to the address book so payments received with the address will be credited to [account].
public function GetNewAddress($account = '') {
$this->RawRequest('getnewaddress', array($account));
return $this->response;
}
public function GetNewDepositAddress() {
return $this->GetNewAddress("");
}
// getpeerinfo
// Returns data about each connected network node.
public function GetPeerInfo() {
$this->RawRequest('getpeerinfo');
return $this->response;
}
// getrawchangeaddress
// Returns a new Bitcoin address, for receiving change. This is for use with raw transactions, NOT normal use.
public function GetRawChangeAddress() {
$this->RawRequest('getrawchangeaddress');
return $this->response;
}
// getrawmempool
// Returns all transaction ids in memory pool.
public function GetRawMemPool() {
$this->RawRequest('getrawmempool');
return $this->response;
}
// getrawtransaction <txid> [verbose=0]
// If verbose=0, returns a string that is
// serialized, hex-encoded data for <txid>.
// If verbose is non-zero, returns an Object
// with information about <txid>.
public function GetRawTransaction($txid, $verbose = 0) {
$this->RawRequest('getrawtransaction', array($txid, $verbose));
return $this->response;
}
// getreceivedbyaccount <account> [minconf=1]
// Returns the total amount received by addresses with <account> in transactions with at least [minconf] confirmations.
public function GetReceivedByAccount($account, $minconf = 1) {
$this->RawRequest('getreceivedbyaccount', array($account, $minconf));
return $this->response;
}
// getreceivedbyaddress <bitcoinaddress> [minconf=1]
// Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations.
public function GetReceivedByAddress($bitcoinaddress, $minconf = 1) {
$this->RawRequest('getreceivedbyaddress', array($bitcoinaddress, $minconf));
return $this->response;
}
// gettransaction <txid>
// Get detailed information about in-wallet transaction <txid>
public function GetTransaction($txid) {
$this->RawRequest('gettransaction', array($txid));
return $this->response;
}
// gettxout <txid> <n> [includemempool=true]
// Returns details about an unspent transaction output.
public function GetTxOut($txid, $n, $includemempool = true) {
$this->RawRequest('gettxout', array($txid, $verbose, $includemempool));
return $this->response;
}
// gettxoutsetinfo
// Returns statistics about the unspent transaction output set.
public function gettxoutsetinfo() {
$this->RawRequest('gettxoutsetinfo');
return $this->response;
}
// getwork [data]
// If [data] is not specified, returns formatted hash data to work on:
// "midstate" : precomputed hash state after hashing the first half of the data (DEPRECATED)
// "data" : block data
// "hash1" : formatted hash buffer for second hash (DEPRECATED)
// "target" : little endian hash target
// If [data] is specified, tries to solve the block and returns true if it was successful.
public function GetWork($data = '') {
if (empty($data)) {
$this->RawRequest('getwork');
} else {
$this->RawRequest('getwork', array($data));
}
return $this->response;
}
// help [command]
// List commands, or get help for a command.
public function Help($command = '') {
if (empty($command)) {
$this->RawRequest('help');
} else {
$this->RawRequest('help', array($command));
}
return $this->response;
}
// importprivkey <bitcoinprivkey> [label] [rescan=true]
// Adds a private key (as returned by dumpprivkey) to your wallet.
public function ImportPrivKey($bitcoinprivkey, $label = '', $rescan = true) {
$this->RawRequest('importprivkey', array($bitcoinprivkey, $label, $rescan));
return $this->response;
}
// importwallet <filename>
// Imports keys from a wallet dump file (see dumpwallet).
public function ImportWallet($filename) {
$this->RawRequest('importwallet', array($filename));
return $this->response;
}
// keypoolrefill [new-size]
// Fills the keypool.
public function KeypoolRefill($new_size = 0) {
if (empty($new_size)) {
$this->RawRequest('keypoolrefill');
} else {
$this->RawRequest('keypoolrefill', array($new_size));
}
return $this->response;
}
// listaccounts [minconf=1]
// Returns Object that has account names as keys, account balances as values.
public function ListAccounts($minconf = 1) {
$this->RawRequest('listaccounts', array($minconf));
return $this->response;
}
// listaddressgroupings
// Lists groups of addresses which have had their common ownership
// made public by common use as inputs or as the resulting change
// in past transactions
public function ListAddressGroupings() {
$this->RawRequest('listaddressgroupings');
return $this->response;
}
// listlockunspent
// Returns list of temporarily unspendable outputs.
public function ListLockUnspent() {
$this->RawRequest('listlockunspent');
return $this->response;
}
/**
* Fetches a list of the total amount of coins received by account.
*
* listreceivedbyaccount [minconf=1] [includeempty=false]
* [minconf] is the minimum number of confirmations before payments are included.
* [includeempty] whether to include accounts that haven't received any payments.
* Returns an array of objects containing:
* "account" : the account of the receiving addresses
* "amount" : total amount received by addresses with this account
* "confirmations" : number of confirmations of the most recent transaction included
* @param <type> $minconf
* @param <type> $includeempty
*
* @return <type>
*/
public function ListReceivedByAccount($minconf = 1, $includeempty = false) {
$this->RawRequest('listreceivedbyaccount', array($minconf, $includeempty));
return $this->response;
}
/**
* Lists the total amount of coins received by address.
* listreceivedbyaddress [minconf=1] [includeempty=false]
* [minconf] is the minimum number of confirmations before payments are included.
* [includeempty] whether to include addresses that haven't received any payments.
* Returns an array of objects containing:
* "address" : receiving address
* "account" : the account of the receiving address
* "amount" : total amount received by the address
* "confirmations" : number of confirmations of the most recent transaction included
* "txids" : list of transactions with outputs to the address
*
* @param <int> $minconf
* @param <bool> $includeempty
*
* @return <array>
*/
public function ListReceivedByAddress($minconf = 1, $includeempty = false) {
$this->RawRequest('listreceivedbyaddress', array($minconf, $includeempty));
return $this->response;
}
/**
* Get all wallet transactions in blocks since block [blockhash], or all wallet transactions if omitted
*
* @param <string> $blockhash
* @param <int> $target_confirmations
*
* @return <array>
*/
public function ListSinceBlock($blockhash = '', $target_confirmations = 6) {
if (empty($blockhash)) {
$this->RawRequest('listsinceblock', array());
} else {
$this->RawRequest('listsinceblock', array($blockhash, $target_confirmations));
}
return $this->response;
}
/**
* Returns up to [count] most recent transactions skipping the first [from] transactions for account [account].
*
* @param <string> $account
* @param <int> $count
* @param <int> $from
*
* @return <array>
*/
public function ListTransactions($account = '', $count = 10, $from = 0) {
$this->RawRequest('listtransactions', array($account, (int)$count, (int)$from));
return $this->response;
}
/**
* listunspent [minconf=1] [maxconf=9999999] ["address",...]
* Returns array of unspent transaction outputs
* with between minconf and maxconf (inclusive) confirmations.
* Optionally filtered to only include txouts paid to specified addresses.
* Results are an array of Objects, each of which has:
* {txid, vout, scriptPubKey, amount, confirmations}
*
* @param <int> $minconf
* @param <int> $maxconf
* @param <string/array> $addrs
*
* @return <array>
*/
public function listunspent($minconf = 1, $maxconf = 9999999, $addrs = null) {
if (empty($addrs)) {
$this->RawRequest('listunspent', array((int)$minconf, (int)$maxconf));
} else {
$addrs = (!is_string($addrs)) ? json_encode($addrs) : $addrs;
$this->RawRequest('listunspent', array((int)$minconf, (int)$maxconf, $addrs));
}
return $this->response;
}
/**
* Updates list of temporarily unspendable outputs.
*
* @param <bool> $unlock
* @param <type> $objs [array-of-Objects]
*
* @return <type>
*/
public function LockUnspent($unlock = true, $objs) {
$objs = (!is_string($objs)) ? json_encode($objs) : $objs;
$this->RawRequest('lockunspent', array((bool)$unlock, $objs));
return $this->response;
}
/**
* Move coins from one account in your wallet to another.
*
* @param <string> $fromaccount
* @param <string> $toaccount
* @param <float> $amount
* @param <int> $minconf
* @param <string> $comment
*
* @return <type>
*/
public function Move($fromaccount, $toaccount, $amount, $minconf = 1, $comment = '') {
$this->RawRequest('move', array($fromaccount, $toaccount, (float)$amount, (int)$minconf, $comment));
return $this->response;
}
/**
* Send coins to an address from a specified account.
*
* @param <string> $fromaccount
* @param <string> $tobitcoinaddress
* @param <float> $amount real and is rounded to the nearest 0.00000001
* @param <int> $minconf
* @param <string> $comment
* @param <string> $comment_to
*
* @return <type>
*/
public function SendFrom($fromaccount, $tobitcoinaddress, $amount, $minconf = 1, $comment = '', $comment_to = '') {
$this->RawRequest('sendfrom', array($fromaccount, $tobitcoinaddress, (float)$amount, (int)$minconf, $comment, $comment_to));
return $this->response;
}
/**
* Send coins to many addresses in a single transaction.
*
* @param <string> $fromaccount an account (not an address)
* @param <string/array> $addr_amount if string, '{"address":amount,...}'
* @param <int> $minconf minimum confirmations
* @param <string> $comment transaction comment
*
* @return <type>
*/
public function SendMany($fromaccount, $addr_amount, $minconf = 1, $comment = '') {
$addr_amount = (!is_string($addr_amount)) ? json_encode($addr_amount) : $addr_amount;
$this->RawRequest('sendmany', array($fromaccount, $addr_amount, (int)$minconf, $comment));
return $this->response;
}
/**
* Submits raw transaction (serialized, hex-encoded) to local node and network.
*
* @param <string> $hex
*
* @return <type>
*/
public function SendRawTransaction($hex) {
$this->RawRequest('sendrawtransaction', array($hex));
return $this->response;
}
/**
* Send coins to an address. Coins are sent from the "" account.
*
* @param <string> $bitcoinaddress
* @param <string> $amount real and is rounded to the nearest 0.00000001
* @param <string> $comment
* @param <string> $comment_to
*
* @return <type>
*/
public function SendToAddress($bitcoinaddress, $amount, $comment = '', $comment_to = '') {
$this->RawRequest('sendtoaddress', array($bitcoinaddress, (float)$amount, $comment, $comment_to));
return $this->response;
}
/**
* Sets the account associated with the given address.
*
* @param <string> $bitcoinaddress
* @param <string> $account
*
* @return <type>
*/
public function SetAccount($bitcoinaddress, $account) {
$this->RawRequest('setaccount', array($bitcoinaddress, $account));
return $this->response;
}
/**
* Enable or disable CPU mining.
*
* @param <bool> $generate true or false to turn generation on or off.
* @param <int> $genproclimit Generation is limited to $genproclimit processors, -1 is unlimited
*
* @return <type>
*/
public function SetGenerate($generate = true, $genproclimit = -1) {
$this->RawRequest('setgenerate', array((bool)$generate, (int)$genproclimit));
return $this->response;
}
/**
* Set the default transaction fee amount you will pay per transactions, in btc/kb
*
* @param <float> $amount a real and is rounded to the nearest 0.00000001 btc per kb
*
* @return <type>
*/
public function SetTxFee($amount) {
$this->RawRequest('settxfee', array((float)$amount));
return $this->response;
}
/**
* Sign a message with the private key of an address
*
* @param <string> $bitcoinaddress
* @param <string> $message
*
* @return <type>
*/
public function SignMessage($bitcoinaddress, $message) {
$this->RawRequest('signmessage', array($bitcoinaddress, $message));
return $this->response;
}
/**
* signrawtransaction <hex string> [{"txid":txid,"vout":n,"scriptPubKey":hex,"redeemScript":hex},...] [<privatekey1>,...] [sighashtype="ALL"]
* Sign inputs for raw transaction (serialized, hex-encoded).
* Second optional argument (may be null) is an array of previous transaction outputs that
* this transaction depends on but may not yet be in the block chain.
* Third optional argument (may be null) is an array of base58-encoded private
* keys that, if given, will be the only keys used to sign the transaction.
* Fourth optional argument is a string that is one of six values; ALL, NONE, SINGLE or
* ALL|ANYONECANPAY, NONE|ANYONECANPAY, SINGLE|ANYONECANPAY.
* Returns json object with keys:
* hex : raw transaction with signature(s) (hex-encoded string)
* complete : 1 if transaction has a complete set of signature (0 if not)
*
* @param <string/array> $hex hex string>
* @param <string/array> $previous [{"txid":txid,"vout":n,"scriptPubKey":hex,"redeemScript":hex},...]
* @param <string/array> $privatekeys [<privatekey1>,...]
* @param <string> $sighashtype [sighashtype="ALL"]
*
* @return <array>
*/
public function SignRawTransaction($hex, $previous = null, $privatekeys = null, $sighashtype = null) {
$params = array();
if (!empty($previous)) {
$previous = (is_array($previous)) ? json_encode($previous): $previous;
array_push($params, $previous);
}
if (!empty($privatekeys)) {
$privatekeys = (is_array($privatekeys)) ? json_encode($privatekeys): $privatekeys;
array_push($params, $privatekeys);
}
if (!empty($sighashtype)) { //ALL, NONE, SINGLE, ALL|ANYONECANPAY, NONE|ANYONECANPAY, SINGLE|ANYONECANPAY
array_push($params, $sighashtype);
}
$this->RawRequest('signrawtransaction', $params);
return $this->response;
}
/**
* Stop Bitcoin server.
*
* @return <type>
*/
public function Stop() {
$this->RawRequest('stop');
return $this->response;
}
/**
* Attempts to submit new block to network.
* [optional-params-obj] parameter is currently ignored.
* See https://en.bitcoin.it/wiki/BIP_0022 for full specification.
*
* @param <string> $data
* @param <null> $optional
*
* @return <type>
*/
public function SubmitBlock($data, $optional = null) {
$this->RawRequest('submitblock', array($data));
return $this->response;
}
/**
* Return information about <bitcoinaddress>.
*
* @param <string> $bitcoinaddress
*
* @return <type>
*/
public function ValidateAddress($bitcoinaddress) {
$this->RawRequest('validateaddress', array($bitcoinaddress));
return $this->response['isvalid'];
}
/**
* Verifies blockchain database.
*
* @param <int> $level
* @param <int> $blocks
*
* @return <type>
*/
public function VerifyChain($level = 1, $blocks = 1) {
$this->RawRequest('verifychain', array((int)$level, (int)$blocks));
return $this->response;
}
/**
* Verify a signed message
*
* @param <string> $bitcoinaddress
* @param <string> $signature
* @param <string> $message
*
* @return <type>
*/
public function VerifyMessage($bitcoinaddress, $signature, $message) {
$this->RawRequest('verifymessage', array($bitcoinaddress, $signature, $message));
return $this->response;
}
///////////////////////////////////////////////////////////////////////////
// EZCOIN Request methods
///////////////////////////////////////////////////////////////////////////
/**
* Allows you bypass $this->RawRequest('UndocumentedMethod') and instead do $this->UndocumentedMethod().
* This method essentially is the 'lite' version of all the methods above :)
*
* @param <type> $name Parameter_Description
* @param <type> $arguments Parameter_Description
*
* @return <mixed> A valid PHP variable reprentation of the JSON-RPC response.
*/
public function __call($name, $arguments) {
$this->_request(strtolower($name), $arguments);
return $this->response;
}
/**
* This method can be used to pass any request to a bitcoin daemon. Particularly useful for altcoins, such as primecoin, which uses 'getprimespersec' rather than 'gethashespersec'
*
* @param <string> $method The JSON-RPC method
* @param <array> $params The JSON-RPC parameter array
*
* @return <mixed> A valid PHP variable reprentation of the JSON-RPC response.
*/
public function RawRequest($method = 'getinfo', $params = array()) {
$this->_request($method, $params);
}
/**
* Coordinates the communication between this script and the outside world via JSON-RPC.
*
* @param <string> $method The JSON-RPC method
* @param <array> $params The JSON-RPC parameter array
*
* @return <null>
*/
private function _request($method = 'getinfo', $params = array()) {
$RawReturn = $this->__json_rpc_call($method, $params);
$this->response = $RawReturn;
$this->response_json = @json_encode($RawReturn);
}
///////////////////////////////////////////////////////////////////////////
// JSON-RPC methods
///////////////////////////////////////////////////////////////////////////
/**
* Coordinates the preperation, connection, sending and recieving, and processing of the JSON-RPC call.
*
* @param <string> $method The JSON-RPC method
* @param <array> $params The JSON-RPC parameter array
*
* @return <mixed> string, int, float, or most likely, an array. A valid PHP variable reprentation of the JSON-RPC response.
*/
private function __json_rpc_call($method, $params = array()) {
try {
// Make sure we are allowed to make this request before we do anything else.
if (in_array(strtolower($method), $this->conf['rpcbannedcommands'])) {
throw new Exception(strtolower($method), 5);
}
// Set the rpcid to a new random-ish number. This helps significantly reduce the likelihood of recieving the wrong response in a high-traffic situation.
$this->rpcid = rand(1, 4096);
// Initialize the response as null.
$this->response_raw = null;
// Create a stream resource for communicating with bitcoind via JSON-RPC
$stream = $this->__json_rpc_connect();
// Send the HTTP request and collect the HTTP response.
$this->response_raw = $this->__json_rpc_request($stream, $method, $params);
// Process the HTTP response.
$response = $this->__json_rpc_process_response($this->response_raw);
// Return the processed response.
return $response;
} catch (Exception $ex) {
//echo $ex->getMessage();
if ($this->conf['throw_exceptions']) {
throw new Exception($this->__json_rpc_err2str($ex->getCode()) . ' ' . $ex->getMessage(), $ex->getCode());