@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/src/protocol.ts CHANGED
@@ -11,6 +11,7 @@ import type {
11
11
  ContactMessage,
12
12
  ContactMode,
13
13
  DeRecEvent,
14
+ DeRecProtocolConfig,
14
15
  Endpoint,
15
16
  DiscoveryParams,
16
17
  PairingParams,
@@ -31,6 +32,7 @@ import type {
31
32
  UpdateChannelInfoParams,
32
33
  UserSecretStore,
33
34
  VerifySharesParams,
35
+ StoresInit,
34
36
  } from './types';
35
37
 
36
38
  /** Shape of the `__DeRec` host object as seen from this file: every member is
@@ -113,13 +115,23 @@ export function buildProtectSecretParams(params: ProtectSecretParams): Record<st
113
115
  if (params.description !== undefined) {
114
116
  out.description = params.description;
115
117
  }
118
+ if (params.weights != null && params.weights.size > 0) {
119
+ const weights: Record<string, number> = {};
120
+ for (const [id, weight] of params.weights) {
121
+ weights[id.toString()] = weight;
122
+ }
123
+ out.weights = weights;
124
+ }
125
+ if (params.threshold != null) {
126
+ out.threshold = params.threshold;
127
+ }
116
128
  return out;
117
129
  }
118
130
 
119
131
  function communicationInfoOrOmit(
120
- info: Record<string, string> | undefined,
132
+ info: Record<string, string> | null | undefined,
121
133
  ): Record<string, string> | undefined {
122
- return info !== undefined && Object.keys(info).length > 0 ? info : undefined;
134
+ return info != null && Object.keys(info).length > 0 ? info : undefined;
123
135
  }
124
136
 
125
137
  /**
@@ -127,9 +139,12 @@ function communicationInfoOrOmit(
127
139
  * `SecretWire`'s own shape: `channel_id`/`replica_id` are the decimal `u64`
128
140
  * strings the `SecretRecovered` event already carries, forwarded verbatim;
129
141
  * `communication_info` is omitted per-entry (not sent as `{}`) whenever a
130
- * helper or replica member has none, matching the Go SDK's `omitempty` and
131
- * the shared golden fixture byte-for-byte; `replicas` is omitted entirely
132
- * when the recovered secret has no replica group.
142
+ * helper or replica member has none — `undefined`, `null`, or an empty map
143
+ * are all treated the same — matching the Go SDK's `omitempty` and the
144
+ * shared golden fixture byte-for-byte; `weight` is forwarded as given
145
+ * whenever a helper carries one; `replicas` is omitted entirely when the
146
+ * recovered secret has no replica group, and `threshold` when the roster
147
+ * records none.
133
148
  *
134
149
  * Exported so it can be golden-tested against the shared wire fixture
135
150
  * without a native module.
@@ -149,6 +164,9 @@ export function buildRestoreParams(
149
164
  if (info !== undefined) {
150
165
  out.communication_info = info;
151
166
  }
167
+ if (helper.weight !== undefined) {
168
+ out.weight = helper.weight;
169
+ }
152
170
  return out;
153
171
  }),
154
172
  secrets: recoveredSecret.secrets.map((secret) => ({
@@ -175,6 +193,9 @@ export function buildRestoreParams(
175
193
  shared_key: Array.from(recoveredSecret.replicas.shared_key),
176
194
  };
177
195
  }
196
+ if (recoveredSecret.threshold != null) {
197
+ recovered.threshold = recoveredSecret.threshold;
198
+ }
178
199
  return { version, recovered_secret: recovered };
179
200
  }
180
201
 
@@ -321,6 +342,20 @@ export class DeRecProtocolBuilder {
321
342
  return this;
322
343
  }
323
344
 
345
+ /**
346
+ * Set every store in one call. Forwards to {@link withChannelStore},
347
+ * {@link withShareStore}, {@link withSecretStore},
348
+ * {@link withUserSecretStore} and {@link withStateStore} in turn, so
349
+ * calling one of them again afterwards replaces just that store.
350
+ */
351
+ withStores(stores: StoresInit): this {
352
+ return this.withChannelStore(stores.channelStore)
353
+ .withShareStore(stores.shareStore)
354
+ .withSecretStore(stores.secretStore)
355
+ .withUserSecretStore(stores.userSecretStore)
356
+ .withStateStore(stores.stateStore);
357
+ }
358
+
324
359
  withTransport(transport: Transport): this {
325
360
  this.stores.transport = transport;
326
361
  return this;
@@ -346,7 +381,22 @@ export class DeRecProtocolBuilder {
346
381
  return this;
347
382
  }
348
383
 
349
- /** Default: 3. */
384
+ /**
385
+ * The starting threshold: the minimum number of subshares required to
386
+ * reconstruct the secret, for a secret with no threshold stored. Every
387
+ * publish stores the threshold it used (`UserSecrets.threshold`), the
388
+ * roster carries it, and `restore` and replica hydration put it back. A
389
+ * round splits at `ProtectSecretParams.threshold`, otherwise at the stored
390
+ * threshold, and only otherwise at this value, so changing it later does
391
+ * not change a stored threshold.
392
+ *
393
+ * A Helper holds one subshare unless `ProtectSecretParams.weights` has
394
+ * given it a weight, so with no weights the threshold is a number of
395
+ * Helpers, as before. Every weight must be below the round's threshold, so
396
+ * no single Helper can recover alone. When this value applies and is
397
+ * lowered to or below a stored weight, every publish fails with
398
+ * `invalid_weight` until `ProtectSecret` assigns valid weights. Default: 3.
399
+ */
350
400
  withThreshold(threshold: number): this {
351
401
  this.config.threshold = threshold;
352
402
  return this;
@@ -382,7 +432,12 @@ export class DeRecProtocolBuilder {
382
432
  return this;
383
433
  }
384
434
 
385
- /** Default: false. */
435
+ /**
436
+ * Who answers an inbound request whose handling failed. `true`: the
437
+ * library sends the response itself. `false`: nothing is sent, and the
438
+ * `RequestFailed` event carries a token to pass to `reject`. Default:
439
+ * false.
440
+ */
386
441
  withAutoRespondOnFailure(enabled: boolean): this {
387
442
  this.config.auto_respond_on_failure = enabled;
388
443
  return this;
@@ -436,6 +491,10 @@ export class DeRecProtocolBuilder {
436
491
  * applies its default.
437
492
  *
438
493
  * Default: unset — no constraints advertised, every peer range accepted.
494
+ *
495
+ * Share size is enforced by the helper; frequency limits are enforced by
496
+ * the app's share store (refuse from `save`/`recordVerification`); the
497
+ * deletion and deactivation timeouts are checked at pairing only.
439
498
  */
440
499
  withParameterRange(range: Partial<ParameterRange>): this {
441
500
  this.config.parameter_range = {
@@ -455,11 +514,89 @@ export class DeRecProtocolBuilder {
455
514
  return this;
456
515
  }
457
516
 
517
+ /**
518
+ * Cap the total number of subshares one sharing round may split the
519
+ * secret into — the sum of every helper's weight. Crosses to the library
520
+ * as a decimal string. Default: unset, no cap.
521
+ *
522
+ * Checked only by `start(ProtectSecret)`, including the publishes the
523
+ * library starts itself: a round over the cap fails with
524
+ * `too_many_subshares` before anything is sent. Recovery applies no cap.
525
+ * Recovering costs time that grows quadratically with the total number of
526
+ * subshares, so the cap bounds what a later recovery must do. A cap below
527
+ * the threshold makes `build()` throw.
528
+ */
529
+ withMaxSubshares(max: bigint | number): this {
530
+ this.config.max_subshares = BigInt(max).toString();
531
+ return this;
532
+ }
533
+
534
+ /**
535
+ * Layer a {@link DeRecProtocolConfig} onto the builder: a field the config
536
+ * sets (present and not `null`) replaces what the builder currently
537
+ * holds, a field it leaves unset is untouched, and `timeouts` merges per
538
+ * field against the builder's current timeouts rather than resetting the
539
+ * other three. Every other nested object replaces as a whole. Apply a
540
+ * general config, then a per-user one, then any individual `with*` call —
541
+ * the last write wins per field, same as two `withConfig` calls in a row.
542
+ *
543
+ * Every key is assigned verbatim into the same `snake_case` config JSON
544
+ * `build()` sends to `protocol_new`, whatever it is spelled as: nothing
545
+ * here validates, defaults, or recognizes field names. Rust does both —
546
+ * `deny_unknown_fields` is what turns a misspelled key into an error.
547
+ *
548
+ * `secret_id` is not a config field — it is the constructor argument —
549
+ * and `build()` always sends the constructor's value last, so a config
550
+ * (e.g. loaded from a per-user record) can never redirect which secret
551
+ * this instance manages even if it carries a `secret_id` key.
552
+ *
553
+ * A config parsed from JSON can carry an own `__proto__` key. Assigning
554
+ * it through plain bracket notation would hit the accessor
555
+ * `Object.prototype` defines for that name and replace this builder's
556
+ * config object's prototype instead of setting a field on it — the value
557
+ * would then vanish from the JSON payload instead of reaching Rust to be
558
+ * rejected as an unknown key. `Object.defineProperty` makes it a genuine
559
+ * own property, the same as every other key here.
560
+ */
561
+ withConfig(config: DeRecProtocolConfig): this {
562
+ for (const [key, value] of Object.entries(config as Record<string, unknown>)) {
563
+ if (value === undefined || value === null) {
564
+ continue;
565
+ }
566
+ if (key === 'timeouts') {
567
+ const held = (this.config.timeouts as Record<string, unknown> | undefined) ?? {};
568
+ const merged: Record<string, unknown> = { ...held };
569
+ for (const [timeoutKey, timeoutValue] of Object.entries(
570
+ value as Record<string, unknown>,
571
+ )) {
572
+ if (timeoutValue !== undefined && timeoutValue !== null) {
573
+ merged[timeoutKey] = timeoutValue;
574
+ }
575
+ }
576
+ this.config.timeouts = merged;
577
+ continue;
578
+ }
579
+ if (key === '__proto__') {
580
+ Object.defineProperty(this.config, key, {
581
+ value,
582
+ writable: true,
583
+ enumerable: true,
584
+ configurable: true,
585
+ });
586
+ continue;
587
+ }
588
+ this.config[key] = value;
589
+ }
590
+ return this;
591
+ }
592
+
458
593
  build(): DeRecProtocol {
459
594
  const config = {
460
- secret_id: this.secretIdValue.toString(),
461
595
  replica_id: null,
462
596
  ...this.config,
597
+ // Last, so a config's own `secret_id` key (it is not a config field;
598
+ // see `withConfig`) can never override the constructor's.
599
+ secret_id: this.secretIdValue.toString(),
463
600
  };
464
601
  const host = (getNative() as unknown as NativeHost).protocol_new(
465
602
  JSON.stringify(config),
@@ -591,6 +728,17 @@ export class DeRecProtocol {
591
728
  return decodeEvents(buffer);
592
729
  }
593
730
 
731
+ /**
732
+ * Accept a pending action from an `ActionRequired` event, returning the
733
+ * events it produces.
734
+ *
735
+ * When the action completes a pairing, the publish that follows reuses the
736
+ * stored helper weights. If one is no longer valid under the round's
737
+ * threshold, that publish fails with `invalid_weight` before anything is
738
+ * sent and this throws: the pairing has completed and its response was
739
+ * sent, but the `PairingCompleted` event is not returned. Call `start`
740
+ * with `ProtectSecret` and valid weights to publish.
741
+ */
594
742
  async accept(actionBytes: Uint8Array): Promise<DeRecEvent[]> {
595
743
  const buffer = (await this.host.accept(actionBytes)) as ArrayBuffer;
596
744
  return decodeEvents(buffer);
@@ -606,16 +754,27 @@ export class DeRecProtocol {
606
754
 
607
755
  /**
608
756
  * Verify `fingerprint` against the channel's locally-derived one. On
609
- * match, the channel transitions from `Pending` to `Paired`. Returns
610
- * `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.
611
763
  *
612
764
  * On a replica destination, confirming is also the decision to adopt the
613
765
  * group's vault: the source's publish is then installed as it arrives,
614
766
  * with no further prompt. Ask the user before calling this; to decline,
615
- * never confirm.
767
+ * never confirm: expired-channel cleanup removes the channel and its key.
768
+ *
769
+ * On a promotion the current snapshot is published to the promoted peer,
770
+ * reusing the stored helper weights. If one is no longer valid under the
771
+ * current threshold, that publish fails with `invalid_weight` before
772
+ * anything is sent and this throws. The promotion stands: calling this
773
+ * again resolves and publishes nothing, so call `start` with
774
+ * `ProtectSecret` and valid weights to reach the promoted peer.
616
775
  */
617
- async verifyFingerprint(channelId: bigint | number, fingerprint: string): Promise<boolean> {
618
- 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);
619
778
  }
620
779
 
621
780
  async removeExpiredChannels(olderThanSecs: number): Promise<string[]> {
@@ -633,6 +792,9 @@ export class DeRecProtocol {
633
792
  * reported as a `PeerNotRestored` event in the returned array and the rest
634
793
  * of the roster is restored.
635
794
  *
795
+ * A helper's or member's `communication_info` may be absent or `null`;
796
+ * both mean the same thing — no entries.
797
+ *
636
798
  * Rejects with a `DeRecError`; among its codes:
637
799
  *
638
800
  * | code | meaning |