# @web4/crypto

> The crypto primitives used in Unichain, extracted into a separate module

Latest version **1.0.0** (published 2022-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @web4/crypto
pnpm add @web4/crypto
yarn add @web4/crypto
bun add @web4/crypto
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-03-27 |
| First published | 2022-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mathias Buus |
| Maintainers | neothawreww |

## Links

- npm: https://www.npmjs.com/package/@web4/crypto
- Repository: https://github.com/bitwebs/crypto
- Issues: https://github.com/bitwebs/crypto/issues
- npm.io page: https://npm.io/package/@web4/crypto

## Dependencies (3)

- [uint64be](https://npm.io/package/uint64be.md) ^3.0.0
- [sodium-native](https://npm.io/package/sodium-native.md) ^3.1.1
- [sodium-javascript](https://npm.io/package/sodium-javascript.md) ^0.5.6

## Recent versions

- 1.0.0 (latest) — 2022-03-27

## README

# @web4/crypto

The LEGACY crypto primitives used in Unichain extracted into a separate module

```
npm install @web4/crypto
```

## Usage

``` js
const crypto = require('@web4/crypto')

const keyPair = crypto.keyPair()
console.log(keyPair) // prints a ed25519 keypair
```

## API

#### `keyPair = crypto.keyPair()`

Returns an `ED25519` keypair that can used for tree signing.

#### `signature = crypto.sign(message, secretKey)`

Signs a message (buffer).

#### `verified = crypto.verify(message, signature, publicKey)`

Verifies a signature for a message.

#### `hash = crypto.data(data)`

Hashes a leaf node in a merkle tree.

#### `hash = crypto.parent(left, right)`

Hash a parent node in a merkle tree. `left` and `right` should look like this:

```js
{
  index: treeIndex,
  hash: hashOfThisNode,
  size: byteSizeOfThisTree
}
```

#### `hash = crypto.tree(peaks)`

Hashes the merkle root of the tree. `peaks` should be an array of the peaks of the tree and should look like above.

#### `dataToSign = crypto.signable(peaksOrHash, length)`

Encodes a buffer to sign. `length` should be how many leaf nodes exist in the tree.

#### `buffer = crypto.randomBytes(size)`

Returns a buffer containing random bytes of size `size`.

#### `hash = crypto.discoveryKey(publicKey)`

Return a hash derived from a `publicKey` that can used for discovery
without disclosing the public key.

## License

MIT

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