Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Commit e3eb3d6

Browse files
committed
Merge branch 'rpc-credentials'
2 parents 6dcbfd1 + c8ac0f3 commit e3eb3d6

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Please note, this library is by far not completed and but can be used in product
1010
First you need to setup a new Electrum wallet. Follow the instructions according to your OS at the [Electrum Download Page](https://electrum.org/#download). After the successfull installation you need to set a rpcport by typing:
1111
```
1212
electrum setconfig rpcport 7777
13-
```
13+
electrum setconfig rpcuser "RPC_USER_NAME"
14+
electrum setconfig rpcpassword "VERY_SECRET_NOT_STARTED_WITH_DIGIT"
15+
```
1416
Then we can create a default wallet, dont forget to note your generated seed, it's nescessary if you want to recover it one day:
1517
```
1618
electrum create
@@ -58,7 +60,7 @@ $response->getVersion();
5860
## Custom Client Configuration
5961
Every Request/Method takes a `Electrum\Client`-instance as parameter which replaces the default one. A custom instance can be usefull if you want to set custom config params like another Hostname or Port.
6062
```php
61-
$client = new \Electrum\Client('http://127.0.0.1', 7777);
63+
$client = new \Electrum\Client('http://127.0.0.1', 7777, 0, 'RPC_USER_NAME', 'VERY_SECRET_NOT_STARTED_WITH_DIGIT');
6264
$method = new \Electrum\Request\Method\Version($client);
6365

6466
try {
@@ -88,4 +90,4 @@ try {
8890
$exception->getMessage()
8991
));
9092
}
91-
```
93+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "epoxa/php-electrum-api",
2+
"name": "padrio/php-electrum-api",
33
"description": "PHP wrapper for Electrum JSONRPC-API",
44
"type": "library",
55
"keywords": ["electrum", "bitcoin", "payment", "jsonrpc", "api"],

src/Client.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ class Client
2222
*/
2323
private $port = 0;
2424

25+
/**
26+
* JSONRPC User Name
27+
* @var string
28+
*/
29+
private $user = '';
30+
31+
/**
32+
* JSONRPC Password
33+
* @var string
34+
*/
35+
private $pass = '';
36+
2537
/**
2638
* Last Message-ID
2739
* @var int
@@ -32,11 +44,20 @@ class Client
3244
* @param string $host
3345
* @param int $port
3446
* @param int $id
47+
* @param string $user
48+
* @param string $password
3549
*/
36-
public function __construct($host = 'http://127.0.0.1', $port = 7777, $id = 0)
37-
{
50+
public function __construct(
51+
$host = 'http://127.0.0.1',
52+
$port = 7777,
53+
$id = 0,
54+
$user = null,
55+
$password = null
56+
) {
3857
$this->setHost($host);
3958
$this->setPort($port);
59+
$this->setUserName($user);
60+
$this->setPassword($password);
4061
$this->setId($id);
4162
}
4263

@@ -110,6 +131,13 @@ private function executeCurlRequest($request)
110131
CURLOPT_POST => true,
111132
CURLOPT_POSTFIELDS => $request,
112133
]);
134+
135+
// Authorization
136+
if ($this->user || $this->pass) {
137+
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
138+
curl_setopt($curl, CURLOPT_USERPWD, $this->user . ":" . $this->pass);
139+
}
140+
113141
// Execute request & convert data to array
114142
$response = curl_exec($curl);
115143

@@ -167,6 +195,48 @@ public function setPort($port)
167195
return $this;
168196
}
169197

198+
/**
199+
* @return string
200+
*/
201+
public function getUserName()
202+
{
203+
return $this->user;
204+
}
205+
206+
/**
207+
* @param $value
208+
*
209+
* @return Request
210+
*
211+
*/
212+
public function setUserName($value)
213+
{
214+
$this->user = $value;
215+
216+
return $this;
217+
}
218+
219+
/**
220+
* @return string
221+
*/
222+
public function getPassword()
223+
{
224+
return $this->pass;
225+
}
226+
227+
/**
228+
* @param $value
229+
*
230+
* @return Request
231+
*
232+
*/
233+
public function setPassword($value)
234+
{
235+
$this->pass = $value;
236+
237+
return $this;
238+
}
239+
170240
/**
171241
* @return int
172242
*/
@@ -194,4 +264,4 @@ public function setId($id)
194264

195265
return $this;
196266
}
197-
}
267+
}

0 commit comments

Comments
 (0)