ndeet/ln-lnd-rest

0.13.3 2021-10-15 07:14 UTC

This package is auto-updated.

Last update: 2024-09-15 13:38:20 UTC


README

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

  • 基于 LND 版本:0.13.3-beta
  • 此包版本:0.13.3

要求

PHP 7.1 及更高版本

安装与使用

Composer

要通过 Composer 安装绑定,请使用以下命令

composer require ndeet/ln-lnd-rest

测试

运行单元测试

composer install
./vendor/bin/phpunit

入门指南

注意

  • 生成的类文档中的示例无法直接运行。您需要传递端口,使用 SSL,并使用您正确安全配置的 LND 安装的 macaroons。
  • 由于时间不足,我尚未测试使用 lnd-0.13.3-beta 生成的代码。祝一切顺利。
<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Get tls and macaroon paths.
$tlsPath = '';
$macaroonPath = '';
$local_user = posix_getpwuid(posix_getuid());

switch (PHP_OS) {
  case "Darwin":
    $tlsPath = $local_user['dir'] . '/Library/Application Support/Lnd/tls.cert';
    $macaroonPath = $local_user['dir'] . '/Library/Application Support/Lnd/macaroon.admin';
    break;

  case "Linux":
    $tlsPath = $local_user['dir'] . '/.lnd/tls.cert';
    $macaroonPath = $local_user['dir'] . '/.lnd/macaroon.admin';
}

if (! $sslCert = file_get_contents($tlsPath)) {
  $certError = <<<EOT
    tls.cert not found in "example" directory. Make sure to copy it from your
    LND config directory.
    MacOS: ~/Library/Application Support/Lnd/tls.cert
    Linux: ~/.lnd/tls.cert
EOT;

  throw new Exception($certError);
}

// We need to use Configuration class for the url and can't pass it directly in GuzzleClient.
$apiConfig = new \Lnd\Rest\Configuration();
$apiConfig->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;
}

?>

如何自己生成代码

您可以使用 swagger-codegen 使用 LND 的 rpc.swagger.json 创建整个包。为此,您需要 swagger-codegen 以及(以本包为例)此 lnrest-config.json

{
  "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"
}

您可以通过将官方 lightningnetwork/lnd 仓库检出所需的标签,然后使用该 rpc.swagger.json 进行代码生成,如下所示。

swagger-codegen generate -c lnrest-config.json -l php -o php-ln-lnd-rest -i /path/to/lightningnetwork/lnd/lnrpc/rpc.swagger.json

关于 subscribeInvoices 端点的说明

REST 端点是根据相同的 gRPC rpc.proto 文件生成的,根据 LND 的首席开发者 @roasbeef,似乎 REST 服务上的流式传输不起作用。但他们计划在未来的版本中提供 Websockets。因此,您可能需要使用 lookupInvoice 端点进行一些自定义的长轮询。

这些是一些自动生成的文档,指向类文档

API 端点文档

Lightning API 端点
钱包解锁器 API

授权文档

所有端点都不需要授权。请确保此包提供的端点不是公开可访问的。如果您设置了钱包密码,您需要先解锁您的钱包,请参阅上述文档和示例。

作者

Andreas Tasch