@derec-alliance/react-native 0.0.9 → 0.0.11

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 (47) hide show
  1. package/README.md +114 -7
  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/Primitives.cpp +38 -9
  6. package/cpp/Primitives.h +5 -3
  7. package/cpp/ProtocolHost.cpp +4 -5
  8. package/cpp/StoreCallbacks.cpp +31 -43
  9. package/cpp/derec_ffi.h +98 -33
  10. package/ios/DeRecFFI.xcframework/ios-arm64/Headers/derec_ffi.h +98 -33
  11. package/ios/DeRecFFI.xcframework/ios-arm64/libderec_library.a +0 -0
  12. package/ios/DeRecFFI.xcframework/ios-arm64_x86_64-simulator/Headers/derec_ffi.h +98 -33
  13. package/ios/DeRecFFI.xcframework/ios-arm64_x86_64-simulator/libderec_library.a +2 -2
  14. package/lib/errors.d.ts +8 -3
  15. package/lib/errors.js +6 -0
  16. package/lib/messages.js +5 -3
  17. package/lib/primitives.d.ts +12 -2
  18. package/lib/primitives.js +17 -2
  19. package/lib/protocol.d.ts +9 -5
  20. package/lib/protocol.js +9 -5
  21. package/lib/types.d.ts +194 -46
  22. package/lib/types.js +4 -1
  23. package/package.json +4 -17
  24. package/schema/derec_descriptor.bin +0 -0
  25. package/schema/proto/committedderecshare.proto +93 -0
  26. package/schema/proto/communicationinfo.proto +104 -0
  27. package/schema/proto/contact.proto +266 -0
  28. package/schema/proto/derecmessage.proto +248 -0
  29. package/schema/proto/derecsecret.proto +163 -0
  30. package/schema/proto/derectransport.proto +38 -0
  31. package/schema/proto/error.proto +68 -0
  32. package/schema/proto/getshare.proto +184 -0
  33. package/schema/proto/pair.proto +284 -0
  34. package/schema/proto/parameterrange.proto +36 -0
  35. package/schema/proto/prepair.proto +164 -0
  36. package/schema/proto/result.proto +194 -0
  37. package/schema/proto/secretidsversions.proto +199 -0
  38. package/schema/proto/storeshare.proto +242 -0
  39. package/schema/proto/transportprotocol.proto +112 -0
  40. package/schema/proto/unpair.proto +132 -0
  41. package/schema/proto/updatechannelinfo.proto +100 -0
  42. package/schema/proto/verify.proto +160 -0
  43. package/src/errors.ts +12 -1
  44. package/src/messages.ts +5 -3
  45. package/src/primitives.ts +23 -2
  46. package/src/protocol.ts +10 -6
  47. package/src/types.ts +185 -43
