openrtc 2.0.0-rc.29

OpenRTC: a Rust-first P2P runtime for device discovery, signaling, and iroh/QUIC networking.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
//! Async browser persistence companion for the upstream iroh-docs WASM actor.
//!
//! iroh-docs keeps its synchronous redb working set in memory in browsers. This
//! module hydrates that working set before it is exposed, then persists logical
//! mutations through a host-provided async IndexedDB adapter. It does not
//! implement redb's synchronous backend contract on top of IndexedDB.

use std::{
    collections::{HashMap, HashSet},
    sync::Mutex,
};

use anyhow::{anyhow, bail, Context, Result};
use futures::StreamExt;
use iroh_docs::{
    actor::{OpenOpts, SyncHandle},
    api::protocol::{AddrInfoOptions, ShareMode},
    engine::{DefaultAuthorStorage, Engine},
    protocol::Docs,
    store::{Query, Store},
    Author, AuthorId, Capability, ContentStatus, DocTicket, NamespaceId, SignedEntry,
};
use js_sys::{Array, Function, Promise, Reflect, Uint8Array};
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncReadExt, AsyncSeekExt};
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;

use crate::wasm_indexeddb_blob_store::IndexedDbBlobStore;

const EVENT_CAPACITY: usize = 256;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PersistedIdentity {
    default_author: Vec<u8>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PersistedAuthor {
    author_id: String,
    author: Vec<u8>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PersistedCapability {
    namespace_id: String,
    capability: Vec<u8>,
    capability_kind: String,
    generation: u64,
    share_revision: u64,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct PersistedEntry {
    namespace_id: String,
    signed_entry: Vec<u8>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ReplicaSnapshot {
    authors: Vec<PersistedAuthor>,
    namespaces: Vec<PersistedCapability>,
    entries: Vec<PersistedEntry>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct PersistentBlobManifest {
    pub(crate) content_hash: String,
    pub(crate) size: u64,
    pub(crate) chunk_count: u32,
    pub(crate) chunk_bytes: u32,
    pub(crate) bao_ready: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct PersistentBlobResumeState {
    pub(crate) expected_size: u64,
    pub(crate) chunk_bytes: u32,
    pub(crate) received_chunks: Vec<u32>,
    pub(crate) complete: bool,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ReplicaEntryRecord {
    record_id: String,
    namespace_id: String,
    author_id: String,
    key_hex: String,
    timestamp: u64,
    content_hash: String,
    content_length: u64,
    #[serde(with = "serde_bytes")]
    signed_entry: Vec<u8>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ReplicaOutbox {
    operation_id: String,
    #[serde(with = "serde_bytes")]
    payload: Vec<u8>,
    created_at: u64,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ReplicaMutation {
    entry: ReplicaEntryRecord,
    #[serde(skip_serializing_if = "Option::is_none")]
    outbox: Option<ReplicaOutbox>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct ReplicaCapabilityRecord {
    namespace_id: String,
    #[serde(with = "serde_bytes")]
    capability: Vec<u8>,
    capability_kind: &'static str,
    generation: u64,
    share_revision: u64,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct WasmNamespaceDescriptor {
    namespace_id: String,
    capability_kind: &'static str,
    #[serde(with = "serde_bytes")]
    capability: Vec<u8>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct WasmDocEntry {
    namespace_id: String,
    author_id: String,
    #[serde(with = "serde_bytes")]
    key: Vec<u8>,
    timestamp: u64,
    content_hash: String,
    content_length: u64,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct WasmMutationReceipt {
    content_hash: String,
    operation_id: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct WasmDeleteReceipt {
    removed: u32,
    operation_id: String,
}

#[derive(Clone)]
pub(crate) struct JsReplicaStore {
    inner: JsValue,
}

impl std::fmt::Debug for JsReplicaStore {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("JsReplicaStore")
            .finish_non_exhaustive()
    }
}

impl JsReplicaStore {
    pub(crate) fn new(inner: JsValue) -> Result<Self> {
        if inner.is_null() || inner.is_undefined() {
            bail!("iroh-docs IndexedDB adapter is required");
        }
        Ok(Self { inner })
    }

    async fn call(&self, method: &str, args: &[JsValue]) -> Result<JsValue> {
        let function = Reflect::get(&self.inner, &JsValue::from_str(method))
            .map_err(js_error)?
            .dyn_into::<Function>()
            .map_err(|_| anyhow!("IndexedDB adapter method {method} is missing"))?;
        let call_args = Array::new();
        for arg in args {
            call_args.push(arg);
        }
        let result = function.apply(&self.inner, &call_args).map_err(js_error)?;
        JsFuture::from(Promise::resolve(&result))
            .await
            .map_err(js_error)
            .with_context(|| format!("IndexedDB adapter method {method} failed"))
    }

    async fn identity(&self) -> Result<PersistedIdentity> {
        let value = self.call("loadIdentity", &[]).await?;
        if value.is_null() || value.is_undefined() {
            bail!("persistent iroh-docs identity is not initialized");
        }
        serde_wasm_bindgen::from_value(value).context("decoding persistent iroh-docs identity")
    }

    async fn snapshot(&self) -> Result<ReplicaSnapshot> {
        let value = self.call("exportSnapshot", &[]).await?;
        serde_wasm_bindgen::from_value(value).context("decoding persistent iroh-docs snapshot")
    }

    async fn import_author(&self, author: &Author) -> Result<()> {
        let bytes = Uint8Array::from(author.to_bytes().as_slice());
        self.call(
            "importAuthor",
            &[JsValue::from_str(&author.id().to_string()), bytes.into()],
        )
        .await?;
        Ok(())
    }

    async fn import_namespace(
        &self,
        capability: &Capability,
        generation: u64,
        share_revision: u64,
    ) -> Result<()> {
        let (kind, bytes) = capability.raw();
        let value = serde_wasm_bindgen::to_value(&ReplicaCapabilityRecord {
            namespace_id: capability.id().to_string(),
            capability: bytes.to_vec(),
            capability_kind: if kind == 1 { "write" } else { "read" },
            generation,
            share_revision,
        })
        .context("encoding iroh-docs capability")?;
        self.call("importNamespace", &[value]).await?;
        Ok(())
    }

    async fn delete_namespace(&self, namespace: NamespaceId) -> Result<()> {
        self.call(
            "deleteNamespace",
            &[JsValue::from_str(&namespace.to_string())],
        )
        .await?;
        Ok(())
    }

    async fn persist_entry(&self, entry: &SignedEntry, durable_outbox: bool) -> Result<String> {
        let signed_entry = postcard::to_stdvec(entry).context("encoding signed iroh-docs entry")?;
        let record_id = hex::encode(blake3::hash(&signed_entry).as_bytes());
        let mutation = ReplicaMutation {
            entry: ReplicaEntryRecord {
                record_id: record_id.clone(),
                namespace_id: entry.entry().namespace().to_string(),
                author_id: entry.author_bytes().to_string(),
                key_hex: hex::encode(entry.key()),
                timestamp: entry.timestamp(),
                content_hash: entry.content_hash().to_string(),
                content_length: entry.content_len(),
                signed_entry: signed_entry.clone(),
            },
            outbox: durable_outbox.then(|| ReplicaOutbox {
                operation_id: record_id.clone(),
                payload: signed_entry,
                created_at: (js_sys::Date::now().max(0.0)) as u64,
            }),
        };
        let value =
            serde_wasm_bindgen::to_value(&mutation).context("encoding iroh-docs mutation")?;
        self.call("applyEntryTransaction", &[value]).await?;
        Ok(record_id)
    }

    pub(crate) async fn flush(&self) -> Result<()> {
        self.call("flush", &[]).await?;
        Ok(())
    }

    async fn acknowledge_outbox(&self, operation_id: &str) -> Result<()> {
        self.call("acknowledgeOutbox", &[JsValue::from_str(operation_id)])
            .await?;
        Ok(())
    }

    pub(crate) async fn blob_manifests(&self) -> Result<Vec<PersistentBlobManifest>> {
        let value = self.call("listBlobManifests", &[]).await?;
        serde_wasm_bindgen::from_value(value).context("decoding persistent blob manifests")
    }

    pub(crate) async fn blob_resume_state(
        &self,
        content_hash: &str,
    ) -> Result<Option<PersistentBlobResumeState>> {
        let value = self
            .call("getBlobResumeState", &[JsValue::from_str(content_hash)])
            .await?;
        if value.is_null() || value.is_undefined() {
            return Ok(None);
        }
        serde_wasm_bindgen::from_value(value)
            .context("decoding persistent blob resume state")
            .map(Some)
    }

    pub(crate) async fn read_blob_range(
        &self,
        content_hash: &str,
        offset: u64,
        length: usize,
    ) -> Result<Option<Vec<u8>>> {
        ensure_js_safe_integer(offset, "persistent blob range offset")?;
        let value = self
            .call(
                "readBlobRange",
                &[
                    JsValue::from_str(content_hash),
                    JsValue::from_f64(offset as f64),
                    JsValue::from_f64(length as f64),
                ],
            )
            .await?;
        if value.is_null() || value.is_undefined() {
            return Ok(None);
        }
        Ok(Some(Uint8Array::new(&value).to_vec()))
    }

    pub(crate) async fn write_blob_range(
        &self,
        content_hash: &str,
        size: u64,
        chunk_bytes: usize,
        offset: u64,
        bytes: &[u8],
    ) -> Result<()> {
        ensure_js_safe_integer(size, "persistent blob size")?;
        ensure_js_safe_integer(offset, "persistent blob range offset")?;
        self.call(
            "writeBlobRange",
            &[
                JsValue::from_str(content_hash),
                JsValue::from_f64(size as f64),
                JsValue::from_f64(chunk_bytes as f64),
                JsValue::from_f64(offset as f64),
                Uint8Array::from(bytes).into(),
            ],
        )
        .await?;
        Ok(())
    }

    pub(crate) async fn finalize_blob_if_complete(&self, content_hash: &str) -> Result<bool> {
        self.call("finalizeBlobIfComplete", &[JsValue::from_str(content_hash)])
            .await?
            .as_bool()
            .ok_or_else(|| anyhow!("IndexedDB adapter returned an invalid blob completion result"))
    }

    pub(crate) async fn read_blob_outboard_node(
        &self,
        content_hash: &str,
        node_id: u64,
    ) -> Result<Option<[u8; 64]>> {
        let value = self
            .call(
                "readBlobOutboardNode",
                &[
                    JsValue::from_str(content_hash),
                    JsValue::from_str(&node_id.to_string()),
                ],
            )
            .await?;
        if value.is_null() || value.is_undefined() {
            return Ok(None);
        }
        let bytes = Uint8Array::new(&value).to_vec();
        Ok(Some(bytes.try_into().map_err(|_| {
            anyhow!("persistent Bao node must contain exactly 64 bytes")
        })?))
    }

    pub(crate) async fn write_blob_outboard_node(
        &self,
        content_hash: &str,
        node_id: u64,
        pair: &[u8; 64],
    ) -> Result<()> {
        self.call(
            "writeBlobOutboardNode",
            &[
                JsValue::from_str(content_hash),
                JsValue::from_str(&node_id.to_string()),
                Uint8Array::from(pair.as_slice()).into(),
            ],
        )
        .await?;
        Ok(())
    }

    pub(crate) async fn mark_blob_bao_ready(&self, content_hash: &str) -> Result<()> {
        self.call("markBlobBaoReady", &[JsValue::from_str(content_hash)])
            .await?;
        Ok(())
    }

    pub(crate) async fn write_blob_staging_chunk(
        &self,
        session_id: &str,
        chunk_index: u32,
        bytes: &[u8],
    ) -> Result<()> {
        self.call(
            "writeBlobStagingChunk",
            &[
                JsValue::from_str(session_id),
                JsValue::from_f64(f64::from(chunk_index)),
                Uint8Array::from(bytes).into(),
            ],
        )
        .await?;
        Ok(())
    }

    pub(crate) async fn finalize_blob_staging(
        &self,
        session_id: &str,
        content_hash: &str,
        size: u64,
        chunk_bytes: usize,
    ) -> Result<()> {
        ensure_js_safe_integer(size, "persistent staged blob size")?;
        self.call(
            "finalizeBlobStaging",
            &[
                JsValue::from_str(session_id),
                JsValue::from_str(content_hash),
                JsValue::from_f64(size as f64),
                JsValue::from_f64(chunk_bytes as f64),
            ],
        )
        .await?;
        Ok(())
    }

    pub(crate) async fn abort_blob_staging(&self, session_id: &str) -> Result<()> {
        self.call("abortBlobStaging", &[JsValue::from_str(session_id)])
            .await?;
        Ok(())
    }

    pub(crate) async fn delete_blob(&self, content_hash: &str, force: bool) -> Result<bool> {
        self.call(
            "deleteBlob",
            &[JsValue::from_str(content_hash), JsValue::from_bool(force)],
        )
        .await?
        .as_bool()
        .ok_or_else(|| anyhow!("IndexedDB adapter returned an invalid blob deletion result"))
    }

    pub(crate) async fn begin_blob_import(
        &self,
        content_hash: &str,
        size: u64,
        chunk_bytes: usize,
    ) -> Result<()> {
        ensure_js_safe_integer(size, "persistent blob size")?;
        self.call(
            "beginBlobImport",
            &[
                JsValue::from_str(content_hash),
                JsValue::from_f64(size as f64),
                JsValue::from_f64(chunk_bytes as f64),
            ],
        )
        .await?;
        Ok(())
    }

    pub(crate) async fn write_blob_chunk(
        &self,
        content_hash: &str,
        size: u64,
        chunk_bytes: usize,
        chunk_index: u32,
        bytes: &[u8],
    ) -> Result<()> {
        ensure_js_safe_integer(size, "persistent blob size")?;
        self.call(
            "writeBlobChunk",
            &[
                JsValue::from_str(content_hash),
                JsValue::from_f64(size as f64),
                JsValue::from_f64(chunk_bytes as f64),
                JsValue::from_f64(f64::from(chunk_index)),
                Uint8Array::from(bytes).into(),
            ],
        )
        .await?;
        Ok(())
    }

    pub(crate) async fn finalize_blob_import(
        &self,
        content_hash: &str,
        size: u64,
        chunk_bytes: usize,
    ) -> Result<()> {
        ensure_js_safe_integer(size, "persistent blob size")?;
        self.call(
            "finalizeBlobImport",
            &[
                JsValue::from_str(content_hash),
                JsValue::from_f64(size as f64),
                JsValue::from_f64(chunk_bytes as f64),
            ],
        )
        .await?;
        Ok(())
    }
}

/// Real upstream iroh-docs actor with an asynchronously persisted browser
/// working set.
pub(crate) struct WasmPersistentDocsActor {
    sync: SyncHandle,
    default_author: AuthorId,
    store: JsReplicaStore,
    capabilities: HashMap<NamespaceId, RuntimeCapabilityFence>,
    docs: Docs,
    blobs: iroh_blobs::BlobsProtocol,
    downloader: iroh_blobs::api::downloader::Downloader,
    blob_retry_after_ms: Mutex<HashMap<iroh_blobs::Hash, u64>>,
    gossip: iroh_gossip::net::Gossip,
}

#[derive(Debug, Clone, Copy)]
struct RuntimeCapabilityFence {
    writable: bool,
    generation: u64,
    share_revision: u64,
}

impl std::fmt::Debug for WasmPersistentDocsActor {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("WasmPersistentDocsActor")
            .field("namespaces", &self.capabilities.len())
            .finish_non_exhaustive()
    }
}

impl WasmPersistentDocsActor {
    pub(crate) async fn hydrate(store: JsReplicaStore, endpoint: iroh::Endpoint) -> Result<Self> {
        let identity = store.identity().await?;
        if identity.default_author.len() != 32 {
            bail!("persistent default iroh-docs author must be exactly 32 bytes");
        }
        let snapshot = store.snapshot().await?;
        let mut redb = Store::memory();

        let mut default_author_bytes = [0u8; 32];
        default_author_bytes.copy_from_slice(&identity.default_author);
        let persistent_default_author = Author::from_bytes(&default_author_bytes);
        let persistent_default_author_id = persistent_default_author.id();
        redb.import_author(persistent_default_author)?;

        for persisted in snapshot.authors {
            let bytes: [u8; 32] = persisted
                .author
                .try_into()
                .map_err(|_| anyhow!("persisted author {} is not 32 bytes", persisted.author_id))?;
            let author = Author::from_bytes(&bytes);
            if author.id().to_string() != persisted.author_id {
                bail!("persisted author id does not match its secret");
            }
            redb.import_author(author)?;
        }

        let mut capabilities = Vec::new();
        for persisted in snapshot.namespaces {
            let bytes: [u8; 32] = persisted.capability.clone().try_into().map_err(|_| {
                anyhow!(
                    "persisted namespace {} capability is not 32 bytes",
                    persisted.namespace_id
                )
            })?;
            let kind = match persisted.capability_kind.as_str() {
                "write" => 1,
                "read" => 2,
                other => bail!("unsupported persisted capability kind {other}"),
            };
            let capability = Capability::from_raw(kind, &bytes)?;
            if capability.id().to_string() != persisted.namespace_id {
                bail!("persisted namespace id does not match its capability");
            }
            redb.import_namespace(capability.clone())?;
            capabilities.push((capability, persisted.generation, persisted.share_revision));
        }

        let blobs_store = IndexedDbBlobStore::open(store.clone()).await?;
        let blobs_api = (*blobs_store).clone();
        let blobs = iroh_blobs::BlobsProtocol::new(&blobs_api, None);
        let gossip = iroh_gossip::net::Gossip::builder().spawn(endpoint.clone());
        let downloader = blobs_api.downloader(&endpoint);
        let engine = Engine::spawn(
            endpoint,
            gossip.clone(),
            redb,
            blobs_api,
            downloader.clone(),
            DefaultAuthorStorage::Mem,
            None,
        )
        .await?;
        let generated_default_author = engine.default_author.get();
        engine
            .default_author
            .set(persistent_default_author_id, &engine.sync)
            .await?;
        if generated_default_author != persistent_default_author_id {
            engine.sync.delete_author(generated_default_author).await?;
        }
        let sync = engine.sync.clone();
        let docs = Docs::new(engine);
        let mut runtime_capabilities = HashMap::new();
        for (capability, generation, share_revision) in &capabilities {
            sync.open(capability.id(), OpenOpts::default().sync())
                .await?;
            runtime_capabilities.insert(
                capability.id(),
                RuntimeCapabilityFence {
                    writable: capability_kind(capability) == "write",
                    generation: *generation,
                    share_revision: *share_revision,
                },
            );
        }
        for persisted in snapshot.entries {
            let namespace: NamespaceId = persisted
                .namespace_id
                .parse()
                .context("parsing persisted iroh-docs namespace")?;
            if !runtime_capabilities.contains_key(&namespace) {
                bail!("persisted entry references an unknown namespace");
            }
            let entry: SignedEntry = postcard::from_bytes(&persisted.signed_entry)
                .context("decoding persisted signed iroh-docs entry")?;
            sync.insert_remote(namespace, entry, [0u8; 32], ContentStatus::Complete)
                .await
                .or_else(ignore_newer_entry)?;
        }

        let actor = Self {
            sync,
            default_author: persistent_default_author_id,
            store,
            capabilities: runtime_capabilities,
            docs,
            blobs,
            downloader,
            blob_retry_after_ms: Mutex::new(HashMap::new()),
            gossip,
        };
        for namespace in actor.capabilities.keys().copied() {
            actor.subscribe_persistence(namespace).await?;
        }
        Ok(actor)
    }

    pub(crate) fn docs_protocol(&self) -> Docs {
        self.docs.clone()
    }

    pub(crate) fn blobs_protocol(&self) -> iroh_blobs::BlobsProtocol {
        self.blobs.clone()
    }

    pub(crate) fn gossip_protocol(&self) -> iroh_gossip::net::Gossip {
        self.gossip.clone()
    }

    async fn subscribe_persistence(&self, namespace: NamespaceId) -> Result<()> {
        let (sender, receiver) = async_channel::bounded(EVENT_CAPACITY);
        self.sync.subscribe(namespace, sender).await?;
        let store = self.store.clone();
        n0_future::task::spawn(async move {
            while let Ok(event) = receiver.recv().await {
                let (entry, local) = match event {
                    iroh_docs::Event::LocalInsert { entry, .. } => (entry, true),
                    iroh_docs::Event::RemoteInsert { entry, .. } => (entry, false),
                };
                if let Err(error) = store.persist_entry(&entry, local).await {
                    web_sys::console::error_1(&JsValue::from_str(&format!(
                        "[OpenRTC][iroh-docs][persistence] {error:#}"
                    )));
                    break;
                }
            }
        });
        Ok(())
    }

    pub(crate) async fn import_author(&self, bytes: Vec<u8>) -> Result<String> {
        let bytes: [u8; 32] = bytes
            .try_into()
            .map_err(|_| anyhow!("iroh-docs author must be exactly 32 bytes"))?;
        let author = Author::from_bytes(&bytes);
        let id = self.sync.import_author(author.clone()).await?;
        self.store.import_author(&author).await?;
        Ok(id.to_string())
    }

    pub(crate) async fn import_namespace(
        &mut self,
        kind: &str,
        bytes: Vec<u8>,
        generation: u64,
        share_revision: u64,
    ) -> Result<String> {
        let bytes: [u8; 32] = bytes
            .try_into()
            .map_err(|_| anyhow!("iroh-docs capability must be exactly 32 bytes"))?;
        let kind = match kind {
            "write" => 1,
            "read" => 2,
            other => bail!("unsupported iroh-docs capability kind {other}"),
        };
        let capability = Capability::from_raw(kind, &bytes)?;
        self.require_current_or_new_capability(
            capability.id(),
            kind == 1,
            generation,
            share_revision,
        )?;
        let namespace = self.sync.import_namespace(capability.clone()).await?;
        let is_new = !self.capabilities.contains_key(&namespace);
        if is_new {
            self.sync
                .open(namespace, OpenOpts::default().sync())
                .await?;
            self.subscribe_persistence(namespace).await?;
        }
        self.store
            .import_namespace(&capability, generation, share_revision)
            .await?;
        self.capabilities.insert(
            namespace,
            RuntimeCapabilityFence {
                writable: kind == 1,
                generation,
                share_revision,
            },
        );
        Ok(namespace.to_string())
    }

    pub(crate) async fn create_namespace(
        &mut self,
        generation: u64,
        share_revision: u64,
    ) -> Result<WasmNamespaceDescriptor> {
        let doc = self.docs.api().create().await?;
        let namespace = doc.id();
        let secret = self.sync.export_secret_key(namespace).await?;
        let capability = Capability::Write(secret);
        if !self.capabilities.contains_key(&namespace) {
            self.subscribe_persistence(namespace).await?;
        }
        self.store
            .import_namespace(&capability, generation, share_revision)
            .await?;
        self.capabilities.insert(
            namespace,
            RuntimeCapabilityFence {
                writable: true,
                generation,
                share_revision,
            },
        );
        Ok(namespace_descriptor(&capability))
    }

    pub(crate) async fn import_ticket(
        &mut self,
        ticket: &str,
        generation: u64,
        share_revision: u64,
    ) -> Result<String> {
        let ticket: DocTicket = ticket.parse().context("parsing iroh-docs ticket")?;
        let capability = ticket.capability.clone();
        let namespace = capability.id();
        let writable = capability_kind(&capability) == "write";
        self.require_current_or_new_capability(namespace, writable, generation, share_revision)?;
        self.docs.api().import(ticket).await?;
        if !self.capabilities.contains_key(&namespace) {
            self.subscribe_persistence(namespace).await?;
        }
        self.store
            .import_namespace(&capability, generation, share_revision)
            .await?;
        self.capabilities.insert(
            namespace,
            RuntimeCapabilityFence {
                writable,
                generation,
                share_revision,
            },
        );
        Ok(namespace.to_string())
    }

    pub(crate) async fn share(&self, namespace: &str, writable: bool) -> Result<String> {
        let namespace = parse_namespace(namespace)?;
        let fence = require_namespace(&self.capabilities, namespace)?;
        if writable && !fence.writable {
            bail!("iroh-docs namespace is read-only at the current capability generation");
        }
        let doc = self
            .docs
            .api()
            .open(namespace)
            .await?
            .ok_or_else(|| anyhow!("iroh-docs namespace is unavailable"))?;
        let ticket = doc
            .share(
                if writable {
                    ShareMode::Write
                } else {
                    ShareMode::Read
                },
                AddrInfoOptions::RelayAndAddresses,
            )
            .await?;
        Ok(ticket.to_string())
    }

    pub(crate) async fn remove_namespace(
        &mut self,
        namespace: &str,
        generation: u64,
        share_revision: u64,
    ) -> Result<()> {
        let namespace = parse_namespace(namespace)?;
        let current = require_namespace(&self.capabilities, namespace)?;
        if (generation, share_revision) < (current.generation, current.share_revision) {
            bail!("stale iroh-docs capability removal");
        }
        self.store.delete_namespace(namespace).await?;
        let drop_result = self.docs.api().drop_doc(namespace).await;
        self.capabilities.remove(&namespace);
        drop_result
    }

    pub(crate) async fn set_bytes(
        &self,
        namespace: &str,
        key: Vec<u8>,
        value: Vec<u8>,
    ) -> Result<WasmMutationReceipt> {
        let namespace = parse_namespace(namespace)?;
        require_writable_namespace(&self.capabilities, namespace)?;
        let doc = self
            .docs
            .api()
            .open(namespace)
            .await?
            .ok_or_else(|| anyhow!("iroh-docs namespace is unavailable"))?;
        let hash = doc
            .set_bytes(self.default_author, key.clone(), value)
            .await?;
        let entry = self
            .sync
            .get_exact(namespace, self.default_author, key.into(), true)
            .await?
            .ok_or_else(|| anyhow!("local iroh-docs entry was not committed"))?;
        let operation_id = self.store.persist_entry(&entry, true).await?;
        Ok(WasmMutationReceipt {
            content_hash: hash.to_string(),
            operation_id,
        })
    }

    pub(crate) async fn set_hash(
        &self,
        namespace: &str,
        key: Vec<u8>,
        hash: &str,
        size: u64,
    ) -> Result<String> {
        let namespace = parse_namespace(namespace)?;
        require_writable_namespace(&self.capabilities, namespace)?;
        let hash: iroh_blobs::Hash = hash.parse().context("parsing iroh-blob hash")?;
        match self.blobs.store().blobs().status(hash).await? {
            iroh_blobs::api::blobs::BlobStatus::Complete { size: actual } if actual == size => {}
            iroh_blobs::api::blobs::BlobStatus::Complete { size: actual } => {
                bail!("iroh-blob size mismatch: expected {size}, found {actual}");
            }
            _ => bail!("iroh-blob is not complete in the active browser store"),
        }
        let doc = self
            .docs
            .api()
            .open(namespace)
            .await?
            .ok_or_else(|| anyhow!("iroh-docs namespace is unavailable"))?;
        doc.set_hash(self.default_author, key.clone(), hash, size)
            .await?;
        let entry = self
            .sync
            .get_exact(namespace, self.default_author, key.into(), true)
            .await?
            .ok_or_else(|| anyhow!("local iroh-docs entry was not committed"))?;
        self.store.persist_entry(&entry, true).await
    }

    pub(crate) async fn delete_prefix(
        &self,
        namespace: &str,
        prefix: Vec<u8>,
    ) -> Result<WasmDeleteReceipt> {
        let namespace = parse_namespace(namespace)?;
        require_writable_namespace(&self.capabilities, namespace)?;
        let doc = self
            .docs
            .api()
            .open(namespace)
            .await?
            .ok_or_else(|| anyhow!("iroh-docs namespace is unavailable"))?;
        let removed = doc.del(self.default_author, prefix.clone()).await?;
        let tombstone = self
            .sync
            .get_exact(namespace, self.default_author, prefix.into(), true)
            .await?
            .ok_or_else(|| anyhow!("iroh-docs tombstone was not committed"))?;
        let operation_id = self.store.persist_entry(&tombstone, true).await?;
        Ok(WasmDeleteReceipt {
            removed: u32::try_from(removed).context("deleted iroh-docs entry count exceeds u32")?,
            operation_id,
        })
    }

    pub(crate) async fn query(
        &self,
        namespace: &str,
        key_prefix: Vec<u8>,
    ) -> Result<Vec<WasmDocEntry>> {
        let namespace = parse_namespace(namespace)?;
        require_namespace(&self.capabilities, namespace)?;
        let doc = self
            .docs
            .api()
            .open(namespace)
            .await?
            .ok_or_else(|| anyhow!("iroh-docs namespace is unavailable"))?;
        let entries = doc
            .get_many(Query::single_latest_per_key().key_prefix(key_prefix))
            .await?;
        futures::pin_mut!(entries);
        let mut output = Vec::new();
        while let Some(entry) = entries.next().await {
            let entry = entry?;
            output.push(WasmDocEntry {
                namespace_id: entry.namespace().to_string(),
                author_id: entry.author().to_string(),
                key: entry.key().to_vec(),
                timestamp: entry.timestamp(),
                content_hash: entry.content_hash().to_string(),
                content_length: entry.content_len(),
            });
        }
        Ok(output)
    }

    pub(crate) async fn hydrate_blob(&self, hash: &str) -> Result<()> {
        let hash: iroh_blobs::Hash = hash.parse().context("parsing iroh-blob hash")?;
        let mut status = self.blobs.store().blobs().status(hash).await?;
        if !matches!(status, iroh_blobs::api::blobs::BlobStatus::Complete { .. }) {
            let now_ms = js_sys::Date::now().max(0.0) as u64;
            let retry_allowed = {
                let mut retry_after = self
                    .blob_retry_after_ms
                    .lock()
                    .expect("browser blob retry mutex poisoned");
                if retry_after
                    .get(&hash)
                    .is_some_and(|retry_at| *retry_at > now_ms)
                {
                    false
                } else {
                    // Hydration polls must not turn a failed peer-local transfer
                    // into relay traffic on every UI render. The normal docs
                    // downloader remains primary; this is one bounded recovery
                    // attempt after a carrier/network transition.
                    retry_after.insert(hash, now_ms.saturating_add(5_000));
                    true
                }
            };
            if retry_allowed {
                let mut providers = HashSet::new();
                for namespace in self.capabilities.keys().copied() {
                    if let Some(peers) = self.sync.get_sync_peers(namespace).await? {
                        for peer in peers {
                            providers.insert(iroh::PublicKey::from_bytes(&peer)?);
                        }
                    }
                }
                if !providers.is_empty() {
                    let _ = self
                        .downloader
                        .download(
                            iroh_blobs::HashAndFormat::raw(hash),
                            providers.into_iter().collect::<Vec<_>>(),
                        )
                        .await;
                    status = self.blobs.store().blobs().status(hash).await?;
                }
            }
        }
        let size = match status {
            iroh_blobs::api::blobs::BlobStatus::Complete { size } => {
                self.blob_retry_after_ms
                    .lock()
                    .expect("browser blob retry mutex poisoned")
                    .remove(&hash);
                size
            }
            _ => bail!("persistent browser blob is unavailable"),
        };
        if size == 0 {
            return Ok(());
        }

        // Exercise the actual iroh-blobs range protocol rather than trusting
        // only the manifest-backed status result. Reading the boundary bytes
        // remains constant-memory even for very large browser objects.
        let mut reader = self.blobs.store().blobs().reader(hash);
        let mut byte = [0u8; 1];
        reader
            .read_exact(&mut byte)
            .await
            .context("reading persistent browser blob prefix")?;
        reader
            .seek(std::io::SeekFrom::Start(size - 1))
            .await
            .context("seeking persistent browser blob suffix")?;
        reader
            .read_exact(&mut byte)
            .await
            .context("reading persistent browser blob suffix")?;
        Ok(())
    }

    pub(crate) async fn acknowledge_outbox(&self, operation_id: &str) -> Result<()> {
        if operation_id.trim().is_empty() {
            bail!("iroh-docs outbox operation id is required");
        }
        self.store.acknowledge_outbox(operation_id).await
    }

    fn require_current_or_new_capability(
        &self,
        namespace: NamespaceId,
        writable: bool,
        generation: u64,
        share_revision: u64,
    ) -> Result<()> {
        let Some(current) = self.capabilities.get(&namespace) else {
            return Ok(());
        };
        if (generation, share_revision) < (current.generation, current.share_revision) {
            bail!("stale iroh-docs capability update");
        }
        if (generation, share_revision) == (current.generation, current.share_revision)
            && writable != current.writable
        {
            bail!("conflicting iroh-docs capability at the same generation and revision");
        }
        Ok(())
    }

    pub(crate) async fn flush(&self) -> Result<()> {
        self.sync.flush_store().await?;
        self.store.flush().await
    }

    pub(crate) async fn shutdown(&self) -> Result<()> {
        self.flush().await?;
        self.sync.shutdown().await?;
        Ok(())
    }
}

fn namespace_descriptor(capability: &Capability) -> WasmNamespaceDescriptor {
    let (_, bytes) = capability.raw();
    WasmNamespaceDescriptor {
        namespace_id: capability.id().to_string(),
        capability_kind: capability_kind(capability),
        capability: bytes.to_vec(),
    }
}

fn capability_kind(capability: &Capability) -> &'static str {
    if capability.raw().0 == 1 {
        "write"
    } else {
        "read"
    }
}

fn parse_namespace(namespace: &str) -> Result<NamespaceId> {
    namespace.parse().context("parsing iroh-docs namespace")
}

fn require_namespace(
    capabilities: &HashMap<NamespaceId, RuntimeCapabilityFence>,
    namespace: NamespaceId,
) -> Result<RuntimeCapabilityFence> {
    capabilities
        .get(&namespace)
        .copied()
        .ok_or_else(|| anyhow!("iroh-docs namespace is not imported by this runtime"))
}

fn require_writable_namespace(
    capabilities: &HashMap<NamespaceId, RuntimeCapabilityFence>,
    namespace: NamespaceId,
) -> Result<()> {
    if !require_namespace(capabilities, namespace)?.writable {
        bail!("iroh-docs namespace is read-only at the current capability generation");
    }
    Ok(())
}

fn ensure_js_safe_integer(value: u64, field: &str) -> Result<()> {
    if value > 9_007_199_254_740_991 {
        bail!("{field} exceeds JavaScript's safe integer range");
    }
    Ok(())
}

fn js_error(value: JsValue) -> anyhow::Error {
    if let Some(message) = value.as_string() {
        return anyhow!(message);
    }
    if let Ok(message) = Reflect::get(&value, &JsValue::from_str("message")) {
        if let Some(message) = message.as_string() {
            return anyhow!(message);
        }
    }
    anyhow!("JavaScript IndexedDB adapter rejected an operation")
}

fn ignore_newer_entry(error: anyhow::Error) -> Result<()> {
    if error.to_string().contains("newer entry exists") {
        Ok(())
    } else {
        Err(error)
    }
}