-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
52 lines (46 loc) · 1.04 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const addon = require('bindings')('equihash');
/**
* Solves an equihash Proof-of-Work
* @param {Buffer} input
* @param {number} [n=90]
* @param {number} [k=5]
* @returns {Promise<{ proof, nonce, n, k }>}
*/
function solve(input, n = 90, k = 5) {
return new Promise((resolve, reject) => {
addon.solve({
n,
k,
seed: input
}, (err, solution, nonce, n, k) => {
if (err) {
return reject(err);
}
resolve({ proof: solution, nonce, n, k });
});
});
};
/**
* Verifies a equihash Proof
* @param {Buffer} input
* @param {Buffer} proof
* @param {number} [nonce=1]
* @param {number} [n=90]
* @param {number} [k=5]
* @returns {Promise<Boolean>}
*/
function verify(input, proof, nonce = 1, n = 90, k = 5) {
if (proof.length < 128) {
return Promise.reject(new Error('Invalid proof length'));
}
const valid = addon.verify({
n,
k,
nonce,
seed: input,
value: proof
});
return Promise.resolve(valid);
};
module.exports = { solve, verify };