@deliberative/utils
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

@deliberative/crypto

codecov Known Vulnerabilities
NPM Version NPM License code-style-prettier
NPM Downloads

This repository is part of the reference implementation of the Deliberative Ledger Protocol, the infrastructure for futuristic deliberative democracies.

It does not have any native dependencies and can be used in both Nodejs and the browser.

The API is not completely stable and the code has not undergone external security audits. Use at your own risk.

Introduction

This library relies on libsodium for random number generation operations, which is a battle-tested project, compiled to WebAssembly for speed. In comparison to tweetnacl this library is much faster. Benchmarks will be posted when there is time.

We have included some utility functions that do random shuffles, pick random subsets of Uint8Arrays etc.

Files

The libsodium directory contains a fork of libsodium whose only differences with the master branch of libsodium are name changes to the implementation structs.

Getting Started

To get started you have to install the package with

npm install @deliberative/utils

You can include as ES module

import dutils from "@deliberative/utils";

as CommonJS module

const dutils = require("@deliberative/utils");

or as UMD in the browser with

<script src="https://cdn.jsdelivr.net/npm/@deliberative/utils@0.1.5/lib/index.min.js"></script>

Examples

You can visit the examples folder, where you will find examples in CommonJS, ES module and html in the browser.

For cryptographic array utilities you can use the following features

import dutils from "@deliberative/utils";

const someRandomArray = await dutils.randomBytes(12); // 12 byte array
console.log(someRandomArray);

// Cryptographic shuffling
const someRandomArrayShuffled = await dutils.arrayRandomShuffle(
  someRandomArray,
);
console.log(someRandomArrayShuffled);

// Choose 5 elements from someRandomArray uniformly.
const someRandomSubArray = await dutils.arrayRandomSubset(someRandomArray, 5); // 5 elements
console.log(someRandomSubArray);

// Choose 5 other elements and chances are that the arrays are different
const someOtherRandomSubArray = await dutils.arrayRandomSubset(
  someRandomArray,
  5,
);
console.log(someOtherRandomSubArray);

const someRandomNumberBetween0and100 = await dutils.randomNumberInRange(0, 100);
const someOtherRandomNumberBetween0and100 = await dutils.randomNumberInRange(
  0,
  100,
);
console.log(someRandomNumberBetween0and100);
console.log(someOtherRandomNumberBetween0and100);

For more examples you can see the tests directory.

Development

If you want to bundle the library yourselves, you need to have Emscripten installed on your machine in order to compile the C code into WebAssembly. We have the -s SINGLE_FILE=1 option for the emcc compiler, which converts the wasm file to a base64 string that will be compiled by the glue js code into a WebAssembly module. This was done for the purpose of interoperability and modularity.

Once you have all the dependencies installed, you can run

npm run build

and Rollup will generate the UMD, ESM and CJS bundles.

For development compilation you can run

npm run build:debug

and everything will work in debug mode.

Releases

Releases are available on Github and npmjs.com

License

The source code is licensed under the terms of the Apache License version 2.0 (see LICENSE).

Copyright

Copyright (C) 2022 Deliberative Technologies P.C.

Package Sidebar

Install

npm i @deliberative/utils

Weekly Downloads

0

Version

0.2.1

License

Apache-2.0

Unpacked Size

538 kB

Total Files

29

Last publish

Collaborators

  • karoukis