Skip to content

Instantly share code, notes, and snippets.

@microlancer
Created December 26, 2020 18:28
Show Gist options
  • Select an option

  • Save microlancer/0f3d111dd535a9e07e3cb44e4fb30e94 to your computer and use it in GitHub Desktop.

Select an option

Save microlancer/0f3d111dd535a9e07e3cb44e4fb30e94 to your computer and use it in GitHub Desktop.
LN Gateway used for LightningPayment + https://github.com/thorie7912/lightning-php
<?php
if ($_SERVER['SERVER_NAME'] != 'ln-gateway') { echo 'Bad host'; exit; }
require_once __DIR__ . '/../app/Util/Autoloader.php';
require_once __DIR__ . '/../app/Util/Di.php';
use App\Util\Autoloader;
use App\Util\Di;
Di::getInstance()->get(Autoloader::class)->register();
class Lightning {
const RPC = "unix:///home/thorie/.lightning/lightning-rpc";
public static function isJson($string) {
if (empty($string)) return false;
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
public static function sendCommand(array $input) {
$factory = new \Socket\Raw\Factory();
$socket = $factory->createClient(self::RPC);
$socket->setBlocking(true);
$socket->write(json_encode($input));
$data = "";
do {
$data .= $socket->read(64);
} while (!self::isJson($data) && $socket->assertAlive() && $socket->selectRead(65.0));
$socket->close();
$json = json_decode($data);
if (json_last_error() !== JSON_ERROR_NONE) {
var_dump($data);
throw new \Exception("Bad JSON");
}
return $json;
}
public static function getinfo() {
$input = [
"method" => "getinfo",
"params" => [],
"id" => 0
];
return self::sendCommand($input);
}
public static function invoice($msatoshi, $label, $description) {
$input = [
"method" => "invoice",
"params" => [
"msatoshi" => $msatoshi,
"label" => $label,
"description" => $description,
"expiry" => 900,
],
"id" => 0
];
return self::sendCommand($input);
}
public static function listinvoices($label) {
$input = [
"method" => "listinvoices",
"params" => [
"label" => $label,
],
"id" => 0
];
return self::sendCommand($input);
}
public static function decodepay($bolt11) {
$input = [
"method" => "decodepay",
"params" => [
"bolt11" => $bolt11,
],
"id" => 0
];
return self::sendCommand($input);
}
public static function pay($bolt11, $msatoshi) {
$ret = self::decodepay($bolt11);
if (intval($ret->result->msatoshi) !== intval($msatoshi)) {
return ["result" =>
"invoice did not match msatoshi specified ({$ret->result->msatoshi} vs $msatoshi)"];
}
/**
pay bolt11 [msatoshi] [description] [riskfactor] [maxfeepercent] [retry_for] [maxdelay] [exemptfee]
Send payment specified by {bolt11} with optional {msatoshi} (if and only if {bolt11} does not have amount), {description} (required if {bolt11} uses description hash), {riskfactor} (default 1.0), {maxfeepercent} (default 0.5) the maximum acceptable fee as a percentage (e.g. 0.5 => 0.5%), {exemptfee} (default 5000 msat) disables the maxfeepercent check for fees below the threshold, {retry_for} (default 60) the integer number of seconds before we stop retrying, and {maxdelay} (default 500) the maximum number of blocks we allow the funds to possibly get locked
*/
$input = [
"method" => "pay",
"params" => [
"bolt11" => $bolt11,
"description" => "desc",
"riskfactor" => "1.0",
"maxfeepercent" => "0.5",
"retry_for" => "10",
],
"id" => 0
];
return self::sendCommand($input);
}
public static function getroute($id, $msatoshi, $riskfactor) {
$input = [
"method" => "getroute",
"params" => [
"id" => $id,
"msatoshi" => $msatoshi,
"riskfactor" => $riskfactor,
],
"id" => 0
];
return self::sendCommand($input);
}
}
$params = array_merge($_GET, $_POST);
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
header("Content-type: application/json");
if (!isset($params['msatoshi']) && isset($params['btc'])) {
$params['msatoshi'] = $params['btc'] * 100000000000;
}
switch ($params['method']) {
case 'getinfo':
$ret = Lightning::getinfo();
break;
case 'pay':
$ret = Lightning::pay($params['bolt11'], $params['msatoshi']);
break;
case 'invoice':
$ret = Lightning::invoice($params['msatoshi'], $params['label'], $params['description']);
break;
case 'listinvoices':
$ret = Lightning::listinvoices($params['label']);
break;
case 'decodepay':
$ret = Lightning::decodepay($params['bolt11']);
break;
case 'getroute':
$ret = Lightning::getroute($params['id'], $params['msatoshi'], $params['riskfactor']);
break;
default:
exit(0);
}
echo json_encode($ret, JSON_PRETTY_PRINT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment