From f3979af4562fbcb642d73a04863618727102142c Mon Sep 17 00:00:00 2001 From: nymkappa Date: Thu, 11 Dec 2025 12:05:47 +0100 Subject: [PATCH] better handling at .split on undefined errors --- backend/src/api/common.ts | 23 +++++++++++++++---- backend/src/api/explorer/channels.api.ts | 3 +++ backend/src/api/explorer/nodes.routes.ts | 2 +- backend/src/api/transaction-utils.ts | 2 +- .../sync-tasks/funding-tx-fetcher.ts | 6 ++++- .../lightning/sync-tasks/node-locations.ts | 2 +- .../lightning/sync-tasks/stats-importer.ts | 3 +++ backend/src/utils/bitcoin-script.ts | 2 +- 8 files changed, 34 insertions(+), 9 deletions(-) diff --git a/backend/src/api/common.ts b/backend/src/api/common.ts index 0f14e0c92..0064b7710 100644 --- a/backend/src/api/common.ts +++ b/backend/src/api/common.ts @@ -241,7 +241,7 @@ export class Common { return true; } // scriptsig-not-pushonly - if (vin.scriptsig_asm) { + if (vin.scriptsig_asm?.length) { for (const op of vin.scriptsig_asm.split(' ')) { if (opcodes[op] && opcodes[op] > opcodes['OP_16']) { return true; @@ -508,7 +508,7 @@ export class Common { } static setLegacySighashFlags(flags: bigint, scriptsig_asm: string): bigint { - for (const item of scriptsig_asm.split(' ')) { + for (const item of scriptsig_asm?.split(' ') ?? []) { // skip op_codes if (item.startsWith('OP_')) { continue; @@ -933,6 +933,13 @@ export class Common { } static findSocketNetwork(addr: string): {network: string | null, url: string} { + if (!addr?.length) { + return { + network: null, + url: '' + }; + } + let network: string | null = null; let url: string = addr; @@ -940,7 +947,7 @@ export class Common { url = addr.split('://')[1]; } - if (!url) { + if (!url?.length) { return { network: null, url: addr, @@ -966,7 +973,15 @@ export class Common { }; } } else if (addr.indexOf('ipv6') !== -1 || (config.LIGHTNING.BACKEND === 'lnd' && url.indexOf(']:'))) { - url = url.split('[')[1].split(']')[0]; + const parts = url.split('['); + if (parts.length < 2) { + return { + network: null, + url: addr, + }; + } else { + url = parts[1].split(']')[0]; + } const ipv = isIP(url); if (ipv === 6) { const parts = addr.split(':'); diff --git a/backend/src/api/explorer/channels.api.ts b/backend/src/api/explorer/channels.api.ts index 2faf06c33..514239d2b 100644 --- a/backend/src/api/explorer/channels.api.ts +++ b/backend/src/api/explorer/channels.api.ts @@ -580,6 +580,9 @@ class ChannelsApi { * Save or update a channel present in the graph */ public async $saveChannel(channel: ILightningApi.Channel, status = 1): Promise { + if (!channel.chan_point?.length) { + return; + } const [ txid, vout ] = channel.chan_point.split(':'); const policy1: Partial = channel.node1_policy || {}; diff --git a/backend/src/api/explorer/nodes.routes.ts b/backend/src/api/explorer/nodes.routes.ts index 811292b4b..6f9539fcb 100644 --- a/backend/src/api/explorer/nodes.routes.ts +++ b/backend/src/api/explorer/nodes.routes.ts @@ -354,7 +354,7 @@ class NodesRoutes { return; } - const nodes = await nodesApi.$getNodesPerISP(req.params.isp); + const nodes = await nodesApi.$getNodesPerISP(req.params.isp || ''); res.header('Pragma', 'public'); res.header('Cache-control', 'public'); res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString()); diff --git a/backend/src/api/transaction-utils.ts b/backend/src/api/transaction-utils.ts index dc1012aad..4ee2ed688 100644 --- a/backend/src/api/transaction-utils.ts +++ b/backend/src/api/transaction-utils.ts @@ -267,7 +267,7 @@ class TransactionUtils { return; } - if (vin.prevout.scriptpubkey_type === 'p2sh') { + if (vin.prevout.scriptpubkey_type === 'p2sh' && vin.scriptsig_asm?.length) { const redeemScript = vin.scriptsig_asm.split(' ').reverse()[0]; vin.inner_redeemscript_asm = this.convertScriptSigAsm(redeemScript); if (vin.witness && vin.witness.length > 2) { diff --git a/backend/src/tasks/lightning/sync-tasks/funding-tx-fetcher.ts b/backend/src/tasks/lightning/sync-tasks/funding-tx-fetcher.ts index d0b92f42e..c279cfb60 100644 --- a/backend/src/tasks/lightning/sync-tasks/funding-tx-fetcher.ts +++ b/backend/src/tasks/lightning/sync-tasks/funding-tx-fetcher.ts @@ -74,11 +74,15 @@ class FundingTxFetcher { public async $fetchChannelOpenTx(channelId: string): Promise<{timestamp: number, txid: string, value: number} | null> { channelId = Common.channelIntegerIdToShortId(channelId); + if (!channelId?.length) { + return null; + } + if (this.fundingTxCache[channelId]) { return this.fundingTxCache[channelId]; } - const parts = channelId.split('x'); + const parts = channelId?.split('x') ?? []; if (parts.length < 3) { logger.debug(`Channel ID ${channelId} does not seem valid, should contains at least 3 parts separated by 'x'`, logger.tags.ln); return null; diff --git a/backend/src/tasks/lightning/sync-tasks/node-locations.ts b/backend/src/tasks/lightning/sync-tasks/node-locations.ts index 17974275c..8e791859f 100644 --- a/backend/src/tasks/lightning/sync-tasks/node-locations.ts +++ b/backend/src/tasks/lightning/sync-tasks/node-locations.ts @@ -25,7 +25,7 @@ export async function $lookupNodeLocation(): Promise { } catch (e) { } for (const node of nodes) { - const sockets: string[] = node.sockets.split(','); + const sockets: string[] = node.sockets?.split(',') ?? []; for (const socket of sockets) { const ip = socket.substring(0, socket.lastIndexOf(':')).replace('[', '').replace(']', ''); const hasClearnet = [4, 6].includes(net.isIP(ip)); diff --git a/backend/src/tasks/lightning/sync-tasks/stats-importer.ts b/backend/src/tasks/lightning/sync-tasks/stats-importer.ts index 14ad82d7e..86f891d59 100644 --- a/backend/src/tasks/lightning/sync-tasks/stats-importer.ts +++ b/backend/src/tasks/lightning/sync-tasks/stats-importer.ts @@ -108,6 +108,9 @@ class LightningStatsImporter { for (const channel of networkGraph.edges) { const short_id = Common.channelIntegerIdToShortId(channel.channel_id); + if (!short_id?.length) { + continue; + } const tx = await fundingTxFetcher.$fetchChannelOpenTx(short_id); if (!tx) { diff --git a/backend/src/utils/bitcoin-script.ts b/backend/src/utils/bitcoin-script.ts index f9755fcb4..f463d8f76 100644 --- a/backend/src/utils/bitcoin-script.ts +++ b/backend/src/utils/bitcoin-script.ts @@ -147,7 +147,7 @@ export { opcodes }; /** extracts m and n from a multisig script (asm), returns nothing if it is not a multisig script */ export function parseMultisigScript(script: string): void | { m: number, n: number } { - if (!script) { + if (!script?.length) { return; } const ops = script.split(' ');