-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (120 loc) · 3.87 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
var native = require('../native');
/** @namespace rustysecrets */
var rustysecrets = {
/**
* Provides an API to perform Shamir's secret sharing, with optional signatures
* @namespace
*/
sss: {
/**
* Performs k-out-of-n Shamir's secret sharing.
*
* @param k {Number} Minimum number of shares to be provided to recover the secret (1 <= k <= 255).
* @param n {Number} Number of shares to emit (2 <= n <= 255).
* @param secret {Buffer} The secret to split.
* @param signShares {Boolean} Sign the shares using Merkle signing.
*
* @returns {String[]} An array of shares
* @throws Will throw an error if the parameters are invalid.
*/
splitSecret: function(k, n, secret, signShares, cb) {
if (!(secret instanceof Buffer)) {
secret = Buffer.from(secret);
}
try {
native.sss_splitSecret(
k,
n,
secret,
signShares || false,
cb
);
} catch (e) {
cb(e, null);
}
},
/**
* Recovers the secret from a k-out-of-n Shamir's secret sharing scheme.
*
* At least `k` distinct shares need to be provided to recover the secret.
*
* @param shares {String[]} The shares to recover the secret from.
* @param verifySignatures {Boolean} Verify the signatures. Must be set to `true` if they are signed, `false` otherwise
*
* @returns {String} The recovered secret
* @throws Will throw an error if there are not enough shares.
* @throws Will throw an error if the shares are invalid.
* @throws Will throw an error if the shares data is not well-formed.
* @throws Will throw an error if `verifySignatures` is not set to the proper value.
*/
recoverSecret: function(shares, verifySignatures, cb) {
try {
native.sss_recoverSecret(
shares,
verifySignatures || false,
cb
);
} catch (e) {
cb(e, null);
}
}
},
/**
* Provides an API to perform Shamir's secret sharing, with MIME types
* @namespace
*/
wrapped: {
/**
* Performs k-out-of-n Shamir's secret sharing.
*
* @param k {Number} Minimum number of shares to be provided to recover the secret (1 <= k <= 255).
* @param n {Number} Number of shares to emit (2 <= n <= 255).
* @param secret {Buffer} The secret to split.
* @param mimeType {Buffer} The MIME type of the secret (or null).
* @param signShares {Boolean} Sign the shares using Merkle signing.
* @param cb {Function} The callback to call with the result.
*/
splitSecret: function(k, n, secret, mimeType, signShares, cb) {
if (!(secret instanceof Buffer)) {
secret = Buffer.from(secret);
}
try {
native.wrapped_splitSecret(
k,
n,
secret,
mimeType || null,
signShares || false,
cb
);
} catch (e) {
cb(e, null);
}
},
/**
* Recovers the secret from a k-out-of-n Shamir's secret sharing scheme.
*
* At least `k` distinct shares need to be provided to recover the secret.
*
* @param shares {String[]} The shares to recover the secret from.
* @param verifySignatures {Boolean} Verify the signatures. Must be set to `true` if they are signed, `false` otherwise
* @param cb {Function} The callback to call with the result.
*/
recoverSecret: function(shares, verifySignatures, cb) {
try {
native.wrapped_recoverSecret(
shares,
verifySignatures || false,
cb
);
} catch (e) {
cb(e, null);
}
}
}
};
// Only export wrapped module and legacy API for now
module.exports = {
sss: rustysecrets.sss,
wrapped: rustysecrets.wrapped
};