@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
@@ -0,0 +1,160 @@
1
+ /*
2
+ * Copyright (c) DeRec Alliance and its Contributors.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ syntax = "proto3";
7
+
8
+ import "google/protobuf/timestamp.proto";
9
+ import "result.proto";
10
+ import "transportprotocol.proto";
11
+
12
+ package org.derecalliance.derec.protobuf;
13
+
14
+
15
+ // VerifyShareRequestMessage initiates a verification challenge from the Owner
16
+ // to a Helper to confirm that the Helper still retains a specific share.
17
+ //
18
+ // # Context
19
+ //
20
+ // This message is part of the verification flow, which allows the Owner to:
21
+ //
22
+ // - periodically check Helper liveness
23
+ // - detect data loss or corruption of stored shares
24
+ // - identify Helpers that are no longer reliable for recovery
25
+ //
26
+ // # Semantics
27
+ //
28
+ // The Owner challenges the Helper by providing:
29
+ //
30
+ // - a `version` identifying the share to verify
31
+ // - a fresh `nonce` to ensure the response is unique and not replayable
32
+ //
33
+ // The Helper MUST compute a cryptographic hash over:
34
+ //
35
+ // committedDeRecShare || nonce
36
+ //
37
+ // and return the result in a VerifyShareResponseMessage.
38
+ //
39
+ // # Security Properties
40
+ //
41
+ // - The nonce ensures freshness and prevents replay attacks
42
+ // - The hash proves possession of the exact share bytes
43
+ // - The Helper cannot produce a valid response without holding the share
44
+ //
45
+ // # Idempotency
46
+ //
47
+ // Each request is uniquely identified by the nonce. Reusing a nonce SHOULD
48
+ // be avoided, as it weakens replay protection guarantees.
49
+ message VerifyShareRequestMessage {
50
+ // Secret id being challenged.
51
+ //
52
+ // This identifies which stored share for a secret the Helper must use to compute
53
+ // the verification proof.
54
+ uint64 secretId = 1;
55
+
56
+ // Share version being challenged.
57
+ //
58
+ // This identifies which stored share version the Helper must use to compute
59
+ // the verification proof.
60
+ uint32 version = 2;
61
+
62
+ // Random challenge nonce generated by the Owner.
63
+ //
64
+ // This value MUST be unpredictable and unique per request to ensure
65
+ // freshness and prevent replay of previous responses.
66
+ uint64 nonce = 3;
67
+
68
+ // Timestamp indicating when this message was created.
69
+ //
70
+ // This value is expressed in UTC and can be used for:
71
+ //
72
+ // - observability and logging
73
+ // - replay detection (in combination with sequence numbers)
74
+ // - timeout handling
75
+ google.protobuf.Timestamp timestamp = 4;
76
+
77
+ // Formerly the singular `replyTo`, superseded by `replyToTransports`.
78
+ reserved 5;
79
+ reserved "replyTo";
80
+
81
+ // Every ephemeral endpoint the requester wants this exchange's response
82
+ // delivered to, in its own preference order.
83
+ //
84
+ // See `StoreShareRequestMessage.replyToTransports` for full semantics.
85
+ repeated TransportProtocol replyToTransports = 6;
86
+ }
87
+
88
+ // VerifyShareResponseMessage contains the result of a verification challenge
89
+ // and, on success, provides a proof of share possession.
90
+ //
91
+ // # Semantics
92
+ //
93
+ // In response to a VerifyShareRequestMessage, the Helper:
94
+ //
95
+ // - retrieves the requested share version
96
+ // - computes the hash over (committedDeRecShare || nonce)
97
+ // - returns the result along with the echoed version and nonce
98
+ //
99
+ // The Owner verifies the response by recomputing the expected hash and
100
+ // comparing it with the received value.
101
+ //
102
+ // # Failure Handling
103
+ //
104
+ // If the verification fails (e.g., incorrect hash or error result), the Owner:
105
+ //
106
+ // - MAY retry the verification
107
+ // - MAY resend the correct share to the Helper
108
+ // - MAY mark the Helper as unreliable after repeated failures
109
+ //
110
+ // # Security Considerations
111
+ //
112
+ // - The hash MUST be computed using SHA-384 as defined by the protocol
113
+ // - The nonce MUST match the request to ensure the response is not replayed
114
+ // - The response MUST be sent over an authenticated and encrypted channel
115
+ //
116
+ // # Idempotency
117
+ //
118
+ // Responses are tied to a specific (version, nonce) pair. Replaying a response
119
+ // with a stale nonce SHOULD be rejected by the Owner.
120
+ message VerifyShareResponseMessage {
121
+ // Result of processing the verification request.
122
+ //
123
+ // Indicates whether the Helper successfully computed the proof or
124
+ // encountered an error (e.g., share not found).
125
+ DeRecResult result = 1;
126
+
127
+ // Secret id this response refers to.
128
+ //
129
+ // MUST match the version provided in the request.
130
+ uint64 secretId = 2;
131
+
132
+ // Share version this response refers to.
133
+ //
134
+ // MUST match the version provided in the request.
135
+ uint32 version = 3;
136
+
137
+ // Challenge nonce echoed from the request.
138
+ //
139
+ // MUST exactly match the nonce provided in the request.
140
+ uint64 nonce = 4;
141
+
142
+ // Verification proof.
143
+ //
144
+ // This is the SHA-384 hash of:
145
+ //
146
+ // committedDeRecShare || nonce
147
+ //
148
+ // where `committedDeRecShare` is the exact byte sequence originally
149
+ // stored by the Helper.
150
+ bytes hash = 5;
151
+
152
+ // Timestamp indicating when this message was created.
153
+ //
154
+ // This value is expressed in UTC and can be used for:
155
+ //
156
+ // - observability and logging
157
+ // - replay detection (in combination with sequence numbers)
158
+ // - timeout handling
159
+ google.protobuf.Timestamp timestamp = 6;
160
+ }
package/src/errors.ts CHANGED
@@ -32,6 +32,7 @@ export const DEREC_ERROR_CATEGORIES = [
32
32
  "protobuf",
33
33
  "invariant",
34
34
  "state_store",
35
+ "transport",
35
36
  ] as const;
36
37
 
37
38
  export type DeRecErrorCategory = (typeof DEREC_ERROR_CATEGORIES)[number];
@@ -62,6 +63,7 @@ export const DEREC_ERROR_CODES = [
62
63
  "already_restored",
63
64
  "restore_conflict",
64
65
  "replica_id_conflict",
66
+ "fingerprint_mismatch",
65
67
  "encryption",
66
68
  "keygen",
67
69
  "finish_pairing_initiator",
@@ -74,6 +76,8 @@ export const DEREC_ERROR_CODES = [
74
76
  "missing_replica_id",
75
77
  "unexpected_replica_id",
76
78
  "incompatible_parameter_range",
79
+ "fingerprint_commitment_mismatch",
80
+ "pairing_expired",
77
81
  "empty_channels",
78
82
  "duplicate_channel_id",
79
83
  "invalid_threshold",
@@ -81,6 +85,7 @@ export const DEREC_ERROR_CODES = [
81
85
  "vss_share_failed",
82
86
  "invalid_weight",
83
87
  "too_many_subshares",
88
+ "invalid_keep_version",
84
89
  "empty_responses",
85
90
  "empty_committed_derec_share",
86
91
  "decode_committed_derec_share",
@@ -97,6 +102,7 @@ export const DEREC_ERROR_CODES = [
97
102
  "ffi_nul_in_string",
98
103
  "transport_invalid",
99
104
  "no_usable_endpoint",
105
+ "transport_send_failed",
100
106
  ] as const;
101
107
 
102
108
  export type DeRecErrorCode = (typeof DEREC_ERROR_CODES)[number];
@@ -116,7 +122,12 @@ export interface DeRecError {
116
122
  * Clear exactly these and retry `restore`.
117
123
  */
