forked from chill117/lnurl-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·325 lines (315 loc) · 8.24 KB
/
cli.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env node
const _ = require('underscore');
const commander = require('commander');
const debug = {
error: require('debug')('lnurl:cli:error'),
};
const fs = require('fs');
const lnurl = require('./index');
const path = require('path');
const pkg = require('./package.json');
const program = new commander.Command();
let stdin = '';
program
.version(pkg.version)
.description(pkg.description);
program
.command('encode [url]')
.description('Encode a url as a bech32-encoded string.')
.action(function(unencoded) {
if (stdin) {
unencoded = stdin.replace('\n', '');
}
const encoded = lnurl.encode(unencoded);
process.stdout.write(encoded);
});
program
.command('decode [encoded]')
.description('Decode a bech32-encoded lnurl.')
.action(function(encoded) {
if (stdin) {
encoded = stdin.replace('\n', '');
}
const decoded = lnurl.decode(encoded);
process.stdout.write(decoded);
});
program
.command('generateApiKey')
.description('Generate a new API key for your lnurl server.')
.option(
'--encoding [value]',
'Encoding to use for ID and key (hex or base64)',
_.identity,
lnurl.Server.prototype.defaultOptions.apiKey.encoding,
)
.option(
'--numBytes.id [value]',
'Number of random bytes to generate for API key ID',
_.identity,
lnurl.Server.prototype.defaultOptions.apiKey.numBytes.id,
)
.option(
'--numBytes.key [value]',
'Number of random bytes to generate for API key',
_.identity,
lnurl.Server.prototype.defaultOptions.apiKey.numBytes.key,
)
.action(function(options) {
options.numBytes = {};
options.numBytes.id = parseInt(options['numBytes.id']);
options.numBytes.key = parseInt(options['numBytes.key']);
options = _.pick(options, 'encoding', 'numBytes');
process.stdout.write(JSON.stringify(lnurl.generateApiKey(options), null, 2));
});
program
.command('generateNewUrl')
.description('Generate a new URL for a server instance')
.option(
'--tag <value>',
'The tag (subprotocol name) of the newly generated URL',
_.identity,
null,
)
.option(
'--uses <value>',
'The number of times the new URL can be used (set to 0 for unlimited uses)',
_.identity,
1,
)
.option(
'--params [values]',
'Stringified JSON object of params for the newly generated URL - e.g for "withdrawRequest" valid params could be {"minWithdrawable": 1000, "maxWithdrawable": 5000}',
_.identity,
null,
)
.option(
'--configFile [value]',
'Optionally load options from a file (supported formats: ".json") (e.g "/path/to/config.json")',
function(value) {
const filePath = path.resolve(value);
fs.statSync(filePath);
return filePath;
},
null
)
.option(
'--host [value]',
'The host for the web server',
_.identity,
lnurl.Server.prototype.defaultOptions.host,
)
.option(
'--port [value]',
'The port for the web server',
_.identity,
lnurl.Server.prototype.defaultOptions.port
)
.option(
'--protocol [value]',
'DEPRECATED - to be removed in a future release',
_.identity,
lnurl.Server.prototype.defaultOptions.protocol
)
.option(
'--url [value]',
'The URL where the server is externally reachable',
_.identity,
lnurl.Server.prototype.getDefaultUrl()
)
.option(
'--endpoint [value]',
'The URI path of the web API end-point',
_.identity,
lnurl.Server.prototype.defaultOptions.endpoint
)
.option(
'--store.backend [value]',
'Which data store backend to use',
_.identity,
lnurl.Server.prototype.defaultOptions.store.backend
)
.option(
'--store.config [value]',
'The options object to use to configure the data store',
_.identity,
lnurl.Server.prototype.defaultOptions.store.config
)
.action(function(options) {
try {
let { tag, params, uses } = options;
options = _.omit(options, ['tag', 'params', 'uses']);
if (!tag) {
throw new Error('--tag is required');
}
if (!params) {
params = {};
} else if (_.isString(params)) {
try {
params = JSON.parse(params);
} catch (error) {
throw new Error('--params must be a valid JSON object');
}
}
uses = uses && parseInt(uses);
if (_.isNaN(uses)) {
throw new Error('--uses must be an integer');
}
if (options.configFile) {
options = JSON.parse(fs.readFileSync(options.configFile, 'utf8'));
}
options = _.omit(options, 'configFile');
prepareGroupOptions(options, ['store']);
if (options.store && options.store.backend === 'memory') {
throw new Error('This command does not work with `--store.backend` set to "memory"');
}
options.listen = false
options.lightning = null;
const server = lnurl.createServer(options);
return server.generateNewUrl(tag, params, { uses }).then(result => {
process.stdout.write(JSON.stringify(result, null, 2));
process.exit();
}).catch(error => {
debug.error(error);
console.error(error.message);
process.exit(1);
});
} catch (error) {
debug.error(error);
console.error(error.message);
process.exit(1);
}
});
program
.command('server')
.description('Start an lnurl application server.')
.option(
'--configFile [value]',
'Optionally load CLI options from a file (supported formats: ".json") (e.g "/path/to/config.json")',
function(value) {
const filePath = path.resolve(value);
fs.statSync(filePath);
return filePath;
},
null
)
.option(
'--host [value]',
'The host for the web server',
_.identity,
lnurl.Server.prototype.defaultOptions.host,
)
.option(
'--port [value]',
'The port for the web server',
_.identity,
lnurl.Server.prototype.defaultOptions.port
)
.option(
'--protocol [value]',
'DEPRECATED - to be removed in a future release',
_.identity,
lnurl.Server.prototype.defaultOptions.protocol
)
.option(
'--url [value]',
'The URL where the server is externally reachable',
_.identity,
lnurl.Server.prototype.getDefaultUrl()
)
.option(
'--endpoint [value]',
'The URI path of the web API end-point',
_.identity,
lnurl.Server.prototype.defaultOptions.endpoint
)
.option(
'--auth.apiKeys [values]',
'List of API keys that can be used to sign LNURLs for your server',
_.identity,
lnurl.Server.prototype.defaultOptions.auth.apiKeys,
)
.option(
'--lightning.backend [value]',
'Which LN backend to use (only lnd supported currently)',
_.identity,
lnurl.Server.prototype.defaultOptions.lightning.backend
)
.option(
'--lightning.config [value]',
'The configuration object to connect to the LN backend',
_.identity,
lnurl.Server.prototype.defaultOptions.lightning.config
)
.option(
'--store.backend [value]',
'Which data store backend to use',
_.identity,
lnurl.Server.prototype.defaultOptions.store.backend
)
.option(
'--store.config [value]',
'The options object to use to configure the data store',
_.identity,
lnurl.Server.prototype.defaultOptions.store.config
)
.action(function(options) {
if (options.configFile) {
options = JSON.parse(fs.readFileSync(options.configFile, 'utf8'));
}
options = _.omit(options, 'configFile');
prepareGroupOptions(options, ['auth', 'lightning', 'store']);
lnurl.createServer(options);
});
const prepareGroupOptions = function(options, groups) {
_.each(groups, group => {
options[group] = _.chain(lnurl.Server.prototype.defaultOptions[group])
.keys()
.map(key => {
let value;
if (!_.isUndefined(options[group])) {
value = options[group][key];
} else {
value = options[`${group}.${key}`];
if (!_.isUndefined(options[`${group}.${key}`])) {
delete options[`${group}.${key}`];
}
}
if (_.isUndefined(value)) {
value = lnurl.Server.prototype.defaultOptions[group][key];
}
switch (group) {
case 'lightning':
case 'store':
if (key === 'backend' && _.isString(value) && value[0] === '{') {
value = JSON.parse(value);
}
if (key === 'config' && _.isString(value)) {
value = JSON.parse(value);
}
break;
case 'auth':
if (key === 'apiKeys' && _.isString(value)) {
value = JSON.parse(value);
}
break;
}
return [key, value];
})
.compact()
.object()
.value();
});
};
if (process.stdin.isTTY) {
program.parse(process.argv);
} else {
process.stdin.on('readable', function() {
var chunk = this.read();
if (chunk !== null) {
stdin += chunk;
}
});
process.stdin.on('end', function() {
program.parse(process.argv);
});
}