1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AraneaDev\Electrum; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Electrum. |
10
|
|
|
*/ |
11
|
|
|
class Electrum |
12
|
|
|
{ |
13
|
|
|
/** @var Client */ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Electrum constructor. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$host = config('electrum.host', 'http://127.0.0.1'); |
22
|
|
|
$port = config('electrum.port', 7777); |
23
|
|
|
|
24
|
|
|
$this->client = new Client([ |
25
|
|
|
'base_uri' => $host.':'.$port, |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the Electrum version. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getVersion() |
35
|
|
|
{ |
36
|
|
|
return $this->sendRequest('version'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the synchronized status. |
41
|
|
|
* |
42
|
|
|
* @return object |
43
|
|
|
*/ |
44
|
|
|
public function isSynchronized() |
45
|
|
|
{ |
46
|
|
|
return $this->sendRequest('is_synchronized'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the total balance. |
51
|
|
|
* |
52
|
|
|
* @return object |
53
|
|
|
*/ |
54
|
|
|
public function getBalance() |
55
|
|
|
{ |
56
|
|
|
return $this->sendRequest('getbalance'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get balance for address. |
61
|
|
|
* |
62
|
|
|
* @param $address |
63
|
|
|
* |
64
|
|
|
* @return object |
65
|
|
|
*/ |
66
|
|
|
public function getAddressBalance($address) |
67
|
|
|
{ |
68
|
|
|
return $this->sendRequest('getaddressbalance', ['address' => $address]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get history for address. |
73
|
|
|
* |
74
|
|
|
* @param $address |
75
|
|
|
* |
76
|
|
|
* @return object |
77
|
|
|
*/ |
78
|
|
|
public function getAddressHistory($address) |
79
|
|
|
{ |
80
|
|
|
return $this->sendRequest('getaddresshistory', ['address' => $address]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get unspent for address. |
85
|
|
|
* |
86
|
|
|
* @param $address |
87
|
|
|
* |
88
|
|
|
* @return object |
89
|
|
|
*/ |
90
|
|
|
public function getAddressUnspent($address) |
91
|
|
|
{ |
92
|
|
|
return $this->sendRequest('getaddressunspent', ['address' => $address]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check whether address is in wallet. |
97
|
|
|
* |
98
|
|
|
* @param $address |
99
|
|
|
* |
100
|
|
|
* @return object |
101
|
|
|
*/ |
102
|
|
|
public function isAddressMine($address) |
103
|
|
|
{ |
104
|
|
|
return $this->sendRequest('ismine', ['address' => $address]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get history of wallet. |
109
|
|
|
* |
110
|
|
|
* @return object |
111
|
|
|
*/ |
112
|
|
|
public function getHistory() |
113
|
|
|
{ |
114
|
|
|
return $this->sendRequest('history'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Create a new payment request. |
119
|
|
|
* |
120
|
|
|
* @param float $amount |
121
|
|
|
* @param string $memo |
122
|
|
|
* @param int $expiration |
123
|
|
|
* |
124
|
|
|
* @return object |
125
|
|
|
*/ |
126
|
|
|
public function createRequest($amount = 0.00, $memo = '', $expiration = 3600) |
127
|
|
|
{ |
128
|
|
|
return $this->sendRequest('addrequest', [ |
129
|
|
|
'amount' => $amount, |
130
|
|
|
'memo' => $memo, |
131
|
|
|
'expiration' => $expiration, |
132
|
|
|
]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get all payment requests. |
137
|
|
|
* |
138
|
|
|
* @return object |
139
|
|
|
*/ |
140
|
|
|
public function getRequests() |
141
|
|
|
{ |
142
|
|
|
return $this->sendRequest('listrequests'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get a payment request by address. |
147
|
|
|
* |
148
|
|
|
* @param $address |
149
|
|
|
* |
150
|
|
|
* @return object |
151
|
|
|
*/ |
152
|
|
|
public function getRequest($address) |
153
|
|
|
{ |
154
|
|
|
return $this->sendRequest('getrequest', ['key' => $address]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Clear a payment request by address. |
159
|
|
|
* |
160
|
|
|
* @param $address |
161
|
|
|
* |
162
|
|
|
* @return object |
163
|
|
|
*/ |
164
|
|
|
public function clearRequest($address) |
165
|
|
|
{ |
166
|
|
|
return $this->sendRequest('rmrequest', ['address' => $address]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Clear all payment requests. |
171
|
|
|
* |
172
|
|
|
* @return object |
173
|
|
|
*/ |
174
|
|
|
public function clearRequests() |
175
|
|
|
{ |
176
|
|
|
return $this->sendRequest('clearrequests'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Validate address. |
181
|
|
|
* |
182
|
|
|
* @param $address |
183
|
|
|
* |
184
|
|
|
* @return object |
185
|
|
|
*/ |
186
|
|
|
public function validateAddress($address) |
187
|
|
|
{ |
188
|
|
|
return $this->sendRequest('validateaddress', ['address' => $address]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get all addresses associated with the wallet. |
193
|
|
|
* |
194
|
|
|
* @return object |
195
|
|
|
*/ |
196
|
|
|
public function getAddresses() |
197
|
|
|
{ |
198
|
|
|
return $this->sendRequest('listaddresses'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get an unused address. |
203
|
|
|
* |
204
|
|
|
* @return object |
205
|
|
|
*/ |
206
|
|
|
public function getUnusedAddress() |
207
|
|
|
{ |
208
|
|
|
return $this->sendRequest('getunusedaddress'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get transaction details. |
213
|
|
|
* |
214
|
|
|
* @param $txid |
215
|
|
|
* |
216
|
|
|
* @return object |
217
|
|
|
*/ |
218
|
|
|
public function getTransaction($txid) |
219
|
|
|
{ |
220
|
|
|
return $this->sendRequest('gettransaction', ['txid' => $txid]); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Sign a address. |
225
|
|
|
* |
226
|
|
|
* @param $address |
227
|
|
|
* |
228
|
|
|
* @return object |
229
|
|
|
*/ |
230
|
|
|
public function signRequest($address) |
231
|
|
|
{ |
232
|
|
|
return $this->sendRequest('signrequest', ['address' => $address]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Broadcast a transaction. |
237
|
|
|
* |
238
|
|
|
* @param $tx |
239
|
|
|
* |
240
|
|
|
* @return object |
241
|
|
|
*/ |
242
|
|
|
public function broadcast($tx) |
243
|
|
|
{ |
244
|
|
|
return $this->sendRequest('broadcast', ['tx' => $tx]); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Serialize JSON tx. |
249
|
|
|
* |
250
|
|
|
* @param $json |
251
|
|
|
* |
252
|
|
|
* @return object |
253
|
|
|
*/ |
254
|
|
|
public function serialize($json) |
255
|
|
|
{ |
256
|
|
|
return $this->sendRequest('serialize', ['jsontx' => $json]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Deserialize JSON tx. |
261
|
|
|
* |
262
|
|
|
* @param $tx |
263
|
|
|
* |
264
|
|
|
* @return object |
265
|
|
|
*/ |
266
|
|
|
public function deserialize($tx) |
267
|
|
|
{ |
268
|
|
|
return $this->sendRequest('deserialize', ['tx' => $tx]); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Encrypt a message. |
273
|
|
|
* |
274
|
|
|
* @param $public_key |
275
|
|
|
* @param $message |
276
|
|
|
* |
277
|
|
|
* @return object |
278
|
|
|
*/ |
279
|
|
|
public function encrypt($public_key, $message) |
280
|
|
|
{ |
281
|
|
|
return $this->sendRequest('encrypt', [ |
282
|
|
|
'pubkey' => $public_key, |
283
|
|
|
'message' => $message, |
284
|
|
|
]); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Decrypt a message. |
289
|
|
|
* |
290
|
|
|
* @param $public_key |
291
|
|
|
* @param $encrypted |
292
|
|
|
* |
293
|
|
|
* @return object |
294
|
|
|
*/ |
295
|
|
|
public function decrypt($public_key, $encrypted) |
296
|
|
|
{ |
297
|
|
|
return $this->sendRequest('decrypt', [ |
298
|
|
|
'pubkey' => $public_key, |
299
|
|
|
'encrypted' => $encrypted, |
300
|
|
|
]); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Check a seed. |
305
|
|
|
* |
306
|
|
|
* @param $seed |
307
|
|
|
* |
308
|
|
|
* @return object |
309
|
|
|
*/ |
310
|
|
|
public function checkSeed($seed) |
311
|
|
|
{ |
312
|
|
|
return $this->sendRequest('check_seed', ['seed' => $seed]); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Create seed. |
317
|
|
|
* |
318
|
|
|
* @return object |
319
|
|
|
*/ |
320
|
|
|
public function createSeed() |
321
|
|
|
{ |
322
|
|
|
return $this->sendRequest('make_seed'); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get seed. |
327
|
|
|
* |
328
|
|
|
* @return object |
329
|
|
|
*/ |
330
|
|
|
public function getSeed() |
331
|
|
|
{ |
332
|
|
|
return $this->sendRequest('getseed'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Freeze an address. |
337
|
|
|
* |
338
|
|
|
* @param $address |
339
|
|
|
* |
340
|
|
|
* @return object |
341
|
|
|
*/ |
342
|
|
|
public function freeze($address) |
343
|
|
|
{ |
344
|
|
|
return $this->sendRequest('freeze', ['address' => $address]); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Get Electrum config value. |
349
|
|
|
* |
350
|
|
|
* @param $key |
351
|
|
|
* |
352
|
|
|
* @return object |
353
|
|
|
*/ |
354
|
|
|
public function getConfig($key) |
355
|
|
|
{ |
356
|
|
|
return $this->SendRequest('getconfig', ['key' => $key]); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Set Electrum config value. |
361
|
|
|
* |
362
|
|
|
* @param $key |
363
|
|
|
* @param $value |
364
|
|
|
* |
365
|
|
|
* @return object |
366
|
|
|
*/ |
367
|
|
|
public function setConfig($key, $value) |
368
|
|
|
{ |
369
|
|
|
return $this->sendRequest('setconfig', [ |
370
|
|
|
'key' => $key, |
371
|
|
|
'value' => $value, |
372
|
|
|
]); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Send a request to the Electrum JSON RPC API. |
377
|
|
|
* |
378
|
|
|
* @param $method |
379
|
|
|
* @param array $params |
380
|
|
|
* |
381
|
|
|
* @throws Exception |
382
|
|
|
* |
383
|
|
|
* @return object |
384
|
|
|
*/ |
385
|
|
|
public function sendRequest($method, $params = []) |
386
|
|
|
{ |
387
|
|
|
$request = $this->client->request('POST', '/', [ |
|
|
|
|
388
|
|
|
'json' => [ |
389
|
|
|
'id' => 'curltext', |
390
|
|
|
'method' => $method, |
391
|
|
|
'params' => $params, |
392
|
|
|
], |
393
|
|
|
]); |
394
|
|
|
|
395
|
|
|
$response = json_decode($request->getBody()->getContents()); |
396
|
|
|
|
397
|
|
|
if (isset($response->error)) { |
398
|
|
|
throw new Exception($response->error->message); |
399
|
|
|
} else { |
400
|
|
|
return $response->result; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|