Skip to main content

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

Project description

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.

from json_work_proof import JWP

jwp = JWP() # defaults to difficulty=20
jwp_harder = JWP(difficulty=25)

Generation

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

jwp = JWP()
token = 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:

expiration = datetime.utcnow() + timedelta(hours=1) # 1 hour from now
token = jwp.generate(claims, expiration=expiration)

token = jwp.generate(claims, expiration=None) # no expiration

Validation

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

jwp = JWP()
try:
  claims = jwp.decode(token)
except JWP.DecodeError.InvalidFormat:
  print("The token is formatted incorrectly")
except JWP.DecodeError.InvalidProof:
  print("The difficulty this token was created at is lower than what is specified in your JWP object")
except JWP.DecodeError.Expired:
  print("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:

claims = jwp.decode(token, verify=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:

claims = jwp.decode(token, expiration_range=JWP.DateRange(start_date, end_date)) # must be in [start_date, end_date]
claims = jwp.decode(token, expiration_range=JWP.DateRange.from_now(3600)) # must be in [now(), now()+3600]
claims = jwp.decode(token, expiration_range=JWP.DateRange.unlimited)) # all expirations are accepted

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json-work-proof-0.1.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_work_proof-0.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file json-work-proof-0.1.2.tar.gz.

File metadata

  • Download URL: json-work-proof-0.1.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for json-work-proof-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ef37fb7e82a7329a8c2a4ae53713f94e703088bf416bb3ff06048170410a2122
MD5 060cdeec6405a70cc56af11f56af2386
BLAKE2b-256 701544f098b419db73ea508ac3e4e42136cdab6cf3b03bc42233476654769c7b

See more details on using hashes here.

File details

Details for the file json_work_proof-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: json_work_proof-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for json_work_proof-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e0afad39c380769d0e718ba9a187ae4a990e566e5df9bcd1c85c036c6e2340d6
MD5 085a76ec02527394a103de73b40b2e0a
BLAKE2b-256 0ec2e2bdaa7c95328826ffc539c9b71ab69d246ec6c20ae471660f16554d73b8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page