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

@iftt/tryte-encode-decode

v1.7.0

Published

Trytes are the encoded data type used by IOTA. This module encodes and decodes trytes.

Downloads

10

Readme

tryte-encode-decode travis npm downloads JavaScript Style Guide

About

Data comes in many shapes and sizes. Storing all data types as strings would be very inefficient in trytes. That is where this module comes in. Whether it be strings, numbers, boolean, arrays, dates, etc. This module aims to encode the data to trytes and also bring it back to it's original type and value.

Supported Encodings

  • [x] String
  • [x] 8-bit Integer [-128 to 127]
  • [x] 16-bit Integer [-32,768 to 32,767]
  • [x] 32-bit Integer [-2,147,483,648 to 2,147,483,647]
  • [ ] 64-bit Integer
  • [x] 8-bit Unsigned Integer [0 to 255]
  • [x] 16-bit Unsigned Integer [0 to 65,535]
  • [x] 32-bit Unsigned Integer [0 to 4,294,967,295]
  • [ ] 64-bit Unsigned Integer
  • [x] Boolean
  • [x] DateUTC
  • [x] Geospatial Coordinates
  • [x] Array

Further Reading

Understanding IOTA Trits & Trytes

Install

# npm
npm install --save tryte-encode-decode

# yarn
yarn add tryte-encode-decode

Example

// import package
// ES5
const tryteConverter = require('@iftt/tryte-encode-decode');
// ES6
import tryteConverter from '@iftt/tryte-encode-decode';

let trytes = tryteConverter.stringToTrytes('This is a test.');
let decodedTrytes = tryteConverter.trytesToString(trytes);

// decodedTrytes === 'This is a test.'

Debug

If you need to debug use the string tryte-encode-decode

DEBUG=tryte-encode-decode node x

API

stringToTrytes

stringToTrytes(str?: string): string [2 trytes per character]

  • encode string to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.stringToTrytes('This is a test.');

// trytes === 'CCWCXCGDEAXCGDEAPCEAHDTCGDHDSA'

trytesToString

trytesToString(str?: string): string

  • decode trytes back to a string
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.stringToTrytes('This is a test.');
let decodedTrytes = tryteConverter.trytesToString(trytes);

// decodedTrytes === 'This is a test.'

dateToTrytes

dateToTrytes(date?: Date): string [7 trytes]

  • encode a date to a string
  • NOTICE: This uses UTC encoding, so any date below January 1st, 1970 will resolve to the aforementioned date.
  • encoded dates are accurate to seconds (not milliseconds)
const tryteConverter = require('@iftt/tryte-encode-decode');

let date = new Date('2019-03-18T04:39:00.000Z');
let trytes = tryteConverter.dateToTrytes(date);

// trytes === 'D9F9RH9'

trytesToDate

trytesToDate(str?: string): Date

  • decode trytes back to a date
  • NOTICE: This uses UTC encoding, so any date below January 1st, 1970 will resolve to the aforementioned date.
const tryteConverter = require('@iftt/tryte-encode-decode');

let date = new Date('2019-03-18T04:39:00.000Z');
let trytes = tryteConverter.dateToTrytes(date);
let decodedTrytes = tryteConverter.trytesToDate(trytes);

// decodedTrytes === '2019-03-18T04:39:00.000Z'

int8ToTrytes

int8ToTrytes(num?: number): string [2 trytes]

  • encode 8-bit sized numbers to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.int8ToTrytes(127);

// trytes === 'IL'

trytesToInt8

trytesToInt8(str?: string): number

  • decode trytes to 8-bit sized numbers
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.int8ToTrytes(127);
let decodedTrytes = tryteConverter.trytesToInt8(trytes);

// decodedTrytes === 127

uInt8ToTrytes

uInt8ToTrytes(num?: number): string [2 trytes]

  • encode 8-bit unsigned sized numbers to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.uInt8ToTrytes(255);

// trytes === 'IL'

int16ToTrytes

