-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathutil.ts
More file actions
125 lines (98 loc) · 3.46 KB
/
util.ts
File metadata and controls
125 lines (98 loc) · 3.46 KB
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
export function getSettingsFromNumber(settingNum: number): { frozen: boolean, active: boolean, whiteList: boolean, initialized: boolean } {
const bitStringArray = settingNum.toString(2).padStart(32, '0').split('').reverse();
return {
initialized: bitStringArray[0] === '1',
active: bitStringArray[1] === '1',
whiteList: bitStringArray[2] === '1',
frozen: bitStringArray[3] === '1',
};
}
function getBit(setting: boolean): string {
return setting ? '1' : '0';
}
export function convertSettingsToNumber(settings: { frozen: boolean, active: boolean, whiteList: boolean, initialized: boolean }): number {
const { frozen, active, whiteList, initialized } = settings;
const bitString = `${getBit(frozen)}${getBit(whiteList)}${getBit(active)}${getBit(initialized)}`;
return parseInt(bitString, 2);
}
export function safeParseInt(value: string): number {
const parsedValue = parseInt(value, 10);
return isNaN(parsedValue) ? 0 : parsedValue;
}
export function stringToBigInt(input: string): bigint {
const encoder = new TextEncoder();
const encodedBytes = encoder.encode(input);
encodedBytes.reverse();
let bigIntValue = BigInt(0);
for (let i = 0; i < encodedBytes.length; i++) {
const byteValue = BigInt(encodedBytes[i]);
const shiftedValue = byteValue << BigInt(8 * i);
bigIntValue = bigIntValue | shiftedValue;
}
return bigIntValue;
}
export function bigIntToString(bigIntValue: bigint): string {
const bytes: number[] = [];
let tempBigInt = bigIntValue;
while (tempBigInt > BigInt(0)) {
const byteValue = Number(tempBigInt & BigInt(255));
bytes.push(byteValue);
tempBigInt = tempBigInt >> BigInt(8);
}
bytes.reverse();
const decoder = new TextDecoder();
const asciiString = decoder.decode(Uint8Array.from(bytes));
return asciiString;
}
export function splitStringToBigInts(input: string): bigint[] {
const chunkSize = 16; // Chunk size to split the string
const numChunks = Math.ceil(input.length / chunkSize);
const bigInts: bigint[] = [];
for (let i = 0; i < numChunks; i++) {
const chunk = input.substr(i * chunkSize, chunkSize);
const bigIntValue = stringToBigInt(chunk);
bigInts.push(bigIntValue);
}
return bigInts;
}
export function joinBigIntsToString(bigInts: bigint[]): string {
let result = '';
for (let i = 0; i < bigInts.length; i++) {
const chunkString = bigIntToString(bigInts[i]);
result += chunkString;
}
return result;
}
export function padArray(array: bigint[], length: number): bigint[] {
const paddingLength = length - array.length;
if (paddingLength <= 0) {
return array; // No padding needed
}
const padding = Array(paddingLength).fill(BigInt(0));
const paddedArray = array.concat(padding);
return paddedArray;
}
export function parseStringToBigIntArray(input: string): bigint[] {
const bigIntRegex = /([0-9]+)u128/g;
const matches = input.match(bigIntRegex);
if (!matches) {
return [];
}
const bigInts = matches.map((match) => BigInt(match.slice(0, -4)));
return bigInts;
}
export function getPublicKeyFromFuture(input: string): string {
const keyRegex = /([0-9]+)field/g;
const matches = input.match(keyRegex);
if (!matches) {
return '';
}
return matches[0];
}
export function getRandomElement<T>(list: T[]): T {
const randomIndex = Math.floor(Math.random() * list.length);
return list[randomIndex];
}
export const removeVisibilitySuffix = (str: string): string => {
return str.replace(/\.public$|\.private$/, '');
};