# @zilliqa-js/util

> Utilities for working with Zilliqa.

Latest version **3.5.0** (published 2023-12-06) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install @zilliqa-js/util
pnpm add @zilliqa-js/util
yarn add @zilliqa-js/util
bun add @zilliqa-js/util
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.5.0 |
| Published | 2023-12-06 |
| First published | 2018-10-31 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 727.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | lukozill, troelsfr, inamirandaz, zilliqateam, fluffypancake5436, zilliqaofficial |

## Links

- npm: https://www.npmjs.com/package/@zilliqa-js/util
- Repository: https://github.com/Zilliqa/zilliqa-developer
- Homepage: https://github.com/Zilliqa/zilliqa-developer#readme
- Issues: https://github.com/Zilliqa/zilliqa-js/issues
- npm.io page: https://npm.io/package/@zilliqa-js/util

## Dependencies (4)

- [long](https://npm.io/package/long.md) ^4.0.0
- [bn.js](https://npm.io/package/bn.js.md) ^4.11.8
- [tslib](https://npm.io/package/tslib.md) 2.3.1
- [camelcase](https://npm.io/package/camelcase.md) ^5.0.0

## Recent versions

- 3.5.0 (latest) — 2023-12-06
- 0.2.2 (next) — 2018-11-15
- 3.4.4 — 2023-08-09
- 3.4.3 — 2023-06-05
- 3.4.3-rc.0 — 2023-06-01
- 3.4.2 — 2023-06-01
- 3.4.2-rc.0 — 2023-05-30
- 3.4.1 — 2023-05-26
- 3.4.0 — 2023-05-26
- 3.4.0-rc.3 — 2023-05-26
- 3.4.0-rc.2 — 2023-05-25
- 3.4.0-rc.1 — 2023-05-25
- 3.4.0-rc.0 — 2023-05-25
- 3.3.4 — 2022-02-03
- 3.3.3 — 2021-11-10
- … 37 more at https://npm.io/package/@zilliqa-js/util/versions

## README

# @zilliqa-js/util

> Utility functions useful in Zilliqa-related programs.

## Classes

### `BN`

See documentation at [bn.js](https://github.com/indutny/bn.js/). This is simply
a re-export of that library to prevent bloating other `@zilliqa-js` packages,
most of which depend on `bn.js` in small ways.

### `Long`

See documentation at [long.js](https://github.com/dcodeIO/long.js). This is
simply a re-export for similar reasons. Note that `long` is only required if you
need to serialise integers with size greater than or equal to `2^53`.

### `PRESETS`

Commonly used variables such as DEVNET_URL. See [source](./src/presets.ts) for
more info.

## Functions

### `intToHexArray(int: number, size: number): string[]`

Converts an integer to an array of hexadecimal strings (little endian). Size is
the total length of bytes to pad to.

#### Parameters

- `int`: `number` - the decimal number to convert.

#### Returns

- `string[]` - hexadecimal array representation of the decimal number.

### `intToByteArray(num: number, size: number): Uint8Array`

Converts an integer to a `Uint8Array` (i.e., byte array).

#### Parameters

- `num`: `number` - the decimal number to convert

#### Returns

- `Uint8Array` - byte array, padded to `size`.

### `hexToByteArray(hex: string): Uint8Array`

Converts a hex-encoded `string` to a `Uint8Array`. Endianess is not important.

#### Parameters

- `hex`: `string`

#### Returns

- `Uint8Array`

### `hexToIntArray(hex: string): number[]`

Converts a hex-encoded string to an array of integers.

#### Parameters

- `hex`: `string`

#### Returns

- `number[]`

### `pack(a: number, b: number): number`

Performs bitwise addition of two 16-bit numbers, returning a 32-bit number.
Throws if either number exceeds 16 bits.

#### Parameters

- `a`: `number` - a 16-bit number.
- `b`: `number` - a 16-bit number.

#### Returns

- `number` - the combined 32-bit number.

### `compareBytes(a: string, b: string): boolean`

Performs a constant time comparison of two hexadecimal values. This avoids
timing attacks.

#### Parameters

- `a`: `string` - hex-encoded string.
- `b`: `string` - hex-encoded string.

#### Returns

- `boolean` - `true` if the values are equal.

### `isHex(str: string): boolean`

Determines if a given string is hex-encoded.

#### Parameters

- `str`: `string`.

#### Returns

- `boolean` - `true` if the string is hex-encoded.

### `isAddress(address: string): boolean`

Determines if a given string is a valid address.

#### Parameters

- `address`: `string`.

#### Returns

- `boolean` - `true` if the string is an address.

### `isBech32(address: string): boolean`

Determines if a given string is a valid Zilliqa bech32 address.

#### Parameters

- `address`: `string`.

#### Returns

- `boolean` - `true` if the string is a valid Zilliqa bech32 address.

### `isPrivateKey(privateKey: string): boolean`

Determines if a given string is a valid private key.

#### Parameters

- `privateKey`: `string`.

#### Returns

- `boolean` - `true` if the string is a valid private key.

### `isPubKey(pubKey: string): boolean`

Determines if a given string is a valid _uncompressed_ public key.

#### Parameters

- `pubKey`: `string`.

#### Returns

- `boolean` - `true` if the string is a valid public key.

### `isSignature(sig: string): boolean`

Determines if a given string is a valid Schnorr signature.

#### Parameters

- `sig`: `string`

#### Returns

- `boolean` - `true` if the string is a valid signature.

### `isNumber(x: unknown): boolean`

Determines if a given value is a valid JS `number`.

#### Parameters

- `x`: `unknown`

#### Returns

- `boolean` - `true` if the string is a valid signature.

### `isBN(x: unknown): boolean`

Determines if a given value is an instance of `BN.js`.

#### Parameters

- `x`: `unknown`

#### Returns

- `boolean` - `true` if the value is a `BN` instance.

### `isString(x: unknown): boolean`

Determines if a given value is a valid JS `string`.

#### Parameters

- `x`: `unknown`

#### Returns

- `boolean` - `true` if the value is a `string`.

### `isPlainObject(x: unknown): boolean`

Determines if a given value is a _plain_ JS object (i.e. directly below `Object`
in the prototype chain).

#### Parameters

- `x`: `unknown`

#### Returns

- `boolean` - `true` if the value is a plain object.

### `matchesObject(x: unknown, test: { [key: string]: Validator[] }): boolean`

Determines if a value has the shape specified by `test`.

#### Parameters

- `x`: `unknown`
- `test`: `{ [key: string]: Validator[] }`

#### Returns

- `boolean` - `true` if the value matches `test`.

### `fromQa(qa: BN, unit: Units, options: Options)`

Converts from `qa` (smallest unit) to `zil` or `li`.

#### Parameters

- `qa`: `BN` - the value to convert from.
- `unit`: `Units` - the unit to be converted to (`'zil' | 'qa'`).
- `options`: `Options` - an object specifying options.

### `toQa(input: string | number | BN, unit: Units)`

Converts `zil` or `li` to `qa` (smallest unit).

#### Parameters

- `input`: `string | number | BN` - the value to convert from.
- `unit`: `Units` - the unit to be converted _from_ (`'zil' | 'li'`).

---
_Source: https://npm.io/package/@zilliqa-js/util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
