# databridges-js-nacl-wrapper

> DataBridges JavaScript NaCl wrapper for databridges library.

Latest version **1.0.1** (published 2022-08-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install databridges-js-nacl-wrapper
pnpm add databridges-js-nacl-wrapper
yarn add databridges-js-nacl-wrapper
bun add databridges-js-nacl-wrapper
```

## 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.1 |
| Published | 2022-08-02 |
| First published | 2022-08-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 4.2.6 |
| Dependencies | 1 |
| Unpacked size | 146.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Optomate Technologies Private Limited. |
| Maintainers | optomate.tech |
| Keywords | optomate, databridges, pubsub, rpc, websocket, socket.io, ws, events, realtime, real-time, nacl, encryption |

## Links

- npm: https://www.npmjs.com/package/databridges-js-nacl-wrapper
- Repository: https://github.com/databridges-io/lib.nodejs.nacl.wrapper
- Issues: https://github.com/databridges-io/lib.nodejs.nacl.wrapper/issues
- npm.io page: https://npm.io/package/databridges-js-nacl-wrapper

## Dependencies (1)

- [crypto](https://npm.io/package/crypto.md) ^1.0.1

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2022-08-02

## README

![](https://img.shields.io/badge/Licence-MIT-green.svg)![](https://shields.io/badge/node.js-%3E=10.0%20LTS-blue)

# Databridges JavaScript NaCl Wrapper Library

Databridges NACL wrapper gives you a simple write and read function using implementation of the secretbox encryption standard defined in NaCl.

Databridges NACL wrapper is available for 

- [JavaScript](https://github.com/databridges-io/lib.javascript.nacl.wrapper.git) 
- [NodeJS](https://github.com/databridges-io/lib.nodejs.nacl.wrapper.git)
- [C#](https://github.com/databridges-io/lib.csharp.nacl.wrapper.git)
- [Python](https://github.com/databridges-io/lib.python.nacl.wrapper.git)
- [Java for Android](https://github.com/databridges-io/lib.android.nacl.wrapper.git)
- [iOS Swift](https://github.com/databridges-io/lib.ios.nacl.wrapper.git)

The above wrappers can be used to send encrypted messages between them.

> The Databridges NACL wrapper for JavaScript Language binding uses `tweetnacl` to deliver implementation of the secretbox encryption standard defined in NaCl.

## Usage Overview

The following topics are covered:

  - [Supported platforms](#supported-platforms)
  - [Installation.](#installation)
  - [Initialization](#initialization)
  - [Global Configuration](#global-configuration)
  - [How to use with Databridges JavaScript Library](#how-to-use-with-databridges-javascript-library)
  - [Change Log](#change-log)
  - [License](#license)

## Supported platforms

| Android | Firefox | Chrome | Edge |
| :-----: | :-----: | :----: | :--: |
|  5.1+   |   78+   |  84+   | 99+  |


## Initialization

Include databridges NaCl wrapper library from any of below CDN:

- **jsDelivr** :

```html
<script src="https://cdn.jsdelivr.net/npm/databridges-js-nacl-wrapper@1.0.1/dist/databridges.nacl.wrapper.min.js" integrity="sha384-O34Ik4FhdcikoNSke8zZHqyXO1JjAn/Jw/pKdp/PYlVQcBB19JdPKZn0uz+wtd5t" crossorigin="anonymous"></script>
```

- **unpkg** :

```html
<script src="https://unpkg.com/databridges-js-nacl-wrapper@1.0.1/dist/databridges.nacl.wrapper.min.js" integrity="sha384-O34Ik4FhdcikoNSke8zZHqyXO1JjAn/Jw/pKdp/PYlVQcBB19JdPKZn0uz+wtd5t" crossorigin="anonymous"></script>
```

Now initialize databridges object in your script.

```js
const secretData = new dbNaClWrapper();
```

## Global Configuration

### Required

The following is the required properties before using to dataBridges NaCl wrapper.

```javascript
secretData.secret = '32 char alphanumeric string';
```

| Properties | Description                                                  |
| ---------- | ------------------------------------------------------------ |
| `secret`   | *(string)* 32 char alpha numeric string. NaCl encryption secret. |

## Encrypt data

To encrypt data using NaCl, databridges wrapper exposes a method named `write`, This will return encrypted data if successful else it will throw error.

#### write() 

```javascript
try {
    const encData = secretData.write("Your Data..");
    console.log('Encrypted:', encData);
} catch (err) {
    console.log('Errors:', err.source, err.code, err.message);
}
```

| Parameter | Description                        |
| --------- | ---------------------------------- |
| `data`    | *(string)* *data* to be encrypted. |

| Return Values | Description        |
| ------------- | ------------------ |
| `string`      | Encrypted  string. |

##### Exceptions: 

| Source             | Code           | Description                                    |
| ------------------ | -------------- | ---------------------------------------------- |
| DBLIB_NACL_WRAPPER | INVALID_SECRET | `secret` is not set with the wrapper instance. |
| DBLIB_NACL_WRAPPER | INVALID_DATA   | If `data` is not passed to the function.       |
| DBLIB_NACL_WRAPPER | NACL_EXCEPTION | Exceptions generated by NaCl library.          |

## Decrypt data

To decrypt data using NaCl, databridges wrapper exposes a method named `read`, This will return decrypted data if successful else it will throw error.

#### read() 

```javascript
try {
    const decData = secretData.read("<Encrypted data.>");
    console.log('Decrypted:', decData);
} catch (err) {
    console.log('Errors', err.source, err.code, err.message);
}
```

| Parameter | Description                        |
| --------- | ---------------------------------- |
| `data`    | *(string)* *data* to be encrypted. |

| Return Values | Description        |
| ------------- | ------------------ |
| `string`      | Encrypted  string. |

##### Exceptions: 

| Source             | Code                | Description                                                  |
| ------------------ | ------------------- | ------------------------------------------------------------ |
| DBLIB_NACL_WRAPPER | INVALID_SECRET      | `secret` is not set with the wrapper instance.               |
| DBLIB_NACL_WRAPPER | INVALID_DATA        | If `data` is not passed to the function OR `data` is not a valid encrypted string. |
| DBLIB_NACL_WRAPPER | NACL_EXCEPTION      | Exceptions generated by NaCl library.                        |
| DBLIB_NACL_WRAPPER | NACL_DECRYPT_FAILED | If decryption fails due to invalid secret or manipulated data. |

## How to use with Databridges JavaScript Library

Below code shows how to integrate the NaCl wrapper with the Databridges library. After initialize you can use the wrapper library to encrypt and decrypt the data when publishing and receiving events.

```javascript
// Initialize both databridges-sio-client-lib and databridges-nacl-wrapper
const dBridges = require('databridges-sio-client-lib');
const dbNaClWrapper = require('databridges-nacl-wrapper');

const dbridge = new dBridges();
const secretData = new dbNaClWrapper();
secretData.secret = "Your32 char secret.";

// .... Your databridges code comes here.

// On Subscription success event.
subscribeChannel.bind("dbridges:subscribe.success", (payload, metadata) => {
    console.log('Channel subscribe => bind', metadata.eventname, payload, JSON.stringify(metadata));
    try {
        // Encrypt data to publish.
        const encData = secretData.write("Your Data.."); 
        subscribeChannel.publish("eventName", encData, "1") 
    } catch (err) {
        console.log('Error:', err.source, err.code, err.message);
    }
});

// On payload Received event.
subscribeChannel.bind("eventName", (payload, metadata) => {
    console.log('eventName=> bind', metadata.eventname, payload, JSON.stringify(metadata));
    try {
        // Decrypt data received in the event.
        const decData = secretData.read(payload);
        console.log('Decrypted:', decData);
    } catch (err) {
        console.log('Error:', err.source, err.code, err.message);
    }
});
```



## Change Log
  * [Change log](CHANGELOG.md): Changes in the recent versions

## License

DataBridges NaCl Wrapper is released under the [MIT license](LICENSE).

```
    Copyright 2022 Optomate Technologies Private Limited.

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```

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