@deliberative/utils
v0.2.1
Published
Utility functions for the deliberative ledger protocol for nodejs and the browser.
Downloads
36
Maintainers
Readme
@deliberative/crypto
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/[email protected]/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.