-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.php
255 lines (212 loc) · 5.05 KB
/
helpers.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
require_once 'requests.php';
function create_lnaddress($username, $wallet_admin_key) {
if(LNADDRESS_ADMIN_KEY == "" || LNADDRESS_DOMAIN_ID == "") {
return false;
}
$request = request(
"POST",
"/lnaddress/api/v1/address/" . LNADDRESS_DOMAIN_ID,
[],
['X-Api-Key: ' . LNADDRESS_ADMIN_KEY],
[
'username' => $username,
'duration' => 100000,
'sats' => 0,
'wallet_key' => $wallet_admin_key,
'domain' => LNADDRESS_DOMAIN_ID,
'wallet_endpoint' => DOMAIN_NAME,
]
);
if($request['status'] != 200) {
throw new Exception("Error creating LNAddress");
}
return true;
}
function create_tipjar($wallet_id, $watchonly_id, $api_key) {
$request = request(
"POST",
"/tipjar/api/v1/tipjars",
[],
['X-Api-Key: ' . $api_key],
[
'wallet' => $wallet_id,
'chain' => true,
'onchain' => $watchonly_id,
'name' => 'My Tip Jar',
]
);
if($request['status'] != 200) {
throw new Exception("Error creating tipjar");
}
return $request['response']['id'];
}
function create_watchonly($zpub, $api_key) {
$config_request = request(
"GET",
"/watchonly/api/v1/config",
[],
['X-Api-Key: ' . $api_key],
);
$request = request(
"POST",
"/watchonly/api/v1/wallet",
[],
['X-Api-Key: ' . $api_key],
[
'masterpub' => $zpub,
'network' => 'Mainnet',
'title' => 'My On-Chain Wallet',
'is_unique' => false,
]
);
if($request['status'] != 200) {
throw new Exception("Error creating watch only wallet");
}
return $request['response']['id'];
}
function create_boltcard($card_uid, $wallet_id, $api_key) {
$k0 = bin2hex(random_bytes(16));
$k1 = bin2hex(random_bytes(16));
$k2 = bin2hex(random_bytes(16));
$request = request(
"POST",
"/boltcards/api/v1/cards",
[],
['X-Api-Key: ' . $api_key],
[
'wallet' => $wallet_id,
'card_name' => 'My First Card',
'uid' => $card_uid,
'k0' => $k0,
'k1' => $k1,
'k2' => $k2,
'counter' => 0,
'tx_limit' => "100000",
'daily_limit' => "1000000",
]
);
if($request['status'] != 201) {
throw new Exception("Error creating boltcard. THE card_uid MUST BE GLOBALLY UNIQUE IN LNBITS.");
}
return [
'otp' => $request['response']['otp'],
'k0' => $k0,
'k1' => $k1,
'k2' => $k2,
];
}
function create_tpos($wallet_id, $api_key, $fiat_currency) {
$request = request(
"POST",
"/tpos/api/v1/tposs",
[],
['X-Api-Key: ' . $api_key],
[
'wallet' => $wallet_id,
'name' => 'My Point of Sale',
'currency' => $fiat_currency,
'tip_wallet' => $wallet_id,
'tip_options' => json_encode([
10,
15,
20,
]),
]
);
if($request['status'] != 201) {
throw new Exception("Error creating TPoS");
}
return $request['response']['id'];
}
function create_lnurlw_link($api_key) {
$request = request(
"POST",
"/withdraw/api/v1/links",
[],
['X-Api-Key: ' . $api_key],
[
'title' => 'My Withdraw Link',
'min_withdrawable' => 10,
'max_withdrawable' => 100000,
'uses' => 250,
'wait_time' => 1,
'is_unique' => true,
]
);
if($request['status'] != 201) {
throw new Exception("Error creating lnurlw link");
}
return $request['response'];
}
function create_lnurlp_link($api_key) {
$request = request(
"POST",
"/lnurlp/api/v1/links",
[],
['X-Api-Key: ' . $api_key],
[
'description' => 'My Pay Link',
'min' => 1,
'max' => 10000000,
'comment_chars' => 255,
]
);
if($request['status'] != 201) {
throw new Exception("Error creating lnurlp link");
}
return $request['response']['lnurl'];
}
function enable_extension($user_id, $extension) {
$request = request(
'GET',
'/extensions',
[
'usr' => $user_id,
'enable' => $extension,
]
);
if($request['status'] != 200) {
throw new Exception("Error enabling extension.");
}
return true;
}
function create_user($username) {
$request = request(
'GET',
'/wallet',
[
'nme' => $username
],
['Accept: text/html']
);
if($request['status'] != 307) {
throw new Exception("Error creating user.");
}
$full_url = DOMAIN_NAME . $request['headers']['location'][0];
$url_components = parse_url($full_url);
$params = [];
parse_str($url_components['query'], $params);
$page_response = request("GET", $request['headers']['location'][0]);
if($page_response['status'] != 200) {
throw new Exception("Error loading wallet page to parse keys.");
}
preg_match('/<strong>Admin key: <\/strong><em>(.*)<\/em><br \/>/', $page_response['raw_response'], $admin_matches);
if(!isset($admin_matches[1]) || empty($admin_matches[1])) {
throw new Exception("Unable to parse Admin API Keys");
}
preg_match('/<strong>Invoice\/read key: <\/strong><em>(.*)<\/em>/', $page_response['raw_response'], $invoice_matches);
if(!isset($invoice_matches[1]) || empty($invoice_matches[1])) {
throw new Exception("Unable to parse Invoice API Keys");
}
return [
'user_id' => $params['usr'],
'wallet_id' => $params['wal'],
'admin_key' => $admin_matches[1],
'invoice_key' => $invoice_matches[1],
'username' => $username,
];
}
function create_qr($string, $size = 110) {
return QRcode::svg($string, uniqid(), false, QR_ECLEVEL_L, $size);
}