-
Notifications
You must be signed in to change notification settings - Fork 5
/
humanizer.js
118 lines (98 loc) · 3.24 KB
/
humanizer.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// humanizer.js - by Mathieu Gorichon - released under MIT License
var humanizer = (function () {
var module = {};
// private
function padLeft(n, width) {
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
}
function dec2bin(num) {
var bin = '';
if(isNaN(num)){
throw new Error('Invalid dec character.')
}
return num.toString(2);
}
function bin2dec(str) {
var dec = '';
dec = parseInt(str, 2);
if(isNaN(dec)){
throw new Error('Invalid binary character.')
}
return dec;
}
// From secrets.js
function hex2bin(str){
var bin = '', num;
for(var i=str.length - 1; i>=0; i--){
num = parseInt(str[i], 16)
if(isNaN(num)){
throw new Error('Invalid hex character.')
}
bin = padLeft(num.toString(2), 4) + bin;
}
return bin;
}
function bin2hex(str){
var hex = '', num;
str = padLeft(str, 4);
for(var i=str.length; i>=4; i-=4){
num = parseInt(str.slice(i-4, i), 2);
if(isNaN(num)){
throw new Error('Invalid binary character.')
}
hex = num.toString(16) + hex;
}
return hex;
}
// End of secrets.js
function getPrefix(sharesCount, threshold) {
return bin2hex(padLeft(dec2bin(sharesCount),6)+padLeft(dec2bin(threshold),6));
}
module.split = function (seed, sharesCount, threshold) {
// Share the seed
var shares = secrets.share(seed, sharesCount, threshold)
// Add prefix at the beginning of each share
for (var i = 0; i < shares.length; i += 1) {
shares[i] = getPrefix(sharesCount, threshold) + shares[i]
}
// Make Human readable shares
var sharesHuman = [];
for (var i = 0; i < shares.length; i += 1) {
sharesHuman[i] = mn_encode(shares[i]);
console.log('Human share ' + i + ': ' + sharesHuman[i]);
}
return sharesHuman;
};
module.reconstruct = function (shares) {
// Convert back human readable shares
var sharesHex = [];
for (var i = 0; i < shares.length; i += 1) {
sharesHex[i] = mn_decode(shares[i]);
}
console.log('Shares: ' + sharesHex);
// Remove prefix
for (var i = 0; i < sharesHex.length; i += 1) {
sharesHex[i] = sharesHex[i].substring(3, sharesHex[i].length);
}
// Combine shares
var newSeedHex = secrets.combine(sharesHex);
console.log('Hex seed: ' + newSeedHex);
// Convert back to human readable seed
return mn_encode(newSeedHex);
};
module.getSharesCount = function(share) {
var hex = mn_decode(share);
var prefix = hex.substring(0,3);
var bin = hex2bin(prefix).substring(0,6)
return bin2dec(bin);
}
module.getSharesThreshold = function(share) {
var hex = mn_decode(share);
var prefix = hex.substring(0,3);
var bin = hex2bin(prefix).substring(6,12);
return bin2dec(bin);
}
return module;
})();
//Tests