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

Commit

Permalink
feat(ui): add ability to specify fallback address in LN invoices
Browse files Browse the repository at this point in the history
resolves #2021
  • Loading branch information
korhaliv committed Aug 19, 2019
1 parent bfec3d5 commit 5960463
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
35 changes: 31 additions & 4 deletions renderer/components/Request/Request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import { Flex } from 'rebass'
import { FormattedMessage } from 'react-intl'
import { FormattedMessage, injectIntl } from 'react-intl'
import { intlShape } from '@zap/i18n'
import { Bar, Button, Header, Panel, Span, Text, Tooltip } from 'components/UI'
import { Form, Label, TextArea, Toggle } from 'components/Form'
Expand Down Expand Up @@ -30,6 +30,7 @@ class Request extends React.Component {
isProcessing: PropTypes.bool,
payReq: PropTypes.string,
showNotification: PropTypes.func.isRequired,
willUseFallback: PropTypes.bool,
}

static defaultProps = {
Expand Down Expand Up @@ -89,9 +90,32 @@ class Request extends React.Component {
*
* @param {object} values submitted form values.
*/
onSubmit = values => {
const { cryptoUnit, createInvoice } = this.props
createInvoice(values.amountCrypto, cryptoUnit, values.memo, values.routingHints)
onSubmit = async values => {
const {
cryptoUnit,
createInvoice,
willUseFallback,
createNewAddress,
intl,
showError,
} = this.props
try {
const invoiceArgs = {
amount: values.amountCrypto,
cryptoUnit,
memo: values.memo,
isPrivate: values.routingHints,
}

if (willUseFallback) {
invoiceArgs.fallbackAddress = await createNewAddress()
}

createInvoice(invoiceArgs)
} catch (e) {
const message = intl.formatMessage({ ...messages.add_error })
showError(message)
}
}

/**
Expand Down Expand Up @@ -189,6 +213,9 @@ class Request extends React.Component {
invoice,
payReq,
showNotification,
createNewAddress,
showError,
willUseFallback,
...rest
} = this.props
const { currentStep } = this.state
Expand Down
2 changes: 2 additions & 0 deletions renderer/containers/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createInvoice, invoiceSelectors } from 'reducers/invoice'
import { showNotification } from 'reducers/notification'
import { walletSelectors } from 'reducers/wallet'
import { infoSelectors } from 'reducers/info'
import { settingsSelectors } from 'reducers/settings'

const mapStateToProps = state => ({
activeWalletSettings: walletSelectors.activeWalletSettings(state),
Expand All @@ -14,6 +15,7 @@ const mapStateToProps = state => ({
isProcessing: state.invoice.invoiceLoading,
payReq: state.invoice.invoice,
invoice: invoiceSelectors.invoice(state),
willUseFallback: settingsSelectors.currentConfig(state).invoices.useAddressFallback,
})

const mapDispatchToProps = {
Expand Down
14 changes: 8 additions & 6 deletions renderer/reducers/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,15 @@ export const receiveInvoices = ({ invoices }) => dispatch => {
/**
* createInvoice - Create an invoice.
*
* @param {number} amount Amount
* @param {string} cryptoUnit Crypto unit (sats, bits, btc)
* @param {string} memo Memo
* @param {boolean} isPrivate Set to true to include routing hints
* @param {string} fallbackAddress on-chain address fallback
* @param {object} options request options
* @param {number} options.amount Amount
* @param {string} options.cryptoUnit Crypto unit (sats, bits, btc)
* @param {string} options.memo Memo
* @param {boolean} options.isPrivate Set to true to include routing hints
* @param {string} options.fallbackAddress on-chain address fallback
* @returns {Function} Thunk
*/
export const createInvoice = (amount, cryptoUnit, memo, isPrivate) => async (
export const createInvoice = ({ amount, cryptoUnit, memo, isPrivate, fallbackAddress }) => async (
dispatch,
getState
) => {
Expand All @@ -195,6 +196,7 @@ export const createInvoice = (amount, cryptoUnit, memo, isPrivate) => async (
memo,
private: isPrivate || activeWalletSettings.type === 'local',
expiry: currentConfig.invoices.expire,
fallback_addr: fallbackAddress,
})
dispatch(createInvoiceSuccess(invoice))
} catch (error) {
Expand Down

0 comments on commit 5960463

Please sign in to comment.