Skip to content

Commit d8c9ba3

Browse files
committed
Merge branch 'master' of github.com:lightningsale/lnd-rest-php
# Conflicts: # src/Client.php # src/Model/GetInfoResponse.php # src/Model/PendingChannels/OpeningChannel.php # src/RestClient.php
1 parent b5b9758 commit d8c9ba3

File tree

5 files changed

+66
-26
lines changed

5 files changed

+66
-26
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
- An Async Rest Client using Proxy Objects (http://ocramius.github.io/ProxyManager/)
66
- An Async gRPC client using Proxy Objects? (Or by default?)
77

8-
### Known issues for the Rest Client:
9-
- `closeChannel` doesn't work
10-
- No support for force closing a channel at this time
11-
- `lookupInvoice` doesn't work
12-
138
### How to use:
149

1510
`composer require lightningsale\lnd-client`

composer.lock

Lines changed: 49 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function pendingChannels(): PendingChannelResponse;
5353

5454
public function sendPayment(string $paymentRequest): SendCoinsResponse;
5555

56-
public function closeChannel(string $fundingTxid, string $outputIndex, bool $force = false): CloseStatusUpdate;
56+
public function closeChannel(string $fundingTxid, string $outputIndex, ?bool $force = false): CloseStatusUpdate;
5757

5858
public function updateChannelPolicy(string $baseFeeMsat, int $feeRate, int $timeLockDelta, ?ChannelPoint $channelPoint = null);
5959

src/LndException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class LndException extends \Exception
1515
{
1616
const SEND_COINS_EXCEPTION = 1000;
17+
const INVALID_PARAMETER_TARGET_CONF = 2001;
1718

1819
public function __construct(string $message, int $code, \Exception $previous = null)
1920
{

src/RestClient.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
use LightningSale\LndClient\Model\ChannelGraph;
1818
use LightningSale\LndClient\Model\GetInfoResponse;
1919
use LightningSale\LndClient\Model\CloseStatusUpdate;
20-
use LightningSale\LndClient\Model\SendResponse;
2120
use LightningSale\LndClient\Model\PendingChannelResponse;
2221
use LightningSale\LndClient\Model\ChannelPoint;
22+
use LightningSale\LndClient\Model\SendCoinsResponse;
2323
use LightningSale\LndClient\Model\Transaction;
2424
use LightningSale\LndClient\Model\WalletBalanceResponse;
2525
use Psr\Log\LoggerInterface;
@@ -44,43 +44,44 @@ public function __construct(\GuzzleHttp\Client $client, LoggerInterface $logger)
4444
private function post(string $uri, $json): array
4545
{
4646
try {
47-
$tmpData = json_encode($json);
47+
$tmpData = $json;
4848
if (isset($tmpData['password']))
49-
$tmpData['password'] = "*** PASSWORD ***";
50-
$this->logger->debug("LndClient Request (post)", [
49+
$tmpData['password'] = '*** PASSWORD ***';
50+
51+
$this->logger->debug('LndClient Request (post)', [
5152
'uri' => $uri,
5253
'json' => $tmpData
5354
]);
5455
$response = $this->httpClient->post($uri, ['json' => $json]);
5556
$body = \GuzzleHttp\json_decode($response->getBody(), true);
5657
return $body;
5758
} catch (BadResponseException $exception) {
58-
$this->logger->critical("LndClient Error", ['exception' => $exception]);
59+
$this->logger->critical('LndClient Error', ['exception' => $exception]);
5960
throw LndException::fromGuzzle($exception);
6061
}
6162
}
6263

6364
private function get(string $uri, array $queryParams = []): array
6465
{
6566
try {
66-
$this->logger->info("LndClient Request (Get)", ['uri' => $uri]);
67+
$this->logger->info('LndClient Request (Get)', ['uri' => $uri]);
6768
$response = $this->httpClient->get($uri, ['query' => $queryParams]);
6869
$body = \GuzzleHttp\json_decode($response->getBody(), true);
6970
return $body;
7071
} catch (BadResponseException $exception) {
71-
$this->logger->critical("LndClient Error", ['exception' => $exception]);
72+
$this->logger->critical('LndClient Error', ['exception' => $exception]);
7273
throw LndException::fromGuzzle($exception);
7374
}
7475
}
7576

7677
private function delete(string $uri, array $queryParams = []){
7778
try {
78-
$this->logger->info("LndClient Request (Delete)", ['uri' => $uri]);
79+
$this->logger->info('LndClient Request (Delete)', ['uri' => $uri]);
7980
$response = $this->httpClient->delete($uri,['query' => $queryParams]);
8081
$body = \GuzzleHttp\json_decode($response->getBody(), true);
8182
return $body;
8283
} catch (BadResponseException $exception) {
83-
$this->logger->critical("LndClient Error", ['exception' => $exception]);
84+
$this->logger->critical('LndClient Error', ['exception' => $exception]);
8485
throw LndException::fromGuzzle($exception);
8586
}
8687
}
@@ -144,7 +145,7 @@ public function openChannel(string $nodePubkey, string $amount, string $pushSat
144145
return ChannelPoint::fromResponse($body);
145146
}
146147

147-
public function closeChannel(string $fundingTxid, string $outputIndex, bool $force = false, ? int $targetConf = 5,? int $satPrByte = null): CloseStatusUpdate
148+
public function closeChannel(string $fundingTxid, string $outputIndex, ? bool $force = false, ? int $targetConf = 5,? int $satPrByte = null): CloseStatusUpdate
148149
{
149150
$url = '/v1/channels/{funding_txid}/{output_index}';
150151
$url = str_replace('{funding_txid}', urlencode($fundingTxid), $url);
@@ -154,7 +155,7 @@ public function closeChannel(string $fundingTxid, string $outputIndex, bool $for
154155
if ($force) $query['force'] = $force;
155156
if ($targetConf && !$satPrByte) $query['target_conf'] = $targetConf;
156157
if (!$targetConf && $satPrByte) $query['sat_per_byte'] = $satPrByte;
157-
if ($targetConf && $satPrByte) throw new LndException("You must specify either 'targetConf' or 'satPrByte', not both!");
158+
if ($targetConf && $satPrByte) throw new LndException("You must specify either 'targetConf' or 'satPrByte', not both!", LndException::INVALID_PARAMETER_TARGET_CONF);
158159

159160
$body = $this->delete($url, $query);
160161
return CloseStatusUpdate::fromResponse($body);
@@ -247,17 +248,13 @@ public function addInvoice(string $memo, string $value, $expiry = 3600): AddInvo
247248
/** @return Invoice[] */
248249
public function listInvoices(bool $pendingOnly = false): array
249250
{
250-
$url = '/v1/invoices/{pending_only}';
251-
$url = str_replace('{pending_only}', $pendingOnly ? 'true' : 'false', $url);
252-
253-
$body = $this->get($url);
251+
$body = $this->get('/v1/invoices', ['pending_only' => $pendingOnly]);
254252
return array_map(function($f) {return Invoice::fromResponse($f);}, $body['invoices'] ?? []);
255253
}
256254

257-
public function lookupInvoice(string $rHashStr): Invoice
255+
public function lookupInvoice(string $rHash): Invoice
258256
{
259-
$url = '/v1/invoices/{r_hash_str}';
260-
$url = str_replace('{r_hash_str}', urlencode($rHashStr), $url);
257+
$url = '/v1/invoice/' . base64_encode($rHash);
261258

262259
$body = $this->get($url);
263260
return Invoice::fromResponse($body);

0 commit comments

Comments
 (0)