118
124
  channel_ids?: string[];
119
- /** The channel the failing inbound message arrived on, when `process()` could tell. */
125
+ /**
126
+ * The channel the error is about: the one named by
127
+ * `"channel_already_paired"`, `"fingerprint_mismatch"` and
128
+ * `"role_mismatch"`, or, on a failed `process()`, the channel the failing
129
+ * inbound message arrived on.
130
+ */
120
131
  channel_id?: string;
121
132
  /**
122
133
  * On a failed `process()`: the events it produced before failing, such as
package/src/messages.ts CHANGED
@@ -26,9 +26,9 @@
26
26
  * surface.
27
27
  *
28
28
  * Both conversions are keyed by field name, matching the lists in
29
- * `message_json.rs`. A name-keyed walk is deliberate: `keep_list` is a
30
- * genuine `number[]` and must stay one, so a structural "array of numbers is
31
- * bytes" rule would corrupt it.
29
+ * `message_json.rs`. A name-keyed walk is deliberate: only the fields that
30
+ * file names change representation, so a structural "array of numbers is
31
+ * bytes" rule cannot corrupt a field that is not bytes.
32
32
  */
33
33
 
34
34
  import { jsonFromBytes, jsonToBytes } from './codec';
@@ -93,6 +93,8 @@ const BYTE_FIELDS = new Set([
93
93
  'mlkem_encapsulation_key',
94
94
  'ecies_public_key',
95
95
  'contact_binding_hash',
96
+ 'fingerprint_commitment',
97
+ 'fingerprint_nonce',
96
98
  'bytes_value',
97
99
  'share',
98
100
  'committed_de_rec_share',
package/src/primitives.ts CHANGED
@@ -44,6 +44,7 @@ import type {
44
44
  GetSecretIdsVersionsResponseMessage,
45
45
  GetShareRequestMessage,
46
46
  GetShareResponseMessage,
47
+ MessageId,
47
48
  PairRequestMessage,
48
49
  PairResponseMessage,
49
50
  PairingProcessResult,
@@ -460,11 +461,13 @@ const pairing = {
460
461
  mlkem_encapsulation_key: ArrayBuffer;
461
462
  ecies_public_key: ArrayBuffer;
462
463
  nonce: bigint;
464
+ fingerprint_commitment: ArrayBuffer;
463
465
  };
464
466
  return {
465
467
  mlkem_encapsulation_key: bytes(result.mlkem_encapsulation_key),
466
468
  ecies_public_key: bytes(result.ecies_public_key),
467
469
  nonce: result.nonce,
470
+ fingerprint_commitment: bytes(result.fingerprint_commitment),
468
471
  };
469
472
  },
470
473
 
@@ -495,11 +498,13 @@ const pairing = {
495
498
  mlkem_encapsulation_key: ArrayBuffer;
496
499
  ecies_public_key: ArrayBuffer;
497
500
  nonce: bigint;
501
+ fingerprint_commitment: ArrayBuffer;
498
502
  };
499
503
  return {
500
504
  mlkem_encapsulation_key: bytes(result.mlkem_encapsulation_key),
501
505
  ecies_public_key: bytes(result.ecies_public_key),
502
506
  nonce: result.nonce,
507
+ fingerprint_commitment: bytes(result.fingerprint_commitment),
503
508
  };
504
509
  },
505
510
  },
