@layerzerolabs/lz-utilities 2.3.41 → 2.3.43

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.
package/dist/index.d.ts CHANGED
@@ -39,6 +39,7 @@ interface Deployment {
39
39
  }
40
40
  /**
41
41
  * Find the matching deployments based on the given options
42
+ * @todo Use Partial<EndpointSpec> instead of options
42
43
  * @param deployments list of deployments
43
44
  * @param nameOrAddress contract name
44
45
  * @param options options to match against
@@ -49,8 +50,14 @@ declare function findDeployment(deployments: Deployment[], nameOrAddress: string
49
50
  source?: string;
50
51
  network?: Network;
51
52
  endpointId?: EndpointId;
52
- }): Deployment | undefined;
53
+ }): Deployment;
53
54
  declare function deploymentToEvmContract<T extends ethers.Contract>(deployment: Deployment, provider?: ethers.providers.Provider): T;
55
+ declare function findContract<T extends ethers.Contract>(provider: ethers.providers.Provider | undefined, deployments: Deployment[], nameOrAddress: string, options: {
56
+ chain?: Chain;
57
+ source?: string;
58
+ network?: Network;
59
+ endpointId?: EndpointId;
60
+ }): T;
54
61
 
55
62
  /**
56
63
  * A function to return dirname of a path
@@ -131,20 +138,43 @@ declare const firstFactory: <TInput extends unknown[], TOutput>(...factories: Fa
131
138
  */
132
139
  type NonPromise<T> = T extends Promise<unknown> ? never : T;
133
140
 
134
- type RequiredOnly<T> = {
135
- [K in keyof T as Required<T>[K] extends NonNullable<Required<T>[K]> ? K : never]: T[K];
136
- };
137
141
  /**
138
- * Make some properties in T required
142
+ * Type representing a hexadecimal string prefixed with '0x'.
139
143
  */
140
- type AtLeast<T, K extends keyof T> = Partial<T> & RequiredOnly<T> & Required<Pick<T, K>>;
144
+ type Hex = `0x${string}`;
141
145
  /**
142
- * RequireAtLeastOne helps create a type where at least one of the properties of an interface (can be any property) is required to exist.
143
- * https://learn.microsoft.com/en-us/javascript/api/@azure/keyvault-certificates/requireatleastone?view=azure-node-latest
146
+ * Type representing a hash string prefixed with '0x'.
144
147
  */