package/src/types.ts CHANGED
@@ -299,9 +299,9 @@ export interface ShareStore {
299
299
  * `versions`. Idempotent: a version that is not stored is skipped, and
300
300
  * an empty array is a no-op.
301
301
  *
302
- * A helper calls this to apply `StoreShareRequestMessage.keepList`, the
303
- * complete set of versions the owner wants retained: every stored
304
- * version outside it is removed once the incoming share is persisted.
302
+ * A helper calls this to apply `StoreShareRequestMessage.keepVersion`:
303
+ * once the incoming share is persisted, every version stored on the
304
+ * channel other than the incoming one and `keepVersion` is removed.
305
305
  * Shares under other channels or partitions must be left untouched.
306
306
  */
307
307
  removeVersions(
@@ -310,26 +310,26 @@ export interface ShareStore {
310
310
  versions: number[],
311
311
  ): Promise<void>;
312
312
  /**
313
- * Owner only: the versions every helper keeps after the owner distributes
314
- * `version`. Asked once per sharing round, before anything is sent,
315
- * including the rounds the library starts itself; the answer becomes
316
- * `keepList` for every helper.
313
+ * Owner only: the one older version every helper keeps after the owner
314
+ * distributes `version`. Asked once per sharing round, before anything is
315
+ * sent, including the rounds the library starts itself; the answer
316
+ * becomes `keepVersion` for every helper.
317
317
  *
318
- * Return `null` or `undefined` to send no `keepList` (it goes out empty):
319
- * helpers then keep every version they hold. An app that wants to cap how
320
- * many versions helpers retain returns that cap here. A returned list is
321
- * used as is, plus `version`, which the library always adds. Helpers
322
- * delete every version that is not listed, so list every version that
323
- * could still become the latest: those that committed (for example, whose
324
- * `SharingComplete` reported `threshold_met`) and those whose round is
325
- * still open. Leave out only versions whose round failed or that the user
326
- * rolled back; a list that leaves out too much can make the secret
327
- * unrecoverable.
318
+ * Resolve to an integer in `0..=4294967295`. A helper that stores
319
+ * `version` keeps it and the returned version, and deletes every other
320
+ * version it holds; `0` keeps no other version. An answer equal to or
321
+ * greater than `version` fails the round with `invalid_keep_version`
322
+ * before anything is sent.
323
+ *
324
+ * Return the last valid version: the newest one whose `SharingComplete`
325
+ * reported `threshold_met`. A version whose round is still open is
326
+ * deleted from every helper that stores `version` unless it is the one
327
+ * returned here. Only the application knows which versions committed.
328
+ * When a helper confirms `version`, the library also removes this
329
+ * device's rows for that channel other than `version` and the returned
330
+ * version.
328
331
  */
329
- keepList(
330
- secretId: string,
331
- version: number,
332
- ): Promise<number[] | null | undefined>;
332
+ keepVersion(secretId: string, version: number): Promise<number>;
333
333
  /**
334
334
  * Record a verification challenge the helper is about to answer for
335
335
  * `(secretId, channelId, version)`. Called once per accepted challenge,
@@ -633,7 +633,10 @@ export enum SenderKind {
633
633
  * are ignored. A man-in-the-middle on the plaintext `PrePair` leg leaves
634
634
  * the two sides with different shared keys and so different fingerprints,
635
635
  * which is what the comparison catches — the role `contact_binding_hash`
636
- * plays for `HashedKeys`.
636
+ * plays for `HashedKeys`. The creator's `PrePair` response also carries a
637
+ * commitment to a nonce it reveals only in its `PairResponse`, and the
638
+ * channel key depends on that nonce, so whoever answers the scanner cannot
639
+ * search offline for keys that make the two fingerprints agree.
637
640
  */
638
641
  export enum ContactMode {
639
642
  InlineKeys = 0,
@@ -695,6 +698,14 @@ export interface ContactMessage {
695
698
  /** Present only when `contact_mode === ContactMode.HashedKeys`. SHA-384 digest (48 bytes). */
696
699
  contact_binding_hash?: Uint8Array;
697
700
  timestamp?: Timestamp;
701
+ /**
702
+ * Present only when `contact_mode === ContactMode.InlineKeys`. The
703
+ * creator's 32-byte commitment to the fingerprint nonce its
704
+ * `PairResponseMessage` reveals. For the other modes it arrives with the
705
+ * keys in the `PrePairResponseMessage`; a contact filled in from a
706
+ * `ProcessPrePairResult` must carry it alongside the two keys.
707
+ */
708
+ fingerprint_commitment?: Uint8Array;
698
709
  /** Every transport endpoint the creator of this contact can be reached
699
710
  * on, in its own preference order. */
700
711
  supported_transports: TransportProtocol[];
@@ -822,6 +833,24 @@ export interface Timeouts {
822
833
  /** How long to wait for an unpair acknowledgement before dropping local
823
834
  * channel state anyway. Library default: 60. */
824
835
  unpair_ack_secs?: number;
836
+ /** How long a recovery waits for a helper to answer its share request. A
837
+ * recovery decides only once every helper it asked has answered or this
838
+ * has elapsed since the requests went out; each silent helper is then
839
+ * reported as `RecoverSecretFailed` with error `"timeout"`. The budget is
840
+ * a **human** one when helpers approve share requests by hand. The same
841
+ * budget bounds a replica version check (`FlowKind.ReplicaDiscovery`):
842
+ * once it elapses, the check concludes on the versions already reported.
843
+ * Library default: 300. */
844
+ get_share_response_secs?: number;
845
+ /** How long a pairing stays possible: how old a contact may be when it is
846
+ * scanned or answered, and how old a pairing request may be when it is
847
+ * accepted. Both sides enforce it and nothing is sent on expiry: the
848
+ * scanner refuses an expired contact, and the creator ignores a request on
849
+ * one and refuses a late accept, with `pairing_expired`. The remedy is to
850
+ * scan or share a fresh contact. A scanner's `expired_channels` timeout
851
+ * should be at least this plus the time a response takes to arrive.
852
+ * Library default: 30. */
853
+ pairing_secs?: number;
825
854
  /** Removal of channels still awaiting out-of-band fingerprint
826
855
  * confirmation — every replica pairing, and every `NoKeys` pairing.
827
856
  * Unlike the others this can be disabled, leaving the sweep to the
@@ -843,7 +872,13 @@ export type ReplicaDiscoveryParams = Record<string, never>;
843
872
  * member is not asked and gets no event when told to leave: when a roster
844
873
  * excluding it arrives it drops its whole `secret_id` partition and emits
845
874
  * `SelfRemovedFromGroup`. The secret survives on the remaining members and
846
- * the helpers. */
875
+ * the helpers.
876
+ *
877
+ * Removal does not replace the group key, so it relies on the removed
878
+ * device's library deleting its copy. That covers a lost or stolen device
879
+ * whose storage was not read. Whoever read a device's storage keeps the
880
+ * group key and can decrypt every later publication it obtains, the one
881
+ * completing the removal included. */
847
882
  export interface UnpairReplicaParams {
848
883
  /** The member to remove, as a **decimal** `u64` string — the same form
849
884
  * `ReplicaPaired.peer_replica_id` hands back. A value naming no current
@@ -853,6 +888,26 @@ export interface UnpairReplicaParams {
853
888
  memo?: string;
854
889
  }
855
890
 
891
+ /** A helper named by a recovery decision (`RecoveryDisputed`,
892
+ * `RecoveryConflict`), so the app can tell the owner who is on each side.
893
+ * `channel_id` is the `u64` as a **decimal** string; `communication_info`
894
+ * is what the helper advertised when it paired, read from its channel
895
+ * record, and is absent when it advertised none. */
896
+ export interface RecoveryHelper {
897
+ channel_id: string;
898
+ communication_info?: Record<string, string>;
899
+ }
900
+
901
+ /** One secret a recovery reconstructed, reported in `RecoveryConflict`.
902
+ * `secret` has the shape of `SecretRecovered.secret`, so a confirmed
903
+ * candidate is passed to `restore` the same way. `supported_by` are the
904
+ * helpers whose shares reconstruct it, in the order their shares were
905
+ * collected. */
906
+ export interface RecoveryCandidate {
907
+ secret: Extract<DeRecEvent, { type: "SecretRecovered" }>["secret"];
908
+ supported_by: RecoveryHelper[];
909
+ }
910
+
856
911
  export type DeRecEvent =
857
912
  | {
858
913
  type: "PairingCompleted";
@@ -906,6 +961,11 @@ export type DeRecEvent =
906
961
  * from the application's own share store does not raise it. */
907
962
  | { type: "ShareRefused"; channel_id: string; version: number; status: StatusEnum; memo: string }
908
963
  | { type: "ShareConfirmed"; channel_id: string; version: number }
964
+ /** A helper rejected or failed to store our share. `status` and `memo`
965
+ * come from the helper's response, except for a helper that did not
966
+ * answer within `timeouts.sharing_round_secs`: `tick()` then reports it
967
+ * with status `FAIL` and memo `"timeout"`. The memo of a helper's own
968
+ * answer is free text and can read the same. */
909
969
  | { type: "ShareRejected"; channel_id: string; version: number; status: StatusEnum; memo: string }
910
970
  /** A publishing round finished — every targeted helper confirmed,
911
971
  * rejected, or timed out.
@@ -937,8 +997,12 @@ export type DeRecEvent =
937
997
  status: StatusEnum;
938
998
  memo: string;
939
999
  }
940
- /** A secret sync could not be delivered to a member at all — distinct from
941
- * `ReplicaSyncRejected`, which is the member answering "no". */
1000
+ /** A secret sync could not be delivered to a member, or the member did not
1001
+ * acknowledge it within `timeouts.sharing_round_secs` — distinct from
1002
+ * `ReplicaSyncRejected`, which is the member answering "no". `reason` is
1003
+ * `"timeout"` when the member did not acknowledge in time (reported by
1004
+ * `tick()`); otherwise the transport or encoding failure, rendered for
1005
+ * display. */
942
1006
  | { type: "ReplicaSyncFailed"; replica_id: string; version: number; reason: string }
943
1007
  /** A member left the group and its roster row was dropped. Fires on the
944
1008
  * members that remain. */
@@ -992,25 +1056,36 @@ export type DeRecEvent =
992
1056
 
993
1057
  secrets: Array<{ secret_id: string; versions: Array<{ version: number; description: string }> }>;
994
1058
  }
1059
+ /** A helper's share was collected for a recovery. Nothing is
1060
+ * reconstructed until every helper asked has answered or
1061
+ * `timeouts.get_share_response_secs` has passed since the requests went
1062
+ * out (each silent helper is then reported as `RecoverSecretFailed` with
1063
+ * error `"timeout"`). The recovery then decides exactly once:
1064
+ * `SecretRecovered`, `RecoveryDisputed`, `RecoveryConflict` or
1065
+ * `RecoveryIncomplete`. */
995
1066
  | { type: "RecoveryShareReceived"; channel_id: string; shares_received: number }
996
- | { type: "RecoveryShareError"; channel_id: string; shares_received: number; error: string }
997
1067
  /** A helper refused a recovery share request: its response carried a
998
1068
  * non-OK `status` (e.g. `UNKNOWN_SHARE_VERSION`) instead of a share. The
999
- * refusal is not collected — it does not count towards `shares_received`
1000
- * and the recovery stays open for the other helpers' shares — but it does
1001
- * answer that helper's `RecoverSecretStarted`. */
1069
+ * refusal is not collected and does not count towards `shares_received`,
1070
+ * but it is that helper's answer: it answers the helper's
1071
+ * `RecoverSecretStarted`, and when it is the last answer due the recovery
1072
+ * decides on the shares it holds. */
1002
1073
  | { type: "RecoveryShareRefused"; channel_id: string; version: number; status: StatusEnum; memo: string }
1003
1074
  /** A helper answered with a share that cannot be part of the secret;
1004
1075
  * `reason` says how it failed. `Malformed` and `InvalidProof` are judged
1005
- * on arrival; `Inconsistent` (valid on its own but disagreeing with the
1006
- * shares the secret was rebuilt from) is reported alongside
1007
- * `SecretRecovered`, once per helper. The share is set aside — it does
1008
- * not count towards `shares_received` and never blocks the recovery. An
1009
- * honest helper never sends one, so the app may treat it as a sign of a
1010
- * damaged or compromised helper, e.g. offer to unpair it. It also
1011
- * answers that helper's `RecoverSecretStarted`. */
1076
+ * on arrival and count as that helper's answer; `Forged` (its group
1077
+ * reconstructs under one key, but not to a secret, or a single share
1078
+ * reconstructs alone) is reported with the recovery's decision, once per
1079
+ * helper. The share is set aside — it does not count towards
1080
+ * `shares_received` and never blocks the recovery. An honest helper never
1081
+ * sends one, so the app may treat it as a sign of a damaged or
1082
+ * compromised helper, e.g. offer to unpair it. A share that only
1083
+ * disagrees with the others is never reported here: it is a dispute
1084
+ * (`RecoveryDisputed` / `RecoveryConflict`), not evidence against its
1085
+ * helper. It also answers that helper's `RecoverSecretStarted`. */
1012
1086
  | { type: "RecoveryShareCorrupted"; channel_id: string; version: number; reason: CorruptionReason }
1013
- /** Recovery completed — the typed `Secret` snapshot the owner
1087
+ /** Recovery completed — one secret reconstructed and every collected
1088
+ * share is part of it. `secret` is the typed `Secret` snapshot the owner
1014
1089
  * originally protected. Mirrors `ReplicaSecretReceived.secret`:
1015
1090
  * `secrets` is the user-facing `Vec<UserSecret>` the application
1016
1091
  * fed to `start(FlowKind.ProtectSecret)`; `helpers` and `replicas`
@@ -1062,6 +1137,47 @@ export type DeRecEvent =
1062
1137
  threshold?: number;
1063
1138
  };
1064
1139
  }
1140
+ /** A recovery reconstructed two or more different secrets for `version`,
1141
+ * so it cannot tell which is the owner's. Each candidate is a complete,
1142
+ * self-consistent secret, rebuilt from the shares of the helpers in its
1143
+ * `supported_by`. Fewer than the threshold of colluding helpers can forge
1144
+ * one, so the library never picks: involve the owner and pass the
1145
+ * candidate's `secret` they confirm to `restore`, exactly as the `secret`
1146
+ * of `SecretRecovered`. The helpers backing a rejected candidate are
1147
+ * suspect. `disputed_by` names the helpers holding valid shares of yet
1148
+ * another split that does not reconstruct, so no helper's answer goes
1149
+ * unshown. The recovery is closed when this is emitted. */
1150
+ | {
1151
+ type: "RecoveryConflict";
1152
+ version: number;
1153
+ candidates: RecoveryCandidate[];
1154
+ disputed_by: RecoveryHelper[];
1155
+ }
1156
+ /** A recovery reconstructed one secret for `version`, but other helpers
1157
+ * hold valid shares of a different split that does not reconstruct.
1158
+ * `secret` is complete and self-consistent, rebuilt from the shares of
1159
+ * `supported_by`, and has the shape of `SecretRecovered.secret`. The
1160
+ * shares of `disputed_by` each pass their own proof but belong to another
1161
+ * split at the same version, which an honest sharing never produces.
1162
+ * Either side can be the owner's: colluding helpers can forge a complete
1163
+ * split, and the honest helpers may be too few to reconstruct theirs. The
1164
+ * library never picks: show the owner what `secret` holds and who stands
1165
+ * on each side, and pass `secret` to `restore` only once they confirm it.
1166
+ * The recovery is closed when this is emitted. */
1167
+ | {
1168
+ type: "RecoveryDisputed";
1169
+ version: number;
1170
+ secret: Extract<DeRecEvent, { type: "SecretRecovered" }>["secret"];
1171
+ supported_by: RecoveryHelper[];
1172
+ disputed_by: RecoveryHelper[];
1173
+ }
1174
+ /** A recovery of `(secret_id, version)` closed without reconstructing a
1175
+ * secret: every helper asked has answered, or the rest timed out, and no
1176
+ * collected shares reconstruct. `secret_id` and `version` name the
1177
+ * recovery that closed, since recoveries of several secrets can be open at
1178
+ * once. `shares_received` is the number of helpers whose shares were
1179
+ * collected. Start `RecoverSecret` again to retry. */
1180
+ | { type: "RecoveryIncomplete"; secret_id: string; version: number; shares_received: number }
1065
1181
 
1066
1182
  | { type: "Unpaired"; channel_id: string }
1067
1183
 
@@ -1390,7 +1506,10 @@ export type DeRecEvent =
1390
1506
  * and the encrypted inner message's timestamp differ, or either is
1391
1507
  * absent. `"StaleResponse"` means a `VerifyShareResponse` answered an
1392
1508
  * earlier challenge that a newer one on the channel has replaced; the
1393
- * outstanding challenge is kept. `trace_id` matches the peer's
1509
+ * outstanding challenge is kept. `"Superseded"` means a
1510
+ * `StoreShareRequest` for a version older than the newest one this
1511
+ * helper stores on the channel, and not stored itself; it is not stored
1512
+ * and not answered. `trace_id` matches the peer's
1394
1513
  * `*Started` event for the same round (`"0"` when the sender set none). */
1395
1514
  | {
1396
1515
  type: "MessageIgnored";
@@ -1592,7 +1711,7 @@ export interface CommunicationInfo {
1592
1711
 
1593
1712
  /** Why a `MessageIgnored` event dropped a message. Matches the Rust
1594
1713
  * `IgnoreReason` discriminants one-for-one. */
1595
- export type IgnoreReason = "PendingVerification" | "Expired" | "TimestampMismatch" | "StaleResponse";
1714
+ export type IgnoreReason = "PendingVerification" | "Expired" | "TimestampMismatch" | "StaleResponse" | "Superseded";
1596
1715
 
1597
1716
  /** Why a `PeerNotRestored` event left a roster entry without a channel.
1598
1717
  * Matches the Rust `NotRestoredReason` discriminants one-for-one. */
@@ -1607,9 +1726,9 @@ export type VerifyFailureReason = "NoSharedKey" | "NoProof" | "DispatchFailed";
1607
1726
  * Matches the Rust `CorruptionReason` discriminants one-for-one:
1608
1727
  * `Malformed` — no decodable share for the requested secret and version;
1609
1728
  * `InvalidProof` — the share fails its own Merkle proof;
1610
- * `Inconsistent` — valid on its own, but its commitment root or ciphertext
1611
- * disagrees with the shares the secret was rebuilt from. */
1612
- export type CorruptionReason = "Malformed" | "InvalidProof" | "Inconsistent";
1729
+ * `Forged` — its group reconstructs under one key, but not to a secret, or
1730
+ * a single share reconstructs alone; an honest split produces neither. */
1731
+ export type CorruptionReason = "Malformed" | "InvalidProof" | "Forged";
1613
1732
 
1614
1733
  /**
1615
1734
  * The label vocabulary for `ActionRequired.action_kind`,
@@ -1763,6 +1882,11 @@ export interface PairResponseMessage {
1763
1882
  * its own derivation. Zero on rejection (non-Ok `result.status`).
1764
1883
  */
1765
1884
  channel_id: bigint;
1885
+ /**
1886
+ * Present only when `result.status === Ok`. The 32-byte fingerprint nonce
1887
+ * the contact creator committed to in `fingerprint_commitment`.
1888
+ */
1889
+ fingerprint_nonce?: Uint8Array;
1766
1890
  }
1767
1891
 
1768
1892
  export interface PrePairRequestMessage {
@@ -1783,6 +1907,12 @@ export interface PrePairResponseMessage {
1783
1907
  ecies_public_key?: Uint8Array;
1784
1908
  nonce: bigint;
1785
1909
  timestamp?: Timestamp;
1910
+ /**
1911
+ * Present only when `result.status === Ok`. The creator's 32-byte
1912
+ * commitment to the fingerprint nonce its `PairResponseMessage` reveals,
1913
+ * covering the two keys above.
1914
+ */
1915
+ fingerprint_commitment?: Uint8Array;
1786
1916
  }
1787
1917
 
1788
1918
  export interface GetShareRequestMessage {
@@ -1832,7 +1962,7 @@ export interface StoreShareRequestMessage {
1832
1962
  share: Uint8Array;
1833
1963
  share_algorithm: number;
1834
1964
  version: number;
1835
- keep_list: number[];
1965
+ keep_version: number;
1836
1966
  version_description: string;
1837
1967
  timestamp?: Timestamp;
1838
1968
  secret_id: bigint;
@@ -1989,6 +2119,10 @@ export interface ProcessPrePairResult {
1989
2119
 
1990
2120
  /** Nonce echoed from the original `ContactMessage`. */
1991
2121
  nonce: bigint;
2122
+
2123
+ /** The creator's 32-byte commitment to its fingerprint nonce. Copy it into
2124
+ * the contact the pair request is built from, alongside the two keys. */
2125
+ fingerprint_commitment: Uint8Array;
1992
2126
  }
1993
2127
 
1994
2128
  /**
@@ -2015,3 +2149,11 @@ export interface UnpairingProcessResult {
2015
2149
  export interface DiscoveryProcessResult {
2016
2150
  secret_list: SecretVersionEntry[];
2017
2151
  }
2152
+
2153
+ /** The identity of one delivered message; see `envelope.message_id`. */
2154
+ export interface MessageId {
2155
+ /** SHA-256 of the envelope's encrypted `message` field. */
2156
+ id: Uint8Array;
2157
+ /** The envelope's send time, in seconds since the Unix epoch. */
2158
+ timestamp: bigint;
2159
+ }