@derec-alliance/react-native 0.0.8 → 0.0.10

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.
Files changed (50) hide show
  1. package/README.md +410 -9
  2. package/android/src/main/jniLibs/arm64-v8a/libderec_library.a +0 -0
  3. package/android/src/main/jniLibs/armeabi-v7a/libderec_library.a +0 -0
  4. package/android/src/main/jniLibs/x86_64/libderec_library.a +0 -0
  5. package/cpp/Convert.cpp +9 -0
  6. package/cpp/Convert.h +8 -2
  7. package/cpp/Primitives.cpp +123 -29
  8. package/cpp/ProtocolHost.cpp +4 -5
  9. package/cpp/StoreCallbacks.cpp +148 -61
  10. package/cpp/StoreCallbacks.h +8 -1
  11. package/cpp/derec_ffi.h +127 -48
  12. package/ios/DeRecFFI.xcframework/Info.plist +5 -5
  13. package/ios/DeRecFFI.xcframework/ios-arm64/Headers/derec_ffi.h +127 -48
  14. package/ios/DeRecFFI.xcframework/ios-arm64/libderec_library.a +0 -0
  15. package/ios/DeRecFFI.xcframework/ios-arm64_x86_64-simulator/Headers/derec_ffi.h +127 -48
  16. package/ios/DeRecFFI.xcframework/ios-arm64_x86_64-simulator/libderec_library.a +2 -2
  17. package/lib/errors.d.ts +11 -1
  18. package/lib/errors.js +66 -1
  19. package/lib/messages.js +5 -3
  20. package/lib/primitives.d.ts +3 -3
  21. package/lib/primitives.js +7 -3
  22. package/lib/protocol.d.ts +110 -10
  23. package/lib/protocol.js +166 -11
  24. package/lib/types.d.ts +474 -48
  25. package/lib/types.js +23 -1
  26. package/package.json +4 -17
  27. package/schema/derec_descriptor.bin +0 -0
  28. package/schema/proto/committedderecshare.proto +93 -0
  29. package/schema/proto/communicationinfo.proto +104 -0
  30. package/schema/proto/contact.proto +258 -0
  31. package/schema/proto/derecmessage.proto +232 -0
  32. package/schema/proto/derecsecret.proto +163 -0
  33. package/schema/proto/derectransport.proto +38 -0
  34. package/schema/proto/error.proto +68 -0
  35. package/schema/proto/getshare.proto +184 -0
  36. package/schema/proto/pair.proto +284 -0
  37. package/schema/proto/parameterrange.proto +36 -0
  38. package/schema/proto/prepair.proto +163 -0
  39. package/schema/proto/result.proto +194 -0
  40. package/schema/proto/secretidsversions.proto +199 -0
  41. package/schema/proto/storeshare.proto +242 -0
  42. package/schema/proto/transportprotocol.proto +112 -0
  43. package/schema/proto/unpair.proto +132 -0
  44. package/schema/proto/updatechannelinfo.proto +100 -0
  45. package/schema/proto/verify.proto +160 -0
  46. package/src/errors.ts +69 -1
  47. package/src/messages.ts +5 -3
  48. package/src/primitives.ts +19 -4
  49. package/src/protocol.ts +175 -13
  50. package/src/types.ts +481 -52
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { DeRecErrorCode } from './errors';
1
2
  export interface SecretStore {
2
3
  load(secretId: string, channelId: string, kind: 0 | 1 | 2): Promise<Uint8Array | null | undefined>;
3
4
  /**
@@ -128,9 +129,18 @@ export type ReplicaFilter = ChannelFilter<ReplicaRoleName>;
128
129
  * return new TextEncoder().encode(`[${inner.join(",")}]`);
129
130
  * ```
130
131
  *
131
- * Do not `JSON.parse` and re-serialise. Every id in these records is a `u64`,
132
+ * Do not re-serialise a parsed record. Every id in these records is a `u64`,
132
133
  * and `JSON.parse` silently rounds anything above 2^53 — the corruption only
133
134
  * appears once a real id happens to be large.
135
+ *
136
+ * A helper record's `weight` is the number of Shamir subshares that helper
137
+ * receives: the weight the last `start(ProtectSecret)` resolved for it, which
138
+ * every publish the library starts on its own reuses (1 for a new pairing and
139
+ * for a record written before the field existed). Parsing a stored helper
140
+ * record to read `Helper.weight` is safe and is the normal way to pre-fill
141
+ * the weights the user is shown: it is a small integer, and the channel id
142
+ * comes from the store call's `channelId` argument, never from the parsed
143
+ * object. Keep persisting the original bytes.
134
144
  */
135
145
  export interface ChannelStore {
136
146
  load(secretId: string, channelId: string, replicaId: string): Promise<Uint8Array | null | undefined>;
@@ -172,6 +182,24 @@ export interface Share {
172
182
  secretId: string;
173
183
  version: number;
174
184
  bytes: Uint8Array;
185
+ /**
186
+ * How many subshares the share carries, recorded on the owner side only:
187
+ * the library sets it on every share the owner saves (including a replica
188
+ * Destination's mirrored copies) and leaves it absent on a helper's share.
189
+ * Owner shares stored before the field existed read back absent; the
190
+ * library never backfills them. Persist it as given and return it
191
+ * unchanged. `null` and `undefined` both mean no count.
192
+ */
193
+ subshares?: number | null;
194
+ /**
195
+ * The threshold this version was split with, recorded on the owner side
196
+ * only, on the same shares as `subshares`: a later round may change the
197
+ * threshold, so this is the only record of the one an older version needs
198
+ * to be recoverable. Absent on a helper's share and on owner shares stored
199
+ * before the field existed. Persist it as given and return it unchanged.
200
+ * `null` and `undefined` both mean none.
201
+ */
202
+ threshold?: number | null;
175
203
  }
176
204
  /**
177
205
  * Share-record persistence.
@@ -188,6 +216,11 @@ export interface ShareStore {
188
216
  load(secretId: string, channelId: string, versions: number[]): Promise<Share[]>;
189
217
  loadMany(secretId: string, channelIds: string[], versions: number[]): Promise<Share[]>;
190
218
  loadAll(secretId: string, channelIds: string[]): Promise<Share[]>;
219
+ /**
220
+ * Reject, or throw, a {@link shareStoreRefusal} `Error` to refuse a share
221
+ * update on a helper channel; any other rejection is an ordinary backend
222
+ * failure.
223
+ */
191
224
  save(secretId: string, channelId: string, share: Share): Promise<void>;
192
225
  latestVersion(secretId: string): Promise<number | null>;
193
226
  removeChannel(secretId: string, channelId: string): Promise<void>;
@@ -196,30 +229,44 @@ export interface ShareStore {
196
229
  * `versions`. Idempotent: a version that is not stored is skipped, and
197
230
  * an empty array is a no-op.
198
231
  *
199
- * A helper calls this to apply `StoreShareRequestMessage.keepList`, the
200
- * complete set of versions the owner wants retained: every stored
201
- * version outside it is removed once the incoming share is persisted.
232
+ * A helper calls this to apply `StoreShareRequestMessage.keepVersion`:
233
+ * once the incoming share is persisted, every version stored on the
234
+ * channel other than the incoming one and `keepVersion` is removed.
202
235
  * Shares under other channels or partitions must be left untouched.
203
236
  */
204
237
  removeVersions(secretId: string, channelId: string, versions: number[]): Promise<void>;
205
238
  /**
206
- * Owner only: the versions every helper keeps after the owner distributes
207
- * `version`. Asked once per sharing round, before anything is sent,
208
- * including the rounds the library starts itself; the answer becomes
209
- * `keepList` for every helper.
239
+ * Owner only: the one older version every helper keeps after the owner
240
+ * distributes `version`. Asked once per sharing round, before anything is
241
+ * sent, including the rounds the library starts itself; the answer
242
+ * becomes `keepVersion` for every helper.
243
+ *
244
+ * Resolve to an integer in `0..=4294967295`. A helper that stores
245
+ * `version` keeps it and the returned version, and deletes every other
246
+ * version it holds; `0` keeps no other version. An answer equal to or
247
+ * greater than `version` fails the round with `invalid_keep_version`
248
+ * before anything is sent.
249
+ *
250
+ * Return the last valid version: the newest one whose `SharingComplete`
251
+ * reported `threshold_met`. A version whose round is still open is
252
+ * deleted from every helper that stores `version` unless it is the one
253
+ * returned here. Only the application knows which versions committed.
254
+ * When a helper confirms `version`, the library also removes this
255
+ * device's rows for that channel other than `version` and the returned
256
+ * version.
257
+ */
258
+ keepVersion(secretId: string, version: number): Promise<number>;
259
+ /**
260
+ * Record a verification challenge the helper is about to answer for
261
+ * `(secretId, channelId, version)`. Called once per accepted challenge,
262
+ * after the challenged share is found and before the helper answers.
210
263
  *
211
- * Return `null` or `undefined` to send no `keepList` (it goes out empty):
212
- * helpers then keep every version they hold. An app that wants to cap how
213
- * many versions helpers retain returns that cap here. A returned list is
214
- * used as is, plus `version`, which the library always adds. Helpers
215
- * delete every version that is not listed, so list every version that
216
- * could still become the latest: those that committed (for example, whose
217
- * `SharingComplete` reported `threshold_met`) and those whose round is
218
- * still open. Leave out only versions whose round failed or that the user
219
- * rolled back; a list that leaves out too much can make the secret
220
- * unrecoverable.
264
+ * Reject, or throw, a {@link shareStoreRefusal} `Error` to refuse the
265
+ * verification on a helper channel; the library answers the owner with
266
+ * the mapped status instead of the verification result. Any other
267
+ * rejection is an ordinary backend failure.
221
268
  */
222
- keepList(secretId: string, version: number): Promise<number[] | null | undefined>;
269
+ recordVerification(secretId: string, channelId: string, version: number): Promise<void>;
223
270
  }
224
271
  export interface UserSecretEntry {
225
272
  id: Uint8Array;
@@ -234,6 +281,14 @@ export interface UserSecrets {
234
281
  * when none is recorded. Store and return it unchanged: replica members
235
282
  * compare it to detect a conflicting copy of the same version. */
236
283
  author_replica_id?: string;
284
+ /** The threshold the next publish splits at unless
285
+ * `ProtectSecretParams.threshold` passes its own. Every publish writes the
286
+ * threshold it used, and `restore` and replica hydration write the
287
+ * recovered roster's. Absent on a snapshot written before the threshold
288
+ * was recorded, or restored from a roster that carries none; the
289
+ * builder's `withThreshold` then applies. Store and return it unchanged.
290
+ * `null` and `undefined` both mean none. */
291
+ threshold?: number | null;
237
292
  }
238
293
  /**
239
294
  * Persistence for the user-facing secret contents, keyed by `secretId`.
@@ -271,6 +326,17 @@ export interface StateStore {
271
326
  remove(secretId: string, keyJson: Uint8Array): Promise<boolean>;
272
327
  loadAll(secretId: string, kind: 0 | 1 | 2 | 3 | 4): Promise<Uint8Array[]>;
273
328
  }
329
+ /**
330
+ * The five stores `DeRecProtocolBuilder.build` requires, bundled for
331
+ * `DeRecProtocolBuilder.withStores`.
332
+ */
333
+ export interface StoresInit {
334
+ channelStore: ChannelStore;
335
+ shareStore: ShareStore;
336
+ secretStore: SecretStore;
337
+ userSecretStore: UserSecretStore;
338
+ stateStore: StateStore;
339
+ }
274
340
  /** A transport protocol, by name. */
275
341
  export type TransportProtocolName = "https" | "grpc";
276
342
  /**
@@ -357,6 +423,17 @@ export declare function protocol_version(): {
357
423
  * `DeRecProtocolBuilder.withReplicaId` on every init.
358
424
  */
359
425
  export declare function generate_replica_id(): bigint;
426
+ /** Reason a `ShareStore` gives for refusing a `save` or `recordVerification`
427
+ * call, carried on the `refusal` property of the `Error` {@link
428
+ * shareStoreRefusal} builds. The app picks the reason; the library alone
429
+ * decides the status and memo that reach the peer. */
430
+ export type ShareStoreRefusalReason = "too_frequent" | "size_limit_exceeded" | "rejected";
431
+ /**
432
+ * Builds the `Error` a `ShareStore`'s `save`/`recordVerification` throws, or
433
+ * rejects its promise with, to refuse the call: `reason` tagged onto the
434
+ * error's `refusal` property.
435
+ */
436
+ export declare function shareStoreRefusal(reason: ShareStoreRefusalReason): Error;
360
437
  /**
361
438
  * Builds a {@link Transport} that tries each endpoint in the order the peer
362
439
  * offered it and stops at the first success.
@@ -413,7 +490,10 @@ export declare enum SenderKind {
413
490
  * are ignored. A man-in-the-middle on the plaintext `PrePair` leg leaves
414
491
  * the two sides with different shared keys and so different fingerprints,
415
492
  * which is what the comparison catches — the role `contact_binding_hash`
416
- * plays for `HashedKeys`.
493
+ * plays for `HashedKeys`. The creator's `PrePair` response also carries a
494
+ * commitment to a nonce it reveals only in its `PairResponse`, and the
495
+ * channel key depends on that nonce, so whoever answers the scanner cannot
496
+ * search offline for keys that make the two fingerprints agree.
417
497
  */
418
498
  export declare enum ContactMode {
419
499
  InlineKeys = 0,
@@ -471,6 +551,14 @@ export interface ContactMessage {
471
551
  /** Present only when `contact_mode === ContactMode.HashedKeys`. SHA-384 digest (48 bytes). */
472
552
  contact_binding_hash?: Uint8Array;
473
553
  timestamp?: Timestamp;
554
+ /**
555
+ * Present only when `contact_mode === ContactMode.InlineKeys`. The
556
+ * creator's 32-byte commitment to the fingerprint nonce its
557
+ * `PairResponseMessage` reveals. For the other modes it arrives with the
558
+ * keys in the `PrePairResponseMessage`; a contact filled in from a
559
+ * `ProcessPrePairResult` must carry it alongside the two keys.
560
+ */
561
+ fingerprint_commitment?: Uint8Array;
474
562
  /** Every transport endpoint the creator of this contact can be reached
475
563
  * on, in its own preference order. */
476
564
  supported_transports: TransportProtocol[];
@@ -492,6 +580,60 @@ export interface DiscoveryParams {
492
580
  export interface ProtectSecretParams {
493
581
  secrets: UserSecret[];
494
582
  description?: string;
583
+ /**
584
+ * Channel ID to how many subshares that channel should hold. The map is the
585
+ * full assignment, not a patch: a paired helper absent from it holds one
586
+ * subshare. The resolved weights are stored on each helper's channel
587
+ * record. A key is a channel id as bigint or decimal string, e.g. the
588
+ * `channel_id` string from an event.
589
+ *
590
+ * Weights are the user's choice. Before `start`, show the user each paired
591
+ * helper's current weight, pre-filled from your `ChannelStore` (the stored
592
+ * `Helper.weight`, which is safe to read; see `ChannelStore`), and pass the
593
+ * user's final choice as the whole map. Reading the stored weights first is
594
+ * the normal flow. Every other `start(ProtectSecret)` the app makes, such
595
+ * as a republish after editing the secrets or the follow-up after
596
+ * `restore`, must pass the stored weights unchanged: a helper left out
597
+ * returns to 1, which silently undoes the user's choice. Only the
598
+ * publishes the library starts on its own (after pairing, after a
599
+ * fingerprint is confirmed) reuse the stored weights by themselves.
600
+ *
601
+ * A stored weight the round's threshold does not allow fails the
602
+ * publishes the library starts on its own with `invalid_weight`, before
603
+ * anything is sent: `process()` throws with the message's `events`
604
+ * (`PairingCompleted` included); `accept()` throws after the pairing has
605
+ * completed and its response was sent, without returning
606
+ * `PairingCompleted`; `verifyFingerprint()` throws after the promotion,
607
+ * which stands, and a retry resolves `true` and publishes nothing. Call
608
+ * `start` with `ProtectSecret` and valid weights to publish.
609
+ *
610
+ * When the paired helpers' resolved total is below the round's threshold
611
+ * (see `threshold`), `start` skips the split: no helper is sent anything, so no
612
+ * `ProtectSecretStarted` / `ProtectSecretFailed` comes back for one. A
613
+ * paired replica still gets its usual secret-only copy and events; with
614
+ * none paired, `start` resolves with no events at all — this is not an
615
+ * error. The version still advances and the secrets/description are
616
+ * saved as the latest snapshot, so the next publish that reaches
617
+ * threshold shares it. Sum the weights about to be assigned against the
618
+ * threshold beforehand, or count the returned helper events afterward,
619
+ * to detect the case.
620
+ */
621
+ weights?: Map<bigint | string, number>;
622
+ /**
623
+ * The threshold this round splits at: the number of distinct subshares
624
+ * that reconstruct this version. Absent keeps the stored threshold
625
+ * (`UserSecrets.threshold`), or the builder's `withThreshold` when none is
626
+ * stored; a value changes it, and the new threshold is stored with this
627
+ * round. Leave it absent on the follow-up after `restore`, which has
628
+ * already put the recovered threshold back. Publishes the library starts
629
+ * on its own always use the stored threshold.
630
+ *
631
+ * The resolved threshold must be at least 2 (`invalid_threshold`), above
632
+ * every resolved weight (`invalid_weight`), and no greater than a
633
+ * `withMaxSubshares` cap (`invalid_input`). Each check runs before
634
+ * anything is written or sent.
635
+ */
636
+ threshold?: number;
495
637
  }
496
638
  export interface VerifySharesParams {
497
639
  secretId: bigint | string;
@@ -605,6 +747,21 @@ export type DeRecEvent = {
605
747
  type: "ShareStored";
606
748
  channel_id: string;
607
749
  version: number;
750
+ }
751
+ /** The library refused an inbound share (helper side) instead of raising
752
+ * `ActionRequired`: nothing was stored, no stored version was removed, and
753
+ * the owner was answered with `status` and `memo`, which it observes as
754
+ * `ShareRejected`. `status` is FORMAT_ERROR (the share does not decode),
755
+ * VERIFICATION_FAILED (it does not verify against its commitment),
756
+ * SIZE_LIMIT_EXCEEDED (outside the helper's size range) or
757
+ * VERSION_CONFLICT (a different share is stored at `version`). A refusal
758
+ * from the application's own share store does not raise it. */
759
+ | {
760
+ type: "ShareRefused";
761
+ channel_id: string;
762
+ version: number;
763
+ status: StatusEnum;
764
+ memo: string;
608
765
  } | {
609
766
  type: "ShareConfirmed";
610
767
  channel_id: string;
@@ -700,10 +857,31 @@ export type DeRecEvent = {
700
857
  version: number;
701
858
  synced: string[];
702
859
  behind: string[];
703
- } | {
860
+ }
861
+ /** A helper's verification proof checked out (owner side). `subshares` is
862
+ * how many subshares this helper holds for `version`: the count on the
863
+ * owner's stored share for this channel and version, not the helper's
864
+ * current weight; absent when that share was stored before the count was
865
+ * recorded. `threshold` is the threshold `version` was split with, from
866
+ * the same owner share, not the current one; absent when that share was
867
+ * stored before the threshold was recorded. To judge whether a version is
868
+ * recoverable, sum `subshares` over its `ShareVerified` events and compare
869
+ * with their `threshold`. If either is absent on any of those events, the
870
+ * result is unknown: an absent count is not `0` or `1`. */
871
+ | {
704
872
  type: "ShareVerified";
705
873
  channel_id: string;
706
874
  version: number;
875
+ subshares?: number;
876
+ threshold?: number;
877
+ }
878
+ /** A helper answered a verification challenge, but its proof does not
879
+ * match the share this device stored. The challenge is spent; a new
880
+ * `VerifyShares` round challenges the helper again. */
881
+ | {
882
+ type: "ShareVerifyInvalid";
883
+ channel_id: string;
884
+ version: number;
707
885
  }
708
886
  /** A helper refused a verification challenge: its response carried a
709
887
  * non-OK `status` instead of a proof. The challenge is spent; a new
@@ -775,7 +953,12 @@ export type DeRecEvent = {
775
953
  /** Every endpoint this peer advertised, in the order it offered them. */
776
954
  transports: Endpoint[];
777
955
  shared_key: Uint8Array;
778
- communication_info?: Record<string, string>;
956
+ communication_info?: Record<string, string> | null;
957
+ /** Shamir subshares this helper received in the round that wrote the
958
+ * roster. `restore` writes it into the helper's channel record, so the
959
+ * next publish gives it the same count. Every event carries it;
960
+ * `restore` reads an absent value as 1. */
961
+ weight?: number;
779
962
  }>;
780
963
  secrets: Array<{
781
964
  id: Uint8Array;
@@ -797,10 +980,15 @@ export type DeRecEvent = {
797
980
  /** Every endpoint this peer advertised, in the order it offered them. */
798
981
  transports: Endpoint[];
799
982
  role: "Source" | "Destination";
800
- communication_info?: Record<string, string>;
983
+ communication_info?: Record<string, string> | null;
801
984
  }>;
802
985
  shared_key: Uint8Array;
803
986
  };
987
+ /** The threshold the secret was split with. Absent when the roster
988
+ * records none. `restore` writes it into the snapshot, so the next
989
+ * publish splits at the same threshold; to change it, pass
990
+ * `threshold` on `start(ProtectSecret)`. */
991
+ threshold?: number;
804
992
  };
805
993
  } | {
806
994
  type: "Unpaired";
@@ -820,6 +1008,31 @@ export type DeRecEvent = {
820
1008
  status: StatusEnum;
821
1009
  memo: string;
822
1010
  }
1011
+ /** The peer answered a message from this device with an
1012
+ * `ErrorResponseMessage`: it could not process it, typically because the
1013
+ * body could not be decoded (`FORMAT_ERROR`). Never answered. */
1014
+ | {
1015
+ type: "ErrorResponseReceived";
1016
+ channel_id: string;
1017
+ status: StatusEnum;
1018
+ memo: string;
1019
+ }
1020
+ /** This device refused an inbound `PairRequest` under a protocol rule
1021
+ * (`INCOMPATIBLE_PARAMETER_RANGE` or `REPLICA_ID_CONFLICT`), whatever the
1022
+ * auto-accept or `auto_respond_on_failure` setting, and sent the requester a
1023
+ * `PairResponse` carrying `status` and `memo`. No channel was created.
1024
+ * Returned by `process()`; it does not throw. `memo` is what the requester
1025
+ * reads, written from its side. `error_code` (`"incompatible_parameter_range"`
1026
+ * or `"replica_id_conflict"`) and `message` carry the local reason, as on
1027
+ * `RequestFailed`; `message` is never sent to the peer. */
1028
+ | {
1029
+ type: "PairingRefused";
1030
+ channel_id: string;
1031
+ status: StatusEnum;
1032
+ memo: string;
1033
+ error_code: DeRecErrorCode;
1034
+ message: string;
1035
+ }
823
1036
  /** Fires alongside `PairingCompleted` on replica-mode pair handshakes.
824
1037
  * `peer_replica_id` is the peer's `u64` as a **decimal** string,
825
1038
  * matching the wire `derec.replica_id` representation and every other
@@ -858,6 +1071,11 @@ export type DeRecEvent = {
858
1071
  transports: Endpoint[];
859
1072
  shared_key: Uint8Array;
860
1073
  communication_info?: Record<string, string>;
1074
+ /** Shamir subshares this helper received in the round that wrote the
1075
+ * roster. `restore` writes it into the helper's channel record, so the
1076
+ * next publish gives it the same count. Every event carries it;
1077
+ * `restore` reads an absent value as 1. */
1078
+ weight?: number;
861
1079
  }>;
862
1080
  secrets: Array<{
863
1081
  id: Uint8Array;
@@ -881,6 +1099,11 @@ export type DeRecEvent = {
881
1099
  }>;
882
1100
  shared_key: Uint8Array;
883
1101
  };
1102
+ /** The threshold the secret was split with. Absent when the roster
1103
+ * records none. `restore` writes it into the snapshot, so the next
1104
+ * publish splits at the same threshold; to change it, pass
1105
+ * `threshold` on `start(ProtectSecret)`. */
1106
+ threshold?: number;
884
1107
  };
885
1108
  shares: Array<{
886
1109
  channel_id: string;
@@ -910,6 +1133,11 @@ export type DeRecEvent = {
910
1133
  transports: Endpoint[];
911
1134
  shared_key: Uint8Array;
912
1135
  communication_info?: Record<string, string>;
1136
+ /** Shamir subshares this helper received in the round that wrote the
1137
+ * roster. `restore` writes it into the helper's channel record, so the
1138
+ * next publish gives it the same count. Every event carries it;
1139
+ * `restore` reads an absent value as 1. */
1140
+ weight?: number;
913
1141
  }>;
914
1142
  secrets: Array<{
915
1143
  id: Uint8Array;
@@ -933,6 +1161,11 @@ export type DeRecEvent = {
933
1161
  }>;
934
1162
  shared_key: Uint8Array;
935
1163
  };
1164
+ /** The threshold the secret was split with. Absent when the roster
1165
+ * records none. `restore` writes it into the snapshot, so the next
1166
+ * publish splits at the same threshold; to change it, pass
1167
+ * `threshold` on `start(ProtectSecret)`. */
1168
+ threshold?: number;
936
1169
  };
937
1170
  shares: Array<{
938
1171
  channel_id: string;
@@ -968,6 +1201,11 @@ export type DeRecEvent = {
968
1201
  transports: Endpoint[];
969
1202
  shared_key: Uint8Array;
970
1203
  communication_info?: Record<string, string>;
1204
+ /** Shamir subshares this helper received in the round that wrote the
1205
+ * roster. `restore` writes it into the helper's channel record, so the
1206
+ * next publish gives it the same count. Every event carries it;
1207
+ * `restore` reads an absent value as 1. */
1208
+ weight?: number;
971
1209
  }>;
972
1210
  secrets: Array<{
973
1211
  id: Uint8Array;
@@ -990,8 +1228,28 @@ export type DeRecEvent = {
990
1228
  }>;
991
1229
  shared_key: Uint8Array;
992
1230
  };
1231
+ /** The threshold the secret was split with. Absent when the roster
1232
+ * records none. `restore` writes it into the snapshot, so the next
1233
+ * publish splits at the same threshold; to change it, pass
1234
+ * `threshold` on `start(ProtectSecret)`. */
1235
+ threshold?: number;
993
1236
  };
994
1237
  }
1238
+ /** This device's library refused a group member's copy of the secret:
1239
+ * `FORMAT_ERROR` (the payload does not decode or is not self-consistent)
1240
+ * or `VERIFICATION_FAILED` (a share map entry does not verify against its
1241
+ * commitment). Nothing was written. On a push the publisher was answered
1242
+ * with this `status` and `memo`, which it observes as
1243
+ * `ReplicaSyncRejected`; a copy served by a catch-up is not answered. */
1244
+ | {
1245
+ type: "ReplicaSecretRefused";
1246
+ channel_id: string;
1247
+ from_replica_id: string;
1248
+ secret_id: string;
1249
+ version: number;
1250
+ status: StatusEnum;
1251
+ memo: string;
1252
+ }
995
1253
  /** Peer's ack of a secret sync we sent. `status` is the `StatusEnum`
996
1254
  * integer (0 = Ok), `memo` is the peer's explanation. */
997
1255
  | {
@@ -1031,6 +1289,37 @@ export type DeRecEvent = {
1031
1289
  type: "AutoAccepted";
1032
1290
  channel_id: string;
1033
1291
  action_kind: PendingActionKind;
1292
+ }
1293
+ /** A request that arrived on an authenticated channel failed while it
1294
+ * was being handled. Every such request is owed exactly one response.
1295
+ * When the library sent it, `responded` is `true` and `action` is absent.
1296
+ * Otherwise `responded` is `false` and `action` is the same opaque token
1297
+ * `ActionRequired` carries: pass it to `reject` to send the response
1298
+ * (`accept` refuses it). `action` is also absent when `reject` could
1299
+ * never answer it: a request on a replica group channel that names no
1300
+ * member, which has no reply target, or, with `autoRespondOnFailure`
1301
+ * off, a replica copy that hands this device the group key and fails
1302
+ * before that key is stored. Resolve each token once: the library is
1303
+ * stateless, so rejecting the same token twice sends two responses.
1304
+ * `action_kind` is the type of the failed request, `"Failed"` when its
1305
+ * body could not be decoded or is a request the channel cannot carry.
1306
+ * `status` is the `StatusEnum` value that was sent, or would have been,
1307
+ * and `memo` the wire-safe memo sent with it, or that would have been:
1308
+ * `reject(action, status, memo)` sends exactly the response
1309
+ * `autoRespondOnFailure` would have sent. `message` is a local
1310
+ * description, never sent to the peer. Reported
1311
+ * instead of a thrown error from `process()` or `accept()`, so the call's
1312
+ * other events are kept. */
1313
+ | {
1314
+ type: "RequestFailed";
1315
+ channel_id: string;
1316
+ action_kind: PendingActionKind;
1317
+ responded: boolean;
1318
+ status: StatusEnum;
1319
+ memo: string;
1320
+ error_code: DeRecErrorCode;
1321
+ message: string;
1322
+ action?: Uint8Array;
1034
1323
  } | {
1035
1324
  type: "NoOp";
1036
1325
  }
@@ -1043,8 +1332,12 @@ export type DeRecEvent = {
1043
1332
  * Confirming does not replay it: after `verifyFingerprint` succeeds, a
1044
1333
  * replica destination calls `start(FlowKind.ReplicaDiscovery)` to pull
1045
1334
  * the copy itself. `"Expired"` means the message was older than the
1046
- * inbound timeout. `trace_id` matches the peer's `*Started` event for
1047
- * the same round (`"0"` when the sender set none). */
1335
+ * inbound timeout. `"TimestampMismatch"` means the envelope's timestamp
1336
+ * and the encrypted inner message's timestamp differ, or either is
1337
+ * absent. `"StaleResponse"` means a `VerifyShareResponse` answered an
1338
+ * earlier challenge that a newer one on the channel has replaced; the
1339
+ * outstanding challenge is kept. `trace_id` matches the peer's
1340
+ * `*Started` event for the same round (`"0"` when the sender set none). */
1048
1341
  | {
1049
1342
  type: "MessageIgnored";
1050
1343
  channel_id: string;
@@ -1090,12 +1383,16 @@ export type DeRecEvent = {
1090
1383
  error: string;
1091
1384
  }
1092
1385
  /** A share-storage request was dispatched to `channel_id`. Emitted per
1093
- * targeted peer by `start(ProtectSecret)`. */
1386
+ * targeted peer by `start(ProtectSecret)`. `weight` is the number of
1387
+ * subshares sent, which is also the weight now stored for this channel:
1388
+ * its `ProtectSecretParams.weights` entry, 1 when the map does not name
1389
+ * it, or the stored weight on a publish the library starts on its own. */
1094
1390
  | {
1095
1391
  type: "ProtectSecretStarted";
1096
1392
  channel_id: string;
1097
1393
  version: number;
1098
1394
  trace_id: string;
1395
+ weight: number;
1099
1396
  }
1100
1397
  /** A share-storage request could not be dispatched to `channel_id`. */
1101
1398
  | {
@@ -1103,6 +1400,9 @@ export type DeRecEvent = {
1103
1400
  channel_id: string;
1104
1401
  version: number;
1105
1402
  error: string;
1403
+ /** Number of subshares that were to be sent to this channel; see
1404
+ * `ProtectSecretStarted.weight`. */
1405
+ weight: number;
1106
1406
  }
1107
1407
  /** A verify-share challenge was dispatched to `channel_id`. */
1108
1408
  | {
@@ -1111,11 +1411,12 @@ export type DeRecEvent = {
1111
1411
  version: number;
1112
1412
  trace_id: string;
1113
1413
  }
1114
- /** A verify-share challenge could not be dispatched to `channel_id`. */
1414
+ /** Verification reached no verdict for `channel_id`. `reason` says why. */
1115
1415
  | {
1116
1416
  type: "VerifySharesFailed";
1117
1417
  channel_id: string;
1118
1418
  version: number;
1419
+ reason: VerifyFailureReason;
1119
1420
  error: string;
1120
1421
  }
1121
1422
  /** A recovery share request was dispatched to `channel_id`. */
@@ -1132,9 +1433,13 @@ export type DeRecEvent = {
1132
1433
  version: number;
1133
1434
  error: string;
1134
1435
  }
1135
- /** An unpair request was dispatched to `channel_id`. Followed by an
1136
- * `Unpaired` event once the peer acknowledges (or in the same event
1137
- * vec, under `UnpairAck.NotRequired`). */
1436
+ /** An unpair request was dispatched to `channel_id`, by
1437
+ * `start(Unpair)` or by `restore` for each recovery channel. Under
1438
+ * `UnpairAck.Required` the channel is now `"Unpairing"`, and an
1439
+ * `Unpaired` event follows once the peer acknowledges or the
1440
+ * acknowledgement window elapses, or `UnpairRejected` if the peer
1441
+ * refuses (the channel is `"Paired"` again). Under
1442
+ * `UnpairAck.NotRequired`, `Unpaired` follows in the same event vec. */
1138
1443
  | {
1139
1444
  type: "UnpairFailed";
1140
1445
  channel_id: string;
@@ -1172,16 +1477,20 @@ export type DeRecEvent = {
1172
1477
  * oracle. Anyone who knows a HashedKeys contact's nonce can elicit a
1173
1478
  * key-publish response. Keep off unless you control both ends of
1174
1479
  * the transport.
1175
- * - `storeShare` — the helper's only admission-control point for
1176
- * inbound shares. The protocol enforces no size, quota or rate limit
1177
- * of its own, and `maxShareSize` is checked for range overlap at
1178
- * pairing time only, never against an actual share. While this is
1179
- * `false`, `ActionRequired` carries `share_size`, so the application
1180
- * can compare it to its quota and call `reject()` with
1181
- * `StatusEnum.SizeLimitExceeded`. Setting it `true` removes that
1182
- * opportunity entirely: every share from every paired Owner is stored
1183
- * unconditionally, at whatever size it arrives. Keep off in any
1184
- * deployment with per-user storage limits.
1480
+ * - `storeShare` — the helper's admission-control point for inbound
1481
+ * shares beyond what the library already checks: this device's own
1482
+ * configured `minShareSize`/`maxShareSize` is enforced against every
1483
+ * inbound share's actual length regardless of this flag, before
1484
+ * `ActionRequired` is ever considered — an out-of-range share is
1485
+ * refused with `StatusEnum.SizeLimitExceeded` either way, and this
1486
+ * flag has no say in it. While this is `false`, `ActionRequired`
1487
+ * carries the decoded request, which is where a tighter or per-user
1488
+ * size limit, storage quota, or any other app-specific decline
1489
+ * belongs — call `reject()` for that. Setting it `true` removes that
1490
+ * opportunity: every in-range share from every paired Owner is stored
1491
+ * unconditionally, unless the share store refuses it. Keep off in any
1492
+ * deployment with per-user storage limits tighter than this device's
1493
+ * own configured range.
1185
1494
  * - `unpair` — destructive. Accepting deletes the local channel
1186
1495
  * record before any UI confirmation.
1187
1496
  * - `updateChannelInfo` — silently overwrites the channel record with
@@ -1255,10 +1564,14 @@ export interface CommunicationInfo {
1255
1564
  }
1256
1565
  /** Why a `MessageIgnored` event dropped a message. Matches the Rust
1257
1566
  * `IgnoreReason` discriminants one-for-one. */
1258
- export type IgnoreReason = "PendingVerification" | "Expired";
1567
+ export type IgnoreReason = "PendingVerification" | "Expired" | "TimestampMismatch" | "StaleResponse";
1259
1568
  /** Why a `PeerNotRestored` event left a roster entry without a channel.
1260
1569
  * Matches the Rust `NotRestoredReason` discriminants one-for-one. */
1261
1570
  export type NotRestoredReason = "NoTransports";
1571
+ /** Why a `VerifySharesFailed` event reached no verdict for a channel.
1572
+ * Matches the Rust `VerifyFailureReason` discriminants
1573
+ * one-for-one. */
1574
+ export type VerifyFailureReason = "NoSharedKey" | "NoProof" | "DispatchFailed";
1262
1575
  /** Why a `RecoveryShareCorrupted` event set a helper's share aside.
1263
1576
  * Matches the Rust `CorruptionReason` discriminants one-for-one:
1264
1577
  * `Malformed` — no decodable share for the requested secret and version;
@@ -1267,12 +1580,13 @@ export type NotRestoredReason = "NoTransports";
1267
1580
  * disagrees with the shares the secret was rebuilt from. */
1268
1581
  export type CorruptionReason = "Malformed" | "InvalidProof" | "Inconsistent";
1269
1582
  /**
1270
- * The label vocabulary for `ActionRequired.action_kind` and
1271
- * `AutoAccepted.action_kind` — one value per pending-action kind the
1272
- * protocol can raise. Matches the Rust `PendingActionKind` discriminants
1273
- * one-for-one.
1583
+ * The label vocabulary for `ActionRequired.action_kind`,
1584
+ * `AutoAccepted.action_kind` and `RequestFailed.action_kind` — one value per
1585
+ * pending-action kind the protocol can raise, plus `"Failed"` for a request
1586
+ * whose body could not be decoded or that the channel cannot carry. Matches the Rust `PendingActionKind`
1587
+ * discriminants one-for-one.
1274
1588
  */
1275
- export type PendingActionKind = "Pairing" | "PrePair" | "StoreShare" | "VerifyShare" | "Discovery" | "GetShare" | "Unpair" | "UpdateChannelInfo";
1589
+ export type PendingActionKind = "Pairing" | "PrePair" | "StoreShare" | "VerifyShare" | "Discovery" | "GetShare" | "Unpair" | "UpdateChannelInfo" | "Failed";
1276
1590
  export interface ParameterRange {
1277
1591
  min_share_size: bigint;
1278
1592
  max_share_size: bigint;
@@ -1285,6 +1599,97 @@ export interface ParameterRange {
1285
1599
  min_unresponsive_deactivation_timeout: bigint;
1286
1600
  max_unresponsive_deactivation_timeout: bigint;
1287
1601
  }
1602
+ /**
1603
+ * `parameter_range` bounds inside `DeRecProtocolConfig`. Each bound is a
1604
+ * JSON number or a decimal string — the string form carries a bound past
1605
+ * `Number.MAX_SAFE_INTEGER` through a config file or a JSON encoder whose
1606
+ * numbers are `f64`. An absent bound is `0` ("no bound"), the same as
1607
+ * `DeRecProtocolBuilder.withParameterRange`.
1608
+ */
1609
+ export interface DeRecParameterRangeConfig {
1610
+ min_share_size?: number | string;
1611
+ max_share_size?: number | string;
1612
+ min_time_between_verifications?: number | string;
1613
+ max_time_between_verifications?: number | string;
1614
+ min_time_between_share_updates?: number | string;
1615
+ max_time_between_share_updates?: number | string;
1616
+ min_unresponsive_deletion_timeout?: number | string;
1617
+ max_unresponsive_deletion_timeout?: number | string;
1618
+ min_unresponsive_deactivation_timeout?: number | string;
1619
+ max_unresponsive_deactivation_timeout?: number | string;
1620
+ }
1621
+ /**
1622
+ * `auto_accept` inside `DeRecProtocolConfig`: the same eight flows as
1623
+ * {@link AutoAcceptPolicy}, named as the canonical JSON spells them rather
1624
+ * than in `camelCase`. An absent flow is `false`. See {@link AutoAcceptPolicy}
1625
+ * for per-field caveats.
1626
+ */
1627
+ export interface DeRecAutoAcceptConfig {
1628
+ pairing?: boolean;
1629
+ pre_pair?: boolean;
1630
+ store_share?: boolean;
1631
+ verify_share?: boolean;
1632
+ discovery?: boolean;
1633
+ get_share?: boolean;
1634
+ unpair?: boolean;
1635
+ update_channel_info?: boolean;
1636
+ }
1637
+ /**
1638
+ * Every data setting of `DeRecProtocolBuilder` in one serializable value, in
1639
+ * the canonical `snake_case` JSON shape every SDK accepts — the format for a
1640
+ * config file or a user record. Passed to `DeRecProtocolBuilder.withConfig`.
1641
+ *
1642
+ * Every field is optional: an absent or `null` field leaves that setting
1643
+ * unset. Stores, the delivery transport and the constructor's `secretId` are
1644
+ * not settings and are not carried here.
1645
+ *
1646
+ * Configs layer: apply a general config, then a per-user one, then any
1647
+ * individual `with*` call, and the last write wins per field —
1648
+ * `timeouts` merges per field too, so a config that sets only
1649
+ * `inbound_message_secs` keeps whatever `sharing_round_secs` an earlier
1650
+ * config or call set. Every other nested object (`parameter_range`,
1651
+ * `auto_accept`, `own_transports`) replaces as a whole.
1652
+ *
1653
+ * Values set through a field here are validated by `build()` exactly as the
1654
+ * matching `with*` setter's value would be: there is one rule set.
1655
+ *
1656
+ * ```json
1657
+ * {
1658
+ * "threshold": 3,
1659
+ * "max_subshares": "12",
1660
+ * "timeouts": { "inbound_message_secs": 120, "sharing_round_secs": 30 },
1661
+ * "parameter_range": { "min_share_size": 1, "max_share_size": 4096 },
1662
+ * "auto_accept": { "pairing": true, "store_share": true },
1663
+ * "auto_respond_on_failure": false,
1664
+ * "auto_reply_to": false,
1665
+ * "unpair_ack": "required",
1666
+ * "unsafe_connection": false,
1667
+ * "communication_info": { "name": "alice" },
1668
+ * "own_transports": [{ "uri": "https://me.example.com/derec", "protocol": "https" }],
1669
+ * "replica_id": "9007199254740993"
1670
+ * }
1671
+ * ```
1672
+ */
1673
+ export interface DeRecProtocolConfig {
1674
+ threshold?: number;
1675
+ /** Decimal string. A `bigint` is rejected; call
1676
+ * `DeRecProtocolBuilder.withMaxSubshares` directly for a `bigint`. */
1677
+ max_subshares?: string;
1678
+ timeouts?: Timeouts;
1679
+ parameter_range?: DeRecParameterRangeConfig;
1680
+ auto_accept?: DeRecAutoAcceptConfig;
1681
+ auto_respond_on_failure?: boolean;
1682
+ auto_reply_to?: boolean;
1683
+ /** Its name, or the discriminant `0` (`"required"`) / `1`
1684
+ * (`"not_required"`). */
1685
+ unpair_ack?: UnpairAck | 0 | 1;
1686
+ unsafe_connection?: boolean;
1687
+ communication_info?: Record<string, string>;
1688
+ own_transports?: Endpoint[];
1689
+ /** Decimal string. A `bigint` is rejected; call
1690
+ * `DeRecProtocolBuilder.withReplicaId` directly for a `bigint`. */
1691
+ replica_id?: string;
1692
+ }
1288
1693
  export interface PairRequestMessage {
1289
1694
  sender_kind: number;
1290
1695
  mlkem_ciphertext: Uint8Array;
@@ -1311,6 +1716,11 @@ export interface PairResponseMessage {
1311
1716
  * its own derivation. Zero on rejection (non-Ok `result.status`).
1312
1717
  */
1313
1718
  channel_id: bigint;
1719
+ /**
1720
+ * Present only when `result.status === Ok`. The 32-byte fingerprint nonce
1721
+ * the contact creator committed to in `fingerprint_commitment`.
1722
+ */
1723
+ fingerprint_nonce?: Uint8Array;
1314
1724
  }
1315
1725
  export interface PrePairRequestMessage {
1316
1726
  nonce: bigint;
@@ -1329,6 +1739,12 @@ export interface PrePairResponseMessage {
1329
1739
  ecies_public_key?: Uint8Array;
1330
1740
  nonce: bigint;
1331
1741
  timestamp?: Timestamp;
1742
+ /**
1743
+ * Present only when `result.status === Ok`. The creator's 32-byte
1744
+ * commitment to the fingerprint nonce its `PairResponseMessage` reveals,
1745
+ * covering the two keys above.
1746
+ */
1747
+ fingerprint_commitment?: Uint8Array;
1332
1748
  }
1333
1749
  export interface GetShareRequestMessage {
1334
1750
  secret_id: bigint;
@@ -1370,7 +1786,7 @@ export interface StoreShareRequestMessage {
1370
1786
  share: Uint8Array;
1371
1787
  share_algorithm: number;
1372
1788
  version: number;
1373
- keep_list: number[];
1789
+ keep_version: number;
1374
1790
  version_description: string;
1375
1791
  timestamp?: Timestamp;
1376
1792
  secret_id: bigint;
@@ -1488,7 +1904,7 @@ export interface PrePairResponseExtractResult {
1488
1904
  response: PrePairResponseMessage;
1489
1905
  }
1490
1906
  export interface ProcessPrePairResult {
1491
- /** Initiator's ML-KEM-768 encapsulation key, validated against the
1907
+ /** Initiator's ML-KEM-1024 encapsulation key, validated against the
1492
1908
  * contact's `contactBindingHash`. */
1493
1909
  mlkem_encapsulation_key: Uint8Array;
1494
1910
  /** Initiator's ECIES public key, validated against the contact's
@@ -1496,7 +1912,17 @@ export interface ProcessPrePairResult {
1496
1912
  ecies_public_key: Uint8Array;
1497
1913
  /** Nonce echoed from the original `ContactMessage`. */
1498
1914
  nonce: bigint;
1915
+ /** The creator's 32-byte commitment to its fingerprint nonce. Copy it into
1916
+ * the contact the pair request is built from, alongside the two keys. */
1917
+ fingerprint_commitment: Uint8Array;
1499
1918
  }
1919
+ /**
1920
+ * A channel paired with how many subshares it holds. A weight of `1` is an
1921
+ * ordinary helper; every weight must satisfy `1 <= weight < threshold`.
1922
+ * `channelId` is a channel id as bigint or decimal string, e.g. the
1923
+ * `channel_id` string from an event.
1924
+ */
1925
+ export type WeightedChannel = [channelId: bigint | string, weight: number];
1500
1926
  export interface SplitResult {
1501
1927
  /** Map keyed by channel id (`bigint`). */
1502
1928
  shares: Map<bigint, CommittedDeRecShare>;