Review Fixes

This commit is contained in:
Jonathan Underwood 2021-10-10 22:04:26 +09:00
parent 094f7e41c2
commit 6fec7177d1
2 changed files with 74 additions and 27 deletions

View File

@ -4,6 +4,7 @@ import { isTorDaemonDisabled } from '../blue_modules/environment';
const CryptoJS = require('crypto-js');
const createHash = require('create-hash');
const torrific = require('../blue_modules/torrific');
const ONION_REGEX = /^(http:\/\/[^/:@]+\.onion(?::\d{1,5})?)(\/.*)?$/; // regex for onion URL
/**
* @see https://github.com/btcontract/lnurl-rfc/blob/master/lnurl-pay.md
@ -11,7 +12,6 @@ const torrific = require('../blue_modules/torrific');
export default class Lnurl {
static TAG_PAY_REQUEST = 'payRequest'; // type of LNURL
static TAG_WITHDRAW_REQUEST = 'withdrawRequest'; // type of LNURL
static ONION_REGEX = /^(http:\/\/[^/:@]+\.onion(?::\d{1,5})?)(\/.*)?$/; // regex for onion URL
constructor(url, AsyncStorage) {
this._lnurl = url;
@ -55,7 +55,7 @@ export default class Lnurl {
}
static parseOnionUrl(url) {
const match = url.match(Lnurl.ONION_REGEX);
const match = url.match(ONION_REGEX);
if (match === null) return null;
const [, baseURI, path] = match;
return [baseURI, path];
@ -64,30 +64,9 @@ export default class Lnurl {
async fetchGet(url) {
const parsedOnionUrl = Lnurl.parseOnionUrl(url);
if (parsedOnionUrl) {
const torDaemonDisabled = await isTorDaemonDisabled();
if (torDaemonDisabled) {
throw new Error('Tor onion url support disabled');
}
const [baseURI, path] = parsedOnionUrl;
const tor = new torrific.Torsbee({
baseURI,
});
const response = await tor.get(path || '/', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
});
const json = response.body;
if (typeof json === 'undefined' || response.err) {
throw new Error('Bad response from server: ' + response.err + ' ' + JSON.stringify(response.body));
}
if (json.status === 'ERROR') {
throw new Error('Reply from server: ' + json.reason);
}
return json;
return _fetchGetTor(parsedOnionUrl);
}
const resp = await fetch(url, { method: 'GET' });
if (resp.status >= 300) {
throw new Error('Bad response from server');
@ -310,8 +289,31 @@ export default class Lnurl {
// ensure only 1 `@` present:
if (address.split('@').length !== 2) return false;
const splitted = address.split('@');
// ensure the host does not contain a port
if (splitted[1].indexOf(':') > -1) return false;
return !!splitted[0].trim() && !!splitted[1].trim();
}
}
async function _fetchGetTor(parsedOnionUrl) {
const torDaemonDisabled = await isTorDaemonDisabled();
if (torDaemonDisabled) {
throw new Error('Tor onion url support disabled');
}
const [baseURI, path] = parsedOnionUrl;
const tor = new torrific.Torsbee({
baseURI,
});
const response = await tor.get(path || '/', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
},
});
const json = response.body;
if (typeof json === 'undefined' || response.err) {
throw new Error('Bad response from server: ' + response.err + ' ' + JSON.stringify(response.body));
}
if (json.status === 'ERROR') {
throw new Error('Reply from server: ' + json.reason);
}
return json;
}

View File

@ -42,6 +42,50 @@ describe('LNURL', function () {
assert.ok(!Lnurl.isLnurl('bs'));
});
it('can parseOnionUrl()', () => {
const vectors = [
{
test: 'http://abc.onion/path',
expected: ['http://abc.onion', '/path'],
},
{
test: 'http://abc.onion:12345/path',
expected: ['http://abc.onion:12345', '/path'],
},
{
test: 'http://abc.onion/',
expected: ['http://abc.onion', '/'],
},
{
test: 'http://abc.onion',
expected: ['http://abc.onion', undefined],
},
{
test: 'https://abc.onion',
expected: null,
},
{
test: 'http://abc.com',
expected: null,
},
{
test: 'http://a@bc.onion',
expected: null,
},
{
test: 'http://a/bc.onion',
expected: null,
},
{
test: 'http://a:bc.onion',
expected: null,
},
];
for (const { test, expected } of vectors) {
assert.deepStrictEqual(Lnurl.parseOnionUrl(test), expected);
}
});
it('can callLnurlPayService() and requestBolt11FromLnurlPayService()', async () => {
const LN = new Lnurl('LNURL1DP68GURN8GHJ7MRWW3UXYMM59E3XJEMNW4HZU7RE0GHKCMN4WFKZ7URP0YLH2UM9WF5KG0FHXYCNV9G9W58');
@ -168,6 +212,7 @@ describe('LNURL', function () {
describe('lightning address', function () {
it('can getUrlFromLnurl()', () => {
assert.strictEqual(Lnurl.getUrlFromLnurl('lnaddress@zbd.gg'), 'https://zbd.gg/.well-known/lnurlp/lnaddress');
assert.strictEqual(Lnurl.getUrlFromLnurl('lnaddress@hidden.onion'), 'http://hidden.onion/.well-known/lnurlp/lnaddress');
});
it('can detect', async () => {