web3util 4.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/3xg6ulq8.cjs +1 -0
  2. package/LICENSE +14 -0
  3. package/README.md +72 -0
  4. package/lib/commonjs/chunk_response_parser.d.ts +14 -0
  5. package/lib/commonjs/chunk_response_parser.js +66 -0
  6. package/lib/commonjs/chunk_response_parser.js.map +1 -0
  7. package/lib/commonjs/converters.d.ts +280 -0
  8. package/lib/commonjs/converters.js +624 -0
  9. package/lib/commonjs/converters.js.map +1 -0
  10. package/lib/commonjs/event_emitter.d.ts +10 -0
  11. package/lib/commonjs/event_emitter.js +44 -0
  12. package/lib/commonjs/event_emitter.js.map +1 -0
  13. package/lib/commonjs/formatter.d.ts +43 -0
  14. package/lib/commonjs/formatter.js +320 -0
  15. package/lib/commonjs/formatter.js.map +1 -0
  16. package/lib/commonjs/hash.d.ts +93 -0
  17. package/lib/commonjs/hash.js +347 -0
  18. package/lib/commonjs/hash.js.map +1 -0
  19. package/lib/commonjs/index.d.ts +18 -0
  20. package/lib/commonjs/index.js +63 -0
  21. package/lib/commonjs/index.js.map +1 -0
  22. package/lib/commonjs/json_rpc.d.ts +21 -0
  23. package/lib/commonjs/json_rpc.js +96 -0
  24. package/lib/commonjs/json_rpc.js.map +1 -0
  25. package/lib/commonjs/objects.d.ts +7 -0
  26. package/lib/commonjs/objects.js +62 -0
  27. package/lib/commonjs/objects.js.map +1 -0
  28. package/lib/commonjs/package.json +1 -0
  29. package/lib/commonjs/promise_helpers.d.ts +47 -0
  30. package/lib/commonjs/promise_helpers.js +155 -0
  31. package/lib/commonjs/promise_helpers.js.map +1 -0
  32. package/lib/commonjs/random.d.ts +28 -0
  33. package/lib/commonjs/random.js +55 -0
  34. package/lib/commonjs/random.js.map +1 -0
  35. package/lib/commonjs/socket_provider.d.ts +128 -0
  36. package/lib/commonjs/socket_provider.js +356 -0
  37. package/lib/commonjs/socket_provider.js.map +1 -0
  38. package/lib/commonjs/string_manipulation.d.ts +80 -0
  39. package/lib/commonjs/string_manipulation.js +147 -0
  40. package/lib/commonjs/string_manipulation.js.map +1 -0
  41. package/lib/commonjs/uint8array.d.ts +6 -0
  42. package/lib/commonjs/uint8array.js +59 -0
  43. package/lib/commonjs/uint8array.js.map +1 -0
  44. package/lib/commonjs/uuid.d.ts +11 -0
  45. package/lib/commonjs/uuid.js +57 -0
  46. package/lib/commonjs/uuid.js.map +1 -0
  47. package/lib/commonjs/validation.d.ts +82 -0
  48. package/lib/commonjs/validation.js +163 -0
  49. package/lib/commonjs/validation.js.map +1 -0
  50. package/lib/commonjs/web3_deferred_promise.d.ts +67 -0
  51. package/lib/commonjs/web3_deferred_promise.js +141 -0
  52. package/lib/commonjs/web3_deferred_promise.js.map +1 -0
  53. package/lib/commonjs/web3_eip1193_provider.d.ts +15 -0
  54. package/lib/commonjs/web3_eip1193_provider.js +109 -0
  55. package/lib/commonjs/web3_eip1193_provider.js.map +1 -0
  56. package/lib/esm/chunk_response_parser.js +62 -0
  57. package/lib/esm/chunk_response_parser.js.map +1 -0
  58. package/lib/esm/converters.js +603 -0
  59. package/lib/esm/converters.js.map +1 -0
  60. package/lib/esm/event_emitter.js +37 -0
  61. package/lib/esm/event_emitter.js.map +1 -0
  62. package/lib/esm/formatter.js +313 -0
  63. package/lib/esm/formatter.js.map +1 -0
  64. package/lib/esm/hash.js +336 -0
  65. package/lib/esm/hash.js.map +1 -0
  66. package/lib/esm/index.js +34 -0
  67. package/lib/esm/index.js.map +1 -0
  68. package/lib/esm/json_rpc.js +81 -0
  69. package/lib/esm/json_rpc.js.map +1 -0
  70. package/lib/esm/objects.js +58 -0
  71. package/lib/esm/objects.js.map +1 -0
  72. package/lib/esm/package.json +1 -0
  73. package/lib/esm/promise_helpers.js +146 -0
  74. package/lib/esm/promise_helpers.js.map +1 -0
  75. package/lib/esm/random.js +50 -0
  76. package/lib/esm/random.js.map +1 -0
  77. package/lib/esm/socket_provider.js +329 -0
  78. package/lib/esm/socket_provider.js.map +1 -0
  79. package/lib/esm/string_manipulation.js +140 -0
  80. package/lib/esm/string_manipulation.js.map +1 -0
  81. package/lib/esm/uint8array.js +53 -0
  82. package/lib/esm/uint8array.js.map +1 -0
  83. package/lib/esm/uuid.js +53 -0
  84. package/lib/esm/uuid.js.map +1 -0
  85. package/lib/esm/validation.js +158 -0
  86. package/lib/esm/validation.js.map +1 -0
  87. package/lib/esm/web3_deferred_promise.js +137 -0
  88. package/lib/esm/web3_deferred_promise.js.map +1 -0
  89. package/lib/esm/web3_eip1193_provider.js +105 -0
  90. package/lib/esm/web3_eip1193_provider.js.map +1 -0
  91. package/lib/types/chunk_response_parser.d.ts +15 -0
  92. package/lib/types/chunk_response_parser.d.ts.map +1 -0
  93. package/lib/types/converters.d.ts +281 -0
  94. package/lib/types/converters.d.ts.map +1 -0
  95. package/lib/types/event_emitter.d.ts +11 -0
  96. package/lib/types/event_emitter.d.ts.map +1 -0
  97. package/lib/types/formatter.d.ts +44 -0
  98. package/lib/types/formatter.d.ts.map +1 -0
  99. package/lib/types/hash.d.ts +94 -0
  100. package/lib/types/hash.d.ts.map +1 -0
  101. package/lib/types/index.d.ts +19 -0
  102. package/lib/types/index.d.ts.map +1 -0
  103. package/lib/types/json_rpc.d.ts +22 -0
  104. package/lib/types/json_rpc.d.ts.map +1 -0
  105. package/lib/types/objects.d.ts +8 -0
  106. package/lib/types/objects.d.ts.map +1 -0
  107. package/lib/types/promise_helpers.d.ts +48 -0
  108. package/lib/types/promise_helpers.d.ts.map +1 -0
  109. package/lib/types/random.d.ts +29 -0
  110. package/lib/types/random.d.ts.map +1 -0
  111. package/lib/types/socket_provider.d.ts +129 -0
  112. package/lib/types/socket_provider.d.ts.map +1 -0
  113. package/lib/types/string_manipulation.d.ts +81 -0
  114. package/lib/types/string_manipulation.d.ts.map +1 -0
  115. package/lib/types/uint8array.d.ts +7 -0
  116. package/lib/types/uint8array.d.ts.map +1 -0
  117. package/lib/types/uuid.d.ts +12 -0
  118. package/lib/types/uuid.d.ts.map +1 -0
  119. package/lib/types/validation.d.ts +83 -0
  120. package/lib/types/validation.d.ts.map +1 -0
  121. package/lib/types/web3_deferred_promise.d.ts +68 -0
  122. package/lib/types/web3_deferred_promise.d.ts.map +1 -0
  123. package/lib/types/web3_eip1193_provider.d.ts +16 -0
  124. package/lib/types/web3_eip1193_provider.d.ts.map +1 -0
  125. package/package.json +57 -0
  126. package/src/chunk_response_parser.ts +99 -0
  127. package/src/converters.ts +713 -0
  128. package/src/event_emitter.ts +37 -0
  129. package/src/formatter.ts +402 -0
  130. package/src/hash.ts +398 -0
  131. package/src/index.ts +36 -0
  132. package/src/json_rpc.ts +130 -0
  133. package/src/objects.ts +65 -0
  134. package/src/promise_helpers.ts +170 -0
  135. package/src/random.ts +53 -0
  136. package/src/socket_provider.ts +581 -0
  137. package/src/string_manipulation.ts +166 -0
  138. package/src/uint8array.ts +59 -0
  139. package/src/uuid.ts +59 -0
  140. package/src/validation.ts +193 -0
  141. package/src/web3_deferred_promise.ts +149 -0
  142. package/src/web3_eip1193_provider.ts +116 -0
