Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
daywiss committed Feb 16, 2017
1 parent d22f23c commit 840f10e
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ can verify the previous hash by hashing its predecessor. Any deviation from this
were tampered with and not part of the pregenerated series. Hence you can "prove" the random outcome was fair.

#Usage
This library can be used standalone to generate a series of random hashes. The engine can then
This library can be used standalone to generate a series of random hashes generated by nodes crypto sha256 hasher. The engine can then
be plugged into random-js in order to give you more control over your random values. By default
the engine will only supply a 32-bit random integer based on the next hash in the series. The
state of the engine can be saved and resumed as needed.
Expand Down Expand Up @@ -72,3 +72,77 @@ significant bits of the hash when generating the random integer.
#API
##Construction
The provable engine has an internal state which you can pass in as the only argument to construction. By default
no parameters are needed, but its suggested you change at least the count argument to something large. This
will specify the number of calls you can make to get hashes before the engine errors.
Hashes are generated on instance construction and are generated sycnronously so long hash counts
may cause blocking. You can forgo seeding the engine, a random UUIDv4 will be generated as the seed.
```js
var Provable = require('provable')
//node the actual list of hashes is not passed in as option. This will be generated on engine construction
//based on the config you supply. You can get the list with engine.hashes()

var config = {
id: // a string representing the unique id of this hash series
index: // integer representing which hash in the series will be used next
count: //the number of hashes in this series
seed: //a string to seed nodes crypto engine to start generating the first hash (last hash of series). Withold to seed with random hash.
clientSeed: //an additional seed value supplied, will update the hash before being returned from nextHash()
onChange: //a function callback which returns the state of the engine after every change. Optionally poll state instead with engine.state()
}

var engine = Provable(config)
//do stuff..
```
##engine()
The default function of the engine is to provide a random 32 bit integer. This allows random-js to wrap the engine
and produce random primitives. The integer is based on sampling the least significant 32 bits of the current hash.
```js
//integer is random between [0, 2^32] inclusive
//this increments your hash index and will call the change callback
var integer = engine()
```
##nextHash()
This gets the next raw hash in the series and increments your hash index. Will throw an error if no more hashes are found.
If hashes run out, then generate a new engine with a new seed. Do not reuse old seeds as you will generate predictable
hashes.
```js
//the next raw sha256 hash in the series. Update hash index and throws if no more hashes found.
var hash = engine.nextHash()
```
##peekHash()
Peek at the next hash without changing engine state. Returns undefined if no hashes are found.
```js
//the next raw sha256 hash in the series, does not change state of engine.
var hash = engine.peekHash()
```
##getHash(index)
Get the hash at specified index. Does not update engine, allows you read access to your hash chain.
```js
//get hash at specific location in hash series. Series starts with hash 0 and ends with hash count-1.
//your engine state will show your current position with engine.state().index
var hash = engine.getHash(100)
```
##generate(count,seed)
Generate a raw hash series. This is used internally by the engine but is exposed in case its useful
to use directly. Consider it like a static function, does not reference the internal state of the
engine. The result is an array of hashes which should be used starting at series[0]. Its important
to use the hashes in the correct order, or they will be predictable.
```js
//generate 10000 hashes and returns an array of them with the seed value of "seed"
var series = engine.generate(10000,'seed')
```

0 comments on commit 840f10e

Please sign in to comment.