AraneaDev /
laravel-electrum
| 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
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 | * @return object |
||
| 64 | */ |
||
| 65 | public function getAddressBalance($address) |
||
| 66 | { |
||
| 67 | return $this->sendRequest('getaddressbalance', ['address' => $address]); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get history for address. |
||
| 72 | * |
||
| 73 | * @param $address |
||
| 74 | * @return object |
||
| 75 | */ |
||
| 76 | public function getAddressHistory($address) |
||
| 77 | { |
||
| 78 | return $this->sendRequest('getaddresshistory', ['address' => $address]); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get unspent for address. |
||
| 83 | * |
||
| 84 | * @param $address |
||
| 85 | * @return object |
||
| 86 | */ |
||
| 87 | public function getAddressUnspent($address) |
||
| 88 | { |
||
| 89 | return $this->sendRequest('getaddressunspent', ['address' => $address]); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Check whether address is in wallet. |
||
| 94 | * |
||
| 95 | * @param $address |
||
| 96 | * @return object |
||
| 97 | */ |
||
| 98 | public function isAddressMine($address) |
||
| 99 | { |
||
| 100 | return $this->sendRequest('ismine', ['address' => $address]); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get history of wallet. |
||
| 105 | * |
||
| 106 | * @return object |
||
| 107 | */ |
||
| 108 | public function getHistory() |
||
| 109 | { |
||
| 110 | return $this->sendRequest('onchain_history'); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Create a new payment request. |
||
| 115 | * |
||
| 116 | * @param float $amount |
||
| 117 | * @param string $memo |
||
| 118 | * @param int $expiration |
||
| 119 | * @return object |
||
| 120 | */ |
||
| 121 | public function createRequest($amount = 0.00, $memo = '', $expiration = 3600) |
||
| 122 | { |
||
| 123 | return $this->sendRequest('add_request', [ |
||
| 124 | 'amount' => $amount, |
||
| 125 | 'memo' => $memo, |
||
| 126 | 'expiration' => $expiration, |
||
| 127 | ]); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get all payment requests. |
||
| 132 | * |
||
| 133 | * @return object |
||
| 134 | */ |
||
| 135 | public function getRequests() |
||
| 136 | { |
||
| 137 | return $this->sendRequest('list_requests'); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get a payment request by address. |
||
| 142 | * |
||
| 143 | * @param $address |
||
| 144 | * @return object |
||
| 145 | */ |
||
| 146 | public function getRequest($address) |
||
| 147 | { |
||
| 148 | return $this->sendRequest('getrequest', ['key' => $address]); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Clear a payment request by address. |
||
| 153 | * |
||
| 154 | * @param $address |
||
| 155 | * @return object |
||
| 156 | */ |
||
| 157 | public function clearRequest($address) |
||
| 158 | { |
||
| 159 | return $this->sendRequest('rmrequest', ['address' => $address]); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Clear all payment requests. |
||
| 164 | * |
||
| 165 | * @return object |
||
| 166 | */ |
||
| 167 | public function clearRequests() |
||
| 168 | { |
||
| 169 | return $this->sendRequest('clear_requests'); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Validate address. |
||
| 174 | * |
||
| 175 | * @param $address |
||
| 176 | * @return object |
||
| 177 | */ |
||
| 178 | public function validateAddress($address) |
||
| 179 | { |
||
| 180 | return $this->sendRequest('validateaddress', ['address' => $address]); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get all addresses associated with the wallet. |
||
| 185 | * |
||
| 186 | * @return object |
||
| 187 | */ |
||
| 188 | public function getAddresses() |
||
| 189 | { |
||
| 190 | return $this->sendRequest('listaddresses'); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get an unused address. |
||
| 195 | * |
||
| 196 | * @return object |
||
| 197 | */ |
||
| 198 | public function getUnusedAddress() |
||
| 199 | { |
||
| 200 | return $this->sendRequest('getunusedaddress'); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get transaction details. |
||
| 205 | * |
||
| 206 | * @param $txid |
||
| 207 | * @return object |
||
| 208 | */ |
||
| 209 | public function getTransaction($txid) |
||
| 210 | { |
||
| 211 | return $this->sendRequest('gettransaction', ['txid' => $txid]); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Sign a address. |
||
| 216 | * |
||
| 217 | * @param $address |
||
| 218 | * @return object |
||
| 219 | */ |
||
| 220 | public function signRequest($address) |
||
| 221 | { |
||
| 222 | return $this->sendRequest('signrequest', ['address' => $address]); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Broadcast a transaction. |
||
| 227 | * |
||
| 228 | * @param $tx |
||
| 229 | * @return object |
||
| 230 | */ |
||
| 231 | public function broadcast($tx) |
||
| 232 | { |
||
| 233 | return $this->sendRequest('broadcast', ['tx' => $tx]); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Serialize JSON tx. |
||
| 238 | * |
||
| 239 | * @param $json |
||
| 240 | * @return object |
||
| 241 | */ |
||
| 242 | public function serialize($json) |
||
| 243 | { |
||
| 244 | return $this->sendRequest('serialize', ['jsontx' => $json]); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Deserialize JSON tx. |
||
| 249 | * |
||
| 250 | * @param $tx |
||
| 251 | * @return object |
||
| 252 | */ |
||
| 253 | public function deserialize($tx) |
||
| 254 | { |
||
| 255 | return $this->sendRequest('deserialize', ['tx' => $tx]); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Encrypt a message. |
||
| 260 | * |
||
| 261 | * @param $public_key |
||
| 262 | * @param $message |
||
| 263 | * @return object |
||
| 264 | */ |
||
| 265 | public function encrypt($public_key, $message) |
||
| 266 | { |
||
| 267 | return $this->sendRequest('encrypt', [ |
||
| 268 | 'pubkey' => $public_key, |
||
| 269 | 'message' => $message, |
||
| 270 | ]); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Decrypt a message. |
||
| 275 | * |
||
| 276 | * @param $public_key |
||
| 277 | * @param $encrypted |
||
| 278 | * @return object |
||
| 279 | */ |
||
| 280 | public function decrypt($public_key, $encrypted) |
||
| 281 | { |
||
| 282 | return $this->sendRequest('decrypt', [ |
||
| 283 | 'pubkey' => $public_key, |
||
| 284 | 'encrypted' => $encrypted, |
||
| 285 | ]); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Freeze an address. |
||
| 290 | * |
||
| 291 | * @param $address |
||
| 292 | * @return object |
||
| 293 | */ |
||
| 294 | public function freeze($address) |
||
| 295 | { |
||
| 296 | return $this->sendRequest('freeze', ['address' => $address]); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get Electrum config value. |
||
| 301 | * |
||
| 302 | * @param $key |
||
| 303 | * @return object |
||
| 304 | */ |
||
| 305 | public function getConfig($key) |
||
| 306 | { |
||
| 307 | return $this->SendRequest('getconfig', ['key' => $key]); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set Electrum config value. |
||
| 312 | * |
||
| 313 | * @param $key |
||
| 314 | * @param $value |
||
| 315 | * @return object |
||
| 316 | */ |
||
| 317 | public function setConfig($key, $value) |
||
| 318 | { |
||
| 319 | return $this->sendRequest('setconfig', [ |
||
| 320 | 'key' => $key, |
||
| 321 | 'value' => $value, |
||
| 322 | ]); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Send a request to the Electrum JSON RPC API. |
||
| 327 | * |
||
| 328 | * @param $method |
||
| 329 | * @param array $params |
||
| 330 | * @return object |
||
| 331 | * |
||
| 332 | * @throws Exception |
||
| 333 | */ |
||
| 334 | public function sendRequest($method, $params = []) |
||
| 335 | { |
||
| 336 | $request = $this->client->request('POST', '/', [ |
||
| 337 | 'auth' => [ |
||
| 338 | config('electrum.user'), |
||
| 339 | config('electrum.pass'), |
||
| 340 | ], |
||
| 341 | 'json' => [ |
||
| 342 | 'id' => 'curltext', |
||
| 343 | 'method' => $method, |
||
| 344 | 'params' => $params, |
||
| 345 | ], |
||
| 346 | ]); |
||
| 347 | |||
| 348 | $response = json_decode($request->getBody()->getContents()); |
||
| 349 | |||
| 350 | if (isset($response->error)) { |
||
| 351 | throw new Exception($response->error->message); |
||
| 352 | } else { |
||
| 353 | return $response->result; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 |