# @ngraveio/bc-ur

> A JS implementation of the Uniform Resources (UR) specification from Blockchain Commons

Latest version **1.1.13** (published 2024-04-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ngraveio/bc-ur
pnpm add @ngraveio/bc-ur
yarn add @ngraveio/bc-ur
bun add @ngraveio/bc-ur
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.13 |
| Published | 2024-04-17 |
| First published | 2021-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 111.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Antonis Poulakis |
| Maintainers | mongrave, pieteruyttersprot, xardasss |
| Keywords | bc-ur |

## Links

- npm: https://www.npmjs.com/package/@ngraveio/bc-ur
- Repository: https://github.com/ngraveio/bc-ur
- Homepage: https://github.com/ngraveio/bc-ur#readme
- Issues: https://github.com/ngraveio/bc-ur/issues
- npm.io page: https://npm.io/package/@ngraveio/bc-ur

## Dependencies (7)

- [crc](https://npm.io/package/crc.md) ^3.8.0
- [jsbi](https://npm.io/package/jsbi.md) ^3.1.5
- [assert](https://npm.io/package/assert.md) ^2.0.0
- [sha.js](https://npm.io/package/sha.js.md) ^2.4.11
- [cbor-sync](https://npm.io/package/cbor-sync.md) ^1.0.4
- [bignumber.js](https://npm.io/package/bignumber.js.md) ^9.0.1
- [@keystonehq/alias-sampling](https://npm.io/package/@keystonehq/alias-sampling.md) ^0.1.1

## Recent versions

- 1.1.13 (latest) — 2024-04-17
- 2.0.0-beta.10 (beta) — 2026-02-20
- 1.2.0-beta.1 (main) — 2024-10-16
- 2.0.0-beta.9 — 2025-02-20
- 2.0.0-beta.8 — 2025-02-20
- 2.0.0-beta.7 — 2025-02-14
- 2.0.0-beta.6 — 2025-02-12
- 2.0.0-beta.5 — 2025-02-07
- 2.0.0-beta.4 — 2025-02-06
- 2.0.0-beta.3 — 2025-01-24
- 2.0.0-beta.2 — 2025-01-16
- 2.0.0-beta.1 — 2025-01-16
- 1.1.12 — 2024-03-03
- 1.1.11 — 2024-03-03
- 1.1.10 — 2024-03-03
- … 9 more at https://npm.io/package/@ngraveio/bc-ur/versions

## README

# BC-UR

This repository is an implementation of the BC-UR encoding, following the [C++ implementation](https://github.com/BlockchainCommons/bc-ur) and trying to provide a similar API for Javascript/Typescript usage.

## Installing

To install, run:
```bash
yarn add @ngraveio/bc-ur
```

## Quick Start

### Encode a message

```js
import {UR, UREncoder} from '@ngraveio/bc-ur'

const message = {any: 'property'}
const messageBuffer = Buffer.from(JSON.stringify(message))

// First step is to create a UR object from a Buffer
const ur = UR.fromBuffer(messageBuffer)

// Then, create the UREncoder object

// The maximum amount of fragments to be generated in total
// For NGRAVE ZERO support please keep to a maximum fragment size of 200
const maxFragmentLength = 200

// The index of the fragment that will be the first to be generated
// If it's more than the "maxFragmentLength", then all the subsequent fragments will only be
// fountain parts
const firstSeqNum = 0

// Create the encoder object
const encoder = new UREncoder(ur, maxFragmentLength, firstSeqNum)

// Keep generating new parts, until a condition is met; for example the user exits the page, or clicks "DONE"
while(!stop) {
  // get the next part in the sequence
  let part = encoder.nextPart()

  // get the part as a string containing the cbor payload and display it with whatever way
  // the part looks like this:
  // ur:bytes/1-9/lpadascfadaxcywenbpljkhdcahkadaemejtswhhylkepmykhhtsytsnoyoyaxaedsuttydmmhhpktpmsrjtdkgslpgh

  displayPart(part)
}
```

### Decode a message

```js
import {URDecoder} from '@ngraveio/bc-ur'

// Create the decoder object
const decoder = new URDecoder()

do {
  // Scan the part from a QRCode
  // the part should look like this:
  // ur:bytes/1-9/lpadascfadaxcywenbpljkhdcahkadaemejtswhhylkepmykhhtsytsnoyoyaxaedsuttydmmhhpktpmsrjtdkgslpgh
  const part = scanQRCode()

  // register the new part with the decoder
  decoder.receivePart(part)

  // check if all the necessary parts have been received to successfully decode the message
} while (!decoder.isComplete())

// If no error has been found
if (decoder.isSuccess()) {
  // Get the UR representation of the message
  const ur = decoder.resultUR()

  // Decode the CBOR message to a Buffer
  const decoded = ur.decodeCBOR()

  // get the original message, assuming it was a JSON object
  const originalMessage = JSON.parse(decoded.toString())
}
else {
  // log and handle the error
  const error = decoder.resultError()
  console.log('Error found while decoding', error)
  handleError(error)
}

```

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