Scrypt hashcash implementation for node & browsers.
npm i scrypt-hashcash
sh = ;
API
text, target, [noncefunction, callback]
)
sh.pow(Generate a proof-of-work token over some text for a given target difficulty.
text
is the material to be hashed over.target
is the difficulty vector to try to outperform (usesh.target(bits)
to generate a target vector for a particular difficulty ofbits
zero-bits.noncefunction
is an optional callback function to return nonces which takes a single argumenti
being the number of iterations so far.callback
is an optional callback for when the promise API is not used.
sh;
text, nonce, target, [callback]
)
sh.verify(Verify some previously found nonce hashes to a lower value than the target difficulty specified.
text
is the material which was hashed to find the nonce.nonce
is the nonce which the client claims to have found to beat the target.target
is the difficult vector to be verfied has having been exceeded.callback
is an optional callback for when the promise API is not used.
sh;
bits
)
sh.target(Generate a target vector for a particular number of zero-bits.
bits
is the number of zero-bits of difficulty required.
> shtarget4;Uint8Array 15 255 255 255 255 255 255 255
hashrate, time
)
sh.difficulty(Calculate how many bits of difficulty are required on average to run at a particular hashrate for a particular time.
hashrate
is the hashrate expected.time
is the amount of time (e.g. seconds).
> s9
sh.measure()
Measure the number of hashes per second the current device can perform.
iterations
is an optional number of iterations to carry out (defaults to 50).
s.measure().then(function(d) {
console.log(d + " hashes per second");
})
h
)
sh.toHex(Convenience function to convert an array to a hex string representation.
h
)
sh.fromHex(Convenience function to convert a hex string to a Uint8Array
.
sh.config
The scrypt
configuration being used. Parameters include dkLen
, N
, r
, and p
. See the Scrypt Wikipedia page for a summary of these variables and this blog post for a treatment of how to tune them for different uses.
Command line usage
You can use scrypt-hashcash
from the command line.
For example, to find a collision of 4 bits against the test vector:
$ scrypt-hashcash 0:030626:adam@cypherspace.org 40:030626:adam@cypherspace.org:8043dfc5850f9525
Then to verify the nonce that was found and get the hash:
$ scrypt-hashcash --verify 0:030626:adam@cypherspace.org:213dc824e7eed707 4Verified: 0120e170dead0aab
Test a bad nonce:
$ scrypt-hashcash --verify 0:030626:adam@cypherspace.org:213dc824e7eed709 4Verification failed! Hash does not have sufficient zero bits
Measure the hashes per second of the current device:
$ scrypt-hashcash --measure79.18 hashes per second