int16ToTrytes(num?: number): string [4 trytes]

  • encode 16-bit sized numbers to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.int16ToTrytes(32767);

// trytes === 'CHXF'

trytesToInt16

trytesToInt16(str?: string): number

  • decode trytes to 16-bit sized numbers
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.int16ToTrytes(32767);
let decodedTrytes = tryteConverter.trytesToInt16(trytes);

// decodedTrytes === 32767

uInt16ToTrytes

uInt16ToTrytes(num?: number): string [4 trytes]

  • encode 16-bit unsigned sized numbers to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.uInt16ToTrytes(65535);

// trytes === 'CHXF'

int32ToTrytes

int32ToTrytes(num?: number): string [7 trytes]

  • encode 32-bit sized numbers to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.int32ToTrytes(2147483647);

// trytes === 'KBHSYMU'

trytesToInt32

trytesToInt32(str?: string): number

  • decode trytes to 32-bit sized numbers
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.int32ToTrytes(2147483647);
let decodedTrytes = tryteConverter.trytesToInt32(trytes);

// decodedTrytes === 2147483647

uInt32ToTrytes

uInt32ToTrytes(num?: number): string [7 trytes]

  • encode 32-bit unsigned sized numbers to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.uInt32ToTrytes(4294967295);

// trytes === 'KBHSYMU'

trytesToUInt

trytesToUInt(str?: string): number

  • decode trytes to any sized unsigned number
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.uInt32ToTrytes(4294967295);
let decodedTrytes = tryteConverter.trytesToUInt(trytes);

// decodedTrytes === 4294967295

booleanToTryte

booleanToTryte(bool?: boolean): string [1 tryte]

  • encode a boolean value to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.booleanToTryte(false);

// trytes === '9'

tryteToBoolean

tryteToBoolean(str?: string): boolean

  • decode a tryte to a boolean value
const tryteConverter = require('@iftt/tryte-encode-decode');

let trytes = tryteConverter.booleanToTryte(false);
let decodedTrytes = tryteConverter.tryteToBoolean(trytes);

// decodedTrytes === false

geoToTrytes

geoToTrytes(geo: { lat: number, lon: number }): string

  • encode geospatial coordinates to trytes
let trytes = tryteConverter.geoToTrytes({ lat: 52.529562, lon: 13.413047 });

// trytes === 'NPHTQORL9XKP'

trytesToGeo

trytesToGeo(trytes: string): { lat: number, lon: number }

  • decode trytes to geospatial coordinates
let trytes = tryteConverter.geoToTrytes({ lat: 52.529562, lon: 13.413047 });
let decodedTrytes = tryteConverter.trytesToGeo(trytes);

// decodedTrytes === { lat: 52.52956250000001, lon : 13.413046874999981 }

type DataTypes

enumerable string

  • 'string' | 'int8' | 'uint8' | 'int16' | 'uint16' | 'int32' | 'uint32' | 'bool' | 'date'

arrayToTrytes

arrayToTrytes(arr?: array<any>, type: DataTypes): string [2 trytes arraySize + arraySize * type encoding size]

  • encode an array of values to trytes
const tryteConverter = require('@iftt/tryte-encode-decode');

let array = ['a', 'b', 'c', 'd', 'e'];
let trytes = tryteConverter.arrayToTrytes(array, 'string');

// trytes === '9E999BPC999BQC999BRC999BSC999BTC'

trytesToArray

trytesToArray(str?: string, type: DataTypes): array<any>

  • decode trytes to an array of values
const tryteConverter = require('@iftt/tryte-encode-decode');

let array = ['a', 'b', 'c', 'd', 'e'];
let trytes = tryteConverter.arrayToTrytes(array, 'string');
let decodedTrytes = tryteConverter.trytesToArray(trytes, 'string');

// decodedTrytes === ['a', 'b', 'c', 'd', 'e']

ISC License (ISC)

Copyright 2019 Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC") Copyright (c) 1995-2003 by Internet Software Consortium

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.