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

lightning-tlv

v0.1.5

Published

Utility for encoding and parsing TLV packets as defined in Lightning Network's BOLT 1.

Downloads

4

Readme

lightning-tlv

Build Status

Utility for encoding and parsing TLV packets as defined in Lightning Network's BOLT 1.

Install

npm install lightning-tlv

Usage Examples

BigSize

Lightning's BigSize integer serialization, which is the big-endian counterparty of Bitcoin's CompactSize.

To use it, we first need to import the class.

import {BigSize} from 'lightning-tlv';

Serialize

const integer = 2038; // coincidentally, also the year of the timestamp problem
const bigSize = new BigSize(integer);
const serialization = bigSize.toBuffer();
console.log(serialization.toString('hex')); // fd07f6

Deserialize

const serialization = Buffer.from('fd07f6ae153910fd', 'hex');
const bigSize = BigSize.parse(serialization);
const bigintValue = bigSize.value;
const regularValue = Number(bigintValue);
const length = bigSize.length; // the parser knows where the BigSize portion ends
console.log(bigintValue); // 2038n
console.log(regularValue); // 2038
console.log(length); // 3

HopPayload TLV

One example TLV used in Lightning is the hop payload, which is a TLV stream consisting of three concatenated TLVs, which are the amount_to_forward (type 2), outgoing_cltv_value (type 4), and short_channel_id (type 6). Types 2 and 4 are a tu64 and a tu32, respectively, which are unsigned big-endian integers with trimmed leading zeroes. short_channel_id is simply a binary identifier.

To interact with TLVs, we first need to import the class. We will also need some of the custom type handlers.

import {TLV, TypeHandler} from 'lightning-tlv'

Serialize

const tu64Handler = new TypeHandler.tu64();
const tu32Handler = new TypeHandler.tu32();

const amountToForwardBuffer = tu64Handler.toBuffer(23);
const amountToForwardTlv = new TLV(2, amountToForwardBuffer);

const outgoingCltvValueBuffer = tu32Handler.toBuffer(34);
const outgoingCltvValueTlv = new TLV(4, outgoingCltvValueBuffer);

const channelIdTlv = new TLV(6, Buffer.from('abcdef10', 'hex'));

const tlvStream = Buffer.concat([amountToForwardTlv.toBuffer(), outgoingCltvValueTlv.toBuffer(), channelIdTlv.toBuffer()]);
console.log(tlvStream.toString('hex')); // 0201170401220604abcdef10

Deserialize

const tlvStream = Buffer.from('0201170401220604abcdef10', 'hex');
let remainingStream = tlvStream;

const amountToForwardTlv = TLV.parse(tlvStream);
remainingStream = remainingStream.slice(amountToForwardTlv.tlvSize);

const outgoingCltvValueTlv = TLV.parse(remainingStream);
remainingStream = remainingStream.slice(outgoingCltvValueTlv.tlvSize);

const channelIdTlv = TLV.parse(remainingStream);

const tu64Handler = new TypeHandler.tu64();
const tu32Handler = new TypeHandler.tu32();

const amountToForward = tu64Handler.fromBuffer(amountToForwardTlv.value);
const outgoingCltvValue = tu32Handler.fromBuffer(outgoingCltvValueTlv.value);
const channelId = channelIdTlv.value;
console.log(amountToForward); // 23n
console.log(outgoingCltvValue); // 34
console.log(channelId.toString('hex')); // abcdef10

License

MIT