> For the complete documentation index, see [llms.txt](https://wiki.4thtech.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.4thtech.io/developers/sdk/utils.md).

# Utils

Helper functions exported by the @4thtech-sdk/utils package.

{% hint style="info" %}
**Status: Production-ready, stable.** The 4thTech SDK and the BlockMail & BlockChat white-label apps are supported; no new features are planned. Last release: SDK 0.5.13 (25 Nov 2025).
{% endhint %}

{% hint style="info" %}
Reference for SDK 0.5.x (production-ready, stable). See [Status](https://wiki.4thtech.io/developers/sdk/status.md).
{% endhint %}

Utility functions and helpers used by the other SDK packages. Version 0.0.2.

## Installation

```bash
npm install @4thtech-sdk/utils
```

## Usage

```ts
import { stringToArrayBuffer, arrayBufferToString } from '@4thtech-sdk/utils';

const buffer = stringToArrayBuffer('hello');
const text = arrayBufferToString(buffer);
```

## Exports

The package index exports the buffer helpers, the Node crypto helpers and the error class.

### Buffer helpers

| Function                              | What it does                              |
| ------------------------------------- | ----------------------------------------- |
| `stringToArrayBuffer(str)`            | Converts a string to an `ArrayBuffer`     |
| `arrayBufferToString(buffer)`         | Converts an `ArrayBuffer` to a string     |
| `hexStringToArrayBuffer(hex)`         | Converts a hex string to an `ArrayBuffer` |
| `arrayBufferToHexString(buffer)`      | Converts an `ArrayBuffer` to a hex string |
| `concatenateArrayBuffers(...buffers)` | Joins several `ArrayBuffer`s into one     |

### Crypto helpers

| Function                                | What it does                                                     |
| --------------------------------------- | ---------------------------------------------------------------- |
| `calculateChecksum(data)`               | Returns the SHA-256 checksum of an `ArrayBuffer` as a hex string |
| `calculateChecksumFromStream(filePath)` | Returns the SHA-256 checksum of a file, read as a stream (Node)  |
| `createSha256Hash(data)`                | Returns the SHA-256 hash of a string                             |
| `getRandomValues(typedArray)`           | Fills a `Uint8Array` with random values                          |
| `subtle`                                | The Web Crypto `SubtleCrypto` object                             |

### Errors

`ContractRevertedError` is thrown by the clients in `@4thtech-sdk/ethereum` when a contract call reverts.

## See also

* [AES Encryption](https://wiki.4thtech.io/developers/sdk/encryption/aes-encryption.md), which uses these helpers in its examples.
