# ssb-bfe

> Binary Field Encodings (BFE) for Secure Scuttlebutt (SSB)

Latest version **3.7.0** (published 2022-11-02) · LGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install ssb-bfe
pnpm add ssb-bfe
yarn add ssb-bfe
bun add ssb-bfe
```

## 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 | 3.7.0 |
| Published | 2022-11-02 |
| First published | 2021-07-06 |
| Weekly downloads | 0 |
| License | LGPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 41.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Anders Rune Jensen |
| Maintainers | arj03, chereseeriepa, happy0, staltz, pfrazee, mixmix, cel, christianbundy, andregarzia, dominictarr, noffle, ahdinosaur, regular, aljoscha-meyer, pietgeursen, luandro, cryp7ix, blakeakapita, mycognosist |

## Links

- npm: https://www.npmjs.com/package/ssb-bfe
- Repository: https://github.com/ssbc/ssb-bfe
- Issues: https://github.com/ssbc/ssb-bfe/issues
- npm.io page: https://npm.io/package/ssb-bfe

## Dependencies (4)

- [ssb-ref](https://npm.io/package/ssb-ref.md) ^2.16.0
- [ssb-uri2](https://npm.io/package/ssb-uri2.md) ^2.4.0
- [ssb-bfe-spec](https://npm.io/package/ssb-bfe-spec.md) ^0.8.0
- [is-canonical-base64](https://npm.io/package/is-canonical-base64.md) ^1.1.1

## Recent versions

- 3.7.0 (latest) — 2022-11-02
- 3.6.1 — 2022-10-04
- 3.6.0 — 2022-09-13
- 3.5.0 — 2022-07-19
- 3.4.0 — 2022-07-15
- 3.3.0 — 2022-05-26
- 3.2.1 — 2022-05-26
- 3.2.0 — 2022-05-22
- 3.1.4 — 2022-05-20
- 3.1.3 — 2021-10-08
- 3.1.2 — 2021-10-04
- 3.1.1 — 2021-08-27
- 3.1.0 — 2021-08-27
- 3.0.2 — 2021-08-24
- 3.0.1 — 2021-08-23
- … 16 more at https://npm.io/package/ssb-bfe/versions

## README

<!--
SPDX-FileCopyrightText: 2021 Anders Rune Jensen

SPDX-License-Identifier: CC0-1.0
-->

# SSB BFE

Javascript implementation of the [SSB binary field encodings] spec.

The spec only has one type of **nil**, but JavaScript has two: `null` and
`undefined`. ssb-bfe will treat these two values in a way that mirrors what
JSON.stringify does:

- BFE Encoding an **object** with a `null` field becomes an object with the
**nil** marker
  - Similar to `JSON.stringify({a: null}) === '{"a": null}'`
- BFE Encoding an **array** with a `null` element becomes an array with the
**nil** marker
  - Similar to `JSON.stringify([null]) === '[null]'`
- BFE Encoding an **object** with a `undefined` field will **omit** that field
  - Similar to `JSON.stringify({a: undefined}) === '{}'`
- BFE Encoding an **array** with an `undefined` element becomes an array with
the **nil** marker
  - Similar to `JSON.stringify([undefined]) === '[null]'`

## API

### `encode(input)`

Takes any JavaScript primitive and returns its encoded counterpart. Is applied
recursively in case the input is an object or an array. All inputs are converted
to [TFD] Buffers, except for objects, arrays, and numbers, which remain the
same.

### `decode(input)`

Takes an encoded value (such as the output from `encode`) and returns the
decoded counterparts as JavaScript primitives.

### `decodeTypeFormat(input, typeName, formatName)`

Takes an encoded value (such as the output from `encode`), a typeName
and formatName and returns the decoded counterparts as JavaScript
primitives. This is much faster than decode if you know the type.

### `bfeTypes`

Returns the `bfe.json` object that can be used to look up information
based on Type and Field. Example:

```
const { bfeTypes } = require('ssb-bfe')
const classic_key_size = bfeTypes[0][0].data_length
```

### `bfeNamedTypes`

Returns the `bfe.json` object converted to a map where the keys are
the type and format names. Example:

```js
const { bfeNamedTypes } = require('ssb-bfe')
const FEED = bfeNamedTypes['feed']
const CLASSIC_FEED_TF = Buffer.from([FEED.code, FEED.formats['classic'].code])
```

### `toTF(typeName, formatName)`

Sometimes when you're wanting to check what sort of buffer you're handling, you want
to pivot on the type and format bytes.

```js
const CLASSIC_MSG_TF = Buffer.from([1, 0])  // << Did I get the right codes?

if (buf.slice(0, 2).equals(CLASSIC_MSG_TF)) {
  // ...
}
```

because remembering those codes is tricky, it's safer to use this convenience method:
```js
const CLASSIC_MSG_TF = bfe.toTF('msg', 'classic')
```

If you remembered the type or format name wrong, you'll instantly get an error!

### Check whether a buffer is an encoded type

Given any input, these functions tell you whether the input is of the specified
type (and format, if applicable). Example:

```js
const bfe = require('ssb-bfe')

const x = bfe.encode('@6CAxOI3f+LUOVrbAl0IemqiS7ATpQvr9Mdw9LC4+Uv0=.ed25519')

bfe.isEncodedFeedClassic(x) // true
```

List of functions:

- `isEncodedFeed(input) => boolean`
- `isEncodedFeedClassic(input) => boolean`
- `isEncodedFeedGabbygroveV1(input) => boolean`
- `isEncodedFeedBamboo(input) => boolean`
- `isEncodedFeedBendybuttV1(input) => boolean`
- `isEncodedFeedButtwooV1(input) => boolean`
- `isEncodedMessage(input) => boolean`
- `isEncodedMessageClassic(input) => boolean`
- `isEncodedMessageGabbygroveV1(input) => boolean`
- `isEncodedMessageCloaked(input) => boolean`
- `isEncodedMessageBamboo(input) => boolean`
- `isEncodedMessageBendybuttV1(input) => boolean`
- `isEncodedMessageButtwooV1(input) => boolean`
- `isEncodedBlob(input) => boolean`
- `isEncodedBlobClassic(input) => boolean`
- `isEncodedEncryptionKey(input) => boolean`
- `isEncodedEncryptionKeyBox2DmDh(input) => boolean`
- `isEncodedEncryptionKeyBox2PoboxDh(input) => boolean`
- `isEncodedSignature(input) => boolean`
- `isEncodedSignatureMsgEd25519(input) => boolean`
- `isEncodedEncrypted(input) => boolean`
- `isEncodedEncryptedBox1(input) => boolean`
- `isEncodedEncryptedBox2(input) => boolean`
- `isEncodedGeneric(input) => boolean`
- `isEncodedGenericStringUTF8(input) => boolean`
- `isEncodedGenericBoolean(input) => boolean`
- `isEncodedGenericNil(input) => boolean`
- `isEncodedGenericAnyBytes(input) => boolean`
- `isEncodedIdentity(input) => boolean`
- `isEncodedIdentityPoBox(input) => boolean`


[ssb binary field encodings]: https://github.com/ssb-ngi-pointer/ssb-binary-field-encodings-spec
[TFD]: https://github.com/ssbc/envelope-spec/blob/master/encoding/tfk.md

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