drand / drand-client Goto Github PK
View Code? Open in Web Editor NEW๐ฒ A JavaScript client to the drand randomness beacon network.
License: Other
๐ฒ A JavaScript client to the drand randomness beacon network.
License: Other
I have the following deno script:
import { HttpCachingChain, HttpChainClient, watch } from "npm:[email protected]";
const chainHash = "8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce";
const publicKey =
"868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31"; // (hex encoded)
const options = {
disableBeaconVerification: false,
noCache: false,
chainVerificationParams: { chainHash, publicKey },
};
if (import.meta.main) {
const chain = new HttpCachingChain("https://api.drand.sh", options);
const client = new HttpChainClient(chain, options);
const abortController = new AbortController();
for await (const beacon of watch(client, abortController)) {
console.log(new Date().toUTCString(), "Watch returned a beacon:", beacon);
}
}
and run it with deno run --allow-read --allow-net main.ts > log.txt
. As you can see in the logs, the same round is returned multiple times. E.g. round: 2618607
is there 302 times.
in package.json we have "main": "lib/drand.ts", which does not exist.
Also shouldn't this point to a compiled .js file? It seems like the npm package does not contain .js builds at all.
I am working on a drand-client in rust. I am using the go implementation drand/client
for reference and so far it checks fine. For verification I am using drand-verify. Before I put some serious effort in drand-rs I want to make sure that the rust-client aligns with Drand requirements and specification. We can have this as the tracking issue for that.
drand-verify is a Rust implementation of drand beacon verification based on paired. It can be compiled to Wasm and comes with JavaScript bindings for different environments.
The Wasm blob is 448K big (compared to 3.0M of the current Go-based build). It also speeds up verification time from 150ms to 50ms.
Those are the benchmarks I ran in Node.js 14.10. Please note that instantiation times canot be compared fairly since my proof of concept uses synconous CommonJS module loading in Node.js where the Wasm instantiation happens during module loading. However, beacon verification times should be fair.
Verification timings current master
> [email protected] test /projects/drand-client
> ava -v -T 10m
(node:1839) ExperimentalWarning: The ESM module loader is experimental.
(node:1840) ExperimentalWarning: The ESM module loader is experimental.
โ optimizing โบ should fail to get info if all clients fail
โ optimizing โบ should fail to get randomness if all clients fail
โ optimizing โบ should get info from working client
โ optimizing โบ should get from the fastest client (107ms)
โ optimizing โบ should get randomness from working client (223ms)
โ optimizing โบ should switch to a faster client (413ms)
(node:1838) ExperimentalWarning: The ESM module loader is experimental.
โ chain โบ should get round 1 when time is less than genesis
โ chain โบ should get round 1 for first round
โ chain โบ should get round 2 for second round
โ chain โบ should get time 0 when round is < 0
โ chain โบ should get time 0 for first round
โ chain โบ should get time 1000 for second round
Verification time: 1726ms (0ms info; 163ms instantiation; 1563ms verify beacon)
Verification time: 1720ms (0ms info; 797ms instantiation; 923ms verify beacon)
Verification time: 1719ms (0ms info; 1210ms instantiation; 509ms verify beacon)
Verification time: 1606ms (0ms info; 1382ms instantiation; 224ms verify beacon)
โ drand โบ should abort get (1.8s)
โ drand โบ should disable beacon verification (1.8s)
Verification time: 155ms (0ms info; 0ms instantiation; 155ms verify beacon)
โ drand โบ should get specific randomness round (2s)
โ optimizing โบ should watch from the fastest client (2.1s)
Verification time: 158ms (0ms info; 0ms instantiation; 158ms verify beacon)
Verification time: 143ms (0ms info; 0ms instantiation; 143ms verify beacon)
โ drand โบ should get latest randomness (2.3s)
Verification time: 313ms (0ms info; 0ms instantiation; 313ms verify beacon)
Verification time: 151ms (0ms info; 0ms instantiation; 151ms verify beacon)
Verification time: 140ms (0ms info; 0ms instantiation; 140ms verify beacon)
โ drand โบ should watch for randomness (1m 16.6s)
โ
18 tests passed
Verification timings using the Rust implementation
> [email protected] test /projects/drand-client
> ava -v -T 10m
(node:1917) ExperimentalWarning: The ESM module loader is experimental.
โ chain โบ should get round 1 when time is less than genesis
โ chain โบ should get round 1 for first round
โ chain โบ should get round 2 for second round
โ chain โบ should get time 0 when round is < 0
โ chain โบ should get time 0 for first round
โ chain โบ should get time 1000 for second round
(node:1918) ExperimentalWarning: The ESM module loader is experimental.
(node:1919) ExperimentalWarning: The ESM module loader is experimental.
โ optimizing โบ should fail to get info if all clients fail
โ optimizing โบ should fail to get randomness if all clients fail
โ optimizing โบ should get info from working client
โ optimizing โบ should get from the fastest client (107ms)
โ optimizing โบ should get randomness from working client (181ms)
Verification time: 44ms (0ms info; 0ms instantiation; 44ms verify beacon)
โ drand โบ should abort get (172ms)
Verification time: 49ms (0ms info; 0ms instantiation; 49ms verify beacon)
Verification time: 44ms (0ms info; 0ms instantiation; 44ms verify beacon)
Verification time: 59ms (0ms info; 0ms instantiation; 59ms verify beacon)
โ optimizing โบ should switch to a faster client (406ms)
Verification time: 49ms (0ms info; 0ms instantiation; 49ms verify beacon)
โ drand โบ should get specific randomness round (413ms)
Verification time: 47ms (0ms info; 0ms instantiation; 47ms verify beacon)
โ drand โบ should disable beacon verification (657ms)
Verification time: 43ms (0ms info; 0ms instantiation; 43ms verify beacon)
โ drand โบ should get latest randomness (755ms)
โ optimizing โบ should watch from the fastest client (2.1s)
Verification time: 45ms (0ms info; 0ms instantiation; 45ms verify beacon)
Verification time: 43ms (0ms info; 0ms instantiation; 43ms verify beacon)
Verification time: 51ms (0ms info; 0ms instantiation; 51ms verify beacon)
โ drand โบ should watch for randomness (56.3s)
โ
18 tests passed
All the jest-http-mock tests fail on the latest LTS version of node, as they appear to use actual fetch
rather than the mock (presumably because fetch
is now available on LTS)
There is a 1.1.0 on npm but the v1.1.0 tag seems to be missing here.
This line says the public_key
from /info
is Ed25519, while the drand cryptographic specification and the library dependency says it's the BLS12-381 G1 point. Please let me know if this comment needs updating or if I'm just losing my mind. Thanks!
Deno was supported in v0.* via storing all the JS on a CDN. This is not currently the case for v1 and should be implemented for parity.
In order to not break v1 users of the client, we pass the DST for beacon verification as an optional parameter and set some defaults.
This can be removed in v2 to ensure users don't accidentally try and verify a beacon with the wrong DST
When trying to use this lib from npm in a React project, I get the error
Failed to compile.
./node_modules/drand-client/verifying.js 16:29
Module parse failed: Unexpected token (16:29)
File was processed with these loaders:
* ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
| // TODO: switch to TinyGo when math/big works for smaller wasm file and non-global exports.
| const go = new Go();
> const url = `${import.meta.url.split('/').slice(0, -1).join('/')}/wasm/drand.wasm`;
| let result;
|
This is because import.meta.url is not supported in Webpack 4. Webpack 5 support probably will take some more time.
I have no great idea how to solve this, but let's see.
In order to support multiple chains, the client should be able to fetch the list of chains using, e.g. http://pl-us.testnet.drand.sh/chains which is currently supported in the go client. This allows a user to make followup requests to chainInfo and programmatically select chains that fulfill their required criteria (e.g. chained vs unchained)
The newly added user-agent to the drand-client can cause some issues with CORS in browsers, as fetch
does not pass the "mode": "cors"
option
see: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options
Would allow the client to select the fastest URL to get randomness from.
https://github.com/drand/drand/blob/master/client/optimizing.go#L135-L169
The fetch API was introduced into node via an experimental flag in node 17, and without a flag in node 18. Currently it isn't polyfilled in this library for node < 17 - we use node-fetch in the tests, but isomorphic-fetch should be added to provide a better experience for users in older versions of node.
Tried to run with the deno
sample code and cli invocation which fails, copied from the README. deno
was installed on macOS with brew install deno
.
import Client, { HTTP } from 'https://cdn.jsdelivr.net/npm/drand-client/drand.js'
const chainHash = '8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce' // (hex encoded)
const urls = [
'https://api.drand.sh',
'https://drand.cloudflare.com'
]
async function main() {
const options = { chainHash }
const client = await Client.wrap(HTTP.forURLs(urls, chainHash), options)
// e.g. use the client to get the latest randomness round:
const res = await client.get()
console.log(res.round, res.randomness)
}
main()
Failing run:
$ deno -V
deno 1.3.2
$ deno run --allow-net index.js
error: Uncaught TypeError: WebAssembly.compile(): Argument 0 must be a buffer source
package.json
{
"name": "foo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"abort-controller": "^3.0.0",
"drand-client": "^0.2.0",
"node-fetch": "^2.6.0"
}
}
Once drand/drand#1103 lands, and we've launched a new testnet network with the swapped curves for the BLS signature, we should make sure to add support for verifying these beacons.
The Noble library currently doesn't support it, but it should have all the required methods to implement it easily now that hash to curve is supported on both groups:
paulmillr/noble-bls12-381#43
Two of the tree testnet endpoints used for testing this lib (https://github.com/drand/drand-client/blob/42b9dfe/drand.test.js#L10-L14 and https://github.com/drand/drand-client/blob/42b9dfe/optimizing.test.js#L12-L16) are currently unavailable for me:
Are they expected to become available again? Are there alternative nodes that can be used for testing with multiple clients?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.