83 lines
2.8 KiB
Handlebars
83 lines
2.8 KiB
Handlebars
<html>
|
|
<head>
|
|
<title>Proof Of Work API</title>
|
|
</head>
|
|
<body>
|
|
<h1>PoW API</h1>
|
|
<p>
|
|
This is a simple API that allows you to generate a proof of work
|
|
challenges and verify the proof of work.
|
|
</p>
|
|
<p>
|
|
The API has two endpoints:
|
|
<ul>
|
|
<li>GET
|
|
<a href='/pow/challenge'>/pow/challenge</a>
|
|
- This endpoint generates a new proof of work challenge.</li>
|
|
<li>POST
|
|
<a href='/pow/challenge'>/pow/challenge</a>
|
|
- This endpoint verifies a proof of work challenge.</li>
|
|
</ul>
|
|
</p>
|
|
<p>
|
|
The proof of work challenge is to find a proof which, when concatenated
|
|
and hashed (via SHA 512) with a challenge string that starts with a
|
|
certain number of zeros. The number of zeros is determined by the
|
|
difficulty level. The difficulty level is a number between 1 and 10. The
|
|
higher the difficulty level, the more zeros the string must start with.
|
|
</p>
|
|
<p>
|
|
The current difficulty is
|
|
{{difficulty}}.
|
|
</p>
|
|
<p>
|
|
The example code for performing a proof of work challenge is:
|
|
<pre>
|
|
const crypto = require('crypto');
|
|
const axios = require('axios');
|
|
|
|
async function getChallenge() {
|
|
const response = await axios.get('https://api.us.dev/pow/challenge');
|
|
return response.data;
|
|
}
|
|
|
|
async function submitSolution(challenge, proof) {
|
|
const response = await axios.post('https://api.us.dev/pow/challenge', {
|
|
challenge,
|
|
proof
|
|
});
|
|
return response.data;
|
|
}
|
|
|
|
function hashProof(challenge, proof) {
|
|
return crypto.createHash('sha512').update(challenge + proof).digest('hex');
|
|
}
|
|
|
|
function verifyChallenge(hash: string, difficulty: number) {
|
|
return hash.startsWith('0'.repeat(difficulty));
|
|
}
|
|
|
|
async function proofOfWork() {
|
|
const challenge = await getChallenge();
|
|
let proof = 0;
|
|
let hash = '';
|
|
do {
|
|
proof++;
|
|
hash = hashProof(challenge, proof);
|
|
} while (!verifyChallenge(hash, 5));
|
|
return await submitSolution(challenge, proof);
|
|
}
|
|
|
|
proofOfWork().then(console.log).catch(console.error);
|
|
</pre>
|
|
</p>
|
|
<p>
|
|
If you're a developer you should use the GET /pow/challenge endpoint to
|
|
get the challenge, then pass the challenge to your client, then have the
|
|
client find a solution to the challenge and then submit their proof as
|
|
part of their later action. Your service should then verify the solution
|
|
using the POST /pow/challenge endpoint (or your own local implementation)
|
|
to verify the solution.
|
|
</p>
|
|
</body>
|
|
</html> |