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

json-work-proof

v0.1.6

Published

JSON Work Proof (JWP) - proof-of-work algorithm

Downloads

16

Readme

JSON Work Proof

JSON Work Proof is a proof-of-work algorithm that creates a token after doing some workload. This token contains certain claims defined by you and verifies that you did this work at this time and for these claims.

It packs the security of the Hashcash algorithm (used for Bitcoin in a similar way) into a modern JWT-like token.

Structure of Token

A token looks like this: eyJ0eXAiOiJKV1AiLCJhbGciOiJTSEEyNTYiLCJkaWYiOjIwfQ.eyJleHAiOjE2MTY4NTA1NzAuNjU1MTQ3MSwiaGVsbG8iOiJ3b3JsZCJ9.VE6YYxIQ46lPzxyNuRYAmAMkEM. It has the same structure as a JWT token and can therefore also be inspected on the Debugger on jwt.io. It contains three elements which are each base64url encoded. The header contains the type of the token (JWP), the hash algorithm used for the challenge (currently only SHA256 supported) and the difficulty at which the token was mined. The payload consists of the claims you specified and optionally an expiration date. The last part contains a salt and a big number (named counter in Hashcash). The work needed to generate a token is actually to find this number. It's hard to find this number, but easy to verify it's correct. (Read more about how it works on Wikipedia)

Possible Applications

Can be used to prevent DDOS attacks or as an alternative to rate limiting or captchas.

E.g. you can use this to prevent brute forcing user logins: The client generates a token with the claims including username and password and sends it along with the login request. The server then first checks if the token is valid before it does any lookup. The scale of bruteforcing can therefore be massively reduced.

Usage

General

To generate and validate tokens you need to use a JWP-object. On creation you can specify the difficulty, which determines how hard the challenge should be. It defaults to 20, which takes about a second to compute on an average computer. Each increment by one, doubles the difficulty and therefore the time it takes to generate.

const JWP = require('json-work-proof')

const jwp = new JWP() // defaults to difficulty=20
const jwp_harder = JWP(25) // first parameter sets difficulty

Generation

To generate a token, that proves you did work, create a JWP-object and call it with your dictionary of claims like this:

const jwp = new JWP()
const token = await jwp.generate({ hello: 'world', count: 88 })

Note: A token expires 5 minutes after creation on default. You can change this by giving a custom expiration date:

const expiration = new Date(new Date().getTime() + 3600*1000) // 1 hour from now
const token = await jwp.generate(claims, expiration)

const token2 = await jwp.generate(claims, null) // no expiration

Validation

To check if a token is valid for a certain difficulty and to read the claims:

const jwp = new JWP()
try {
  let claims = jwp.decode(token)
} catch (e) {
  if (e instanceof JWP.InvalidFormatError) {
    console.log("The token is formatted incorrectly")
  } else if (e instanceof JWP.InvalidProofError) {
    console.log("The difficulty this token was created at is lower than what is specified in your JWP object")
  } else if (e instanceof JWP.ExpiredError) {
    console.log("The token expiration is too old or too new")
  }
}

If you just want to read the claims without verifying the proof and expiration date, you can use this instead:

const claims = jwp.decode(token, false)

By default it expects the expiration date to be between now and 30 minutes in the future. You can also specify your own range of valid expiration dates like this:

const claims1 = jwp.decode(token, true, new JWP.DateRange(start_date, end_date)) # must be in [start_date, end_date]
const claims2 = jwp.decode(token, true, JWP.DateRange.fromNow(3600*1000)) # must be in [now(), now()+3600s]
const claims3 = jwp.decode(token, true, JWP.DateRange.unlimited)) # all expirations are accepted