@bcts/sskr 1.0.0-alpha.9 → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -2
- package/dist/index.cjs +131 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +46 -25
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +46 -25
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +163 -118
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +118 -72
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -17
- package/src/encoding.ts +19 -4
- package/src/error.ts +15 -1
- package/src/index.ts +6 -3
- package/src/secret.ts +18 -0
- package/src/share.ts +6 -0
- package/src/spec.ts +39 -5
package/dist/index.mjs
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
import { MAX_SECRET_LEN as MAX_SECRET_LEN$1, MAX_SHARE_COUNT as MAX_SHARE_COUNT$1, MIN_SECRET_LEN as MIN_SECRET_LEN$1, ShamirError, recoverSecret, splitSecret } from "@bcts/shamir";
|
|
2
2
|
import { SecureRandomNumberGenerator } from "@bcts/rand";
|
|
3
|
-
|
|
4
3
|
//#region src/error.ts
|
|
5
4
|
/**
|
|
6
5
|
* Error types for SSKR operations.
|
|
7
6
|
*/
|
|
8
|
-
let SSKRErrorType = /* @__PURE__ */ function(SSKRErrorType
|
|
9
|
-
SSKRErrorType
|
|
10
|
-
SSKRErrorType
|
|
11
|
-
SSKRErrorType
|
|
12
|
-
SSKRErrorType
|
|
13
|
-
SSKRErrorType
|
|
14
|
-
SSKRErrorType
|
|
15
|
-
SSKRErrorType
|
|
16
|
-
SSKRErrorType
|
|
17
|
-
SSKRErrorType
|
|
18
|
-
SSKRErrorType
|
|
19
|
-
SSKRErrorType
|
|
20
|
-
SSKRErrorType
|
|
21
|
-
SSKRErrorType
|
|
22
|
-
SSKRErrorType
|
|
23
|
-
SSKRErrorType
|
|
24
|
-
return SSKRErrorType
|
|
7
|
+
let SSKRErrorType = /* @__PURE__ */ function(SSKRErrorType) {
|
|
8
|
+
SSKRErrorType["DuplicateMemberIndex"] = "DuplicateMemberIndex";
|
|
9
|
+
SSKRErrorType["GroupSpecInvalid"] = "GroupSpecInvalid";
|
|
10
|
+
SSKRErrorType["GroupCountInvalid"] = "GroupCountInvalid";
|
|
11
|
+
SSKRErrorType["GroupThresholdInvalid"] = "GroupThresholdInvalid";
|
|
12
|
+
SSKRErrorType["MemberCountInvalid"] = "MemberCountInvalid";
|
|
13
|
+
SSKRErrorType["MemberThresholdInvalid"] = "MemberThresholdInvalid";
|
|
14
|
+
SSKRErrorType["NotEnoughGroups"] = "NotEnoughGroups";
|
|
15
|
+
SSKRErrorType["SecretLengthNotEven"] = "SecretLengthNotEven";
|
|
16
|
+
SSKRErrorType["SecretTooLong"] = "SecretTooLong";
|
|
17
|
+
SSKRErrorType["SecretTooShort"] = "SecretTooShort";
|
|
18
|
+
SSKRErrorType["ShareLengthInvalid"] = "ShareLengthInvalid";
|
|
19
|
+
SSKRErrorType["ShareReservedBitsInvalid"] = "ShareReservedBitsInvalid";
|
|
20
|
+
SSKRErrorType["SharesEmpty"] = "SharesEmpty";
|
|
21
|
+
SSKRErrorType["ShareSetInvalid"] = "ShareSetInvalid";
|
|
22
|
+
SSKRErrorType["ShamirError"] = "ShamirError";
|
|
23
|
+
return SSKRErrorType;
|
|
25
24
|
}({});
|
|
26
25
|
/**
|
|
27
26
|
* Error class for SSKR operations.
|
|
@@ -37,31 +36,35 @@ var SSKRError = class SSKRError extends Error {
|
|
|
37
36
|
}
|
|
38
37
|
static defaultMessage(type, shamirError) {
|
|
39
38
|
switch (type) {
|
|
40
|
-
case
|
|
41
|
-
case
|
|
42
|
-
case
|
|
43
|
-
case
|
|
44
|
-
case
|
|
45
|
-
case
|
|
46
|
-
case
|
|
47
|
-
case
|
|
48
|
-
case
|
|
49
|
-
case
|
|
50
|
-
case
|
|
51
|
-
case
|
|
52
|
-
case
|
|
53
|
-
case
|
|
54
|
-
case
|
|
39
|
+
case "DuplicateMemberIndex": return "When combining shares, the provided shares contained a duplicate member index";
|
|
40
|
+
case "GroupSpecInvalid": return "Invalid group specification.";
|
|
41
|
+
case "GroupCountInvalid": return "When creating a split spec, the group count is invalid";
|
|
42
|
+
case "GroupThresholdInvalid": return "SSKR group threshold is invalid";
|
|
43
|
+
case "MemberCountInvalid": return "SSKR member count is invalid";
|
|
44
|
+
case "MemberThresholdInvalid": return "SSKR member threshold is invalid";
|
|
45
|
+
case "NotEnoughGroups": return "SSKR shares did not contain enough groups";
|
|
46
|
+
case "SecretLengthNotEven": return "SSKR secret is not of even length";
|
|
47
|
+
case "SecretTooLong": return "SSKR secret is too long";
|
|
48
|
+
case "SecretTooShort": return "SSKR secret is too short";
|
|
49
|
+
case "ShareLengthInvalid": return "SSKR shares did not contain enough serialized bytes";
|
|
50
|
+
case "ShareReservedBitsInvalid": return "SSKR shares contained invalid reserved bits";
|
|
51
|
+
case "SharesEmpty": return "SSKR shares were empty";
|
|
52
|
+
case "ShareSetInvalid": return "SSKR shares were invalid";
|
|
53
|
+
case "ShamirError": return shamirError != null ? `SSKR Shamir error: ${shamirError.message}` : "SSKR Shamir error";
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
static fromShamirError(error) {
|
|
58
|
-
return new SSKRError(
|
|
57
|
+
return new SSKRError("ShamirError", void 0, error);
|
|
59
58
|
}
|
|
60
59
|
};
|
|
61
|
-
|
|
62
60
|
//#endregion
|
|
63
61
|
//#region src/secret.ts
|
|
64
62
|
/**
|
|
63
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
64
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
65
68
|
* A secret to be split into shares.
|
|
66
69
|
*/
|
|
67
70
|
var Secret = class Secret {
|
|
@@ -80,9 +83,9 @@ var Secret = class Secret {
|
|
|
80
83
|
static new(data) {
|
|
81
84
|
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
82
85
|
const len = bytes.length;
|
|
83
|
-
if (len < MIN_SECRET_LEN) throw new SSKRError(
|
|
84
|
-
if (len > MAX_SECRET_LEN) throw new SSKRError(
|
|
85
|
-
if ((len & 1) !== 0) throw new SSKRError(
|
|
86
|
+
if (len < MIN_SECRET_LEN) throw new SSKRError("SecretTooShort");
|
|
87
|
+
if (len > MAX_SECRET_LEN) throw new SSKRError("SecretTooLong");
|
|
88
|
+
if ((len & 1) !== 0) throw new SSKRError("SecretLengthNotEven");
|
|
86
89
|
return new Secret(new Uint8Array(bytes));
|
|
87
90
|
}
|
|
88
91
|
/**
|
|
@@ -99,12 +102,24 @@ var Secret = class Secret {
|
|
|
99
102
|
}
|
|
100
103
|
/**
|
|
101
104
|
* Returns a reference to the secret data.
|
|
105
|
+
*
|
|
106
|
+
* Mirrors Rust's `Secret::data(&self) -> &[u8]`
|
|
107
|
+
* (`bc-sskr-rust/src/secret.rs:43`).
|
|
102
108
|
*/
|
|
103
109
|
getData() {
|
|
104
110
|
return this.data;
|
|
105
111
|
}
|
|
106
112
|
/**
|
|
107
113
|
* Returns the secret data as a Uint8Array.
|
|
114
|
+
*
|
|
115
|
+
* Mirrors Rust's `impl AsRef<[u8]> for Secret`
|
|
116
|
+
* (`bc-sskr-rust/src/secret.rs:46-49`). In Rust, `as_ref()` is
|
|
117
|
+
* provided via the `AsRef<[u8]>` trait, which lets the `Secret` flow
|
|
118
|
+
* naturally through any API expecting `impl AsRef<[u8]>`. TypeScript
|
|
119
|
+
* has no equivalent of that trait, so we expose the same backing
|
|
120
|
+
* buffer through both {@link getData} (the field accessor) and
|
|
121
|
+
* `asRef` (the trait-style accessor) for ergonomic parity. Callers
|
|
122
|
+
* may pick whichever name reads better at the call site.
|
|
108
123
|
*/
|
|
109
124
|
asRef() {
|
|
110
125
|
return this.data;
|
|
@@ -124,10 +139,30 @@ var Secret = class Secret {
|
|
|
124
139
|
return new Secret(new Uint8Array(this.data));
|
|
125
140
|
}
|
|
126
141
|
};
|
|
127
|
-
|
|
128
142
|
//#endregion
|
|
129
143
|
//#region src/spec.ts
|
|
130
144
|
/**
|
|
145
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
146
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
147
|
+
*
|
|
148
|
+
*/
|
|
149
|
+
/**
|
|
150
|
+
* Strictly parses a non-negative integer string the way Rust's
|
|
151
|
+
* `<usize as FromStr>::from_str` does (`bc-sskr-rust/src/spec.rs:102, 108`).
|
|
152
|
+
*
|
|
153
|
+
* Accepts an optional leading `+` followed by one or more decimal digits.
|
|
154
|
+
* Rejects whitespace, decimals (`"2.5"`), trailing characters (`"2x"`),
|
|
155
|
+
* the `-` sign, and the empty string. Returns `undefined` on rejection so
|
|
156
|
+
* callers surface the appropriate `GroupSpecInvalid` error in the same
|
|
157
|
+
* spot Rust's `?` would.
|
|
158
|
+
*/
|
|
159
|
+
const STRICT_USIZE_RE = /^\+?\d+$/;
|
|
160
|
+
function parseStrictUsize(s) {
|
|
161
|
+
if (!STRICT_USIZE_RE.test(s)) return void 0;
|
|
162
|
+
const n = Number(s);
|
|
163
|
+
return Number.isSafeInteger(n) ? n : void 0;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
131
166
|
* A specification for a group of shares within an SSKR split.
|
|
132
167
|
*/
|
|
133
168
|
var GroupSpec = class GroupSpec {
|
|
@@ -149,9 +184,9 @@ var GroupSpec = class GroupSpec {
|
|
|
149
184
|
* greater than the member count.
|
|
150
185
|
*/
|
|
151
186
|
static new(memberThreshold, memberCount) {
|
|
152
|
-
if (memberCount === 0) throw new SSKRError(
|
|
153
|
-
if (memberCount > MAX_SHARE_COUNT$1) throw new SSKRError(
|
|
154
|
-
if (memberThreshold > memberCount) throw new SSKRError(
|
|
187
|
+
if (memberCount === 0) throw new SSKRError("MemberCountInvalid");
|
|
188
|
+
if (memberCount > MAX_SHARE_COUNT$1) throw new SSKRError("MemberCountInvalid");
|
|
189
|
+
if (memberThreshold > memberCount) throw new SSKRError("MemberThresholdInvalid");
|
|
155
190
|
return new GroupSpec(memberThreshold, memberCount);
|
|
156
191
|
}
|
|
157
192
|
/**
|
|
@@ -168,16 +203,24 @@ var GroupSpec = class GroupSpec {
|
|
|
168
203
|
}
|
|
169
204
|
/**
|
|
170
205
|
* Parses a group specification from a string.
|
|
171
|
-
* Format: "M-of-N" where M is the threshold and N is the count.
|
|
206
|
+
* Format: `"M-of-N"` where `M` is the threshold and `N` is the count.
|
|
207
|
+
*
|
|
208
|
+
* Mirrors Rust's `GroupSpec::parse` (`bc-sskr-rust/src/spec.rs:97-112`),
|
|
209
|
+
* which calls `parts[0].parse::<usize>()`. Rust's `usize::FromStr` is
|
|
210
|
+
* strict: it rejects whitespace, decimals, trailing characters, and the
|
|
211
|
+
* `-` sign, accepting only an optional `+` prefix followed by digits.
|
|
212
|
+
* We mirror that with the regex `^\+?\d+$` so strings like `"2.5"`,
|
|
213
|
+
* `"2x"`, `" 2"`, `"-2"`, and `""` all surface
|
|
214
|
+
* {@link SSKRErrorType.GroupSpecInvalid} on both sides.
|
|
172
215
|
*/
|
|
173
216
|
static parse(s) {
|
|
174
217
|
const parts = s.split("-");
|
|
175
|
-
if (parts.length !== 3) throw new SSKRError(
|
|
176
|
-
const memberThreshold =
|
|
177
|
-
if (
|
|
178
|
-
if (parts[1] !== "of") throw new SSKRError(
|
|
179
|
-
const memberCount =
|
|
180
|
-
if (
|
|
218
|
+
if (parts.length !== 3) throw new SSKRError("GroupSpecInvalid");
|
|
219
|
+
const memberThreshold = parseStrictUsize(parts[0]);
|
|
220
|
+
if (memberThreshold === void 0) throw new SSKRError("GroupSpecInvalid");
|
|
221
|
+
if (parts[1] !== "of") throw new SSKRError("GroupSpecInvalid");
|
|
222
|
+
const memberCount = parseStrictUsize(parts[2]);
|
|
223
|
+
if (memberCount === void 0) throw new SSKRError("GroupSpecInvalid");
|
|
181
224
|
return GroupSpec.new(memberThreshold, memberCount);
|
|
182
225
|
}
|
|
183
226
|
/**
|
|
@@ -216,9 +259,9 @@ var Spec = class Spec {
|
|
|
216
259
|
* greater than the maximum share count.
|
|
217
260
|
*/
|
|
218
261
|
static new(groupThreshold, groups) {
|
|
219
|
-
if (groupThreshold === 0) throw new SSKRError(
|
|
220
|
-
if (groupThreshold > groups.length) throw new SSKRError(
|
|
221
|
-
if (groups.length > MAX_SHARE_COUNT$1) throw new SSKRError(
|
|
262
|
+
if (groupThreshold === 0) throw new SSKRError("GroupThresholdInvalid");
|
|
263
|
+
if (groupThreshold > groups.length) throw new SSKRError("GroupThresholdInvalid");
|
|
264
|
+
if (groups.length > MAX_SHARE_COUNT$1) throw new SSKRError("GroupCountInvalid");
|
|
222
265
|
return new Spec(groupThreshold, groups);
|
|
223
266
|
}
|
|
224
267
|
/**
|
|
@@ -246,7 +289,6 @@ var Spec = class Spec {
|
|
|
246
289
|
return this._groups.reduce((sum, g) => sum + g.memberCount(), 0);
|
|
247
290
|
}
|
|
248
291
|
};
|
|
249
|
-
|
|
250
292
|
//#endregion
|
|
251
293
|
//#region src/share.ts
|
|
252
294
|
/**
|
|
@@ -291,7 +333,6 @@ var SSKRShare = class {
|
|
|
291
333
|
return this._value;
|
|
292
334
|
}
|
|
293
335
|
};
|
|
294
|
-
|
|
295
336
|
//#endregion
|
|
296
337
|
//#region src/encoding.ts
|
|
297
338
|
/**
|
|
@@ -337,7 +378,7 @@ function sskrCombine(shares) {
|
|
|
337
378
|
}
|
|
338
379
|
function serializeShare(share) {
|
|
339
380
|
const valueData = share.value().getData();
|
|
340
|
-
const result = new Uint8Array(valueData.length +
|
|
381
|
+
const result = new Uint8Array(valueData.length + 5);
|
|
341
382
|
const id = share.identifier();
|
|
342
383
|
const gt = share.groupThreshold() - 1 & 15;
|
|
343
384
|
const gc = share.groupCount() - 1 & 15;
|
|
@@ -351,19 +392,19 @@ function serializeShare(share) {
|
|
|
351
392
|
result[2] = gt << 4 | gc;
|
|
352
393
|
result[3] = gi << 4 | mt;
|
|
353
394
|
result[4] = mi;
|
|
354
|
-
result.set(valueData,
|
|
395
|
+
result.set(valueData, 5);
|
|
355
396
|
return result;
|
|
356
397
|
}
|
|
357
398
|
function deserializeShare(source) {
|
|
358
|
-
if (source.length <
|
|
399
|
+
if (source.length < 5) throw new SSKRError("ShareLengthInvalid");
|
|
359
400
|
const groupThreshold = (source[2] >> 4) + 1;
|
|
360
401
|
const groupCount = (source[2] & 15) + 1;
|
|
361
|
-
if (groupThreshold > groupCount) throw new SSKRError(
|
|
402
|
+
if (groupThreshold > groupCount) throw new SSKRError("GroupThresholdInvalid");
|
|
362
403
|
const identifier = source[0] << 8 | source[1];
|
|
363
404
|
const groupIndex = source[3] >> 4;
|
|
364
405
|
const memberThreshold = (source[3] & 15) + 1;
|
|
365
|
-
if (source[4] >> 4 !== 0) throw new SSKRError(
|
|
366
|
-
return new SSKRShare(identifier, groupIndex, groupThreshold, groupCount, source[4] & 15, memberThreshold, Secret.new(source.subarray(
|
|
406
|
+
if (source[4] >> 4 !== 0) throw new SSKRError("ShareReservedBitsInvalid");
|
|
407
|
+
return new SSKRShare(identifier, groupIndex, groupThreshold, groupCount, source[4] & 15, memberThreshold, Secret.new(source.subarray(5)));
|
|
367
408
|
}
|
|
368
409
|
function generateShares(spec, masterSecret, randomGenerator) {
|
|
369
410
|
const identifierBytes = new Uint8Array(2);
|
|
@@ -399,7 +440,7 @@ function combineShares(shares) {
|
|
|
399
440
|
let identifier = 0;
|
|
400
441
|
let groupThreshold = 0;
|
|
401
442
|
let groupCount = 0;
|
|
402
|
-
if (shares.length === 0) throw new SSKRError(
|
|
443
|
+
if (shares.length === 0) throw new SSKRError("SharesEmpty");
|
|
403
444
|
let nextGroup = 0;
|
|
404
445
|
const groups = [];
|
|
405
446
|
let secretLen = 0;
|
|
@@ -410,12 +451,12 @@ function combineShares(shares) {
|
|
|
410
451
|
groupCount = share.groupCount();
|
|
411
452
|
groupThreshold = share.groupThreshold();
|
|
412
453
|
secretLen = share.value().len();
|
|
413
|
-
} else if (share.identifier() !== identifier || share.groupThreshold() !== groupThreshold || share.groupCount() !== groupCount || share.value().len() !== secretLen) throw new SSKRError(
|
|
454
|
+
} else if (share.identifier() !== identifier || share.groupThreshold() !== groupThreshold || share.groupCount() !== groupCount || share.value().len() !== secretLen) throw new SSKRError("ShareSetInvalid");
|
|
414
455
|
let groupFound = false;
|
|
415
456
|
for (const group of groups) if (share.groupIndex() === group.groupIndex) {
|
|
416
457
|
groupFound = true;
|
|
417
|
-
if (share.memberThreshold() !== group.memberThreshold) throw new SSKRError(
|
|
418
|
-
for (const memberIndex of group.memberIndexes) if (share.memberIndex() === memberIndex) throw new SSKRError(
|
|
458
|
+
if (share.memberThreshold() !== group.memberThreshold) throw new SSKRError("MemberThresholdInvalid");
|
|
459
|
+
for (const memberIndex of group.memberIndexes) if (share.memberIndex() === memberIndex) throw new SSKRError("DuplicateMemberIndex");
|
|
419
460
|
if (group.memberIndexes.length < group.memberThreshold) {
|
|
420
461
|
group.memberIndexes.push(share.memberIndex());
|
|
421
462
|
group.memberShares.push(share.value().clone());
|
|
@@ -432,7 +473,7 @@ function combineShares(shares) {
|
|
|
432
473
|
nextGroup++;
|
|
433
474
|
}
|
|
434
475
|
}
|
|
435
|
-
if (nextGroup < groupThreshold) throw new SSKRError(
|
|
476
|
+
if (nextGroup < groupThreshold) throw new SSKRError("NotEnoughGroups");
|
|
436
477
|
const masterIndexes = [];
|
|
437
478
|
const masterShares = [];
|
|
438
479
|
for (const group of groups) {
|
|
@@ -442,12 +483,13 @@ function combineShares(shares) {
|
|
|
442
483
|
const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);
|
|
443
484
|
masterIndexes.push(group.groupIndex);
|
|
444
485
|
masterShares.push(groupSecret);
|
|
445
|
-
} catch {
|
|
446
|
-
continue;
|
|
486
|
+
} catch (e) {
|
|
487
|
+
if (e instanceof ShamirError) continue;
|
|
488
|
+
throw e;
|
|
447
489
|
}
|
|
448
490
|
if (masterIndexes.length === groupThreshold) break;
|
|
449
491
|
}
|
|
450
|
-
if (masterIndexes.length < groupThreshold) throw new SSKRError(
|
|
492
|
+
if (masterIndexes.length < groupThreshold) throw new SSKRError("NotEnoughGroups");
|
|
451
493
|
let masterSecretData;
|
|
452
494
|
try {
|
|
453
495
|
masterSecretData = recoverSecret(masterIndexes, masterShares);
|
|
@@ -457,10 +499,14 @@ function combineShares(shares) {
|
|
|
457
499
|
}
|
|
458
500
|
return Secret.new(masterSecretData);
|
|
459
501
|
}
|
|
460
|
-
|
|
461
502
|
//#endregion
|
|
462
503
|
//#region src/index.ts
|
|
463
504
|
/**
|
|
505
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
506
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
507
|
+
*
|
|
508
|
+
*/
|
|
509
|
+
/**
|
|
464
510
|
* The minimum length of a secret.
|
|
465
511
|
*/
|
|
466
512
|
const MIN_SECRET_LEN = MIN_SECRET_LEN$1;
|
|
@@ -483,8 +529,8 @@ const METADATA_SIZE_BYTES = 5;
|
|
|
483
529
|
/**
|
|
484
530
|
* The minimum number of bytes required to encode a share.
|
|
485
531
|
*/
|
|
486
|
-
const MIN_SERIALIZE_SIZE_BYTES =
|
|
487
|
-
|
|
532
|
+
const MIN_SERIALIZE_SIZE_BYTES = 5 + MIN_SECRET_LEN;
|
|
488
533
|
//#endregion
|
|
489
|
-
export { GroupSpec, MAX_GROUPS_COUNT, MAX_SECRET_LEN, MAX_SHARE_COUNT, METADATA_SIZE_BYTES, MIN_SECRET_LEN, MIN_SERIALIZE_SIZE_BYTES, SSKRError, SSKRErrorType,
|
|
534
|
+
export { GroupSpec, MAX_GROUPS_COUNT, MAX_SECRET_LEN, MAX_SHARE_COUNT, METADATA_SIZE_BYTES, MIN_SECRET_LEN, MIN_SERIALIZE_SIZE_BYTES, SSKRError, SSKRErrorType, Secret, Spec, sskrCombine, sskrGenerate, sskrGenerateUsing };
|
|
535
|
+
|
|
490
536
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["MAX_SHARE_COUNT","sskrShares: SSKRShare[]","groupsShares: SSKRShare[][]","groupSecrets: Uint8Array[]","memberSecrets: Uint8Array[]","memberSSKRShares: SSKRShare[]","groups: Group[]","g: Group","masterIndexes: number[]","masterShares: Uint8Array[]","masterSecretData: Uint8Array","SHAMIR_MIN_SECRET_LEN","SHAMIR_MAX_SECRET_LEN","SHAMIR_MAX_SHARE_COUNT"],"sources":["../src/error.ts","../src/secret.ts","../src/spec.ts","../src/share.ts","../src/encoding.ts","../src/index.ts"],"sourcesContent":["// Ported from bc-sskr-rust/src/error.rs\n\nimport type { ShamirError } from \"@bcts/shamir\";\n\n/**\n * Error types for SSKR operations.\n */\nexport enum SSKRErrorType {\n DuplicateMemberIndex = \"DuplicateMemberIndex\",\n GroupSpecInvalid = \"GroupSpecInvalid\",\n GroupCountInvalid = \"GroupCountInvalid\",\n GroupThresholdInvalid = \"GroupThresholdInvalid\",\n MemberCountInvalid = \"MemberCountInvalid\",\n MemberThresholdInvalid = \"MemberThresholdInvalid\",\n NotEnoughGroups = \"NotEnoughGroups\",\n SecretLengthNotEven = \"SecretLengthNotEven\",\n SecretTooLong = \"SecretTooLong\",\n SecretTooShort = \"SecretTooShort\",\n ShareLengthInvalid = \"ShareLengthInvalid\",\n ShareReservedBitsInvalid = \"ShareReservedBitsInvalid\",\n SharesEmpty = \"SharesEmpty\",\n ShareSetInvalid = \"ShareSetInvalid\",\n ShamirError = \"ShamirError\",\n}\n\n/**\n * Error class for SSKR operations.\n */\nexport class SSKRError extends Error {\n readonly type: SSKRErrorType;\n readonly shamirError?: ShamirError | undefined;\n\n constructor(type: SSKRErrorType, message?: string, shamirError?: ShamirError) {\n super(message ?? SSKRError.defaultMessage(type, shamirError));\n this.type = type;\n this.shamirError = shamirError;\n this.name = \"SSKRError\";\n }\n\n private static defaultMessage(type: SSKRErrorType, shamirError?: ShamirError): string {\n switch (type) {\n case SSKRErrorType.DuplicateMemberIndex:\n return \"When combining shares, the provided shares contained a duplicate member index\";\n case SSKRErrorType.GroupSpecInvalid:\n return \"Invalid group specification\";\n case SSKRErrorType.GroupCountInvalid:\n return \"When creating a split spec, the group count is invalid\";\n case SSKRErrorType.GroupThresholdInvalid:\n return \"SSKR group threshold is invalid\";\n case SSKRErrorType.MemberCountInvalid:\n return \"SSKR member count is invalid\";\n case SSKRErrorType.MemberThresholdInvalid:\n return \"SSKR member threshold is invalid\";\n case SSKRErrorType.NotEnoughGroups:\n return \"SSKR shares did not contain enough groups\";\n case SSKRErrorType.SecretLengthNotEven:\n return \"SSKR secret is not of even length\";\n case SSKRErrorType.SecretTooLong:\n return \"SSKR secret is too long\";\n case SSKRErrorType.SecretTooShort:\n return \"SSKR secret is too short\";\n case SSKRErrorType.ShareLengthInvalid:\n return \"SSKR shares did not contain enough serialized bytes\";\n case SSKRErrorType.ShareReservedBitsInvalid:\n return \"SSKR shares contained invalid reserved bits\";\n case SSKRErrorType.SharesEmpty:\n return \"SSKR shares were empty\";\n case SSKRErrorType.ShareSetInvalid:\n return \"SSKR shares were invalid\";\n case SSKRErrorType.ShamirError:\n return shamirError != null\n ? `SSKR Shamir error: ${shamirError.message}`\n : \"SSKR Shamir error\";\n }\n }\n\n static fromShamirError(error: ShamirError): SSKRError {\n return new SSKRError(SSKRErrorType.ShamirError, undefined, error);\n }\n}\n\nexport type SSKRResult<T> = T;\n","// Ported from bc-sskr-rust/src/secret.rs\n\nimport { MIN_SECRET_LEN, MAX_SECRET_LEN } from \"./index.js\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * A secret to be split into shares.\n */\nexport class Secret {\n private readonly data: Uint8Array;\n\n private constructor(data: Uint8Array) {\n this.data = data;\n }\n\n /**\n * Creates a new Secret instance with the given data.\n *\n * @param data - The secret data to be split into shares.\n * @returns A new Secret instance.\n * @throws SSKRError if the length of the secret is less than\n * MIN_SECRET_LEN, greater than MAX_SECRET_LEN, or not even.\n */\n static new(data: Uint8Array | string): Secret {\n const bytes = typeof data === \"string\" ? new TextEncoder().encode(data) : data;\n const len = bytes.length;\n\n if (len < MIN_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooShort);\n }\n if (len > MAX_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooLong);\n }\n if ((len & 1) !== 0) {\n throw new SSKRError(SSKRErrorType.SecretLengthNotEven);\n }\n\n return new Secret(new Uint8Array(bytes));\n }\n\n /**\n * Returns the length of the secret.\n */\n len(): number {\n return this.data.length;\n }\n\n /**\n * Returns true if the secret is empty.\n */\n isEmpty(): boolean {\n return this.len() === 0;\n }\n\n /**\n * Returns a reference to the secret data.\n */\n getData(): Uint8Array {\n return this.data;\n }\n\n /**\n * Returns the secret data as a Uint8Array.\n */\n asRef(): Uint8Array {\n return this.data;\n }\n\n /**\n * Check equality with another Secret.\n */\n equals(other: Secret): boolean {\n if (this.data.length !== other.data.length) {\n return false;\n }\n for (let i = 0; i < this.data.length; i++) {\n if (this.data[i] !== other.data[i]) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Clone the secret.\n */\n clone(): Secret {\n return new Secret(new Uint8Array(this.data));\n }\n}\n","// Ported from bc-sskr-rust/src/spec.rs\n\nimport { MAX_SHARE_COUNT } from \"@bcts/shamir\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * A specification for a group of shares within an SSKR split.\n */\nexport class GroupSpec {\n private readonly _memberThreshold: number;\n private readonly _memberCount: number;\n\n private constructor(memberThreshold: number, memberCount: number) {\n this._memberThreshold = memberThreshold;\n this._memberCount = memberCount;\n }\n\n /**\n * Creates a new GroupSpec instance with the given member threshold and count.\n *\n * @param memberThreshold - The minimum number of member shares required to\n * reconstruct the secret within the group.\n * @param memberCount - The total number of member shares in the group.\n * @returns A new GroupSpec instance.\n * @throws SSKRError if the member count is zero, if the member count is\n * greater than the maximum share count, or if the member threshold is\n * greater than the member count.\n */\n static new(memberThreshold: number, memberCount: number): GroupSpec {\n if (memberCount === 0) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberCount > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberThreshold > memberCount) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n return new GroupSpec(memberThreshold, memberCount);\n }\n\n /**\n * Returns the member share threshold for this group.\n */\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n /**\n * Returns the number of member shares in this group.\n */\n memberCount(): number {\n return this._memberCount;\n }\n\n /**\n * Parses a group specification from a string.\n * Format: \"M-of-N\" where M is the threshold and N is the count.\n */\n static parse(s: string): GroupSpec {\n const parts = s.split(\"-\");\n if (parts.length !== 3) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberThreshold = parseInt(parts[0], 10);\n if (isNaN(memberThreshold)) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n if (parts[1] !== \"of\") {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberCount = parseInt(parts[2], 10);\n if (isNaN(memberCount)) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n return GroupSpec.new(memberThreshold, memberCount);\n }\n\n /**\n * Creates a default GroupSpec (1-of-1).\n */\n static default(): GroupSpec {\n return GroupSpec.new(1, 1);\n }\n\n /**\n * Returns a string representation of the group spec.\n */\n toString(): string {\n return `${this._memberThreshold}-of-${this._memberCount}`;\n }\n}\n\n/**\n * A specification for an SSKR split.\n */\nexport class Spec {\n private readonly _groupThreshold: number;\n private readonly _groups: GroupSpec[];\n\n private constructor(groupThreshold: number, groups: GroupSpec[]) {\n this._groupThreshold = groupThreshold;\n this._groups = groups;\n }\n\n /**\n * Creates a new Spec instance with the given group threshold and groups.\n *\n * @param groupThreshold - The minimum number of groups required to\n * reconstruct the secret.\n * @param groups - The list of GroupSpec instances that define the groups\n * and their members.\n * @returns A new Spec instance.\n * @throws SSKRError if the group threshold is zero, if the group threshold\n * is greater than the number of groups, or if the number of groups is\n * greater than the maximum share count.\n */\n static new(groupThreshold: number, groups: GroupSpec[]): Spec {\n if (groupThreshold === 0) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groupThreshold > groups.length) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groups.length > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.GroupCountInvalid);\n }\n return new Spec(groupThreshold, groups);\n }\n\n /**\n * Returns the group threshold.\n */\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n /**\n * Returns a slice of the group specifications.\n */\n groups(): GroupSpec[] {\n return this._groups;\n }\n\n /**\n * Returns the number of groups.\n */\n groupCount(): number {\n return this._groups.length;\n }\n\n /**\n * Returns the total number of shares across all groups.\n */\n shareCount(): number {\n return this._groups.reduce((sum, g) => sum + g.memberCount(), 0);\n }\n}\n","// Ported from bc-sskr-rust/src/share.rs\n\nimport type { Secret } from \"./secret.js\";\n\n/**\n * A share in the SSKR scheme.\n */\nexport class SSKRShare {\n private readonly _identifier: number;\n private readonly _groupIndex: number;\n private readonly _groupThreshold: number;\n private readonly _groupCount: number;\n private readonly _memberIndex: number;\n private readonly _memberThreshold: number;\n private readonly _value: Secret;\n\n constructor(\n identifier: number,\n groupIndex: number,\n groupThreshold: number,\n groupCount: number,\n memberIndex: number,\n memberThreshold: number,\n value: Secret,\n ) {\n this._identifier = identifier;\n this._groupIndex = groupIndex;\n this._groupThreshold = groupThreshold;\n this._groupCount = groupCount;\n this._memberIndex = memberIndex;\n this._memberThreshold = memberThreshold;\n this._value = value;\n }\n\n identifier(): number {\n return this._identifier;\n }\n\n groupIndex(): number {\n return this._groupIndex;\n }\n\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n groupCount(): number {\n return this._groupCount;\n }\n\n memberIndex(): number {\n return this._memberIndex;\n }\n\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n value(): Secret {\n return this._value;\n }\n}\n","// Ported from bc-sskr-rust/src/encoding.rs\n\nimport type { RandomNumberGenerator } from \"@bcts/rand\";\nimport { SecureRandomNumberGenerator } from \"@bcts/rand\";\nimport { splitSecret, recoverSecret, ShamirError } from \"@bcts/shamir\";\n\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\nimport { Secret } from \"./secret.js\";\nimport type { Spec } from \"./spec.js\";\nimport { SSKRShare } from \"./share.js\";\nimport { METADATA_SIZE_BYTES } from \"./index.js\";\n\n/**\n * Generates SSKR shares for the given Spec and Secret.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerate(spec: Spec, masterSecret: Secret): Uint8Array[][] {\n const rng = new SecureRandomNumberGenerator();\n return sskrGenerateUsing(spec, masterSecret, rng);\n}\n\n/**\n * Generates SSKR shares for the given Spec and Secret using the provided\n * random number generator.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @param randomGenerator - The random number generator to use for generating\n * shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerateUsing(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): Uint8Array[][] {\n const groupsShares = generateShares(spec, masterSecret, randomGenerator);\n\n const result: Uint8Array[][] = groupsShares.map((group) => group.map(serializeShare));\n\n return result;\n}\n\n/**\n * Combines the given SSKR shares into a Secret.\n *\n * @param shares - A array of SSKR shares to be combined.\n * @returns The reconstructed Secret.\n * @throws SSKRError if the shares do not meet the necessary quorum of groups\n * and member shares within each group.\n */\nexport function sskrCombine(shares: Uint8Array[]): Secret {\n const sskrShares: SSKRShare[] = [];\n\n for (const share of shares) {\n const sskrShare = deserializeShare(share);\n sskrShares.push(sskrShare);\n }\n\n return combineShares(sskrShares);\n}\n\nfunction serializeShare(share: SSKRShare): Uint8Array {\n // pack the id, group and member data into 5 bytes:\n // 76543210 76543210 76543210\n // 76543210 76543210\n // ----------------====----====----====----\n // identifier: 16\n // group-threshold: 4\n // group-count: 4\n // group-index: 4\n // member-threshold: 4\n // reserved (MUST be zero): 4\n // member-index: 4\n\n const valueData = share.value().getData();\n const result = new Uint8Array(valueData.length + METADATA_SIZE_BYTES);\n\n const id = share.identifier();\n const gt = (share.groupThreshold() - 1) & 0xf;\n const gc = (share.groupCount() - 1) & 0xf;\n const gi = share.groupIndex() & 0xf;\n const mt = (share.memberThreshold() - 1) & 0xf;\n const mi = share.memberIndex() & 0xf;\n\n const id1 = id >> 8;\n const id2 = id & 0xff;\n\n result[0] = id1;\n result[1] = id2;\n result[2] = (gt << 4) | gc;\n result[3] = (gi << 4) | mt;\n result[4] = mi;\n result.set(valueData, METADATA_SIZE_BYTES);\n\n return result;\n}\n\nfunction deserializeShare(source: Uint8Array): SSKRShare {\n if (source.length < METADATA_SIZE_BYTES) {\n throw new SSKRError(SSKRErrorType.ShareLengthInvalid);\n }\n\n const groupThreshold = (source[2] >> 4) + 1;\n const groupCount = (source[2] & 0xf) + 1;\n\n if (groupThreshold > groupCount) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n\n const identifier = (source[0] << 8) | source[1];\n const groupIndex = source[3] >> 4;\n const memberThreshold = (source[3] & 0xf) + 1;\n const reserved = source[4] >> 4;\n if (reserved !== 0) {\n throw new SSKRError(SSKRErrorType.ShareReservedBitsInvalid);\n }\n const memberIndex = source[4] & 0xf;\n const value = Secret.new(source.subarray(METADATA_SIZE_BYTES));\n\n return new SSKRShare(\n identifier,\n groupIndex,\n groupThreshold,\n groupCount,\n memberIndex,\n memberThreshold,\n value,\n );\n}\n\nfunction generateShares(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): SSKRShare[][] {\n // assign a random identifier\n const identifierBytes = new Uint8Array(2);\n randomGenerator.fillRandomData(identifierBytes);\n const identifier = (identifierBytes[0] << 8) | identifierBytes[1];\n\n const groupsShares: SSKRShare[][] = [];\n\n let groupSecrets: Uint8Array[];\n try {\n groupSecrets = splitSecret(\n spec.groupThreshold(),\n spec.groupCount(),\n masterSecret.getData(),\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n for (let groupIndex = 0; groupIndex < spec.groups().length; groupIndex++) {\n const group = spec.groups()[groupIndex];\n const groupSecret = groupSecrets[groupIndex];\n\n let memberSecrets: Uint8Array[];\n try {\n memberSecrets = splitSecret(\n group.memberThreshold(),\n group.memberCount(),\n groupSecret,\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n const memberSSKRShares: SSKRShare[] = memberSecrets.map((memberSecret, memberIndex) => {\n const secret = Secret.new(memberSecret);\n return new SSKRShare(\n identifier,\n groupIndex,\n spec.groupThreshold(),\n spec.groupCount(),\n memberIndex,\n group.memberThreshold(),\n secret,\n );\n });\n\n groupsShares.push(memberSSKRShares);\n }\n\n return groupsShares;\n}\n\ninterface Group {\n groupIndex: number;\n memberThreshold: number;\n memberIndexes: number[];\n memberShares: Secret[];\n}\n\nfunction combineShares(shares: SSKRShare[]): Secret {\n let identifier = 0;\n let groupThreshold = 0;\n let groupCount = 0;\n\n if (shares.length === 0) {\n throw new SSKRError(SSKRErrorType.SharesEmpty);\n }\n\n let nextGroup = 0;\n const groups: Group[] = [];\n let secretLen = 0;\n\n for (let i = 0; i < shares.length; i++) {\n const share = shares[i];\n\n if (i === 0) {\n // on the first one, establish expected values for common metadata\n identifier = share.identifier();\n groupCount = share.groupCount();\n groupThreshold = share.groupThreshold();\n secretLen = share.value().len();\n } else {\n // on subsequent shares, check that common metadata matches\n if (\n share.identifier() !== identifier ||\n share.groupThreshold() !== groupThreshold ||\n share.groupCount() !== groupCount ||\n share.value().len() !== secretLen\n ) {\n throw new SSKRError(SSKRErrorType.ShareSetInvalid);\n }\n }\n\n // sort shares into member groups\n let groupFound = false;\n for (const group of groups) {\n if (share.groupIndex() === group.groupIndex) {\n groupFound = true;\n if (share.memberThreshold() !== group.memberThreshold) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n for (const memberIndex of group.memberIndexes) {\n if (share.memberIndex() === memberIndex) {\n throw new SSKRError(SSKRErrorType.DuplicateMemberIndex);\n }\n }\n if (group.memberIndexes.length < group.memberThreshold) {\n group.memberIndexes.push(share.memberIndex());\n group.memberShares.push(share.value().clone());\n }\n }\n }\n\n if (!groupFound) {\n const g: Group = {\n groupIndex: share.groupIndex(),\n memberThreshold: share.memberThreshold(),\n memberIndexes: [share.memberIndex()],\n memberShares: [share.value().clone()],\n };\n groups.push(g);\n nextGroup++;\n }\n }\n\n // Check that we have enough groups to recover the master secret\n if (nextGroup < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Here, all of the shares are unpacked into member groups. Now we go\n // through each group and recover the group secret, and then use the\n // result to recover the master secret\n const masterIndexes: number[] = [];\n const masterShares: Uint8Array[] = [];\n\n for (const group of groups) {\n // Only attempt to recover the group secret if we have enough shares\n if (group.memberIndexes.length < group.memberThreshold) {\n continue;\n }\n\n // Recover the group secret\n try {\n const memberSharesData = group.memberShares.map((s) => s.getData());\n const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);\n masterIndexes.push(group.groupIndex);\n masterShares.push(groupSecret);\n } catch {\n // If we can't recover this group, just skip it\n continue;\n }\n\n // Stop if we have enough groups to recover the master secret\n if (masterIndexes.length === groupThreshold) {\n break;\n }\n }\n\n // If we don't have enough groups to recover the master secret, return an error\n if (masterIndexes.length < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Recover the master secret\n let masterSecretData: Uint8Array;\n try {\n masterSecretData = recoverSecret(masterIndexes, masterShares);\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n return Secret.new(masterSecretData);\n}\n","// Blockchain Commons Sharded Secret Key Reconstruction (SSKR)\n// Ported from bc-sskr-rust\n//\n// Sharded Secret Key Reconstruction (SSKR) is a protocol for splitting a\n// secret into a set of shares across one or more groups, such that the\n// secret can be reconstructed from any combination of shares totaling or\n// exceeding a threshold number of shares within each group and across all\n// groups. SSKR is a generalization of Shamir's Secret Sharing (SSS) that\n// allows for multiple groups and multiple thresholds.\n\nimport {\n MIN_SECRET_LEN as SHAMIR_MIN_SECRET_LEN,\n MAX_SECRET_LEN as SHAMIR_MAX_SECRET_LEN,\n MAX_SHARE_COUNT as SHAMIR_MAX_SHARE_COUNT,\n} from \"@bcts/shamir\";\n\n/**\n * The minimum length of a secret.\n */\nexport const MIN_SECRET_LEN = SHAMIR_MIN_SECRET_LEN;\n\n/**\n * The maximum length of a secret.\n */\nexport const MAX_SECRET_LEN = SHAMIR_MAX_SECRET_LEN;\n\n/**\n * The maximum number of shares that can be generated from a secret.\n */\nexport const MAX_SHARE_COUNT = SHAMIR_MAX_SHARE_COUNT;\n\n/**\n * The maximum number of groups in a split.\n */\nexport const MAX_GROUPS_COUNT = MAX_SHARE_COUNT;\n\n/**\n * The number of bytes used to encode the metadata for a share.\n */\nexport const METADATA_SIZE_BYTES = 5;\n\n/**\n * The minimum number of bytes required to encode a share.\n */\nexport const MIN_SERIALIZE_SIZE_BYTES = METADATA_SIZE_BYTES + MIN_SECRET_LEN;\n\n// Error types\nexport { SSKRError, SSKRErrorType, type SSKRResult } from \"./error.js\";\n\n// Secret\nexport { Secret } from \"./secret.js\";\n\n// Specifications\nexport { GroupSpec, Spec } from \"./spec.js\";\n\n// Share\nexport { SSKRShare } from \"./share.js\";\n\n// Encoding/Decoding\nexport { sskrGenerate, sskrGenerateUsing, sskrCombine } from \"./encoding.js\";\n"],"mappings":";;;;;;;AAOA,I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../src/share.ts","../src/encoding.ts","../src/index.ts"],"sourcesContent":["/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/error.rs\n\nimport type { ShamirError } from \"@bcts/shamir\";\n\n/**\n * Error types for SSKR operations.\n */\nexport enum SSKRErrorType {\n DuplicateMemberIndex = \"DuplicateMemberIndex\",\n GroupSpecInvalid = \"GroupSpecInvalid\",\n GroupCountInvalid = \"GroupCountInvalid\",\n GroupThresholdInvalid = \"GroupThresholdInvalid\",\n MemberCountInvalid = \"MemberCountInvalid\",\n MemberThresholdInvalid = \"MemberThresholdInvalid\",\n NotEnoughGroups = \"NotEnoughGroups\",\n SecretLengthNotEven = \"SecretLengthNotEven\",\n SecretTooLong = \"SecretTooLong\",\n SecretTooShort = \"SecretTooShort\",\n ShareLengthInvalid = \"ShareLengthInvalid\",\n ShareReservedBitsInvalid = \"ShareReservedBitsInvalid\",\n SharesEmpty = \"SharesEmpty\",\n ShareSetInvalid = \"ShareSetInvalid\",\n ShamirError = \"ShamirError\",\n}\n\n/**\n * Error class for SSKR operations.\n */\nexport class SSKRError extends Error {\n readonly type: SSKRErrorType;\n readonly shamirError?: ShamirError | undefined;\n\n constructor(type: SSKRErrorType, message?: string, shamirError?: ShamirError) {\n super(message ?? SSKRError.defaultMessage(type, shamirError));\n this.type = type;\n this.shamirError = shamirError;\n this.name = \"SSKRError\";\n }\n\n private static defaultMessage(type: SSKRErrorType, shamirError?: ShamirError): string {\n switch (type) {\n case SSKRErrorType.DuplicateMemberIndex:\n return \"When combining shares, the provided shares contained a duplicate member index\";\n case SSKRErrorType.GroupSpecInvalid:\n return \"Invalid group specification.\";\n case SSKRErrorType.GroupCountInvalid:\n return \"When creating a split spec, the group count is invalid\";\n case SSKRErrorType.GroupThresholdInvalid:\n return \"SSKR group threshold is invalid\";\n case SSKRErrorType.MemberCountInvalid:\n return \"SSKR member count is invalid\";\n case SSKRErrorType.MemberThresholdInvalid:\n return \"SSKR member threshold is invalid\";\n case SSKRErrorType.NotEnoughGroups:\n return \"SSKR shares did not contain enough groups\";\n case SSKRErrorType.SecretLengthNotEven:\n return \"SSKR secret is not of even length\";\n case SSKRErrorType.SecretTooLong:\n return \"SSKR secret is too long\";\n case SSKRErrorType.SecretTooShort:\n return \"SSKR secret is too short\";\n case SSKRErrorType.ShareLengthInvalid:\n return \"SSKR shares did not contain enough serialized bytes\";\n case SSKRErrorType.ShareReservedBitsInvalid:\n return \"SSKR shares contained invalid reserved bits\";\n case SSKRErrorType.SharesEmpty:\n return \"SSKR shares were empty\";\n case SSKRErrorType.ShareSetInvalid:\n return \"SSKR shares were invalid\";\n case SSKRErrorType.ShamirError:\n return shamirError != null\n ? `SSKR Shamir error: ${shamirError.message}`\n : \"SSKR Shamir error\";\n }\n }\n\n static fromShamirError(error: ShamirError): SSKRError {\n return new SSKRError(SSKRErrorType.ShamirError, undefined, error);\n }\n}\n\n/**\n * Mirrors Rust's `Result<T, Error>` (`bc-sskr-rust/src/error.rs:54`).\n *\n * The TypeScript port surfaces failures by throwing {@link SSKRError}\n * rather than returning a sum type, so this alias is a no-op\n * (`SSKRResult<T>` ≡ `T`). It is kept so signatures published in\n * `@bcts/sskr` remain visually parallel to their Rust counterparts.\n */\nexport type SSKRResult<T> = T;\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/secret.rs\n\nimport { MIN_SECRET_LEN, MAX_SECRET_LEN } from \"./index.js\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * A secret to be split into shares.\n */\nexport class Secret {\n private readonly data: Uint8Array;\n\n private constructor(data: Uint8Array) {\n this.data = data;\n }\n\n /**\n * Creates a new Secret instance with the given data.\n *\n * @param data - The secret data to be split into shares.\n * @returns A new Secret instance.\n * @throws SSKRError if the length of the secret is less than\n * MIN_SECRET_LEN, greater than MAX_SECRET_LEN, or not even.\n */\n static new(data: Uint8Array | string): Secret {\n const bytes = typeof data === \"string\" ? new TextEncoder().encode(data) : data;\n const len = bytes.length;\n\n if (len < MIN_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooShort);\n }\n if (len > MAX_SECRET_LEN) {\n throw new SSKRError(SSKRErrorType.SecretTooLong);\n }\n if ((len & 1) !== 0) {\n throw new SSKRError(SSKRErrorType.SecretLengthNotEven);\n }\n\n return new Secret(new Uint8Array(bytes));\n }\n\n /**\n * Returns the length of the secret.\n */\n len(): number {\n return this.data.length;\n }\n\n /**\n * Returns true if the secret is empty.\n */\n isEmpty(): boolean {\n return this.len() === 0;\n }\n\n /**\n * Returns a reference to the secret data.\n *\n * Mirrors Rust's `Secret::data(&self) -> &[u8]`\n * (`bc-sskr-rust/src/secret.rs:43`).\n */\n getData(): Uint8Array {\n return this.data;\n }\n\n /**\n * Returns the secret data as a Uint8Array.\n *\n * Mirrors Rust's `impl AsRef<[u8]> for Secret`\n * (`bc-sskr-rust/src/secret.rs:46-49`). In Rust, `as_ref()` is\n * provided via the `AsRef<[u8]>` trait, which lets the `Secret` flow\n * naturally through any API expecting `impl AsRef<[u8]>`. TypeScript\n * has no equivalent of that trait, so we expose the same backing\n * buffer through both {@link getData} (the field accessor) and\n * `asRef` (the trait-style accessor) for ergonomic parity. Callers\n * may pick whichever name reads better at the call site.\n */\n asRef(): Uint8Array {\n return this.data;\n }\n\n /**\n * Check equality with another Secret.\n */\n equals(other: Secret): boolean {\n if (this.data.length !== other.data.length) {\n return false;\n }\n for (let i = 0; i < this.data.length; i++) {\n if (this.data[i] !== other.data[i]) {\n return false;\n }\n }\n return true;\n }\n\n /**\n * Clone the secret.\n */\n clone(): Secret {\n return new Secret(new Uint8Array(this.data));\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/spec.rs\n\nimport { MAX_SHARE_COUNT } from \"@bcts/shamir\";\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\n\n/**\n * Strictly parses a non-negative integer string the way Rust's\n * `<usize as FromStr>::from_str` does (`bc-sskr-rust/src/spec.rs:102, 108`).\n *\n * Accepts an optional leading `+` followed by one or more decimal digits.\n * Rejects whitespace, decimals (`\"2.5\"`), trailing characters (`\"2x\"`),\n * the `-` sign, and the empty string. Returns `undefined` on rejection so\n * callers surface the appropriate `GroupSpecInvalid` error in the same\n * spot Rust's `?` would.\n */\nconst STRICT_USIZE_RE = /^\\+?\\d+$/;\nfunction parseStrictUsize(s: string): number | undefined {\n if (!STRICT_USIZE_RE.test(s)) return undefined;\n const n = Number(s);\n // `Number.isSafeInteger` rejects values outside ±2^53; usize on\n // 64-bit Rust covers a larger range but for SSKR's `member_count`\n // (capped at 16) this is far beyond any meaningful input.\n return Number.isSafeInteger(n) ? n : undefined;\n}\n\n/**\n * A specification for a group of shares within an SSKR split.\n */\nexport class GroupSpec {\n private readonly _memberThreshold: number;\n private readonly _memberCount: number;\n\n private constructor(memberThreshold: number, memberCount: number) {\n this._memberThreshold = memberThreshold;\n this._memberCount = memberCount;\n }\n\n /**\n * Creates a new GroupSpec instance with the given member threshold and count.\n *\n * @param memberThreshold - The minimum number of member shares required to\n * reconstruct the secret within the group.\n * @param memberCount - The total number of member shares in the group.\n * @returns A new GroupSpec instance.\n * @throws SSKRError if the member count is zero, if the member count is\n * greater than the maximum share count, or if the member threshold is\n * greater than the member count.\n */\n static new(memberThreshold: number, memberCount: number): GroupSpec {\n if (memberCount === 0) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberCount > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.MemberCountInvalid);\n }\n if (memberThreshold > memberCount) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n return new GroupSpec(memberThreshold, memberCount);\n }\n\n /**\n * Returns the member share threshold for this group.\n */\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n /**\n * Returns the number of member shares in this group.\n */\n memberCount(): number {\n return this._memberCount;\n }\n\n /**\n * Parses a group specification from a string.\n * Format: `\"M-of-N\"` where `M` is the threshold and `N` is the count.\n *\n * Mirrors Rust's `GroupSpec::parse` (`bc-sskr-rust/src/spec.rs:97-112`),\n * which calls `parts[0].parse::<usize>()`. Rust's `usize::FromStr` is\n * strict: it rejects whitespace, decimals, trailing characters, and the\n * `-` sign, accepting only an optional `+` prefix followed by digits.\n * We mirror that with the regex `^\\+?\\d+$` so strings like `\"2.5\"`,\n * `\"2x\"`, `\" 2\"`, `\"-2\"`, and `\"\"` all surface\n * {@link SSKRErrorType.GroupSpecInvalid} on both sides.\n */\n static parse(s: string): GroupSpec {\n const parts = s.split(\"-\");\n if (parts.length !== 3) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberThreshold = parseStrictUsize(parts[0]);\n if (memberThreshold === undefined) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n if (parts[1] !== \"of\") {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n const memberCount = parseStrictUsize(parts[2]);\n if (memberCount === undefined) {\n throw new SSKRError(SSKRErrorType.GroupSpecInvalid);\n }\n\n return GroupSpec.new(memberThreshold, memberCount);\n }\n\n /**\n * Creates a default GroupSpec (1-of-1).\n */\n static default(): GroupSpec {\n return GroupSpec.new(1, 1);\n }\n\n /**\n * Returns a string representation of the group spec.\n */\n toString(): string {\n return `${this._memberThreshold}-of-${this._memberCount}`;\n }\n}\n\n/**\n * A specification for an SSKR split.\n */\nexport class Spec {\n private readonly _groupThreshold: number;\n private readonly _groups: GroupSpec[];\n\n private constructor(groupThreshold: number, groups: GroupSpec[]) {\n this._groupThreshold = groupThreshold;\n this._groups = groups;\n }\n\n /**\n * Creates a new Spec instance with the given group threshold and groups.\n *\n * @param groupThreshold - The minimum number of groups required to\n * reconstruct the secret.\n * @param groups - The list of GroupSpec instances that define the groups\n * and their members.\n * @returns A new Spec instance.\n * @throws SSKRError if the group threshold is zero, if the group threshold\n * is greater than the number of groups, or if the number of groups is\n * greater than the maximum share count.\n */\n static new(groupThreshold: number, groups: GroupSpec[]): Spec {\n if (groupThreshold === 0) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groupThreshold > groups.length) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n if (groups.length > MAX_SHARE_COUNT) {\n throw new SSKRError(SSKRErrorType.GroupCountInvalid);\n }\n return new Spec(groupThreshold, groups);\n }\n\n /**\n * Returns the group threshold.\n */\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n /**\n * Returns a slice of the group specifications.\n */\n groups(): GroupSpec[] {\n return this._groups;\n }\n\n /**\n * Returns the number of groups.\n */\n groupCount(): number {\n return this._groups.length;\n }\n\n /**\n * Returns the total number of shares across all groups.\n */\n shareCount(): number {\n return this._groups.reduce((sum, g) => sum + g.memberCount(), 0);\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/share.rs\n\nimport type { Secret } from \"./secret.js\";\n\n/**\n * A share in the SSKR scheme.\n */\nexport class SSKRShare {\n private readonly _identifier: number;\n private readonly _groupIndex: number;\n private readonly _groupThreshold: number;\n private readonly _groupCount: number;\n private readonly _memberIndex: number;\n private readonly _memberThreshold: number;\n private readonly _value: Secret;\n\n constructor(\n identifier: number,\n groupIndex: number,\n groupThreshold: number,\n groupCount: number,\n memberIndex: number,\n memberThreshold: number,\n value: Secret,\n ) {\n this._identifier = identifier;\n this._groupIndex = groupIndex;\n this._groupThreshold = groupThreshold;\n this._groupCount = groupCount;\n this._memberIndex = memberIndex;\n this._memberThreshold = memberThreshold;\n this._value = value;\n }\n\n identifier(): number {\n return this._identifier;\n }\n\n groupIndex(): number {\n return this._groupIndex;\n }\n\n groupThreshold(): number {\n return this._groupThreshold;\n }\n\n groupCount(): number {\n return this._groupCount;\n }\n\n memberIndex(): number {\n return this._memberIndex;\n }\n\n memberThreshold(): number {\n return this._memberThreshold;\n }\n\n value(): Secret {\n return this._value;\n }\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Ported from bc-sskr-rust/src/encoding.rs\n\nimport type { RandomNumberGenerator } from \"@bcts/rand\";\nimport { SecureRandomNumberGenerator } from \"@bcts/rand\";\nimport { splitSecret, recoverSecret, ShamirError } from \"@bcts/shamir\";\n\nimport { SSKRError, SSKRErrorType } from \"./error.js\";\nimport { Secret } from \"./secret.js\";\nimport type { Spec } from \"./spec.js\";\nimport { SSKRShare } from \"./share.js\";\nimport { METADATA_SIZE_BYTES } from \"./index.js\";\n\n/**\n * Generates SSKR shares for the given Spec and Secret.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerate(spec: Spec, masterSecret: Secret): Uint8Array[][] {\n const rng = new SecureRandomNumberGenerator();\n return sskrGenerateUsing(spec, masterSecret, rng);\n}\n\n/**\n * Generates SSKR shares for the given Spec and Secret using the provided\n * random number generator.\n *\n * @param spec - The Spec instance that defines the group and member thresholds.\n * @param masterSecret - The Secret instance to be split into shares.\n * @param randomGenerator - The random number generator to use for generating\n * shares.\n * @returns A vector of groups, each containing a vector of shares,\n * each of which is a Uint8Array.\n */\nexport function sskrGenerateUsing(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): Uint8Array[][] {\n const groupsShares = generateShares(spec, masterSecret, randomGenerator);\n\n const result: Uint8Array[][] = groupsShares.map((group) => group.map(serializeShare));\n\n return result;\n}\n\n/**\n * Combines the given SSKR shares into a Secret.\n *\n * @param shares - A array of SSKR shares to be combined.\n * @returns The reconstructed Secret.\n * @throws SSKRError if the shares do not meet the necessary quorum of groups\n * and member shares within each group.\n */\nexport function sskrCombine(shares: Uint8Array[]): Secret {\n const sskrShares: SSKRShare[] = [];\n\n for (const share of shares) {\n const sskrShare = deserializeShare(share);\n sskrShares.push(sskrShare);\n }\n\n return combineShares(sskrShares);\n}\n\nfunction serializeShare(share: SSKRShare): Uint8Array {\n // pack the id, group and member data into 5 bytes:\n // 76543210 76543210 76543210\n // 76543210 76543210\n // ----------------====----====----====----\n // identifier: 16\n // group-threshold: 4\n // group-count: 4\n // group-index: 4\n // member-threshold: 4\n // reserved (MUST be zero): 4\n // member-index: 4\n\n const valueData = share.value().getData();\n const result = new Uint8Array(valueData.length + METADATA_SIZE_BYTES);\n\n const id = share.identifier();\n const gt = (share.groupThreshold() - 1) & 0xf;\n const gc = (share.groupCount() - 1) & 0xf;\n const gi = share.groupIndex() & 0xf;\n const mt = (share.memberThreshold() - 1) & 0xf;\n const mi = share.memberIndex() & 0xf;\n\n const id1 = id >> 8;\n const id2 = id & 0xff;\n\n result[0] = id1;\n result[1] = id2;\n result[2] = (gt << 4) | gc;\n result[3] = (gi << 4) | mt;\n result[4] = mi;\n result.set(valueData, METADATA_SIZE_BYTES);\n\n return result;\n}\n\nfunction deserializeShare(source: Uint8Array): SSKRShare {\n if (source.length < METADATA_SIZE_BYTES) {\n throw new SSKRError(SSKRErrorType.ShareLengthInvalid);\n }\n\n const groupThreshold = (source[2] >> 4) + 1;\n const groupCount = (source[2] & 0xf) + 1;\n\n if (groupThreshold > groupCount) {\n throw new SSKRError(SSKRErrorType.GroupThresholdInvalid);\n }\n\n const identifier = (source[0] << 8) | source[1];\n const groupIndex = source[3] >> 4;\n const memberThreshold = (source[3] & 0xf) + 1;\n const reserved = source[4] >> 4;\n if (reserved !== 0) {\n throw new SSKRError(SSKRErrorType.ShareReservedBitsInvalid);\n }\n const memberIndex = source[4] & 0xf;\n const value = Secret.new(source.subarray(METADATA_SIZE_BYTES));\n\n return new SSKRShare(\n identifier,\n groupIndex,\n groupThreshold,\n groupCount,\n memberIndex,\n memberThreshold,\n value,\n );\n}\n\nfunction generateShares(\n spec: Spec,\n masterSecret: Secret,\n randomGenerator: RandomNumberGenerator,\n): SSKRShare[][] {\n // assign a random identifier\n const identifierBytes = new Uint8Array(2);\n randomGenerator.fillRandomData(identifierBytes);\n const identifier = (identifierBytes[0] << 8) | identifierBytes[1];\n\n const groupsShares: SSKRShare[][] = [];\n\n let groupSecrets: Uint8Array[];\n try {\n groupSecrets = splitSecret(\n spec.groupThreshold(),\n spec.groupCount(),\n masterSecret.getData(),\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n for (let groupIndex = 0; groupIndex < spec.groups().length; groupIndex++) {\n const group = spec.groups()[groupIndex];\n const groupSecret = groupSecrets[groupIndex];\n\n let memberSecrets: Uint8Array[];\n try {\n memberSecrets = splitSecret(\n group.memberThreshold(),\n group.memberCount(),\n groupSecret,\n randomGenerator,\n );\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n const memberSSKRShares: SSKRShare[] = memberSecrets.map((memberSecret, memberIndex) => {\n const secret = Secret.new(memberSecret);\n return new SSKRShare(\n identifier,\n groupIndex,\n spec.groupThreshold(),\n spec.groupCount(),\n memberIndex,\n group.memberThreshold(),\n secret,\n );\n });\n\n groupsShares.push(memberSSKRShares);\n }\n\n return groupsShares;\n}\n\ninterface Group {\n groupIndex: number;\n memberThreshold: number;\n memberIndexes: number[];\n memberShares: Secret[];\n}\n\nfunction combineShares(shares: SSKRShare[]): Secret {\n let identifier = 0;\n let groupThreshold = 0;\n let groupCount = 0;\n\n if (shares.length === 0) {\n throw new SSKRError(SSKRErrorType.SharesEmpty);\n }\n\n let nextGroup = 0;\n const groups: Group[] = [];\n let secretLen = 0;\n\n for (let i = 0; i < shares.length; i++) {\n const share = shares[i];\n\n if (i === 0) {\n // on the first one, establish expected values for common metadata\n identifier = share.identifier();\n groupCount = share.groupCount();\n groupThreshold = share.groupThreshold();\n secretLen = share.value().len();\n } else {\n // on subsequent shares, check that common metadata matches\n if (\n share.identifier() !== identifier ||\n share.groupThreshold() !== groupThreshold ||\n share.groupCount() !== groupCount ||\n share.value().len() !== secretLen\n ) {\n throw new SSKRError(SSKRErrorType.ShareSetInvalid);\n }\n }\n\n // sort shares into member groups\n let groupFound = false;\n for (const group of groups) {\n if (share.groupIndex() === group.groupIndex) {\n groupFound = true;\n if (share.memberThreshold() !== group.memberThreshold) {\n throw new SSKRError(SSKRErrorType.MemberThresholdInvalid);\n }\n for (const memberIndex of group.memberIndexes) {\n if (share.memberIndex() === memberIndex) {\n throw new SSKRError(SSKRErrorType.DuplicateMemberIndex);\n }\n }\n if (group.memberIndexes.length < group.memberThreshold) {\n group.memberIndexes.push(share.memberIndex());\n group.memberShares.push(share.value().clone());\n }\n }\n }\n\n if (!groupFound) {\n const g: Group = {\n groupIndex: share.groupIndex(),\n memberThreshold: share.memberThreshold(),\n memberIndexes: [share.memberIndex()],\n memberShares: [share.value().clone()],\n };\n groups.push(g);\n nextGroup++;\n }\n }\n\n // Check that we have enough groups to recover the master secret\n if (nextGroup < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Here, all of the shares are unpacked into member groups. Now we go\n // through each group and recover the group secret, and then use the\n // result to recover the master secret\n const masterIndexes: number[] = [];\n const masterShares: Uint8Array[] = [];\n\n for (const group of groups) {\n // Only attempt to recover the group secret if we have enough shares\n if (group.memberIndexes.length < group.memberThreshold) {\n continue;\n }\n\n // Recover the group secret. Mirrors Rust's `if let Ok(group_secret)\n // = recover_secret(...)` (`bc-sskr-rust/src/encoding.rs:289-294`):\n // we silently skip groups whose Shamir recovery fails (the\n // seedtool-cli #6 fix), but **rethrow any non-Shamir error** so\n // unexpected exceptions don't get swallowed — Rust's match-by-Result\n // already enforces that distinction at the type level.\n try {\n const memberSharesData = group.memberShares.map((s) => s.getData());\n const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);\n masterIndexes.push(group.groupIndex);\n masterShares.push(groupSecret);\n } catch (e) {\n if (e instanceof ShamirError) {\n // Equivalent to Rust's \"Err arm\" of `if let Ok` — the seedtool\n // #6 fix demands we keep iterating to other groups.\n continue;\n }\n throw e;\n }\n\n // Stop if we have enough groups to recover the master secret\n if (masterIndexes.length === groupThreshold) {\n break;\n }\n }\n\n // If we don't have enough groups to recover the master secret, return an error\n if (masterIndexes.length < groupThreshold) {\n throw new SSKRError(SSKRErrorType.NotEnoughGroups);\n }\n\n // Recover the master secret\n let masterSecretData: Uint8Array;\n try {\n masterSecretData = recoverSecret(masterIndexes, masterShares);\n } catch (e) {\n if (e instanceof ShamirError) {\n throw SSKRError.fromShamirError(e);\n }\n throw e;\n }\n\n return Secret.new(masterSecretData);\n}\n","/**\n * Copyright © 2023-2026 Blockchain Commons, LLC\n * Copyright © 2025-2026 Parity Technologies\n *\n */\n\n// Blockchain Commons Sharded Secret Key Reconstruction (SSKR)\n// Ported from bc-sskr-rust\n//\n// Sharded Secret Key Reconstruction (SSKR) is a protocol for splitting a\n// secret into a set of shares across one or more groups, such that the\n// secret can be reconstructed from any combination of shares totaling or\n// exceeding a threshold number of shares within each group and across all\n// groups. SSKR is a generalization of Shamir's Secret Sharing (SSS) that\n// allows for multiple groups and multiple thresholds.\n\nimport {\n MIN_SECRET_LEN as SHAMIR_MIN_SECRET_LEN,\n MAX_SECRET_LEN as SHAMIR_MAX_SECRET_LEN,\n MAX_SHARE_COUNT as SHAMIR_MAX_SHARE_COUNT,\n} from \"@bcts/shamir\";\n\n/**\n * The minimum length of a secret.\n */\nexport const MIN_SECRET_LEN = SHAMIR_MIN_SECRET_LEN;\n\n/**\n * The maximum length of a secret.\n */\nexport const MAX_SECRET_LEN = SHAMIR_MAX_SECRET_LEN;\n\n/**\n * The maximum number of shares that can be generated from a secret.\n */\nexport const MAX_SHARE_COUNT = SHAMIR_MAX_SHARE_COUNT;\n\n/**\n * The maximum number of groups in a split.\n */\nexport const MAX_GROUPS_COUNT = MAX_SHARE_COUNT;\n\n/**\n * The number of bytes used to encode the metadata for a share.\n */\nexport const METADATA_SIZE_BYTES = 5;\n\n/**\n * The minimum number of bytes required to encode a share.\n */\nexport const MIN_SERIALIZE_SIZE_BYTES = METADATA_SIZE_BYTES + MIN_SECRET_LEN;\n\n// Error types\nexport { SSKRError, SSKRErrorType, type SSKRResult } from \"./error.js\";\n\n// Secret\nexport { Secret } from \"./secret.js\";\n\n// Specifications\nexport { GroupSpec, Spec } from \"./spec.js\";\n\n// Encoding/Decoding\nexport { sskrGenerate, sskrGenerateUsing, sskrCombine } from \"./encoding.js\";\n"],"mappings":";;;;;;AAaA,IAAY,gBAAL,yBAAA,eAAA;AACL,eAAA,0BAAA;AACA,eAAA,sBAAA;AACA,eAAA,uBAAA;AACA,eAAA,2BAAA;AACA,eAAA,wBAAA;AACA,eAAA,4BAAA;AACA,eAAA,qBAAA;AACA,eAAA,yBAAA;AACA,eAAA,mBAAA;AACA,eAAA,oBAAA;AACA,eAAA,wBAAA;AACA,eAAA,8BAAA;AACA,eAAA,iBAAA;AACA,eAAA,qBAAA;AACA,eAAA,iBAAA;;KACD;;;;AAKD,IAAa,YAAb,MAAa,kBAAkB,MAAM;CACnC;CACA;CAEA,YAAY,MAAqB,SAAkB,aAA2B;AAC5E,QAAM,WAAW,UAAU,eAAe,MAAM,YAAY,CAAC;AAC7D,OAAK,OAAO;AACZ,OAAK,cAAc;AACnB,OAAK,OAAO;;CAGd,OAAe,eAAe,MAAqB,aAAmC;AACpF,UAAQ,MAAR;GACE,KAAA,uBACE,QAAO;GACT,KAAA,mBACE,QAAO;GACT,KAAA,oBACE,QAAO;GACT,KAAA,wBACE,QAAO;GACT,KAAA,qBACE,QAAO;GACT,KAAA,yBACE,QAAO;GACT,KAAA,kBACE,QAAO;GACT,KAAA,sBACE,QAAO;GACT,KAAA,gBACE,QAAO;GACT,KAAA,iBACE,QAAO;GACT,KAAA,qBACE,QAAO;GACT,KAAA,2BACE,QA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C;CAIpD,IAAI;AACJ,KAAI;AACF,qBAAmB,cAAc,eAAe,aAAa;UACtD,GAAG;AACV,MAAI,aAAa,YACf,OAAM,UAAU,gBAAgB,EAAE;AAEpC,QAAM;;AAGR,QAAO,OAAO,IAAI,iBAAiB;;;;;;;;;;;;AC1TrC,MAAa,iBAAiBC;;;;AAK9B,MAAa,iBAAiBC;;;;AAK9B,MAAa,kBAAkBC;;;;AAK/B,MAAa,mBAAmB;;;;AAKhC,MAAa,sBAAsB;;;;AAKnC,MAAa,2BAAA,IAAiD"}
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bcts/sskr",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Blockchain Commons Sharded Secret Key Reconstruction (SSKR) for TypeScript",
|
|
6
6
|
"license": "BSD-2-Clause-Patent",
|
|
7
|
-
"author": "
|
|
8
|
-
"homepage": "https://
|
|
7
|
+
"author": "Parity Technologies <admin@parity.io> (https://parity.io)",
|
|
8
|
+
"homepage": "https://bcts.dev",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/
|
|
11
|
+
"url": "https://github.com/paritytech/bcts",
|
|
12
12
|
"directory": "packages/sskr"
|
|
13
13
|
},
|
|
14
14
|
"bugs": {
|
|
15
|
-
"url": "https://github.com/
|
|
15
|
+
"url": "https://github.com/paritytech/bcts/issues"
|
|
16
16
|
},
|
|
17
17
|
"main": "dist/index.cjs",
|
|
18
18
|
"module": "dist/index.mjs",
|
|
@@ -33,11 +33,10 @@
|
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "tsdown",
|
|
36
|
-
"dev": "tsdown --watch",
|
|
37
36
|
"test": "vitest run",
|
|
38
37
|
"test:watch": "vitest",
|
|
39
|
-
"lint": "eslint 'src/**/*.ts'",
|
|
40
|
-
"lint:fix": "eslint 'src/**/*.ts' --fix",
|
|
38
|
+
"lint": "eslint 'src/**/*.ts' 'tests/**/*.ts'",
|
|
39
|
+
"lint:fix": "eslint 'src/**/*.ts' 'tests/**/*.ts' --fix",
|
|
41
40
|
"typecheck": "tsc --noEmit",
|
|
42
41
|
"clean": "rm -rf dist",
|
|
43
42
|
"docs": "typedoc",
|
|
@@ -57,19 +56,19 @@
|
|
|
57
56
|
"node": ">=18.0.0"
|
|
58
57
|
},
|
|
59
58
|
"dependencies": {
|
|
60
|
-
"@bcts/rand": "^1.0.0-
|
|
61
|
-
"@bcts/shamir": "^1.0.0-
|
|
59
|
+
"@bcts/rand": "^1.0.0-beta.0",
|
|
60
|
+
"@bcts/shamir": "^1.0.0-beta.0"
|
|
62
61
|
},
|
|
63
62
|
"devDependencies": {
|
|
64
63
|
"@bcts/eslint": "^0.1.0",
|
|
65
64
|
"@bcts/tsconfig": "^0.1.0",
|
|
66
|
-
"@eslint/js": "^
|
|
67
|
-
"@types/node": "^
|
|
68
|
-
"eslint": "^
|
|
65
|
+
"@eslint/js": "^10.0.1",
|
|
66
|
+
"@types/node": "^25.6.0",
|
|
67
|
+
"eslint": "^10.2.1",
|
|
69
68
|
"ts-node": "^10.9.2",
|
|
70
|
-
"tsdown": "^0.
|
|
71
|
-
"typedoc": "^0.28.
|
|
72
|
-
"typescript": "^
|
|
73
|
-
"vitest": "^
|
|
69
|
+
"tsdown": "^0.21.0",
|
|
70
|
+
"typedoc": "^0.28.19",
|
|
71
|
+
"typescript": "^6.0.3",
|
|
72
|
+
"vitest": "^4.1.5"
|
|
74
73
|
}
|
|
75
74
|
}
|
package/src/encoding.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
3
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
// Ported from bc-sskr-rust/src/encoding.rs
|
|
2
8
|
|
|
3
9
|
import type { RandomNumberGenerator } from "@bcts/rand";
|
|
@@ -289,15 +295,24 @@ function combineShares(shares: SSKRShare[]): Secret {
|
|
|
289
295
|
continue;
|
|
290
296
|
}
|
|
291
297
|
|
|
292
|
-
// Recover the group secret
|
|
298
|
+
// Recover the group secret. Mirrors Rust's `if let Ok(group_secret)
|
|
299
|
+
// = recover_secret(...)` (`bc-sskr-rust/src/encoding.rs:289-294`):
|
|
300
|
+
// we silently skip groups whose Shamir recovery fails (the
|
|
301
|
+
// seedtool-cli #6 fix), but **rethrow any non-Shamir error** so
|
|
302
|
+
// unexpected exceptions don't get swallowed — Rust's match-by-Result
|
|
303
|
+
// already enforces that distinction at the type level.
|
|
293
304
|
try {
|
|
294
305
|
const memberSharesData = group.memberShares.map((s) => s.getData());
|
|
295
306
|
const groupSecret = recoverSecret(group.memberIndexes, memberSharesData);
|
|
296
307
|
masterIndexes.push(group.groupIndex);
|
|
297
308
|
masterShares.push(groupSecret);
|
|
298
|
-
} catch {
|
|
299
|
-
|
|
300
|
-
|
|
309
|
+
} catch (e) {
|
|
310
|
+
if (e instanceof ShamirError) {
|
|
311
|
+
// Equivalent to Rust's "Err arm" of `if let Ok` — the seedtool
|
|
312
|
+
// #6 fix demands we keep iterating to other groups.
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
throw e;
|
|
301
316
|
}
|
|
302
317
|
|
|
303
318
|
// Stop if we have enough groups to recover the master secret
|