Passed
Push — master ( 718b0c...02021b )
by Aranea
13:50
created

Electrum::createSeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
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');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->sendRequest('version') returns the type object which is incompatible with the documented return type string.
Loading history...
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
        $response = $this->sendRequest('history');
115
        $result = json_decode($response);
0 ignored issues
show
Bug introduced by
$response of type object is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $result = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
116
        if (json_last_error() === 0) return $result;
117
        return $response;
118
    }
119
120
    /**
121
     * Create a new payment request.
122
     *
123
     * @param float  $amount
124
     * @param string $memo
125
     * @param int    $expiration
126
     *
127
     * @return object
128
     */
129
    public function createRequest($amount = 0.00, $memo = '', $expiration = 3600)
130
    {
131
        return $this->sendRequest('add_request', [
132
            'amount'     => $amount,
133
            'memo'       => $memo,
134
            'expiration' => $expiration,
135
        ]);
136
    }
137
138
    /**
139
     * Get all payment requests.
140
     *
141
     * @return object
142
     */
143
    public function getRequests()
144
    {
145
        return $this->sendRequest('list_requests');
146
    }
147
148
    /**
149
     * Get a payment request by address.
150
     *
151
     * @param $address
152
     *
153
     * @return object
154
     */
155
    public function getRequest($address)
156
    {
157
        return $this->sendRequest('getrequest', ['key' => $address]);
158
    }
159
160
    /**
161
     * Clear a payment request by address.
162
     *
163
     * @param $address
164
     *
165
     * @return object
166
     */
167
    public function clearRequest($address)
168
    {
169
        return $this->sendRequest('rmrequest', ['address' => $address]);
170
    }
171
172
    /**
173
     * Clear all payment requests.
174
     *
175
     * @return object
176
     */
177
    public function clearRequests()
178
    {
179
        return $this->sendRequest('clear_requests');
180
    }
181
182
    /**
183
     * Validate address.
184
     *
185
     * @param $address
186
     *
187
     * @return object
188
     */
189
    public function validateAddress($address)
190
    {
191
        return $this->sendRequest('validateaddress', ['address' => $address]);
192
    }
193
194
    /**
195
     * Get all addresses associated with the wallet.
196
     *
197
     * @return object
198
     */
199
    public function getAddresses()
200
    {
201
        return $this->sendRequest('listaddresses');
202
    }
203
204
    /**
205
     * Get an unused address.
206
     *
207
     * @return object
208
     */
209
    public function getUnusedAddress()
210
    {
211
        return $this->sendRequest('getunusedaddress');
212
    }
213
214
    /**
215
     * Get transaction details.
216
     *
217
     * @param $txid
218
     *
219
     * @return object
220
     */
221
    public function getTransaction($txid)
222
    {
223
        return $this->sendRequest('gettransaction', ['txid' => $txid]);
224
    }
225
226
    /**
227
     * Sign a address.
228
     *
229
     * @param $address
230
     *
231
     * @return object
232
     */
233
    public function signRequest($address)
234
    {
235
        return $this->sendRequest('signrequest', ['address' => $address]);
236
    }
237
238
    /**
239
     * Broadcast a transaction.
240
     *
241
     * @param $tx
242
     *
243
     * @return object
244
     */
245
    public function broadcast($tx)
246
    {
247
        return $this->sendRequest('broadcast', ['tx' => $tx]);
248
    }
249
250
    /**
251
     * Serialize JSON tx.
252
     *
253
     * @param $json
254
     *
255
     * @return object
256
     */
257
    public function serialize($json)
258
    {
259
        return $this->sendRequest('serialize', ['jsontx' => $json]);
260
    }
261
262
    /**
263
     * Deserialize JSON tx.
264
     *
265
     * @param $tx
266
     *
267
     * @return object
268
     */
269
    public function deserialize($tx)
270
    {
271
        return $this->sendRequest('deserialize', ['tx' => $tx]);
272
    }
273
274
    /**
275
     * Encrypt a message.
276
     *
277
     * @param $public_key
278
     * @param $message
279
     *
280
     * @return object
281
     */
282
    public function encrypt($public_key, $message)
283
    {
284
        return $this->sendRequest('encrypt', [
285
            'pubkey'    => $public_key,
286
            'message'   => $message,
287
        ]);
288
    }
289
290
    /**
291
     * Decrypt a message.
292
     *
293
     * @param $public_key
294
     * @param $encrypted
295
     *
296
     * @return object
297
     */
298
    public function decrypt($public_key, $encrypted)
299
    {
300
        return $this->sendRequest('decrypt', [
301
            'pubkey'    => $public_key,
302
            'encrypted' => $encrypted,
303
        ]);
304
    }
305
306
    /**
307
     * Freeze an address.
308
     *
309
     * @param $address
310
     *
311
     * @return object
312
     */
313
    public function freeze($address)
314
    {
315
        return $this->sendRequest('freeze', ['address' => $address]);
316
    }
317
318
    /**
319
     * Get Electrum config value.
320
     *
321
     * @param $key
322
     *
323
     * @return object
324
     */
325
    public function getConfig($key)
326
    {
327
        return $this->SendRequest('getconfig', ['key' => $key]);
328
    }
329
330
    /**
331
     * Set Electrum config value.
332
     *
333
     * @param $key
334
     * @param $value
335
     *
336
     * @return object
337
     */
338
    public function setConfig($key, $value)
339
    {
340
        return $this->sendRequest('setconfig', [
341
            'key'   => $key,
342
            'value' => $value,
343
        ]);
344
    }
345
346
    /**
347
     * Send a request to the Electrum JSON RPC API.
348
     *
349
     * @param $method
350
     * @param array $params
351
     *
352
     * @throws Exception
353
     *
354
     * @return object
355
     */
356
    public function sendRequest($method, $params = [])
357
    {
358
        $request = $this->client->request('POST', '/', [
359
            'auth' => [
360
                config('electrum.user'),
361
                config('electrum.pass')
362
            ],
363
            'json' => [
364
                'id'     => 'curltext',
365
                'method' => $method,
366
                'params' => $params,
367
            ],
368
        ]);
369
370
        $response = json_decode($request->getBody()->getContents());
371
372
        if (isset($response->error)) {
373
            throw new Exception($response->error->message);
374
        } else {
375
            return $response->result;
376
        }
377
    }
378
}
379