@@ -0,0 +1,170 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { isNullish } from 'web3-validator';
19
+
20
+ export type Timer = ReturnType<typeof setInterval>;
21
+ export type Timeout = ReturnType<typeof setTimeout>;
22
+
23
+ /**
24
+ * An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser.
25
+ * @param object - to check if it is a `Promise`
26
+ * @returns `true` if it is an `object` or a `function` that has a `then` function. And returns `false` otherwise.
27
+ */
28
+ export function isPromise(object: unknown): boolean {
29
+ return (
30
+ (typeof object === 'object' || typeof object === 'function') &&
31
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
32
+ typeof (object as { then: unknown }).then === 'function'
33
+ );
34
+ }
35
+
36
+ export type AsyncFunction<T, K = unknown> = (...args: K[]) => Promise<T>;
37
+
38
+ export function waitWithTimeout<T>(
39
+ awaitable: Promise<T> | AsyncFunction<T>,
40
+ timeout: number,
41
+ error: Error,
42
+ ): Promise<T>;
43
+ export function waitWithTimeout<T>(
44
+ awaitable: Promise<T> | AsyncFunction<T>,
45
+ timeout: number,
46
+ ): Promise<T | undefined>;
47
+
48
+ /**
49
+ * Wait for a promise but interrupt it if it did not resolve within a given timeout.
50
+ * If the timeout reached, before the promise code resolve, either throw an error if an error object was provided, or return `undefined`.
51
+ * @param awaitable - The promise or function to wait for.
52
+ * @param timeout - The timeout in milliseconds.
53
+ * @param error - (Optional) The error to throw if the timeout reached.
54
+ */
55
+ export async function waitWithTimeout<T>(
56
+ awaitable: Promise<T> | AsyncFunction<T>,
57
+ timeout: number,
58
+ error?: Error,
59
+ ): Promise<T | undefined> {
60
+ let timeoutId: Timeout | undefined;
61
+ const result = await Promise.race([
62
+ awaitable instanceof Promise ? awaitable : awaitable(),
63
+ new Promise<undefined | Error>((resolve, reject) => {
64
+ timeoutId = setTimeout(() => (error ? reject(error) : resolve(undefined)), timeout);
65
+ }),
66
+ ]);
67
+ if (timeoutId) {
68
+ clearTimeout(timeoutId);
69
+ }
70
+ if (result instanceof Error) {
71
+ throw result;
72
+ }
73
+ return result;
74
+ }
75
+
76
+ /**
77
+ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null),
78
+ * or until a timeout is reached. It returns promise and intervalId.
79
+ * @param func - The function to call.
80
+ * @param interval - The interval in milliseconds.
81
+ */
82
+ export function pollTillDefinedAndReturnIntervalId<T>(
83
+ func: AsyncFunction<T>,
84
+ interval: number,
85
+ ): [Promise<Exclude<T, undefined>>, Timer] {
86
+ let intervalId: Timer | undefined;
87
+ const polledRes = new Promise<Exclude<T, undefined>>((resolve, reject) => {
88
+ intervalId = setInterval(
89
+ (function intervalCallbackFunc() {
90
+ (async () => {
91
+ try {
92
+ const res = await waitWithTimeout(func, interval);
93
+
94
+ if (!isNullish(res)) {
95
+ clearInterval(intervalId);
96
+ resolve(res as unknown as Exclude<T, undefined>);
97
+ }
98
+ } catch (error) {
99
+ clearInterval(intervalId);
100
+ reject(error);
101
+ }
102
+ })() as unknown;
103
+ return intervalCallbackFunc;
104
+ })(), // this will immediate invoke first call
105
+ interval,
106
+ );
107
+ });
108
+
109
+ return [polledRes as unknown as Promise<Exclude<T, undefined>>, intervalId!];
110
+ }
111
+
112
+ /**
113
+ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null),
114
+ * or until a timeout is reached.
115
+ * pollTillDefinedAndReturnIntervalId() function should be used instead of pollTillDefined if you need IntervalId in result.
116
+ * This function will be deprecated in next major release so use pollTillDefinedAndReturnIntervalId().
117
+ * @param func - The function to call.
118
+ * @param interval - The interval in milliseconds.
119
+ */
120
+ export async function pollTillDefined<T>(
121
+ func: AsyncFunction<T>,
122
+ interval: number,
123
+ ): Promise<Exclude<T, undefined>> {
124
+ return pollTillDefinedAndReturnIntervalId(func, interval)[0];
125
+ }
126
+ /**
127
+ * Enforce a timeout on a promise, so that it can be rejected if it takes too long to complete
128
+ * @param timeout - The timeout to enforced in milliseconds.
129
+ * @param error - The error to throw if the timeout is reached.
130
+ * @returns A tuple of the timeout id and the promise that will be rejected if the timeout is reached.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const [timerId, promise] = web3.utils.rejectIfTimeout(100, new Error('time out'));
135
+ * ```
136
+ */
137
+ export function rejectIfTimeout(timeout: number, error: Error): [Timer, Promise<never>] {
138
+ let timeoutId: Timer | undefined;
139
+ const rejectOnTimeout = new Promise<never>((_, reject) => {
140
+ timeoutId = setTimeout(() => {
141
+ reject(error);
142
+ }, timeout);
143
+ });
144
+ return [timeoutId!, rejectOnTimeout];
145
+ }
146
+ /**
147
+ * Sets an interval that repeatedly executes the given cond function with the specified interval between each call.
148
+ * If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned.
149
+ * @param cond - The function/condition to call.
150
+ * @param interval - The interval in milliseconds.
151
+ * @returns - an array with the interval ID and the Promise.
152
+ */
153
+ export function rejectIfConditionAtInterval<T>(
154
+ cond: AsyncFunction<T | undefined>,
155
+ interval: number,
156
+ ): [Timer, Promise<never>] {
157
+ let intervalId: Timer | undefined;
158
+ const rejectIfCondition = new Promise<never>((_, reject) => {
159
+ intervalId = setInterval(() => {
160
+ (async () => {
161
+ const error = await cond();
162
+ if (error) {
163
+ clearInterval(intervalId);
164
+ reject(error);
165
+ }
166
+ })() as unknown;
167
+ }, interval);
168
+ });
169
+ return [intervalId!, rejectIfCondition];
170
+ }
package/src/random.ts ADDED
@@ -0,0 +1,53 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ /**
19
+ * @module Utils
20
+ */
21
+
22
+ import { getRandomBytesSync } from 'ethereum-cryptography/random.js';
23
+ import { bytesToHex } from './converters.js';
24
+
25
+ /**
26
+ * Returns a random byte array by the given bytes size
27
+ * @param size - The size of the random byte array returned
28
+ * @returns - random byte array
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * console.log(web3.utils.randomBytes(32));
33
+ * > Uint8Array(32) [
34
+ * 93, 172, 226, 32, 33, 176, 156, 156,
35
+ * 182, 30, 240, 2, 69, 96, 174, 197,
36
+ * 33, 136, 194, 241, 197, 156, 110, 111,
37
+ * 66, 87, 17, 88, 67, 48, 245, 183
38
+ * ]
39
+ * ```
40
+ */
41
+ export const randomBytes = (size: number): Uint8Array => getRandomBytesSync(size);
42
+
43
+ /**
44
+ * Returns a random hex string by the given bytes size
45
+ * @param byteSize - The size of the random hex string returned
46
+ * @returns - random hex string
47
+ *
48
+ * ```ts
49
+ * console.log(web3.utils.randomHex(32));
50
+ * > 0x139f5b88b72a25eab053d3b57fe1f8a9dbc62a526b1cb1774d0d7db1c3e7ce9e
51
+ * ```
52
+ */
53
+ export const randomHex = (byteSize: number): string => bytesToHex(randomBytes(byteSize));