Merge pull request #3410 from BlueWallet/fix-tor-lnurl-withdraw

FIX: support lnurl-withdraw via Tor (closes #3086)
This commit is contained in:
GLaDOS 2021-07-20 19:59:47 +01:00 committed by GitHub
commit d8b2a0ce74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View file

@ -73,7 +73,7 @@ export class LightningCustodianWallet extends LegacyWallet {
baseURI: this.baseURI,
});
if (isTorCapable && this.baseURI?.indexOf('.onion') !== -1) {
if (isTorCapable && this.baseURI && this.baseURI?.indexOf('.onion') !== -1) {
this._api = new torrific.Torsbee({
baseURI: this.baseURI,
});

View file

@ -29,7 +29,9 @@ import loc, { formatBalanceWithoutSuffix, formatBalancePlain } from '../../loc';
import Lnurl from '../../class/lnurl';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import Notifications from '../../blue_modules/notifications';
import { isTorCapable } from '../../blue_modules/environment';
const currency = require('../../blue_modules/currency');
const torrific = require('../../blue_modules/torrific');
const LNDCreateInvoice = () => {
const { wallets, saveToDisk, setSelectedWallet } = useContext(BlueStorageContext);
@ -176,12 +178,22 @@ const LNDCreateInvoice = () => {
if (lnurlParams) {
const { callback, k1 } = lnurlParams;
const callbackUrl = callback + (callback.indexOf('?') !== -1 ? '&' : '?') + 'k1=' + k1 + '&pr=' + invoiceRequest;
const resp = await fetch(callbackUrl, { method: 'GET' });
if (resp.status >= 300) {
const text = await resp.text();
throw new Error(text);
let reply;
if (isTorCapable && callbackUrl.includes('.onion')) {
const api = new torrific.Torsbee();
const torResponse = await api.get(callbackUrl);
reply = torResponse.body;
if (reply && typeof reply === 'string') reply = JSON.parse(reply);
} else {
const resp = await fetch(callbackUrl, { method: 'GET' });
if (resp.status >= 300) {
const text = await resp.text();
throw new Error(text);
}
reply = await resp.json();
}
const reply = await resp.json();
if (reply.status === 'ERROR') {
throw new Error('Reply from server: ' + reply.reason);
}
@ -217,14 +229,22 @@ const LNDCreateInvoice = () => {
const url = Lnurl.getUrlFromLnurl(data);
// calling the url
let reply;
try {
const resp = await fetch(url, { method: 'GET' });
if (resp.status >= 300) {
throw new Error('Bad response from server');
}
const reply = await resp.json();
if (reply.status === 'ERROR') {
throw new Error('Reply from server: ' + reply.reason);
if (isTorCapable && url.includes('.onion')) {
const api = new torrific.Torsbee();
const torResponse = await api.get(url);
reply = torResponse.body;
if (reply && typeof reply === 'string') reply = JSON.parse(reply);
} else {
const resp = await fetch(url, { method: 'GET' });
if (resp.status >= 300) {
throw new Error('Bad response from server');
}
reply = await resp.json();
if (reply.status === 'ERROR') {
throw new Error('Reply from server: ' + reply.reason);
}
}
if (reply.tag === Lnurl.TAG_PAY_REQUEST) {