2024-10-02 20:03:30 +00:00
|
|
|
import { cachedFetcher } from '@/lib/fetch'
|
2024-08-13 14:48:30 +00:00
|
|
|
import { toPositiveNumber } from '@/lib/validate'
|
2024-11-06 02:08:33 +00:00
|
|
|
import { authenticatedLndGrpc, getIdentity, getHeight, getWalletInfo, getNode } from 'ln-service'
|
2021-04-29 21:58:43 +00:00
|
|
|
|
2024-10-16 16:25:04 +00:00
|
|
|
const lnd = global.lnd || authenticatedLndGrpc({
|
2021-06-11 00:49:20 +00:00
|
|
|
cert: process.env.LND_CERT,
|
2021-06-09 21:29:15 +00:00
|
|
|
macaroon: process.env.LND_MACAROON,
|
|
|
|
socket: process.env.LND_SOCKET
|
2024-10-16 16:25:04 +00:00
|
|
|
}).lnd
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') global.lnd = lnd
|
2021-04-29 21:58:43 +00:00
|
|
|
|
2023-05-17 01:09:55 +00:00
|
|
|
// Check LND GRPC connection
|
2024-10-02 20:03:30 +00:00
|
|
|
getWalletInfo({ lnd }, (err, result) => {
|
2023-05-17 01:09:55 +00:00
|
|
|
if (err) {
|
|
|
|
console.error('LND GRPC connection error')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log('LND GRPC connection successful')
|
|
|
|
})
|
|
|
|
|
2024-08-13 14:48:30 +00:00
|
|
|
export async function estimateRouteFee ({ lnd, destination, tokens, mtokens, request, timeout }) {
|
|
|
|
return await new Promise((resolve, reject) => {
|
2024-10-17 18:25:49 +00:00
|
|
|
const params = {}
|
|
|
|
if (request) {
|
|
|
|
params.payment_request = request
|
|
|
|
} else {
|
|
|
|
params.dest = Buffer.from(destination, 'hex')
|
|
|
|
params.amt_sat = tokens ? toPositiveNumber(tokens) : toPositiveNumber(BigInt(mtokens) / BigInt(1e3))
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:48:30 +00:00
|
|
|
lnd.router.estimateRouteFee({
|
2024-10-17 18:25:49 +00:00
|
|
|
...params,
|
2024-08-13 14:48:30 +00:00
|
|
|
timeout
|
|
|
|
}, (err, res) => {
|
|
|
|
if (err) {
|
2024-10-17 18:25:49 +00:00
|
|
|
if (res?.failure_reason) {
|
|
|
|
reject(new Error(`Unable to estimate route: ${res.failure_reason}`))
|
|
|
|
} else {
|
|
|
|
reject(err)
|
|
|
|
}
|
2024-08-13 14:48:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.routing_fee_msat < 0 || res.time_lock_delay <= 0) {
|
|
|
|
reject(new Error('Unable to estimate route, excessive values: ' + JSON.stringify(res)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve({
|
|
|
|
routingFeeMsat: toPositiveNumber(res.routing_fee_msat),
|
|
|
|
timeLockDelay: toPositiveNumber(res.time_lock_delay)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// created_height is the accepted_height, timeout is the expiry height
|
|
|
|
// ln-service remaps the `htlcs` field of lookupInvoice to `payments` and
|
|
|
|
// see: https://github.com/alexbosworth/lightning/blob/master/lnd_responses/htlc_as_payment.js
|
|
|
|
// and: https://lightning.engineering/api-docs/api/lnd/lightning/lookup-invoice/index.html#lnrpcinvoicehtlc
|
|
|
|
export function hodlInvoiceCltvDetails (inv) {
|
|
|
|
if (!inv.payments) {
|
|
|
|
throw new Error('No payments found')
|
|
|
|
}
|
|
|
|
if (!inv.is_held) {
|
|
|
|
throw new Error('Invoice is not held')
|
|
|
|
}
|
|
|
|
|
|
|
|
const acceptHeight = inv.payments.reduce((max, htlc) => {
|
|
|
|
const createdHeight = toPositiveNumber(htlc.created_height)
|
|
|
|
return createdHeight > max ? createdHeight : max
|
|
|
|
}, 0)
|
|
|
|
const expiryHeight = inv.payments.reduce((min, htlc) => {
|
|
|
|
const timeout = toPositiveNumber(htlc.timeout)
|
|
|
|
return timeout < min ? timeout : min
|
|
|
|
}, Number.MAX_SAFE_INTEGER)
|
|
|
|
|
|
|
|
return {
|
|
|
|
expiryHeight: toPositiveNumber(expiryHeight),
|
|
|
|
acceptHeight: toPositiveNumber(acceptHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPaymentFailureStatus (withdrawal) {
|
|
|
|
if (withdrawal && !withdrawal.is_failed) {
|
|
|
|
throw new Error('withdrawal is not failed')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (withdrawal?.failed.is_insufficient_balance) {
|
2024-10-02 20:03:30 +00:00
|
|
|
return {
|
|
|
|
status: 'INSUFFICIENT_BALANCE',
|
|
|
|
message: 'you didn\'t have enough sats'
|
|
|
|
}
|
2024-08-13 14:48:30 +00:00
|
|
|
} else if (withdrawal?.failed.is_invalid_payment) {
|
2024-10-02 20:03:30 +00:00
|
|
|
return {
|
|
|
|
status: 'INVALID_PAYMENT',
|
|
|
|
message: 'invalid payment'
|
|
|
|
}
|
2024-08-13 14:48:30 +00:00
|
|
|
} else if (withdrawal?.failed.is_pathfinding_timeout) {
|
2024-10-02 20:03:30 +00:00
|
|
|
return {
|
|
|
|
status: 'PATHFINDING_TIMEOUT',
|
|
|
|
message: 'no route found'
|
|
|
|
}
|
2024-08-13 14:48:30 +00:00
|
|
|
} else if (withdrawal?.failed.is_route_not_found) {
|
2024-10-02 20:03:30 +00:00
|
|
|
return {
|
|
|
|
status: 'ROUTE_NOT_FOUND',
|
|
|
|
message: 'no route found'
|
|
|
|
}
|
2024-08-13 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 20:03:30 +00:00
|
|
|
return {
|
|
|
|
status: 'UNKNOWN_FAILURE',
|
|
|
|
message: 'unknown failure'
|
|
|
|
}
|
2024-08-13 14:48:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 00:26:29 +00:00
|
|
|
export const getBlockHeight = cachedFetcher(async function fetchBlockHeight ({ lnd, ...args }) {
|
2024-10-02 20:03:30 +00:00
|
|
|
try {
|
2024-10-06 22:58:46 +00:00
|
|
|
const { current_block_height: height } = await getHeight({ lnd, ...args })
|
2024-10-02 20:03:30 +00:00
|
|
|
return height
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Unable to fetch block height: ${err.message}`)
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
maxSize: 1,
|
|
|
|
cacheExpiry: 60 * 1000, // 1 minute
|
2024-10-06 22:58:46 +00:00
|
|
|
forceRefreshThreshold: 5 * 60 * 1000, // 5 minutes
|
|
|
|
keyGenerator: () => 'getHeight'
|
2024-10-02 20:03:30 +00:00
|
|
|
})
|
|
|
|
|
2024-10-09 00:26:29 +00:00
|
|
|
export const getOurPubkey = cachedFetcher(async function fetchOurPubkey ({ lnd, ...args }) {
|
2024-10-02 20:03:30 +00:00
|
|
|
try {
|
2024-10-18 12:19:40 +00:00
|
|
|
const identity = await getIdentity({ lnd, ...args })
|
2024-10-02 20:03:30 +00:00
|
|
|
return identity.public_key
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Unable to fetch identity: ${err.message}`)
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
maxSize: 1,
|
|
|
|
cacheExpiry: 0, // never expire
|
2024-10-06 22:58:46 +00:00
|
|
|
forceRefreshThreshold: 0, // never force refresh
|
|
|
|
keyGenerator: () => 'getOurPubkey'
|
2024-10-02 20:03:30 +00:00
|
|
|
})
|
|
|
|
|
2024-10-09 00:26:29 +00:00
|
|
|
export const getNodeSockets = cachedFetcher(async function fetchNodeSockets ({ lnd, ...args }) {
|
2024-10-02 20:03:30 +00:00
|
|
|
try {
|
2024-10-06 22:58:46 +00:00
|
|
|
return (await getNode({ lnd, is_omitting_channels: true, ...args }))?.sockets
|
2024-10-02 20:03:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Unable to fetch node info: ${err.message}`)
|
|
|
|
}
|
|
|
|
}, {
|
2024-10-06 22:58:46 +00:00
|
|
|
maxSize: 100,
|
2024-10-02 20:03:30 +00:00
|
|
|
cacheExpiry: 1000 * 60 * 60 * 24, // 1 day
|
2024-10-06 22:58:46 +00:00
|
|
|
forceRefreshThreshold: 1000 * 60 * 60 * 24 * 7, // 1 week
|
|
|
|
keyGenerator: (args) => {
|
|
|
|
const { public_key: publicKey } = args
|
|
|
|
return publicKey
|
|
|
|
}
|
2024-10-02 20:03:30 +00:00
|
|
|
})
|
|
|
|
|
2021-06-09 21:29:15 +00:00
|
|
|
export default lnd
|