npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@synonymdev/ln-constr-parser

v1.1.0

Published

Lightning Connection String Parser

Downloads

246

Readme

ln-constr-parser

NPM version

Convenient, dependency-free Lightning connection string parser for front- and backend.

Usage

import {parseConnectionString} from '@synonymdev/ln-constr-parser';

const connectionString = '0200000000a3eff613189ca6c4070c89206ad658e286751eca1f29262948247a5f@127.0.0.1:9735';
const {host, hostType, port, pubKey} = parseConnectionString(connectionString);

console.log(host); // 127.0.0.1
console.log(hostType); // ipv4. Depending on the address provided, it could also be ipv6, torv3 or domain.
console.log(port); // 9735
console.log(pubKey); // 0200000000a3eff613189ca6c4070c89206ad658e286751eca1f29262948247a5f

The port is optional. You can make the port mandatory so the parser will throw an error. This also avoids the ipv6 ambiguity problem (see below).

parseConnectionString(connectionString, {portMandatory: true})

Parse Errors

In case of an invalid connection string, the parser will throw a detailed error of what is wrong.

import {parseConnectionString, ParseFailureCode, ParseError} from '@synonymdev/ln-constr-parser';

try {
    const connectionString = 'pubkey@host:port';
    parseConnectionString(connectionString);
} catch (e) {
    if (e instanceof ParseError) {
        console.log('Parse failed. Reason:', e.code)
        if (e.code === ParseFailureCode.INVALID_HOST) {
            console.log('Host value is invalid.')
        } else if (e.codee === ParseFailureCode.INVALID_IPV6) {
            console.log('IPv6 host is invalid.')
        } else if (e.codee === ParseFailureCode.AMBIGUOUS_IPV6) {
            console.log('IPv6 host is ambiguous. Use square brackets [] like pubkey@[ipv6]:port.')
        } else if (e.code === ParseFailureCode.INVALID_PORT) {
            console.log('Invalid port.')
        } else if (e.code === ParseFailureCode.INVALID_PUBKEY) {
            console.log('Invalid lightning pubkey.')
        } else if (e.code === ParseFailureCode.INVALID_ATS) {
            console.log('The connection string must contain one single @ symbol.')
        }
    }
}

Supported Address Formats

The library supports all commonly used address formats in the form of

  • pubkey@host:port
  • pubkey@host
  • pubkey@[host]:port
  • pubkey@[host]

ipv4

ipv6

  • Square brackets pubkey@[2001:db8:3333:4444:5555:6666:7777:8888]:port.
  • Square brackets compressed pubkey@[2001:db8::8888]:port.
  • Square brackets no port pubkey@[2001:db8:3333:4444:5555:6666:7777:8888].
  • Square brackets compressed no port pubkey@[2001:db8::8888].
  • Regular uncompressed pubkey@2001:db8:3333:4444:5555:6666:7777:8888:port.
  • No port uncompressed pubkey@2001:db8:3333:4444:5555:6666:7777:8888.

⚠️ Ambiguity problem If you don't make the port mandatory, it is adviced to use square brackets [] with IPv6. It is impossible to separate a compressed IPv6 from the port. Example: 2001:db8::8888:9735 may as well be a IPv6 (2001:db8::8888:9735) OR a IPv6:port (2001:db8::8888 and port 9735). The library will do its best to parse it correctly but will throw an error in case of ambiguity.

torv3

  • Regular pubkey@giexynrrloc2fewstcybenljdksidtglfydecbellzkl63din6w73eid.onion:port.
  • No port pubkey@giexynrrloc2fewstcybenljdksidtglfydecbellzkl63din6w73eid.onion.
  • Square brackets pubkey@[giexynrrloc2fewstcybenljdksidtglfydecbellzkl63din6w73eid.onion]:port.
  • Square brackets no port pubkey@[giexynrrloc2fewstcybenljdksidtglfydecbellzkl63din6w73eid.onion].

domain

Parse Subcomponents

The library also provides functions to parse the individual components of a connection string.

import {parseAddress, parseHost, parsePort, parsePubkey} from '@synonymdev/ln-constr-parser';

const {host, hostType, port} = parseAddress('127.0.0.1:9000')
const {host, hostType} = parseHost('127.0.0.1')
const port = parsePort('900')
const pubkey = parsePubkey('0200000000a3eff613189ca6c4070c89206ad658e286751eca1f29262948247a5f')

May the ⚡ be with you.