-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (40 loc) · 1.39 KB
/
index.js
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
53
54
55
'use strict'
const beamhash = require('bindings')('hasherbeamhash.node');
module.exports = {
/**
* Verify BeamHashII solution.
*
* @param inputBuf {Buffer}
* @param nonceBuf {Buffer}
* @param solutionBuf {Buffer}
* @returns {boolean} True if valid, otherwise false.
*/
verify2: verify2,
/**
* Verify BeamHashIII solution.
*
* @param inputBuf {Buffer}
* @param nonceBuf {Buffer}
* @param solutionBuf {Buffer}
* @returns {boolean} True if valid, otherwise false.
*/
verify3: verify3
};
function verify2(inputBuf, nonceBuf, solutionBuf) {
_expectBuffer(inputBuf, 'inputBuf', 32);
_expectBuffer(nonceBuf, 'nonceBuf', 8);
_expectBuffer(solutionBuf, 'solutionBuf', 104);
return beamhash.verify2(inputBuf, nonceBuf, solutionBuf);
}
function verify3(inputBuf, nonceBuf, solutionBuf) {
_expectBuffer(inputBuf, 'inputBuf', 32);
_expectBuffer(nonceBuf, 'nonceBuf', 8);
_expectBuffer(solutionBuf, 'solutionBuf');
return beamhash.verify3(inputBuf, nonceBuf, solutionBuf);
}
function _expectBuffer(buffer, name, size) {
if (!Buffer.isBuffer(buffer))
throw new Error(`"${name}" is expected to be a Buffer. Got ${(typeof buffer)} instead.`);
if (size && buffer.length !== size)
throw new Error(`"${name}" is expected to be exactly ${size} bytes. Got ${buffer.length} instead.`);
}