-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashChain.js
66 lines (52 loc) · 1.26 KB
/
hashChain.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
56
57
58
59
60
61
62
63
64
65
66
const assert = require("assert");
const { v4: uuid } = require("uuid");
const { sha256 } = require("./utils");
// generate a provable hash chain count to -1
function generateHashChain(count, seed) {
assert(seed, "requires seed");
assert(seed, "requires count");
var result = Array(count);
// generate chain in revese order
for (var i = count - 1; i >= 0; i--) {
seed = sha256(seed);
result[i] = seed;
}
return result;
}
// manages generating and itteratng through a provable hashchain
// the last hash of the previous chain will be used to generate the next.
function HashChain({ seed = uuid(), count = 1000000, index = 0 }) {
assert(count >= 1, "requires count");
assert(seed, "requires seed");
const chain = generateHashChain(count, seed);
function state() {
return { count, seed, index };
}
function peek() {
return chain[index + 1];
}
function get() {
return chain[index];
}
function next() {
const hash = peek();
assert(hash, "chain has ended");
// increment index and return hash
return {
hash,
count,
index: ++index,
};
}
function last() {
return chain[index - 1];
}
return {
state,
peek,
get,
next,
last,
};
}
module.exports = HashChain;