Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix(lnurl): show notification on success / error
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed May 28, 2020
1 parent 340d170 commit e68c8f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
43 changes: 26 additions & 17 deletions electron/lnurl/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,21 @@ export default class LnurlService {
}

/**
* send - Send a message to the renderer.
* sendMessage - Send a message to the main window.
*
* @param {string} msg Message
* @param {object} data Data
* @param {string} msg message to send.
* @param {object} data additional data to accompany the message.
*/
send(msg, data) {
this.mainWindow.webContents.send(msg, data)
sendMessage(msg, data) {
if (this.mainWindow) {
mainLog.info('Sending message to renderer process: %o', { msg, data })
this.mainWindow.webContents.send(msg, data)
} else {
mainLog.warn('Unable to send message to renderer process (main window not available): %o', {
msg,
data,
})
}
}

/**
Expand Down Expand Up @@ -93,7 +101,7 @@ export default class LnurlService {
throw new Error('Unable to process lnurl')
}
} catch (error) {
this.send('lnurlError', { message: error.message })
this.sendMessage('lnurlError', { message: error.message })
throw error
}
}
Expand All @@ -110,7 +118,7 @@ export default class LnurlService {

if (this.isWithdrawProcessing) {
mainLog.warn('Error processing lnurl withdraw request: busy')
this.send('lnurlWithdrawError', { service, reason: 'service busy' })
this.sendMessage('lnurlWithdrawError', { service, reason: 'service busy' })
return
}
this.isWithdrawProcessing = true
Expand All @@ -119,13 +127,13 @@ export default class LnurlService {
const withdrawParams = { status, reason, service }
this.isWithdrawProcessing = false
mainLog.error('Unable to process lnurl withdraw request: %o', withdrawParams)
this.send('lnurlWithdrawError', withdrawParams)
this.sendMessage('lnurlWithdrawError', withdrawParams)
return
}

const withdrawParams = { amount: maxWithdrawable, memo: defaultDescription, service }
mainLog.info('Processing lnurl withdraw request: %o', withdrawParams)
this.send('lnurlWithdrawRequest', withdrawParams)
this.sendMessage('lnurlWithdrawRequest', withdrawParams)
}

/**
Expand All @@ -152,7 +160,8 @@ export default class LnurlService {
*/
onFinishWithdraw = async (event, { paymentRequest }) => {
mainLog.info('Finishing lnurl withdraw request: %o', this.withdrawParams)
const { callback, secret, service } = this.withdrawParams
const { callback, secret, lnurl } = this.withdrawParams
const service = getServiceName(lnurl)
try {
if (callback && secret && paymentRequest) {
const { data } = await makeWithdrawRequest({ callback, secret, invoice: paymentRequest })
Expand All @@ -162,11 +171,11 @@ export default class LnurlService {
}

mainLog.info('Completed withdraw request: %o', data)
this.send('lnurlWithdrawSuccess', { service })
this.sendMessage('lnurlWithdrawSuccess', { service })
}
} catch (e) {
mainLog.warn('Unable to complete lnurl withdraw request: %s', e.message)
this.send('lnurlWithdrawError', { service, reason: e.message })
this.sendMessage('lnurlWithdrawError', { service, reason: e.message })
} finally {
this.resetWithdraw()
}
Expand All @@ -181,7 +190,7 @@ export default class LnurlService {

if (this.isChannelProcessing) {
mainLog.warn('Error processing lnurl channel request: busy')
this.send('lnurlChannelError', { service, reason: 'service busy' })
this.sendMessage('lnurlChannelError', { service, reason: 'service busy' })
return
}
this.isChannelProcessing = true
Expand All @@ -190,13 +199,13 @@ export default class LnurlService {
const channelParams = { status, service }
this.isChannelProcessing = false
mainLog.error('Unable to process lnurl channel request: %o', channelParams)
this.send('lnurlChannelError', channelParams)
this.sendMessage('lnurlChannelError', channelParams)
return
}

const channelParams = { service }
mainLog.info('Processing lnurl channel request: %o', channelParams)
this.send('lnurlChannelRequest', channelParams)
this.sendMessage('lnurlChannelRequest', channelParams)
}

/**
Expand Down Expand Up @@ -234,11 +243,11 @@ export default class LnurlService {
}

mainLog.info('Completed channel request: %o', data)
this.send('lnurlChannelSuccess', { service })
this.sendMessage('lnurlChannelSuccess', { service })
}
} catch (e) {
mainLog.warn('Unable to complete lnurl channel request: %s', e.message)
this.send('lnurlChannelError', { service, reason: e.message })
this.sendMessage('lnurlChannelError', { service, reason: e.message })
} finally {
this.resetChannel()
}
Expand Down
5 changes: 4 additions & 1 deletion renderer/reducers/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import createIpc from 'redux-electron-ipc'
import { initApp, terminateApp, openPreferences, lnurlError } from './app'
import { killNeutrino } from './neutrino'
import { receiveLocale } from './locale'
import { lnurlChannelRequest, lnurlChannelError } from './channels'
import { lnurlChannelRequest, lnurlChannelSuccess, lnurlChannelError } from './channels'
import {
bitcoinPaymentUri,
lightningPaymentUri,
lnurlWithdrawRequest,
lnurlWithdrawSuccess,
lnurlWithdrawError,
} from './pay'
import { lndconnectUri } from './onboarding'
Expand Down Expand Up @@ -37,8 +38,10 @@ const ipc = createIpc({
saveInvoiceSuccess,
saveInvoiceFailure,
lnurlChannelRequest,
lnurlChannelSuccess,
lnurlChannelError,
lnurlWithdrawRequest,
lnurlWithdrawSuccess,
lnurlWithdrawError,
})

Expand Down
2 changes: 2 additions & 0 deletions test/unit/utils/lnurl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('lnurl', () => {

const params = await fetchLnurlParams(parseLnUrl(url))
expect(params).toEqual({
lnurl:
'https://service.com/api?q=3fc3645b439ce8e7f2553a69e5267081d96dcd340693afabe04be7b0ccd178df',
tag: 'withdrawRequest',
callback: 'cb',
secret: 'secret',
Expand Down
3 changes: 3 additions & 0 deletions utils/lnurl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ export async function fetchLnurlParams(lnurl) {

if (status === LNURL_STATUS_ERROR) {
return {
lnurl,
status,
reason,
}
}

if (tag === 'withdrawRequest') {
return {
lnurl,
tag,
callback,
secret,
Expand All @@ -68,6 +70,7 @@ export async function fetchLnurlParams(lnurl) {
}
if (tag === 'channelRequest') {
return {
lnurl,
tag,
callback,
secret,
Expand Down

0 comments on commit e68c8f5

Please sign in to comment.