Skip to content

Commit

Permalink
Merge branch 'master' of github.com:lightningsale/lnd-rest-php
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Client.php
#	src/Model/GetInfoResponse.php
#	src/Model/PendingChannels/OpeningChannel.php
#	src/RestClient.php
  • Loading branch information
Richard87 committed Mar 4, 2018
1 parent b5b9758 commit d8c9ba3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
- An Async Rest Client using Proxy Objects (http://ocramius.github.io/ProxyManager/)
- An Async gRPC client using Proxy Objects? (Or by default?)

### Known issues for the Rest Client:
- `closeChannel` doesn't work
- No support for force closing a channel at this time
- `lookupInvoice` doesn't work

### How to use:

`composer require lightningsale\lnd-client`
Expand Down
51 changes: 49 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function pendingChannels(): PendingChannelResponse;

public function sendPayment(string $paymentRequest): SendCoinsResponse;

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

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

Expand Down
1 change: 1 addition & 0 deletions src/LndException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class LndException extends \Exception
{
const SEND_COINS_EXCEPTION = 1000;
const INVALID_PARAMETER_TARGET_CONF = 2001;

public function __construct(string $message, int $code, \Exception $previous = null)
{
Expand Down
33 changes: 15 additions & 18 deletions src/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
use LightningSale\LndClient\Model\ChannelGraph;
use LightningSale\LndClient\Model\GetInfoResponse;
use LightningSale\LndClient\Model\CloseStatusUpdate;
use LightningSale\LndClient\Model\SendResponse;
use LightningSale\LndClient\Model\PendingChannelResponse;
use LightningSale\LndClient\Model\ChannelPoint;
use LightningSale\LndClient\Model\SendCoinsResponse;
use LightningSale\LndClient\Model\Transaction;
use LightningSale\LndClient\Model\WalletBalanceResponse;
use Psr\Log\LoggerInterface;
Expand All @@ -44,43 +44,44 @@ public function __construct(\GuzzleHttp\Client $client, LoggerInterface $logger)
private function post(string $uri, $json): array
{
try {
$tmpData = json_encode($json);
$tmpData = $json;
if (isset($tmpData['password']))
$tmpData['password'] = "*** PASSWORD ***";
$this->logger->debug("LndClient Request (post)", [
$tmpData['password'] = '*** PASSWORD ***';

$this->logger->debug('LndClient Request (post)', [
'uri' => $uri,
'json' => $tmpData
]);
$response = $this->httpClient->post($uri, ['json' => $json]);
$body = \GuzzleHttp\json_decode($response->getBody(), true);
return $body;
} catch (BadResponseException $exception) {
$this->logger->critical("LndClient Error", ['exception' => $exception]);
$this->logger->critical('LndClient Error', ['exception' => $exception]);
throw LndException::fromGuzzle($exception);
}
}

private function get(string $uri, array $queryParams = []): array
{
try {
$this->logger->info("LndClient Request (Get)", ['uri' => $uri]);
$this->logger->info('LndClient Request (Get)', ['uri' => $uri]);
$response = $this->httpClient->get($uri, ['query' => $queryParams]);
$body = \GuzzleHttp\json_decode($response->getBody(), true);
return $body;
} catch (BadResponseException $exception) {
$this->logger->critical("LndClient Error", ['exception' => $exception]);
$this->logger->critical('LndClient Error', ['exception' => $exception]);
throw LndException::fromGuzzle($exception);
}
}

private function delete(string $uri, array $queryParams = []){
try {
$this->logger->info("LndClient Request (Delete)", ['uri' => $uri]);
$this->logger->info('LndClient Request (Delete)', ['uri' => $uri]);
$response = $this->httpClient->delete($uri,['query' => $queryParams]);
$body = \GuzzleHttp\json_decode($response->getBody(), true);
return $body;
} catch (BadResponseException $exception) {
$this->logger->critical("LndClient Error", ['exception' => $exception]);
$this->logger->critical('LndClient Error', ['exception' => $exception]);
throw LndException::fromGuzzle($exception);
}
}
Expand Down Expand Up @@ -144,7 +145,7 @@ public function openChannel(string $nodePubkey, string $amount, string $pushSat
return ChannelPoint::fromResponse($body);
}

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

$body = $this->delete($url, $query);
return CloseStatusUpdate::fromResponse($body);
Expand Down Expand Up @@ -247,17 +248,13 @@ public function addInvoice(string $memo, string $value, $expiry = 3600): AddInvo
/** @return Invoice[] */
public function listInvoices(bool $pendingOnly = false): array
{
$url = '/v1/invoices/{pending_only}';
$url = str_replace('{pending_only}', $pendingOnly ? 'true' : 'false', $url);

$body = $this->get($url);
$body = $this->get('/v1/invoices', ['pending_only' => $pendingOnly]);
return array_map(function($f) {return Invoice::fromResponse($f);}, $body['invoices'] ?? []);
}

public function lookupInvoice(string $rHashStr): Invoice
public function lookupInvoice(string $rHash): Invoice
{
$url = '/v1/invoices/{r_hash_str}';
$url = str_replace('{r_hash_str}', urlencode($rHashStr), $url);
$url = '/v1/invoice/' . base64_encode($rHash);

$body = $this->get($url);
return Invoice::fromResponse($body);
Expand Down

0 comments on commit d8c9ba3

Please sign in to comment.