This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathasciiToTrytes.js
99 lines (79 loc) · 3.34 KB
/
asciiToTrytes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Conversion of ascii encoded bytes to trytes.
// Input is a string (can be stringified JSON object), return value is Trytes
//
// How the conversion works:
// 2 Trytes === 1 Byte
// There are a total of 27 different tryte values: 9ABCDEFGHIJKLMNOPQRSTUVWXYZ
//
// 1. We get the decimal value of an individual ASCII character
// 2. From the decimal value, we then derive the two tryte values by basically calculating the tryte equivalent (e.g. 100 === 19 + 3 * 27)
// a. The first tryte value is the decimal value modulo 27 (27 trytes)
// b. The second value is the remainder (decimal value - first value), divided by 27
// 3. The two values returned from Step 2. are then input as indices into the available values list ('9ABCDEFGHIJKLMNOPQRSTUVWXYZ') to get the correct tryte value
//
// EXAMPLES
// Lets say we want to convert the ASCII character "Z".
// 1. 'Z' has a decimal value of 90.
// 2. 90 can be represented as 9 + 3 * 27. To make it simpler:
// a. First value: 90 modulo 27 is 9. This is now our first value
// b. Second value: (90 - 9) / 27 is 3. This is our second value.
// 3. Our two values are now 9 and 3. To get the tryte value now we simply insert it as indices into '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'
// a. The first tryte value is '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[9] === "I"
// b. The second tryte value is '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'[3] === "C"
// Our tryte pair is "IC"
//
// RESULT:
// The ASCII char "Z" is represented as "IC" in trytes.
//
function toTrytes(input) {
// If input is not a string, return null
if ( typeof input !== 'string' ) return null
var TRYTE_VALUES = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var trytes = "";
for (var i = 0; i < input.length; i++) {
var char = input[i];
var asciiValue = char.charCodeAt(0);
// If not recognizable ASCII character, return null
if (asciiValue > 255) {
//asciiValue = 32
return null;
}
var firstValue = asciiValue % 27;
var secondValue = (asciiValue - firstValue) / 27;
var trytesValue = TRYTE_VALUES[firstValue] + TRYTE_VALUES[secondValue];
trytes += trytesValue;
}
return trytes;
}
//
// Trytes to bytes
// Reverse operation from the byteToTrytes function in send.js
// 2 Trytes == 1 Byte
// We assume that the trytes are a JSON encoded object thus for our encoding:
// First character = {
// Last character = }
// Everything after that is 9's padding
//
function fromTrytes(inputTrytes) {
// If input is not a string, return null
if ( typeof inputTrytes !== 'string' ) return null
// If input length is odd, return null
if ( inputTrytes.length % 2 ) return null
var TRYTE_VALUES = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var outputString = "";
for (var i = 0; i < inputTrytes.length; i += 2) {
// get a trytes pair
var trytes = inputTrytes[i] + inputTrytes[i + 1];
var firstValue = TRYTE_VALUES.indexOf(trytes[0]);
var secondValue = TRYTE_VALUES.indexOf(trytes[1]);
var decimalValue = firstValue + secondValue * 27;
var character = String.fromCharCode(decimalValue);
outputString += character;
}
return outputString;
}
module.exports = {
toTrytes: toTrytes,
fromTrytes: fromTrytes
}