robclark56 / RaspiBolt-Extras

More Ways to Use The RaspiBolt
MIT License
21 stars 8 forks source link

Creating the lnd.php #5

Closed rocky3xr closed 5 years ago

rocky3xr commented 5 years ago

Hey @robclark56 , I'm going to create the lnd.php for my website, I have my hosting on https://www.digitalocean.com/

Should I change CHANGE_ME for my LAN Ip 192.168.1.2 or My FQDN testalicante1.ddns.net? (as I don't have Static IP, my IPS gives me dynamic...)

As well Instead 8080 I'm typing 8081 (as the first one is reserved for my router)

<?php
/*
*  Example PHP file to get a Payment Request from an lnd instance on a different host
   See: https://github.com/robclark56/RaspiBolt-Extras/blob/master/RBE_REST_WAN.md

   Feel free to copy and use

   See this link to learn the full LND REST API: 
      https://github.com/ndeet/php-ln-lnd-rest/tree/master/docs/Api

   Optional GET Parameters:
    - memo=Text    (eg: memo=Thanks+for+the+Donation)
    - amt=Sataoshi (eg: amt=100000)
    - image_only   (eg: image_only=1). memo and/or amt must also be set.
Create $macaroon_hex using this command: 
   $  xxd -ps -u -c 1000  /home/bitcoin/.lnd/invoice.macaroon
*/
function getPaymentRequest($memo='',$satoshi=0){
 $lnd_ip         ='192.168.1.2';
 $lnd_port       ='8081';
 $macaroon_base64='192.168.1.2';

 $data = json_encode(array("memo"  => $memo,
                           "value" => "$satoshi"
                         )     
                    );            

 $ch = curl_init("https://$lnd_ip:$lnd_port/v1/invoices");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Grpc-Metadata-macaroon: $macaroon_base64"
    ));
 $response = curl_exec($ch);
 curl_close($ch);
 $PR = json_decode($response);
 return $PR->payment_request;
}
if($_GET['memo'] || isset($_GET['amt'])){
 //Only make one PR
 $memo = $_GET['memo']?$_GET['memo']:'Example Payment Request';
 $amt = $_GET['amt']?$_GET['amt']:'0';
 $pr = getPaymentRequest($memo,$amt);
} else {
  //Make two default PRs
  $donation = getPaymentRequest('Donation');
  $fixed    = getPaymentRequest('Fixed Payment',100000);
}
if($pr && $_GET['image_only']){
 header('Cache-Control: no-store, no-cache, must-revalidate');
 header('Cache-Control: post-check=0, pre-check=0', FALSE);
 header('Pragma: no-cache');
 header("Location: http://qrickit.com/api/qr.php?qrsize=200&d=$pr");
 exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example LND Payment Request</title>
</head>

<body>
<?php
if($pr){
?>
<br>
<img src="http://qrickit.com/api/qr.php?qrsize=200&d=<?php echo $pr;?>">
<br>
<button class="btn" data-clipboard-text="<?php echo $pr;?>">Copy to clipboard</button>

<?php
} else {
?>

<h3>Payment Request - Donation</h3>
<?php echo $donation;?>
<br>
<img src="http://qrickit.com/api/qr.php?qrsize=200&d=<?php echo $donation;?>">
<br>
<button class="btn" data-clipboard-text="<?php echo $donation;?>">Copy to clipboard</button>
<hr>
<h3>Payment Request - Fixed Amount</h3>
<?php echo $fixed;?>
<br>
<img src="http://qrickit.com/api/qr.php?qrsize=200&d=<?php echo $fixed;?>">
<br>
<button class="btn" data-clipboard-text="<?php echo $fixed;?>">Copy to clipboard</button>  
<?php
}
?>
<script src='https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.12/clipboard.min.js'></script>
<script >var clip = new Clipboard('.btn');</script>

</body>
</html>
robclark56 commented 5 years ago

Seems you have

That means you need to use the your FQDN in

rocky3xr commented 5 years ago

What do you mean with "Then copy the macaroon file to digital ocean" where I have to copy? I have to copy the file into the console?

robclark56 commented 5 years ago

@rocky3xr This is the instruction I was referring to:

https://github.com/robclark56/RaspiBolt-Extras/blob/master/RBE_REST_WAN.md#create-macroon-file-on-host

rocky3xr commented 5 years ago

should it be the same macaroon file that I generated for my FQDN domain?

I have configured the REST lnd-WAN as in the guide, my issue is now when I go to the domain where I have the website. As the website is on digital ocean hosting and the FQDN is on noip

robclark56 commented 5 years ago

The macaroon must

When the RaspiBolt receives the curl request, it checks the macaroon info and only responds if it sees the request was sent to the FQDN or Static IP (among other things).

It is irrelevant what service is providing your FQDN (BTW - I also use noip). It is only important that the FQDN is correct and traffic sent to that FQDN arrives at your firewall (and your firewall lets the LND traffic through to the RaspiBolt).

rocky3xr commented 5 years ago

Thanks @robclark56 , everything works smoothly now