Skip to content

Instantly share code, notes, and snippets.

@SeverinAlexB
Last active April 2, 2024 11:06
Show Gist options
  • Save SeverinAlexB/76bef46ca35c81ffa553f945709037fb to your computer and use it in GitHub Desktop.
Save SeverinAlexB/76bef46ca35c81ffa553f945709037fb to your computer and use it in GitHub Desktop.
Lightning Network short channel id (CLN) to decimal id (LND) converter
/** By LnRouter.app */
function bitShift(n: number, shiftBy: number): number {
let base = n;
for (let i = 0; i < shiftBy; i++) {
base = base * 2;
}
return base;
}
export function shortChannelIdToDecimalId(shortChannelId: string): string {
const [blockHeightS, transactionIndexS, outputIndexS] = shortChannelId.split("x");
const blockHeight = Number.parseInt(blockHeightS);
const transactionIndex = Number.parseInt(transactionIndexS);
const outputIndex = Number.parseInt(outputIndexS);
const result = BigInt(bitShift(blockHeight, 40)) | BigInt(bitShift(transactionIndex, 16)) | BigInt(outputIndex);
return result.toString();
}
const decimalId = shortChannelIdToDecimalId('799046x927x0')
console.log(decimalId) // 878560368188653568
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment