-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyzoString.js
210 lines (163 loc) · 6.94 KB
/
nyzoString.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class NyzoStringPrivateSeed {
constructor(seed) {
this.seed = seed;
}
getSeed() {
return this.seed;
}
}
class NyzoStringPublicIdentifier {
constructor(identifier) {
this.identifier = identifier;
}
getIdentifier() {
return this.identifier;
}
}
class NyzoStringPrefilledData {
constructor(receiverIdentifier, senderData) {
this.receiverIdentifier = receiverIdentifier;
this.senderData = senderData;
}
getReceiverIdentifier() {
return this.receiverIdentifier;
}
getSenderData() {
return this.senderData;
}
}
var characterLookup = ('0123456789' +
'abcdefghijkmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNPQRSTUVWXYZ' +
'-.~_').split('');
var characterToValueMap = [];
for (var i = 0; i < characterLookup.length; i++) {
characterToValueMap[characterLookup[i]] = i;
}
function arraysAreEqual(array1, array2) {
var arraysAreEqual;
if (array1 == null || array2 == null) {
arraysAreEqual = array1 == null && array2 == null;
} else {
arraysAreEqual = array1.length == array2.length;
for (var i = 0; i < array1.length && arraysAreEqual; i++) {
if (array1[i] != array2[i]) {
arraysAreEqual = false;
}
}
}
return arraysAreEqual;
}
function decode(encodedString) {
var result = null;
// Trim the string.
encodedString = encodedString.trim();
// Map characters from the old encoding to the new encoding. A few characters were changed to make Nyzo strings more
// URL-friendly.
encodedString = encodedString.replace(/\*/g, '-').replace(/\+/g, '.').replace(/=/g, '~');
// Map characters that may be mistyped. Nyzo strings contain neither 'l' nor 'O'.
encodedString = encodedString.replace(/l/g, '1').replace(/O/g, '0');
// Get the type from the prefix.
var prefix = encodedString.substring(0, 4);
// Get the array representation of the encoded string.
var expandedArray = byteArrayForEncodedString(encodedString);
// Get the content length from the next byte and calculate the checksum length.
var contentLength = expandedArray[3] & 0xff;
var checksumLength = expandedArray.length - contentLength - 4;
// Only continue if the checksum length is valid.
if (checksumLength >= 4 && checksumLength <= 6) {
// Calculate the checksum and compare it to the provided checksum. Only create the result array if the checksums
// match.
var headerLength = 4;
var calculatedChecksum = doubleSha256(expandedArray.subarray(0, headerLength +
contentLength)).subarray(0, checksumLength);
var providedChecksum = expandedArray.subarray(expandedArray.length - checksumLength, expandedArray.length);
if (arraysAreEqual(calculatedChecksum, providedChecksum)) {
// Get the content array. This is the encoded object with the prefix, length byte, and checksum removed.
var contentBytes = expandedArray.subarray(headerLength, expandedArray.length - checksumLength);
// Make the object from the content array.
if (prefix === 'key_') {
result = new NyzoStringPrivateSeed(contentBytes);
} else if (prefix === 'id__') {
result = new NyzoStringPublicIdentifier(contentBytes);
} else if (prefix === 'pre_') {
result = new NyzoStringPrefilledData(contentBytes.subarray(0, 32),
contentBytes.subarray(33, contentBytes.length));
} else if (prefix === 'tx__') {
result = Transaction.fromBytes(contentBytes);
}
}
}
return result;
}
function byteArrayForEncodedString(encodedString) {
var arrayLength = (encodedString.length * 6 + 7) / 8;
var array = new Uint8Array(arrayLength);
for (var i = 0; i < arrayLength; i++) {
var leftCharacter = encodedString.charAt(i * 8 / 6);
var rightCharacter = encodedString.charAt(i * 8 / 6 + 1);
var leftValue = characterToValueMap[leftCharacter];
var rightValue = characterToValueMap[rightCharacter];
var bitOffset = (i * 2) % 6;
array[i] = ((((leftValue << 6) + rightValue) >> 4 - bitOffset) & 0xff);
}
return array;
}
function encodedStringForByteArray(array) {
var index = 0;
var bitOffset = 0;
var encodedString = "";
while (index < array.length) {
// Get the current and next byte.
var leftByte = array[index] & 0xff;
var rightByte = index < array.length - 1 ? array[index + 1] & 0xff : 0;
// Append the character for the next 6 bits in the array.
var lookupIndex = (((leftByte << 8) + rightByte) >> (10 - bitOffset)) & 0x3f;
encodedString += characterLookup[lookupIndex];
// Advance forward 6 bits.
if (bitOffset == 0) {
bitOffset = 6;
} else {
index++;
bitOffset -= 2;
}
}
return encodedString;
}
function encodeNyzoString(prefix, contentBytes) {
// Get the prefix array from the type.
var prefixBytes = byteArrayForEncodedString(prefix);
// Determine the length of the expanded array with the header and the checksum. The header is the type-specific
// prefix in characters followed by a single byte that indicates the length of the content array (four bytes
// total). The checksum is a minimum of 4 bytes and a maximum of 6 bytes, widening the expanded array so that
// its length is divisible by 3.
var checksumLength = 4 + (3 - (contentBytes.length + 2) % 3) % 3;
var expandedLength = 4 + contentBytes.length + checksumLength;
// Create the array and add the header and the content. The first three bytes turn into the user-readable
// prefix in the encoded string. The next byte specifies the length of the content array, and it is immediately
// followed by the content array.
var expandedArray = new Uint8Array(expandedLength);
for (var i = 0; i < prefixBytes.length; i++) {
expandedArray[i] = prefixBytes[i];
}
expandedArray[3] = contentBytes.length;
for (var i = 0; i < contentBytes.length; i++) {
expandedArray[i + 4] = contentBytes[i];
}
// Compute the checksum and add the appropriate number of bytes to the end of the array.
var checksum = doubleSha256(expandedArray.subarray(0, 4 + contentBytes.length));
for (var i = 0; i < checksumLength; i++) {
expandedArray[expandedArray.length - checksumLength + i] = checksum[i];
}
// Build and return the encoded string from the expanded array.
return encodedStringForByteArray(expandedArray);
}
function nyzoStringFromPrivateKey(byteArray) {
return encodeNyzoString('key_', byteArray);
}
function nyzoStringFromPublicIdentifier(byteArray) {
return encodeNyzoString('id__', byteArray);
}
function nyzoStringFromTransaction(byteArray) {
return encodeNyzoString('tx__', byteArray);
}