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

Commit

Permalink
fix(lnd): support paying to invoices with payment secret
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Jan 28, 2021
1 parent c599c83 commit bd8cb54
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
19 changes: 16 additions & 3 deletions renderer/reducers/payment/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import pick from 'lodash/pick'
import defaults from 'lodash/defaults'
import omitBy from 'lodash/omitBy'
import isNil from 'lodash/isNil'
import cloneDeep from 'lodash/cloneDeep'
import createReducer from '@zap/utils/createReducer'
import { isPubkey } from '@zap/utils/crypto'
import { isPubkey, getTag } from '@zap/utils/crypto'
import delay from '@zap/utils/delay'
import genId from '@zap/utils/genId'
import { mainLog } from '@zap/utils/log'
Expand Down Expand Up @@ -316,13 +317,25 @@ export const payInvoice = ({
if (route && route.isExact) {
let result = {}
try {
const routeToUse = { ...route }
const routeToUse = cloneDeep(route)
delete routeToUse.isExact
delete routeToUse.paymentHash
result = await grpc.services.Router.sendToRoute({

// Add payment secret into the route.
const paymentAddress = getTag(payReq, 'payment_secret')
if (paymentAddress) {
routeToUse.hops[routeToUse.hops.length - 1].tlvPayload = true
routeToUse.hops[routeToUse.hops.length - 1].mppRecord = {
paymentAddr: Buffer.from(paymentAddress, 'hex'),
totalAmtMsat: routeToUse.totalAmtMsat - routeToUse.totalFeesMsat,
}
}

result = await grpc.services.Router.sendToRouteV2({
paymentHash: route.paymentHash ? Buffer.from(route.paymentHash, 'hex') : null,
route: routeToUse,
})

if (result.failure) {
throw new Error(result.failure.code)
}
Expand Down
3 changes: 3 additions & 0 deletions renderer/reducers/payment/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export const prepareBolt11Probe = (payReq, feeLimit) => {
// Extract route hints from the invoice.
const routingInfo = getTag(invoice, 'routing_info') || []
const paymentHash = getTag(invoice, 'payment_hash')
const paymentAddress = getTag(invoice, 'payment_secret')

const hopHints = routingInfo.map(hint => ({
nodeId: hint.pubkey,
chanId: chanNumber({ id: hint.short_channel_id }).number,
Expand All @@ -191,6 +193,7 @@ export const prepareBolt11Probe = (payReq, feeLimit) => {
finalCltvDelta: getTag(invoice, 'min_final_cltv_expiry') || DEFAULT_CLTV_DELTA,
routeHints: [{ hopHints }],
paymentHash: generateProbeHash(paymentHash),
paymentAddr: Buffer.from(paymentAddress, 'hex'),
}
}

Expand Down
2 changes: 1 addition & 1 deletion stories/containers/activity.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import { storiesOf } from '@storybook/react'
import { action } from '@storybook/addon-actions'
import { boolean } from '@storybook/addon-knobs'
import lightningPayReq from 'bolt11'
import lightningPayReq from '@ln-zap/bolt11'
import { Modal } from 'components/UI'
import { InvoiceModal } from 'components/Activity/InvoiceModal'
import { PaymentModal } from 'components/Activity/PaymentModal'
Expand Down
2 changes: 1 addition & 1 deletion stories/containers/request/request.component.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react'
import { storiesOf } from '@storybook/react'
import { action } from '@storybook/addon-actions'
import lightningPayReq from 'bolt11'
import lightningPayReq from '@ln-zap/bolt11'
import { convert } from '@zap/utils/btc'
import { RequestSummary } from 'components/Request'
import { Provider } from '../../Provider'
Expand Down
2 changes: 1 addition & 1 deletion stories/helpers.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 { Box, Flex } from 'rebass/styled-components'
import lightningPayReq from 'bolt11'
import lightningPayReq from '@ln-zap/bolt11'
import { Bar, Heading, Page } from '@zap/renderer/components/UI'

export const Window = props => <Page height="calc(100vh - 40px)" {...props} />
Expand Down
12 changes: 6 additions & 6 deletions utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import get from 'lodash/get'
import config from 'config'
import range from 'lodash/range'
import { address } from 'bitcoinjs-lib'
import lightningRequestReq from 'bolt11'
import lightningRequestReq from '@ln-zap/bolt11'
import bip21 from 'bip21'
import coininfo from 'coininfo'
import { CoinBig } from '@zap/utils/coin'
Expand All @@ -23,10 +23,10 @@ export const networks = {

export const coinTypes = {
bitcoin: {
mainnet: 'bitcoin',
testnet: 'testnet',
regtest: 'regtest',
simnet: 'simnet',
mainnet: 'bc',
testnet: 'tb',
regtest: 'bcrt',
simnet: 'sb',
},
}

Expand Down Expand Up @@ -180,7 +180,7 @@ export const isBolt11 = (input, chain = 'bitcoin', network = 'mainnet') => {
}
try {
const decoded = lightningRequestReq.decode(input)
if (decoded.coinType !== get(coinTypes, `${chain}.${network}`)) {
if (decoded.network.bech32 !== get(coinTypes, `${chain}.${network}`)) {
throw new Error('Invalid coin type')
}
return true
Expand Down

0 comments on commit bd8cb54

Please sign in to comment.