-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
86 lines (69 loc) · 3.13 KB
/
util.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
'use strict';
function hexStringAsUint8Array(identifier) {
identifier = identifier.split('-').join('');
var array = new Uint8Array(identifier.length / 2);
for (var i = 0; i < array.length; i++) {
array[i] = parseInt(identifier.substring(i * 2, i * 2 + 2), 16);
}
return array;
}
function sha256Uint8(array) {
var ascii = '';
for (var i = 0; i < array.length; i++) {
ascii += String.fromCharCode(array[i]);
}
return hexStringAsUint8Array(sha256(ascii));
}
function doubleSha256(array) {
return sha256Uint8(sha256Uint8(array));
}
function senderDataAsUint8Array(senderData) {
// Process normalized sender-data strings.
let array = null;
if (senderData.length == 67) {
let lowercase = senderData.toLowerCase();
if (lowercase[0] == 'x' && lowercase[1] == '(' && lowercase[66] == ')') {
// Get the underscore index to determine the length of the data.
let underscoreIndex = lowercase.indexOf('_');
if (underscoreIndex < 0) {
underscoreIndex = maximumSenderDataLength * 2 + 2;
}
let dataLength = underscoreIndex / 2 - 1;
// Ensure that all characters in the data field are correct. The left section must be all alphanumeric, and
// the right section must be underscores. The string was converted to lowercase.
let allAreCorrect = true;
for (let i = 2; i < 66 && allAreCorrect; i++) {
// This could be written more succinctly, but it would be more difficult to read.
if (i < underscoreIndex) {
allAreCorrect = (lowercase[i] >= '0' && lowercase[i] <= '9') ||
(lowercase[i] >= 'a' && lowercase[i] <= 'f');
} else {
allAreCorrect = lowercase[i] == '_';
}
}
if (allAreCorrect) {
let amtUnderScores = senderData.split("").filter(x => x === '_').length;
// The amount of characters on the left and right side (data and underscores) should be divisible by 2, the presence of 0 underscores indicates that the entire 64 character span has been utilized by hexadecimal characters
if(amtUnderScores > 0 && (amtUnderScores % 2) != 0) {
allAreCorrect = false;
}
}
// If all characters are correct, decode the data. Otherwise, leave the result null to indicate that the
// input is not a valid sender-data string.
if (allAreCorrect) {
array = hexStringAsUint8Array(senderData.substring(2, 2 + dataLength * 2));
}
}
}
// If processing of a normalized sender-data string did not produce a result, process as a plain-text string.
if (array == null) {
array = new Uint8Array(Math.min(senderData.length, 32));
for (var i = 0; i < array.length; i++) {
array[i] = senderData.charCodeAt(i);
}
}
return array;
}
function printAmount(amountMicronyzos) {
return '∩' + (amountMicronyzos / 1000000).toFixed(6);
}