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

Commit

Permalink
feat(lnd): support for interceptor nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed Nov 10, 2020
1 parent 801f20b commit 115cc21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
10 changes: 9 additions & 1 deletion renderer/reducers/payment/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import get from 'lodash/get'
import cloneDeep from 'lodash/cloneDeep'
import { chanNumber } from 'bolt07'
import { getTag, decodePayReq, getNodeAlias, generatePreimage } from '@zap/utils/crypto'
import {
getTag,
decodePayReq,
getNodeAlias,
generatePreimage,
generateProbeHash,
} from '@zap/utils/crypto'
import { convert } from '@zap/utils/btc'
import { getIntl } from '@zap/i18n'
import { sha256digest } from '@zap/utils/sha256'
Expand Down Expand Up @@ -169,6 +175,7 @@ export const prepareBolt11Probe = (payReq, feeLimit) => {

// Extract route hints from the invoice.
const routingInfo = getTag(invoice, 'routing_info') || []
const paymentHash = getTag(invoice, 'payment_hash')
const hopHints = routingInfo.map(hint => ({
nodeId: hint.pubkey,
chanId: chanNumber({ id: hint.short_channel_id }).number,
Expand All @@ -183,6 +190,7 @@ export const prepareBolt11Probe = (payReq, feeLimit) => {
amtMsat: millisatoshis,
finalCltvDelta: getTag(invoice, 'min_final_cltv_expiry') || DEFAULT_CLTV_DELTA,
routeHints: [{ hopHints }],
paymentHash: generateProbeHash(paymentHash),
}
}

Expand Down
9 changes: 0 additions & 9 deletions services/grpc/router.methods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { grpcLog } from '@zap/utils/log'
import { generatePreimage } from '@zap/utils/crypto'
import { logGrpcCmd } from './helpers'

export const KEYSEND_PREIMAGE_TYPE = '5482373484'
Expand All @@ -15,10 +14,6 @@ export const KEYSEND_PREIMAGE_TYPE = '5482373484'
* @returns {Promise} The route route when state is SUCCEEDED
*/
async function probePayment(payload) {
// Use a payload that has the payment hash set to some random bytes.
// This will cause the payment to fail at the final destination.
payload.paymentHash = new Uint8Array(generatePreimage())

logGrpcCmd('Router.probePayment', payload)

let result
Expand Down Expand Up @@ -94,10 +89,6 @@ async function probePayment(payload) {
* @returns {Promise} The route route when state is SUCCEEDED
*/
async function probePaymentV2(payload) {
// Use a payload that has the payment hash set to some random bytes.
// This will cause the payment to fail at the final destination.
payload.paymentHash = new Uint8Array(generatePreimage())

logGrpcCmd('Router.probePaymentV2', payload)

let result
Expand Down
18 changes: 18 additions & 0 deletions utils/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import bip21 from 'bip21'
import coininfo from 'coininfo'
import { CoinBig } from '@zap/utils/coin'
import { convert } from '@zap/utils/btc'
import { sha256digest } from '@zap/utils/sha256'

export const PREIMAGE_BYTE_LENGTH = 32

Expand Down Expand Up @@ -315,3 +316,20 @@ export const getFeeRange = (routes = []) => ({
* @returns {Uint8Array} hash bytes
*/
export const generatePreimage = () => randomBytes(PREIMAGE_BYTE_LENGTH)

/**
* generateHash - Generates probe hash from payment hash.
*
* @param {string} paymentHash payment hash (hex)
* @returns {Uint8Array} probe hash (bytes)
*/
export const generateProbeHash = paymentHash => {
const idx = Buffer.from('probing-01:', 'utf8')
const hash = Buffer.from(paymentHash, 'hex')
const totalLength = idx.length + hash.length

const probeHashBuffer = Buffer.concat([idx, hash], totalLength)
const probeHash = sha256digest(probeHashBuffer)

return probeHash
}

0 comments on commit 115cc21

Please sign in to comment.