@@ -626,7 +631,7 @@ const sharing = {
626
631
  version: number,
627
632
  secret_id: bigint,
628
633
  committed_share: CommittedDeRecShare,
629
- keep_list: number[],
634
+ keep_version: number,
630
635
  description: string,
631
636
  shared_key: Uint8Array,
632
637
  reply_to?: TransportProtocol[],
@@ -637,7 +642,7 @@ const sharing = {
637
642
  version,
638
643
  secret_id,
639
644
  encodeMessage(MessageKind.CommittedDeRecShare, committed_share),
640
- keep_list,
645
+ keep_version,
641
646
  utf8Encode(description),
642
647
  shared_key,
643
648
  replyTo(reply_to),
@@ -891,4 +896,20 @@ export const envelope = {
891
896
  read_trace_id(envelope_bytes: Uint8Array): bigint {
892
897
  return call('read_trace_id_from_envelope', envelope_bytes) as bigint;
893
898
  },
899
+ /**
900
+ * Identify an envelope for replay and reflection detection. `id` is the
901
+ * same for every copy of one message, whatever its unauthenticated
902
+ * cleartext fields say, and different for every other message. Keep the
903
+ * `id` of every envelope sent and of every envelope `process` accepted
904
+ * until `max(received or sent at, timestamp) + inbound_message_secs`, keep
905
+ * the record across restarts, and drop an inbound envelope whose `id` is
906
+ * kept. The README has the full contract.
907
+ */
908
+ message_id(envelope_bytes: Uint8Array): MessageId {
909
+ const result = call('message_id_from_envelope', envelope_bytes) as {
910
+ id: ArrayBuffer;
911
+ timestamp: bigint;
912
+ };
913
+ return { id: bytes(result.id), timestamp: result.timestamp };
914
+ },
894
915
  };
package/src/protocol.ts CHANGED
@@ -754,23 +754,27 @@ export class DeRecProtocol {
754
754
 
755
755
  /**
756
756
  * Verify `fingerprint` against the channel's locally-derived one. On
757
- * match, the channel transitions from `Pending` to `Paired`. Returns
758
- * `true` on confirmation, `false` on mismatch.
757
+ * match, the channel transitions from `Pending` to `Paired`.
758
+ *
759
+ * A mismatch rejects with code `fingerprint_mismatch` — a possible
760
+ * man-in-the-middle. A `Pending` pairing is revoked: its records, channel
761
+ * key and pairing material are deleted, so pair again. A channel already
762
+ * `Paired` is left as it is.
759
763
  *
760
764
  * On a replica destination, confirming is also the decision to adopt the
761
765
  * group's vault: the source's publish is then installed as it arrives,
762
766
  * with no further prompt. Ask the user before calling this; to decline,
763
- * never confirm.
767
+ * never confirm: expired-channel cleanup removes the channel and its key.
764
768
  *
765
769
  * On a promotion the current snapshot is published to the promoted peer,
766
770
  * reusing the stored helper weights. If one is no longer valid under the
767
771
  * current threshold, that publish fails with `invalid_weight` before
768
772
  * anything is sent and this throws. The promotion stands: calling this
769
- * again resolves `true` and publishes nothing, so call `start` with
773
+ * again resolves and publishes nothing, so call `start` with
770
774
  * `ProtectSecret` and valid weights to reach the promoted peer.
771
775
  */
772
- async verifyFingerprint(channelId: bigint | number, fingerprint: string): Promise<boolean> {
773
- return (await this.host.verifyFingerprint(toBigInt(channelId), fingerprint)) as boolean;
776
+ async verifyFingerprint(channelId: bigint | number, fingerprint: string): Promise<void> {
777
+ await this.host.verifyFingerprint(toBigInt(channelId), fingerprint);
774
778
  }
775
779
 
776
780
  async removeExpiredChannels(olderThanSecs: number): Promise<string[]> {