npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ledger-client

v0.9.22

Published

An example of client code for the Brave ledger.

Downloads

415

Readme

ledger-client

An example of client code for the Brave ledger.

API

To begin:

  • The client must maintain a secure, persistent storage in which it can store a JSON object.

  • Some calls require a callback of the form:

      var callback = function (err, result, delayTime) { ... }

    When the callback is invoked, if err is null, and result is not null, then result must be put into persistent storage. (If err is null, then the operation has succeeded, regardless of whether result is defined or not.)

  • The Ledger protocol requires that the client uses a pseudo-random delay be introduced at certain points during operations. Accordingly, if the delayTime parameter is defined, then the client should wait at least delayTime milliseconds before making a call to

      client.sync(callback)

    There is no harm in retrying earlier, but, from the network's perspective, it will be a no-op.

Creating an Endpoint

    var Client = require('ledger-client')
    this.client = new Client(personaId, options, state)
    this.client.sync(callback)

where the value for personaId (if not null) is a UUID v4 value and options is:

    // all properties are optional...
    { server            : 'https://ledger.brave.com'
    , debugP            : false
    , loggingP          : false
    , verboseP          : false
    }

and state is either: whatever was previously stored in persistent storage, or {}.

The client endpoint should not be referenced until the callback is invoked.

Bravery Properties

To retrieve the Bravery properties for the Ledger, the client calls:

    var properties = this.client.getBraveryProperties()

where properties is a list of configuration options:

| Property | Meaning | Examples | |------------:|-----------------------------|------------------------------| | setting | "adFree" or "adReplacement" | adFree | | days | the reconcilation period | 30 | | fee | for "adFree" | { currency: USD, amount: 5 } |

To update the Bravery properties for the Ledger, the client calls:

    this.client.setBraveryProperties(properties, function (err, result) {
      if (err) return console.log(err)

      if (result) result must be put into persistent storage as the client's new state
    })

Note that this will likely result in the callback being invoked with a result parameter, indicating that persistent storage be updated.

Wallet Properties

    var address = this.client.getWalletAddress()

    this.client.getWalletProperties(function (err, properties) {
      if (err) return console.log(err)

      console.log('wallet balance=' + properties.balance + 'BTC')
    })

Wallet Recovery

    this.client.recoverWallet(recoveryId, passPhrase, function (err, result) {
      if (err) return console.log(err)

      console.log('recovered amount=' + result.satoshis + ' satoshis')
    })

Reconcilation, Part One

The client should periodically call:

    var nowP = client.isReadyToReconcile()

If true is returned, then it is time for the periodic reconcilation to occur.

Alternatively,

    var msec = client.timeUntilReconcile()

will return false if reconcilation is already underway, or the number of milliseconds before reconcilation should occur (a negative number indicates that reconcilation is overdue).

It may be necessary to reset the reconcilation timestamp,

    var timestamp = new Date().getTime()  // reconcile now (for some reason)

    this.client.setTimeUntilReconcile(timestamp, function (err, result) {
      if (err) return console.log(err)

      if (result) result must be put into persistent storage as the client's new state
    })

The more likely invocation is

    this.client.setTimeUntilReconcile(null, function (err, result) { ... })

which resets the reconcilation timestamp.

Reconcilation, Part Deux

When it is time to reconcile, the client calls:

    client.reconcile(viewingId, callback)

The viewingId parameter (if not null) is a UUID v4 value, that may be used for subequent calls to vote()`.

Statistical Voting

After a successful reconciliation, the client is authorized to cast one or more ballots, as indicated by the ballots method. Each vote is cast using the vote method:

if (client.ballots() > 0) {
  // select publisher identity
  client.vote(publisher, viewingId)
}

The viewingId parameter is optional, otherwise it should correspond to a value used in a previous call to the reconcile method.

Logging

If options.loggingP is true, then the client may call

var entries = client.report()

which returns either an array of (zero or more) logging entries. Each entry contains three fields:

    { who  : function that made entry
      what : { parameters }
      when : timestamp (as milliseconds since epoch)
    }

Examples

The file blastoff.js is a (non-sensical) example of how to use the API -- it blasts through the various API calls, doing a sanity check. Invoke using:

% npm run blastoff
...
please click here for payment: bitcoin:...?amount=0.0083
^C

// transfer funds to user wallet, wait as long (or as little) as you want

% npm run touchdown

When reconciliation completes (but before voting occurs), the process will exit. Examine config.json to see the entry in the transactions array.