Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@iota/converter

Package Overview
Dependencies
Maintainers
5
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@iota/converter

Convert values & trytes to trits and back

  • 1.0.0-beta.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
decreased by-0.56%
Maintainers
5
Weekly downloads
 
Created
Source

@iota/converter

Methods for converting ascii, values & trytes to trits and back.

Installation

Install using npm:

npm install @iota/converter

or using yarn:

yarn add @iota/converter

API Reference

converter.asciiToTrytes(input)

ParamTypeDescription
inputstringascii input

Converts an ascii encoded string to trytes.

How conversion works:

An ascii value of 1 Byte can be represented in 2 Trytes:

  1. We get the decimal unicode value of an individual ASCII character.

  2. From the decimal value, we then derive the two tryte values by calculating the tryte equivalent (e.g.: 100 is expressed as 19 + 3 * 27), given that tryte alphabet contains 27 trytes values: a. The first tryte value is the decimal value modulo 27 (which is the length of the alphabet). b. The second value is the remainder of decimal value - first value devided by 27.

  3. The two values returned from Step 2. are then input as indices into the available trytes alphabet (9ABCDEFGHIJKLMNOPQRSTUVWXYZ), to get the correct tryte value.

Example:

Lets say we want to convert ascii character Z.

  1. Z has a decimal unicode value of 90.

  2. 90 can be represented as 9 + 3 * 27. To make it simpler: a. First value is 90 % 27 = 9. b. Second value is (90 - 9) / 27 = 3.

  3. Our two values are 9 and 3. To get the tryte value now we simply insert it as indices into the tryte alphabet: a. The first tryte value is '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[9] = I b. The second tryte value is '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[3] = C

Therefore ascii character Z is represented as IC in trytes.

Returns: string - string of trytes

converter.trytesToAscii(trytes)

ParamTypeDescription
trytesstringtrytes

Converts trytes of even length to an ascii string

Returns: string - string in ascii

converter.trits(input)

ParamTypeDescription
inputString | NumberTryte string or value to be converted.

Converts trytes or values to trits

Returns: Int8Array - trits

converter.trytes(trits)

ParamType
tritsInt8Array

Converts trits to trytes

Returns: String - trytes

converter.value(trits)

ParamType
tritsInt8Array

Converts trits into an integer value

converter.fromValue(value)

ParamType
valueNumber

Converts an integer value to trits

Returns: Int8Array - trits

Keywords

FAQs

Package last updated on 21 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc