LndRest
A Lightning Network Daemon (LND) package for LND's REST endpoints. PHP classes are generated by the Swagger Codegen project using LND's rpc.swagger.json, (*1)
- Based on LND version: 0.13.3-beta
- This package version: 0.13.3
Requirements
PHP 7.1 and later, (*2)
Installation & Usage
Composer
To install the bindings via Composer, use the following command:, (*3)
composer require ndeet/ln-lnd-rest
Tests
To run the unit tests:, (*4)
composer install
./vendor/bin/phpunit
Getting Started
Note:
- The examples in the generated classes docs are not working out of the box. You will need to pass the port, use SSL and also
use macaroons of your properly secured LND installation.
- Generated classes with lnd-0.13.3-beta are not tested yet by me due to lack of time currently. Fingers crossed everything works., (*5)
setHost('https://localhost:8001');
// First we need to unlock the encrypted wallet. This needs only run once.
$walletInstance = new Lnd\Rest\Api\WalletUnlockerApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client([
'debug' => TRUE,
'verify' => $tlsPath,
'headers' => [
'Grpc-Metadata-macaroon' => $macaroon
]
]),
$apiConfig
);
$unlockRequest = new \Lnd\Rest\Model\LnrpcUnlockWalletRequest([
'walletPassword' => base64_encode('YOUR_WALLET_PASS')
]);
try {
$unlocked = $walletInstance->unlockWallet($unlockRequest);
// gives 408 timeout but unlock successful, afterwards 404 not found
} catch (Exception $e) {
echo 'Exception when calling WalletUnlockerApi->unlockWallet(): ', $e->getMessage(), PHP_EOL;
}
// We can now use the getInfo endpoint:
$apiInstance = new Lnd\Rest\Api\LightningApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client([
'debug' => TRUE,
'verify' => $tlsPath,
'headers' => [
'Grpc-Metadata-macaroon' => bin2hex(file_get_contents($macaroonPath))
]
]),
$apiConfig
);
try {
$result = $apiInstance->getInfo();
var_dump($result);
} catch (Exception $e) {
echo 'Exception when calling LightningApi->getInfo: ', $e->getMessage(), PHP_EOL;
}
// Let's generate an lightning invoice.
$invoice = new \Lnd\Rest\Model\LnrpcInvoice([
'memo' => 'testinvoice memo',
'value' => 1001,
'expiry' => 3600
]);
try {
$invoiceResult = $apiInstance->addInvoice($invoice);
var_dump($invoiceResult);
} catch (Exception $e) {
echo 'Exception when calling LightningApi->addInvoice: ', $e->getMessage(), PHP_EOL;
}
?>
How to generate the code yourself
You can use swagger-codegen to create this whole package out of the rpc.swagger.json of LND. To do this you need swagger-codegen and (for this package as example) this lnrest-config.json:, (*6)
{
"variableNamingConvention": "camelCase",
"invokerPackage": "Lnd\\Rest",
"packagePath": "LndRest",
"srcBasePath": "src",
"composerVendorName": "ndeet",
"gitUserId": "ndeet",
"composerProjectName": "ln-lnd-rest",
"gitRepoId": "php-ln-lnd-rest",
"artifactVersion": "0.13.3",
"license": "MIT"
}
You can generate the code by having the official lightningnetwork/lnd repo checked out to the tag you need and then use that rpc.swagger.json for the code genration like this., (*7)
swagger-codegen generate -c lnrest-config.json -l php -o php-ln-lnd-rest -i /path/to/lightningnetwork/lnd/lnrpc/rpc.swagger.json
Notice regarding to subscribeInvoices endpoint
The REST endpoints are generated from the same gRPC rpc.proto file and it seems the streaming does not work on REST services according to LND's lead developer @roasbeef. But they plan to provide Websockets in a future release. So you may need to do some custom long polling using lookupInvoice endpoint., (*8)
This are some autogenerated docs pointing to classes docs:, (*9)
Documentation for API endpoints
Lightning API Endpoints
Wallet Unlocker API, (*10)
Documentation for authorization
All endpoints do not require authorization. Make sure that those endpoints provided
by this package are not publicly accessible. If you set a wallet password you need
to unlock your wallet first, see documentation and example above., (*11)
Author
Andreas Tasch, (*12)