Git Product home page Git Product logo

ln-constr-parser's Introduction

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.

ln-constr-parser's People

Contributors

severinalexb avatar

Stargazers

22388o⚡️  avatar Miguel Medeiros avatar Philipp Walter avatar Reza avatar  avatar

Watchers

 avatar John Carvalho avatar

Forkers

22388o

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.