Skip to content

Commit

Permalink
feat: add optional param k1 for lnurl (#61)
Browse files Browse the repository at this point in the history
* feat: add optional param k1 for lnurl

* Added unit tests for Server.generateNewUrl

Remove async/await syntax

---------

Co-authored-by: Charles Hill <[email protected]>
  • Loading branch information
ChrisCho-H and chill117 authored Aug 29, 2023
1 parent 0d1e6cc commit 966f5bd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,17 +600,12 @@ Server.prototype.prepareStore = function(options) {
};

Server.prototype.generateNewUrl = function(tag, params, options) {
return this.generateSecret().then(secret => {
return Promise.resolve().then(() => {
params = params || {};
return typeof params.k1 !== 'undefined' ? params.k1 : this.generateSecret();
}).then(secret => {
return this.createUrl(secret, tag, params, options).then(result => {
let query;
switch (tag) {
case 'login':
query = { tag, k1: secret };
break;
default:
query = { q: secret };
break;
}
const query = tag === 'login' ? { tag, k1: secret } : { q: secret};
const newUrl = this.getCallbackUrl(query);
const encoded = encode(newUrl);
return { encoded, secret, url: newUrl };
Expand Down
54 changes: 54 additions & 0 deletions test/unit/lib/Server/generateNewUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const assert = require('assert');
const querystring = require('querystring');
const url = require('url');
const { validParams } = require('../../../fixtures');

describe('generateNewUrl(tag, params[, options])', function() {

let server;
before(function() {
server = this.helpers.createServer({
listen: false,
lightning: null,
});
return server.onReady();
});

after(function() {
if (server) return server.close();
});

it('withdrawRequest', function() {
const tag = 'withdrawRequest';
const params = Object.assign({}, validParams['create'][tag]);
return server.generateNewUrl(tag, params).then(result => {
assert.ok(result && typeof result === 'object');
assert.ok(result.encoded);
assert.ok(result.secret);
assert.ok(result.url);
const parsed = url.parse(result.url);
const query = querystring.parse(parsed.query);
assert.strictEqual(query.q, result.secret);
return server.generateNewUrl(tag, params).then(result2 => {
assert.notStrictEqual(result2.secret, result.secret);
});
});
});

it('pre-defined secret (k1)', function() {
const tag = 'withdrawRequest';
const params = Object.assign({}, validParams['create'][tag], {
k1: 'pre-defined 12345',
});
return server.generateNewUrl(tag, params).then(result => {
assert.ok(result && typeof result === 'object');
assert.ok(result.encoded);
assert.ok(result.secret);
assert.ok(result.url);
const parsed = url.parse(result.url);
const query = querystring.parse(parsed.query);
assert.strictEqual(query.q, result.secret);
assert.strictEqual(result.secret, params.k1);
});
});
});

0 comments on commit 966f5bd

Please sign in to comment.