145
- type RequireAtLeastOne<T> = {
146
- [K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
147
- }[keyof T];
148
+ type Hash = `0x${string}`;
149
+ /**
150
+ * Checks if a given string is a valid hexadecimal string.
151
+ * @param value - The string to check.
152
+ * @returns True if the string is a valid hexadecimal string, false otherwise.
153
+ */
154
+ declare function isHex(value: string): value is Hex;
155
+ /**
156
+ * Checks if a given string is a valid hash string.
157
+ * @param value - The string to check.
158
+ * @returns True if the string is a valid hash string, false otherwise.
159
+ */
160
+ declare function isHash(value: string): value is Hash;
161
+ /**
162
+ * Represents a byte array.
163
+ */
164
+ type Bytes = Uint8Array;
165
+
166
+ declare function assert(condition: boolean, message?: string): asserts condition;
167
+ /**
168
+ * assertType
169
+ * assertType can be used to assert that a value is of a certain type, and without naming a new variable explicitly.
170
+ * @param value
171
+ * @param fn
172
+ * @param message
173
+ */
174
+ declare function assertType<T, M>(value: T, fn: (v: unknown) => v is M, message?: string): asserts value is T & M;
175
+ declare function assertDefined<T>(value?: T, message?: string): asserts value is NonNullable<T>;
176
+ declare function asType<T, M>(value: T, fn: (v: unknown) => v is M, message?: string): M;
177
+ declare function assumeType<T>(value: unknown): asserts value is T;
148
178
 
149
179
  /**
150
180
  * Calls a defined callback function on each element of an array, and returns an array that contains the results.
@@ -153,15 +183,71 @@ type RequireAtLeastOne<T> = {
153
183
  */
154
184
  declare function safeMap<T, R>(elements: T[], callbackfn: (item: T, index: number) => R): [R[], Error | undefined];
155
185
 
186
+ /**
187
+ * Converts a string or number value to a corresponding enum value.
188
+ * @param enumType - The enum object.
189
+ * @param value - The value to convert.
190
+ * @returns The converted enum value.
191
+ * @throws Error if the value is not a valid enum value.
192
+ * @example
193
+ * // Usage
194
+ * enum Color {
195
+ * Red = 'red',
196
+ * Green = 'green',
197
+ * Blue = 'blue'
198
+ * }
199
+ *
200
+ * const color: Color = asEnum(Color, 'red');
201
+ * expect(color).toBe(Color.Red);
202
+ *
203
+ * enum Direction {
204
+ * Up = 1,
205
+ * Down,
206
+ * }
207
+ *
208
+ * const direction: Direction = asEnum(Direction, 1);
209
+ * expect(direction).toBe(Direction.Up);
210
+ */
211
+ declare function asEnum<T extends object>(enumType: T, value: string | number): T[keyof T];
212
+
213
+ interface PadOptions {
214
+ dir?: 'left' | 'right' | undefined;
215
+ size?: number | null | undefined;
216
+ }
217
+ type PadReturnType<value extends Bytes | Hex> = value extends Hex ? Hex : Bytes;
218
+ type SizeExceedsPaddingSizeErrorType = SizeExceedsPaddingSizeError & {
219
+ name: 'SizeExceedsPaddingSizeError';
220
+ };
221
+ declare class SizeExceedsPaddingSizeError extends Error {
222
+ name: string;
223
+ constructor({ size, targetSize, type }: {
224
+ size: number;
225
+ targetSize: number;
226
+ type: 'hex' | 'bytes';
227
+ });
228
+ }
229
+ /**
230
+ * Pads a hexadecimal string or byte array to a specified size.
231
+ *
232
+ * @param hexOrBytes - The hexadecimal string or byte array to pad.
233
+ * @param options - The padding options.
234
+ * @param options.dir - The direction of the padding. Defaults to undefined.
235
+ * @param options.size - The size to pad to. Defaults to 32.
236
+ * @returns The padded hexadecimal string or byte array.
237
+ */
238
+ declare function padify<value extends Bytes | Hex>(hexOrBytes: value, { dir, size }?: PadOptions): PadReturnType<value>;
239
+
156
240
  /**
157
241
  * A function to convert Uint8Array to hex string
242
+ * @deprecated use `hexlify` instead
158
243
  * @param bytes Uint8Array
159
- * @returns hex string
244
+ * @returns hex string without 0x prefix, e.g., '0102030405'
160
245
  */
161
246
  declare function bytesToHex(bytes: Uint8Array): string;
162
247
  /**
163
248
  * A function to convert hex string to Uint8Array
164
- * @param hex hex string
249
+ * @deprecated use `arrayify` instead
250
+ * @param hex hex string, e.g., '0x0102030405' or '0102030405'
165
251
  * @returns Uint8Array
166
252
  */
167
253
  declare function hexToBytes(hex: string): Uint8Array;
@@ -176,13 +262,196 @@ declare function trim0x(hex: string): string;
176
262
  * @param hex hex string
177
263
  * @returns hex string with 0x prefix
178
264
  */
179
- declare function ensure0x(hex: string): string;
265
+ declare function ensure0x(hex: string): Hex;
180
266
  /**
181
267
  * A function to check if a string is a hex string
268
+ * @deprecated use `isHex` instead
182
269
  * @param value
183
270
  * @returns
184
271
  */
185
272
  declare function isHexString(value: string): boolean;
273
+ /**
274
+ * A function to convert a string|number|Uint8Array|Buffer|BigInt to Uint8Array
275
+ * @param value - the value to convert
276
+ * @param size - the size of the Uint8Array to return, if not specified, the size of the input will be returned
277
+ */
278
+ declare function arrayify(value: string | number | Uint8Array | Buffer | bigint, size?: number): Uint8Array;
279
+ /**
280
+ * A function to convert a string|number|Uint8Array|Buffer|BigInt to hex string
281
+ */
282
+ declare function hexlify(value: string | number | Uint8Array | Buffer | bigint): Hex;
283
+
284
+ /**
285
+ * Represents a type that allows partial modification of all properties in a given object type.
286
+ * This type is similar to the built-in `Partial` type in TypeScript.
287
+ * However, it supports deep partial modification, allowing partial modification of nested object properties.
288
+ *
289
+ * @typeparam T - The object type to be partially modified.
290
+ */
291
+ type DeepPartial<T> = {
292
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
293
+ };
294
+ /**
295
+ * Represents a type that makes all properties of the given type required deeply.
296
+ *
297
+ * This utility type recursively makes all properties of the given type required, including nested properties.
298
+ * If a property is already required, it remains unchanged.
299
+ *
300
+ * @typeParam T - The type to make all properties required.
301
+ * @returns A new type with all properties required.
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * type Person = {
306
+ * name?: string;
307
+ * age?: number;
308
+ * address?: {
309
+ * street?: string;
310
+ * city?: string;
311
+ * };
312
+ * };
313
+ *
314
+ * type RequiredPerson = DeepRequired<Person>;
315
+ * // Result: {
316
+ * // name: string;
317
+ * // age: number;
318
+ * // address: {
319
+ * // street: string;
320
+ * // city: string;
321
+ * // };
322
+ * // }
323
+ * ```
324
+ */
325
+ type DeepRequired<T> = {
326
+ [P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
327
+ };
328
+ /**
329
+ * Checks if an object has all the required properties specified by the given paths.
330
+ *
331
+ * @template T - The type of the object.
332
+ * @param {DeepPartial<T>} obj - The object to check.
333
+ * @param {string[]} paths - The paths of the required properties.
334
+ * @returns {boolean} - Returns true if the object has all the required properties, otherwise returns false.
335
+ */
336
+ declare function hasRequiredProperties<T>(obj: DeepPartial<T>, paths: string[]): boolean;
337
+ /**
338
+ * Retrieves the nested keys of an object type.
339
+ *
340
+ * @typeParam T - The object type.
341
+ * @returns The union of all nested keys in the object type.
342
+ *
343
+ * @example
344
+ * // Given the following object type:
345
+ * type Person = {
346
+ * name: string;
347
+ * age: number;
348
+ * address: {
349
+ * street: string;
350
+ * city: string;
351
+ * };
352
+ * };
353
+ *
354
+ * // The `NestedKeys` type will return the following union type:
355
+ * // "name" | "age" | "address" | "address.street" | "address.city"
356
+ * type AllKeys = NestedKeys<Person>;
357
+ */
358
+ type NestedKeys<T> = {
359
+ [K in keyof T]: T[K] extends object ? K | `${K & string}.${NestedKeys<T[K]> & string}` : K;
360
+ }[keyof T];
361
+ /**
362
+ * Creates a new type that includes only the properties from the input type `T` that are required and not nullable.
363
+ *
364
+ * @typeParam T - The input type.
365
+ * @returns A new type that includes only the required and non-nullable properties from `T`.
366
+ *
367
+ * @example
368
+ * // Define a type with optional and nullable properties
369
+ * type Person = {
370
+ * name?: string;
371
+ * age?: number | null;
372
+ * email: string;
373
+ * };
374
+ *
375
+ * // Create a new type with only the required and non-nullable properties from `Person`
376
+ * type RequiredPerson = RequiredOnly<Person>;
377
+ *
378
+ * // `RequiredPerson` will be:
379
+ * // {
380
+ * // email: string;
381
+ * // }
382
+ */
383
+ type RequiredOnly<T> = {
384
+ [K in keyof T as Required<T>[K] extends NonNullable<Required<T>[K]> ? K : never]: T[K];
385
+ };
386
+ /**
387
+ * `AtLeast` ensures that at least the specified keys `K` of the type `T` are required,
388
+ * while the rest of the properties are optional.
389
+ *
390
+ * @template T - The original type.
391
+ * @template K - The keys of `T` that should be required.
392
+ *
393
+ * @example
394
+ * interface User {
395
+ * id: string;
396
+ * name: string;
397
+ * email?: string;
398
+ * age?: number;
399
+ * }
400
+ *
401
+ * // At least 'id' and 'name' are required, the rest are optional
402
+ * const user: AtLeast<User, 'email'> = {
403
+ * id: '123',
404
+ * name: 'Alice',
405
+ * email: 'alice@example.com'
406
+ * // age are optional
407
+ * };
408
+ */
409
+ type AtLeast<T, K extends keyof T> = Partial<T> & RequiredOnly<T> & Required<Pick<T, K>>;
410
+ /**
411
+ * RequireAtLeastOne helps create a type where at least one of the properties of an interface (can be any property) is required to exist.
412
+ * https://learn.microsoft.com/en-us/javascript/api/@azure/keyvault-certificates/requireatleastone?view=azure-node-latest
413
+ */
414
+ type RequireAtLeastOne<T> = {
415
+ [K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>;
416
+ }[keyof T];
417
+
418
+ /**
419
+ * Finds files in the current directory and its parent directories.
420
+ * @param cwd The current working directory.
421
+ * @param expectations The expected file names.
422
+ * @returns An array of file paths that match the expectations.
423
+ */
424
+ declare function findUp(cwd: string, expectations: string[]): string[];
425
+
426
+ /**
427
+ * Enables TypeScript support for the specified file or module.
428
+ *
429
+ * @param relativeToPath - The path relative to which the TypeScript module should be resolved.
430
+ * @returns void
431
+ *
432
+ * @remarks
433
+ * This function enables TypeScript support by registering the 'ts-node' module and configuring it with the provided options.
434
+ * The 'ts-node' module allows for on-the-fly TypeScript transpilation without the need for a separate build step.
435
+ *
436
+ * @example
437
+ * enableTS(process.cwd());
438
+ */
439
+ declare function enableTS(relativeToPath: string): void;
440
+ /**
441
+ * Loads a JavaScript or TypeScript module.
442
+ *
443
+ * @param fileName - The name of the file to load.
444
+ * @param relativeToPath - The path relative to which the file should be resolved.
445
+ * @returns The loaded module.
446
+ *
447
+ * @example
448
+ * // Load a JavaScript module
449
+ * const myModule = loadJSorTS('myModule.js', '/path/to/file.js');
450
+ *
451
+ * // Load a TypeScript module
452
+ * const myModule = loadJSorTS('myModule.ts', '/path/to/file.js');
453
+ */
454
+ declare function loadJSorTS(fileName: string, relativeToPath: string): any;
186
455
 
187
456
  declare const logger: winston.Logger;
188
457
  declare function sleep(timeout: number): Promise<void>;
@@ -195,4 +464,4 @@ declare function extractUrlInfo(url: string): {
195
464
  port: string;
196
465
  };
197
466
 
198
- export { type AccountMnemonic, type AtLeast, type Deployment, type Factory, type NonPromise, type RequireAtLeastOne, type RequiredOnly, bytesToHex, createLogger, deploymentToEvmContract, dirname, ensure0x, extractUrlInfo, findDeployment, first, firstFactory, getAptosAccountFromMnemonic, getBIP044Path, getCircularReplacer, getEvmAccountFromMnemonic, getKeypairFromMnemonic, getLogger, getProjectPackageManager, getProjectRootDir, getSolanaAccountFromMnemonic, hexToBytes, initLogger, isHexString, isHttpServiceReachable, logger, parallel, pkgroot, safeMap, sequence, sleep, trim0x };
467
+ export { type AccountMnemonic, type AtLeast, type Bytes, type DeepPartial, type DeepRequired, type Deployment, type Factory, type Hash, type Hex, type NestedKeys, type NonPromise, type PadReturnType, type RequireAtLeastOne, type RequiredOnly, SizeExceedsPaddingSizeError, type SizeExceedsPaddingSizeErrorType, arrayify, asEnum, asType, assert, assertDefined, assertType, assumeType, bytesToHex, createLogger, deploymentToEvmContract, dirname, enableTS, ensure0x, extractUrlInfo, findContract, findDeployment, findUp, first, firstFactory, getAptosAccountFromMnemonic, getBIP044Path, getCircularReplacer, getEvmAccountFromMnemonic, getKeypairFromMnemonic, getLogger, getProjectPackageManager, getProjectRootDir, getSolanaAccountFromMnemonic, hasRequiredProperties, hexToBytes, hexlify, initLogger, isHash, isHex, isHexString, isHttpServiceReachable, loadJSorTS, logger, padify, parallel, pkgroot, safeMap, sequence, sleep, trim0x };