@alanshaw/drand-client-twitter

A client for the drand twitter relay

Usage no npm install needed!

<script type="module">
  import alanshawDrandClientTwitter from 'https://cdn.skypack.dev/@alanshaw/drand-client-twitter';
</script>

README

drand-client-twitter

dependencies Status JavaScript Style Guide

A client for the drand twitter relay.

Install

In Deno you can grab and use the client from a CDN e.g. https://unpkg.com/@alanshaw/drand-client-twitter?module.

In Node.js, install with:

npm i @alanshaw/drand-client-twitter

Usage

The client uses the user timeline API exclusively. It is best suited for watching or getting the latest randomness generated. It's possible to get historic randomness up to the API limits of around 3,200 tweets. In order to retrieve historic randomness the client will walk the chain of tweets (max 200 per page).

Deno

import Client from 'https://unpkg.com/drand-client/drand.js'
import Twitter from 'https://unpkg.com/@alanshaw/drand-client-twitter?module'

const chainInfo = {
  public_key: '868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31',
  period: 30,
  genesis_time: 1595431050,
  hash: '8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce'
}

async function main () {
  const c = await Client.wrap([
    new Twitter({ screenName: 'loebot', bearerToken: 'AAAAAAAAAAAAAAAAAAAAAHJT...', chainInfo })
  ], { chainInfo })

  for await (const res of c.watch()) {
    console.log(res)
  }
}

main()

Node.js

import Client from 'drand-client'
import Twitter from '@alanshaw/drand-client-twitter'
import fetch from 'node-fetch'
import AbortController from 'abort-controller'

global.fetch = fetch
global.AbortController = AbortController

const chainInfo = {
  public_key: '868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31',
  period: 30,
  genesis_time: 1595431050,
  hash: '8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce'
}

async function main () {
  const c = await Client.wrap([
    new Twitter({ screenName: 'loebot', bearerToken: 'AAAAAAAAAAAAAAAAAAAAAHJT...', chainInfo })
  ], { chainInfo })

  for await (const res of c.watch()) {
    console.log(res)
  }
}

main()

Running the examples

Add credentials.json to the examples/ directory like:

{
  "bearerToken": "AAAAAAAAAAAAAAAAAAAAAHJT..."
}

Run a client that watches for randomness and prints it to the console:

node ./examples/node.js
# or
deno run --allow-net --allow-read ./examples/deno.js

API

import Twitter from '@alanshaw/drand-client-twitter'

See the drand client API Reference docs.

new Twitter(config)

Create a new drand Twitter client.

  • config.screenName - screen name of the twitter user that is relaying randomness (required).
  • config.bearerToken - access token for using the Twitter API (required).
  • config.chainInfo - information about the chain of randomness that is being relayed, for beacon verification (required).

Optional configuration:

  • config.latestRoundDelay - expected delay in ms between randomness being generated and it being available on twitter, reduce this for faster latest randomness but more requests to twitter (default 650ms).
  • config.latestRoundRetries - maximum retries for getting the latest round before it fails (default 5).
  • config.latestRoundRetryBackoff - backoff interval in ms between retries - doubled each retry (default 50).

Contribute

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Alan Shaw