-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLnd_wrapper.php
153 lines (126 loc) · 4.48 KB
/
Lnd_wrapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
class LndWrapper
{
private $macaroonHex;
private $endpoint;
private $coin = 'BTC';
private $tlsPath;
/**
* Call this method to get singleton
*/
public static function instance()
{
static $instance = false;
if( $instance === false )
{
// Late static binding (PHP 5.3+)
$instance = new static();
}
return $instance;
}
/**
* Make constructor private, so nobody can call "new Class".
*/
private function __construct() {
}
/**
* Make clone magic method private, so nobody can clone instance.
*/
private function __clone() {}
/**
* Make sleep magic method private, so nobody can serialize instance.
*/
private function __sleep() {}
/**
* Make wakeup magic method private, so nobody can unserialize instance.
*/
private function __wakeup() {}
/**
* Custom function to make curl requests
*/
private function curlWrap( $url, $json, $action, $headers ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
switch($action){
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
break;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//This is set to 0 for development mode. Set 1 when production (self-signed certificate error)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CAINFO, $this->tlsPath);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
/**
* Set endpoint credentials
*/
public function setCredentials ( $endpoint , $macaroonHex , $tlsPath){
$this->endpoint = $endpoint;
$this->macaroonHex = $macaroonHex;
$this->tlsPath = $tlsPath;
}
public function setCoin ( $coin ){
$this->coin = $coin;
}
public function getCoin () {
return $this->coin;
}
public function generateQr( $paymentRequest ){
$size = "150x150";
$encoding = "UTF-8";
return 'https://chart.googleapis.com/chart?cht=qr' . '&chs=' . $size . '&chl=' . $paymentRequest . '&choe=' . $encoding;
}
/**
* Generate Payment Request
*/
public function createInvoice ( $invoice ) {
$header = array('Grpc-Metadata-macaroon: ' . $this->macaroonHex , 'Content-type: application/json');
$createInvoiceResponse = $this->curlWrap( $this->endpoint . '/v1/invoices', json_encode( $invoice ), 'POST', $header );
$createInvoiceResponse = json_decode($createInvoiceResponse);
return $createInvoiceResponse;
}
public function getInvoiceInfoFromPayReq ($paymentRequest) {
$header = array('Grpc-Metadata-macaroon: ' . $this->macaroonHex , 'Content-type: application/json');
$invoiceInfoResponse = $this->curlWrap( $this->endpoint . '/v1/payreq/' . $paymentRequest,'', "GET", $header );
$invoiceInfoResponse = json_decode( $invoiceInfoResponse );
return $invoiceInfoResponse;
}
public function getInvoiceInfoFromHash ( $paymentHash ) {
$header = array('Grpc-Metadata-macaroon: ' . $this->macaroonHex , 'Content-type: application/json');
$invoiceInfoResponse = $this->curlWrap( $this->endpoint . '/v1/invoice/' . $paymentHash,'', "GET", $header );
$invoiceInfoResponse = json_decode( $invoiceInfoResponse );
return $invoiceInfoResponse;
}
public function getLivePrice() {
$ticker = "BTCUSD";
if($this->coin === 'LTC'){
$ticker = 'LTCUSD';
}
$tickerUrl = "https://apiv2.bitcoinaverage.com/indices/global/ticker/" . $ticker;
$aHTTP = array(
'http' =>
array(
'method' => 'GET',
)
);
$content = file_get_contents($tickerUrl, false);
return json_decode($content, true)['ask'];
}
}