# @stacks/encryption

> Encryption utilities for Stacks

Latest version **7.6.0** (published 2026-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @stacks/encryption
pnpm add @stacks/encryption
yarn add @stacks/encryption
bun add @stacks/encryption
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.6.0 |
| Published | 2026-07-29 |
| First published | 2020-09-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 768.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 975 |
| Author | Hiro Systems PBC |
| Maintainers | blockstack-devops, stacks-foundation, jannik-stacks, rafa-stacks |

## Links

- npm: https://www.npmjs.com/package/@stacks/encryption
- Repository: https://github.com/stx-labs/stacks.js
- Homepage: https://hiro.so/stacks-js
- Issues: https://github.com/blockstack/blockstack.js/issues
- npm.io page: https://npm.io/package/@stacks/encryption

## Dependencies (8)

- [bs58](https://npm.io/package/bs58.md) ^5.0.0
- [base64-js](https://npm.io/package/base64-js.md) ^1.5.1
- [@scure/bip39](https://npm.io/package/@scure/bip39.md) 1.1.0
- [@noble/hashes](https://npm.io/package/@noble/hashes.md) 1.1.5
- [ripemd160-min](https://npm.io/package/ripemd160-min.md) ^0.0.6
- [@stacks/common](https://npm.io/package/@stacks/common.md) ^7.6.0
- [varuint-bitcoin](https://npm.io/package/varuint-bitcoin.md) ^1.1.2
- [@noble/secp256k1](https://npm.io/package/@noble/secp256k1.md) 1.7.1

## Recent versions

- 7.6.0 (latest) — 2026-07-29
- 7.5.1-pr.1854.0 (pr) — 2026-07-13
- 7.4.1-beta.3 (beta) — 2026-05-22
- 7.0.0-next.89 (next) — 2024-10-25
- 6.12.2-nakamoto.0 (nakamoto) — 2024-03-21
- 4.4.0-stacks2.1-alpha.0 (alpha) — 2022-08-23
- 7.5.0 — 2026-06-23
- 7.4.1-pr.1854.5 — 2026-06-21
- 7.4.1-pr.1854.4 — 2026-06-12
- 7.4.1-pr.1854.3 — 2026-06-02
- 7.4.1-pr.1854.2 — 2026-05-28
- 7.4.1-pr.1854.1 — 2026-05-27
- 7.4.1-pr.1854.0 — 2026-05-27
- 7.4.1-beta.2 — 2026-05-22
- 7.4.1-beta.1 — 2026-05-22
- … 685 more at https://npm.io/package/@stacks/encryption/versions

## README

# @stacks/encryption

Encryption functions used by Stacks.js packages.

## Installation

```
npm install @stacks/encryption
```

### Encrypt and decrypt string

```typescript
import { encryptECIES, decryptECIES } from '@stacks/encryption';
import { utf8ToBytes } from '@stacks/common';

const privateKey = 'a5c61c6ca7b3e7e55edee68566aeab22e4da26baa285c7bd10e8d2218aa3b229';
const publicKey = '027d28f9951ce46538951e3697c62588a87f1f1f295de4a14fdd4c780fc52cfe69';

const testString = 'all work and no play makes jack a dull boy';

// Encrypt string with public key
const cipherObj = await encryptECIES(publicKey, utf8ToBytes(testString), true);

// Decrypt the cipher with private key to get the message
const deciphered = await decryptECIES(privateKey, cipherObj);
console.log(deciphered);
```

### Sign content using ECDSA

```typescript
import { signECDSA, verifyECDSA } from '@stacks/encryption';

const privateKey = 'a5c61c6ca7b3e7e55edee68566aeab22e4da26baa285c7bd10e8d2218aa3b229';
const testString = 'all work and no play makes jack a dull boy';

const sigObj = signECDSA(privateKey, testString);
// Verify content using ECDSA
const result = verifyECDSA(testString, sigObj.publicKey, sigObj.signature);
console.log(result); // true
```

### `encryptMnemonic` and `decryptMnemonic`

```typescript
import { bytesToHex, hexToBytes } from '@stacks/common';
import { encryptMnemonic, decryptMnemonic } from '@stacks/encryption';

const rawPhrase = 'march eager husband pilot waste rely exclude taste twist donkey actress scene';
const rawPassword = 'rawPassword';
const mockSalt = hexToBytes('ff'.repeat(16));

// Encrypt a raw mnemonic phrase to be password protected
const encoded = await encryptMnemonic(rawPhrase, rawPassword, { getRandomBytes: () => mockSalt });

// Decrypt an encrypted mnemonic phrase with a password
const decoded = await decryptMnemonic(bytesToHex(encoded), rawPassword);

console.log(decoded);
```

### Private key to address

```typescript
import { getPublicKeyFromPrivate, publicKeyToBtcAddress } from '@stacks/encryption';

const privateKey = '00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01';
const expectedAddress = '1WykMawQRnLh7SWmmoRL4qTDNCgAsVRF1';

const publicKey = getPublicKeyFromPrivate(privateKey);
const address = publicKeyToBtcAddress(publicKey);
console.log(address === expectedAddress); // true
```

### Make private key

```typescript
import { makeECPrivateKey, publicKeyToBtcAddress } from '@stacks/encryption';
import { SECP256K1Client } from 'jsontokens';

const privateKey = makeECPrivateKey();
// Private key is also usable with the jsontokens package
const publicKey = SECP256K1Client.derivePublicKey(privateKey);
const address = publicKeyToBtcAddress(publicKey);
console.log(address);
```

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