TLV Parser for Node.js

1.5.14 · active · verified Tue Apr 21

The `node-tlv` library provides a robust parser and builder for Tag-Length-Value (TLV) encoded data, commonly found in smart card protocols like EMV and ISO7816. It supports parsing both simple TLV and BER-TLV structures, allowing developers to extract and manipulate data elements such as DGI, EMV, ISO7816, and DOL tags. Currently at stable version 1.5.14, the package does not explicitly state a release cadence, but its versioning and continuous updates suggest ongoing maintenance. Its key differentiator lies in its focused support for financial and smart card industry standards, offering both parsing of raw hexadecimal strings into structured TLV objects and the ability to programmatically construct complex TLV hierarchies, useful for building responses like PPSE FCI or PSE records. It is designed for Node.js environments, providing a programmatic interface to navigate and construct TLV data structures.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a raw hexadecimal string containing TLV data into a structured object, navigate its child elements, and find specific tags by their hexadecimal value or string representation, crucial for EMV transaction processing.

const TLV = require('node-tlv');
const assert = require('assert');

// A sample GPO (Get Processing Options) response in hex string format.
const resp = '770E8202580094080801010010010301';

// Parse the raw hexadecimal string into a TLV object tree.
const tlv = TLV.parse(resp);

// Assertions to verify the top-level TLV object's tag, length, and value.
assert(tlv.getTag() === '77');
assert(tlv.getLength() === 14); // Length in bytes for the value '8202580094080801010010010301'
assert(tlv.getValue() === '8202580094080801010010010301');

// Retrieve child TLV objects from the parsed structure.
const child = tlv.getChild();
assert(child.length === 2);
assert(child[0].getTag() === '82');

// Find a specific TLV tag using either a number or string representation.
const aip = tlv.find(0x82); // Application Interchange Profile (AIP)
assert(aip.getTag() === '82');
assert(aip.getLength() === 2);
assert(aip.getValue() === '5800');

const afl = tlv.find('94'); // Application File Locator (AFL)
assert(afl.getTag() === '94');
assert(afl.getLength() === 8);
assert(afl.getValue() === '0801010010010301');

view raw JSON →