-
Notifications
You must be signed in to change notification settings - Fork 71
/
bc_daemon.php
178 lines (131 loc) · 5.14 KB
/
bc_daemon.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
<?php
/******************************************************************************
Wallet Configuration
******************************************************************************/
$GLOBALS["wallet_ip"] = "127.0.0.1";
$GLOBALS["wallet_port"] = "8332";
$GLOBALS["wallet_user"] = "usrename";
$GLOBALS["wallet_pass"] = "password";
/******************************************************************************
Block Chain And Network Information
These functions return general information about
the block chain, the wallet itself, and the network
the wallet/node is attached to.
******************************************************************************/
function getblock ($block_hash)
{
// The JSON-RPC request starts with a method name
$request_array["method"] = "getblock";
// For getblock a block hash is required
$request_array["params"][0] = $block_hash;
// Send the request to the wallet
$info = wallet_fetch ($request_array);
// This function returns an array containing the block
// data for the specified block hash
return ($info);
}
function getblockhash ($block_index)
{
// The JSON-RPC request starts with a method name
$request_array["method"] = "getblockhash";
// For getblockhash a block index is required
$request_array["params"][0] = $block_index;
// Send the request to the wallet
$info = wallet_fetch ($request_array);
// This function returns a string containing the block
// hash value for the specified block in the chain
return ($info);
}
function getinfo ()
{
// The JSON-RPC request starts with a method name
$request_array["method"] = "getinfo";
// getinfo has no parameters
// Send the request to the wallet
$info = wallet_fetch ($request_array);
// This function returns an array containing information
// about the wallet's network and block chain
return ($info);
}
function getnetworkhashps ($block_index=NULL)
{
// The JSON-RPC request starts with a method name
$request_array["method"] = "getnetworkhashps";
// block index is an optional parameter. If no block
// index is specified you get the network hashrate for
// the latest block
if (isset ($block_index))
{
$request_array["params"][0] = $block_index;
}
// Send the request to the wallet
$info = wallet_fetch ($request_array);
// This function returns a string containing the calculated
// network hash rate for the latest block
return ($info);
}
function getrawtransaction ($tx_id, $verbose=1)
{
// The JSON-RPC request starts with a method name
$request_array["method"] = "getrawtransaction";
// For getrawtransaction a txid is required
$request_array["params"][0] = $tx_id;
$request_array["params"][1] = $verbose;
// Send the request to the wallet
$info = wallet_fetch ($request_array);
// This function returns a string containing the block
// hash value for the specified block in the chain
return ($info);
}
/******************************************************************************
JSON-RPC Fetch
This function is used to request information form the daemon.
******************************************************************************/
function wallet_fetch ($request_array)
{
// Encode the request as JSON for the wallet
$request = json_encode ($request_array);
// Create curl connection object
$coind = curl_init();
// Set the IP address and port for the wallet server
curl_setopt ($coind, CURLOPT_URL, $GLOBALS["wallet_ip"]);
curl_setopt ($coind, CURLOPT_PORT, $GLOBALS["wallet_port"]);
// Tell curl to use basic HTTP authentication
curl_setopt($coind, CURLOPT_HTTPAUTH, CURLAUTH_BASIC) ;
// Provide the username and password for the connection
curl_setopt($coind, CURLOPT_USERPWD, $GLOBALS["wallet_user"].":".$GLOBALS["wallet_pass"]);
// JSON-RPC Header for the wallet
curl_setopt($coind, CURLOPT_HTTPHEADER, array ("Content-type: application/json"));
// Prepare curl for a POST request
curl_setopt($coind, CURLOPT_POST, TRUE);
// Provide the JSON data for the request
curl_setopt($coind, CURLOPT_POSTFIELDS, $request);
// Indicate we want the response as a string
curl_setopt($coind, CURLOPT_RETURNTRANSFER, TRUE);
// Required by RPCSSL self-signed cert
curl_setopt($coind, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($coind, CURLOPT_SSL_VERIFYHOST, FALSE);
// execute the request
$response_data = curl_exec($coind);
// Close the connection
curl_close($coind);
// The JSON response is read into an array
$info = json_decode ($response_data, TRUE);
// If an error message was received the message is returned
// to the calling code as a string.
if (isset ($info["error"]) || $info["error"] != "")
{
return $info["error"]["message"]."(Error Code: ".$info["error"]["code"].")";
}
// If there was no error the result is returned to the calling code
else
{
return $info["result"];
}
}
/******************************************************************************
This script is Copyright � 2013 Jake Paysnoe.
I hereby release this script into the public domain.
Jake Paysnoe Jun 26, 2013
******************************************************************************/
?>