quai-browser 0.1.0-alpha.14

Browser Fetch, WebSocket and explicit injected-provider adapters for Quai
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
//! Native-independent browser boundaries plus wasm-only Fetch and injected Quai adapters.
//!
//! Browser APIs require an application-selected endpoint/provider. Construction performs no
//! network requests and never prompts for accounts. Heavy derivation and persistent wallet
//! storage remain separate worker/storage responsibilities.
#[cfg(any(target_arch = "wasm32", test))]
use quai_rpc::RemoteError;
use quai_rpc::RpcError;
#[cfg(any(target_arch = "wasm32", test))]
use serde::Deserialize;
#[cfg(any(target_arch = "wasm32", test))]
use serde_json::Value;

#[cfg(target_arch = "wasm32")]
mod browser;
#[cfg(target_arch = "wasm32")]
mod resource;
#[cfg(target_arch = "wasm32")]
pub use browser::{
    BrowserFetchTransport, InjectedProvider, InjectedSubmissionTransport,
    WalletSendAcknowledgement, WalletSendError, WalletSendIdentity, WalletSendObservation,
    fill_random,
};
#[cfg(target_arch = "wasm32")]
pub use quai_signer::{DomainPolicy, TypedData};
#[cfg(target_arch = "wasm32")]
pub use resource::BrowserResourceFetch;

/// Browser resource limits. Concurrent calls fail fast when capacity is exhausted.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct BrowserConfig {
    /// Total request wait deadline in milliseconds; browser suspension can delay timers.
    pub request_timeout_ms: u32,
    /// Maximum serialized outgoing JSON bytes.
    pub max_request_bytes: usize,
    /// Maximum streamed response or serialized injected result bytes.
    pub max_response_bytes: usize,
    /// Maximum active operations across all clones; no unbounded waiting queue.
    pub max_in_flight: usize,
}
impl BrowserConfig {
    /// Replace `request_timeout_ms`.
    pub const fn with_request_timeout_ms(mut self, request_timeout_ms: u32) -> Self {
        self.request_timeout_ms = request_timeout_ms;
        self
    }
    /// Replace `max_request_bytes`.
    pub const fn with_max_request_bytes(mut self, max_request_bytes: usize) -> Self {
        self.max_request_bytes = max_request_bytes;
        self
    }
    /// Replace `max_response_bytes`.
    pub const fn with_max_response_bytes(mut self, max_response_bytes: usize) -> Self {
        self.max_response_bytes = max_response_bytes;
        self
    }
    /// Replace `max_in_flight`.
    pub const fn with_max_in_flight(mut self, max_in_flight: usize) -> Self {
        self.max_in_flight = max_in_flight;
        self
    }
}
impl Default for BrowserConfig {
    fn default() -> Self {
        Self {
            request_timeout_ms: 10_000,
            max_request_bytes: 1024 * 1024,
            max_response_bytes: 2 * 1024 * 1024,
            max_in_flight: 8,
        }
    }
}
impl BrowserConfig {
    /// Validate limits without touching browser globals or the network.
    pub fn validate(&self) -> Result<(), BrowserError> {
        if self.request_timeout_ms == 0
            || self.request_timeout_ms > i32::MAX as u32
            || self.max_request_bytes == 0
            || self.max_request_bytes > 16 * 1024 * 1024
            || self.max_response_bytes == 0
            || self.max_response_bytes > 16 * 1024 * 1024
            || self.max_in_flight == 0
            || self.max_in_flight > 128
            || self
                .max_response_bytes
                .checked_mul(self.max_in_flight)
                .is_none_or(|n| n > 128 * 1024 * 1024)
        {
            return Err(BrowserError::InvalidConfig);
        }
        Ok(())
    }
}

/// Sanitized browser failures. No provider strings, payloads, keys or URLs are retained.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum BrowserError {
    /// IndexedDB open/transaction/schema/quota failure; no remote text is retained.
    #[error("browser snapshot storage failed")]
    Storage,
    /// A concurrent writer or stale tab changed the snapshot revision.
    #[error("browser snapshot revision conflict")]
    StorageConflict,
    /// Configuration or endpoint does not match this adapter.
    #[error("invalid browser adapter configuration")]
    InvalidConfig,
    /// A configured concurrent-operation limit was reached.
    #[error("browser adapter concurrency limit reached")]
    Busy,
    /// Permission-sensitive RPC must use an explicit wallet API.
    #[error("method requires an explicit supported wallet operation")]
    AuthorizationRequired,
    /// The injected wallet reports another network.
    #[error("injected provider chain ID mismatch")]
    ChainMismatch,
    /// Accounts, chain or connection changed while an operation was awaiting the wallet.
    #[error("injected provider context changed during request")]
    ContextChanged,
    /// Wallet result or message/address request has an invalid shape.
    #[error("invalid injected provider result or request")]
    InvalidResult,
    /// The specified account is not currently exposed by the wallet.
    #[error("account is not authorized by the injected wallet")]
    AccountUnavailable,
    /// Numeric EIP-1193 failure, including 4001 user rejection and 4200 unsupported method.
    #[error("injected provider rejected request (code {0})")]
    Provider(i64),
    /// Web Crypto entropy is unavailable or the request exceeds its 65,536-byte limit.
    #[error("secure browser entropy unavailable")]
    EntropyUnavailable,
    /// Bounded RPC/transport error.
    #[error(transparent)]
    Rpc(#[from] RpcError),
}

#[cfg(any(target_arch = "wasm32", test))]
fn read_method(method: &str) -> bool {
    matches!(
        method,
        "quai_chainId"
            | "quai_blockNumber"
            | "quai_getBalance"
            | "quai_getTransactionCount"
            | "quai_gasPrice"
            | "quai_estimateGas"
            | "quai_getCode"
            | "quai_getStorageAt"
            | "quai_call"
            | "quai_getTransactionByHash"
            | "quai_getTransactionReceipt"
            | "quai_getOutpointsByAddress"
            | "quai_estimateFeeForQi"
            | "quai_getHeaderByNumber"
            | "quai_getHeaderByHash"
            | "quai_getBlockByNumber"
            | "quai_getBlockByHash"
            | "quai_getLogs"
            | "quai_listRunningChains"
            | "quai_getOutpointDeltasForAddressesInRange"
            | "quai_getLatestUTXOSetSize"
            | "quai_qiToQuai"
            | "quai_quaiToQi"
            | "quai_calculateConversionAmount"
            | "quai_getWrappedQiDeposit"
            | "quai_getLockedBalance"
            | "quai_createAccessList"
            | "quai_getPendingHeader"
            | "quai_getProtocolExpansionNumber"
            | "txpool_status"
            | "txpool_content"
            | "txpool_inspect"
            | "quai_accounts"
    )
}

#[cfg(any(target_arch = "wasm32", test))]
fn encode(value: &Value, max: usize) -> Result<String, RpcError> {
    use std::io::Write;
    struct Writer {
        bytes: Vec<u8>,
        max: usize,
    }
    impl Write for Writer {
        fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
            if bytes.len() > self.max - self.bytes.len() {
                return Err(std::io::Error::other("request limit"));
            }
            self.bytes.extend_from_slice(bytes);
            Ok(bytes.len())
        }
        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }
    let mut writer = Writer {
        bytes: Vec::new(),
        max,
    };
    serde_json::to_writer(&mut writer, value).map_err(|_| RpcError::RequestTooLarge)?;
    String::from_utf8(writer.bytes).map_err(|_| RpcError::InvalidConfig)
}

#[cfg(any(target_arch = "wasm32", test))]
fn decode_response(bytes: &[u8], expected_id: u64) -> Result<Value, RpcError> {
    #[derive(Default)]
    enum Slot {
        #[default]
        Absent,
        Present(Value),
    }
    fn slot<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Slot, D::Error> {
        Value::deserialize(d).map(Slot::Present)
    }
    #[derive(Deserialize)]
    struct Envelope {
        jsonrpc: String,
        id: u64,
        #[serde(default, deserialize_with = "slot")]
        result: Slot,
        #[serde(default, deserialize_with = "slot")]
        error: Slot,
    }
    let envelope: Envelope = serde_json::from_slice(bytes)
        .map_err(|_| RpcError::InvalidResponse("malformed envelope"))?;
    if envelope.jsonrpc != "2.0" || envelope.id != expected_id {
        return Err(RpcError::InvalidResponse(
            "protocol version or request ID mismatch",
        ));
    }
    match (envelope.result, envelope.error) {
        (Slot::Present(result), Slot::Absent) => Ok(result),
        (Slot::Absent, Slot::Present(error)) => Err(RpcError::Remote(
            serde_json::from_value::<RemoteError>(error)
                .map_err(|_| RpcError::InvalidResponse("malformed error"))?,
        )),
        _ => Err(RpcError::InvalidResponse(
            "expected exactly one result or error",
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn config_and_authorization_boundaries() {
        BrowserConfig::default().validate().unwrap();
        assert!(
            BrowserConfig {
                max_in_flight: 0,
                ..Default::default()
            }
            .validate()
            .is_err()
        );
        assert!(
            BrowserConfig {
                max_response_bytes: 16 * 1024 * 1024,
                max_in_flight: 128,
                ..Default::default()
            }
            .validate()
            .is_err()
        );
        for method in [
            "quai_sendRawTransaction",
            "quai_sendTransaction",
            "quai_requestAccounts",
            "personal_sign",
            "quai_signTypedData_v4",
            "wallet_switchEthereumChain",
        ] {
            assert!(!read_method(method));
        }
        assert!(read_method("quai_getHeaderByNumber"));
    }
    #[test]
    fn bounded_encoding_and_strict_response_envelopes() {
        assert!(matches!(
            encode(&serde_json::json!(["abcdef"]), 3),
            Err(RpcError::RequestTooLarge)
        ));
        assert_eq!(
            decode_response(br#"{"jsonrpc":"2.0","id":1,"result":null}"#, 1).unwrap(),
            Value::Null
        );
        for value in [
            br#"{"jsonrpc":"2.0","id":2,"result":null}"#.as_slice(),
            br#"{"jsonrpc":"2.0","id":1,"id":1,"result":null}"#,
            br#"{"jsonrpc":"2.0","id":1,"result":null,"error":null}"#,
        ] {
            assert!(decode_response(value, 1).is_err());
        }
    }
}

#[cfg(target_arch = "wasm32")]
mod storage;
#[cfg(target_arch = "wasm32")]
pub use storage::{
    BrowserSnapshot, BrowserSnapshotStore, BrowserSnapshotUpdate, BrowserStorageScope,
    compare_exchange_snapshots,
};

/// Browser WebSocket request and notification resource limits.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct BrowserSocketConfig {
    /// Request size, response size, deadline and shared concurrency limits.
    pub rpc: BrowserConfig,
    /// Active plus pending subscriptions, 1..=128.
    pub max_subscriptions: usize,
    /// Queued notifications per subscription, 1..=1024.
    pub max_notifications: usize,
    /// Total queued UTF-8 notification bytes across the session, at most 128 MiB.
    pub max_queued_bytes: usize,
}
impl BrowserSocketConfig {
    /// Replace `rpc`.
    pub const fn with_rpc(mut self, rpc: BrowserConfig) -> Self {
        self.rpc = rpc;
        self
    }
    /// Replace `max_subscriptions`.
    pub const fn with_max_subscriptions(mut self, max_subscriptions: usize) -> Self {
        self.max_subscriptions = max_subscriptions;
        self
    }
    /// Replace `max_notifications`.
    pub const fn with_max_notifications(mut self, max_notifications: usize) -> Self {
        self.max_notifications = max_notifications;
        self
    }
    /// Replace `max_queued_bytes`.
    pub const fn with_max_queued_bytes(mut self, max_queued_bytes: usize) -> Self {
        self.max_queued_bytes = max_queued_bytes;
        self
    }
}
impl Default for BrowserSocketConfig {
    fn default() -> Self {
        Self {
            rpc: BrowserConfig::default(),
            max_subscriptions: 16,
            max_notifications: 64,
            max_queued_bytes: 8 * 1024 * 1024,
        }
    }
}
impl BrowserSocketConfig {
    /// Validate without opening a socket or accessing browser globals.
    pub fn validate(&self) -> Result<(), BrowserError> {
        self.rpc.validate()?;
        if self
            .rpc
            .max_request_bytes
            .checked_mul(self.rpc.max_in_flight)
            .is_none_or(|n| n > 128 * 1024 * 1024)
            || self
                .rpc
                .max_response_bytes
                .checked_mul(self.max_subscriptions)
                .is_none_or(|n| n > 128 * 1024 * 1024)
            || !(1..=128).contains(&self.max_subscriptions)
            || !(1..=1024).contains(&self.max_notifications)
            || !(1..=128 * 1024 * 1024).contains(&self.max_queued_bytes)
        {
            return Err(BrowserError::InvalidConfig);
        }
        Ok(())
    }
}
#[cfg(target_arch = "wasm32")]
mod code_wait;
#[cfg(target_arch = "wasm32")]
pub use code_wait::wait_for_contract_code;
#[cfg(target_arch = "wasm32")]
mod transaction_wait;
#[cfg(target_arch = "wasm32")]
pub use transaction_wait::{BrowserTransactionWaitError, wait_for_transaction};
#[cfg(target_arch = "wasm32")]
mod account_wait;
#[cfg(target_arch = "wasm32")]
mod receipt_wait;
#[cfg(target_arch = "wasm32")]
pub use account_wait::{BrowserAccountWaitError, wait_for_account_transaction};
#[cfg(target_arch = "wasm32")]
mod socket;
#[cfg(target_arch = "wasm32")]
pub use receipt_wait::{BrowserReceiptWaitError, wait_for_receipt};
#[cfg(target_arch = "wasm32")]
pub use socket::{BrowserSubscription, BrowserWebSocketTransport};

/// Explicit browser transaction-wait limits. Milliseconds must be positive and fit a
/// browser timer; polling attempts are independently bounded. No implicit defaults.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct BrowserWaitConfig {
    /// Required positive observed depth, including the containing block.
    pub confirmations: u64,
    /// Overall observable monotonic timeout, including RPCs and polling delays.
    pub timeout_ms: u32,
    /// Delay between incomplete observations, no longer than the timeout.
    pub poll_interval_ms: u32,
    /// At most 100,000 completed observations; zero is invalid.
    pub max_polls: u32,
}
impl BrowserWaitConfig {
    /// Every limit is required: there is deliberately no default.
    pub const fn new(
        confirmations: u64,
        timeout_ms: u32,
        poll_interval_ms: u32,
        max_polls: u32,
    ) -> Self {
        Self {
            confirmations,
            timeout_ms,
            poll_interval_ms,
            max_polls,
        }
    }
    /// Replace `confirmations`.
    pub const fn with_confirmations(mut self, confirmations: u64) -> Self {
        self.confirmations = confirmations;
        self
    }
    /// Replace `timeout_ms`.
    pub const fn with_timeout_ms(mut self, timeout_ms: u32) -> Self {
        self.timeout_ms = timeout_ms;
        self
    }
    /// Replace `poll_interval_ms`.
    pub const fn with_poll_interval_ms(mut self, poll_interval_ms: u32) -> Self {
        self.poll_interval_ms = poll_interval_ms;
        self
    }
    /// Replace `max_polls`.
    pub const fn with_max_polls(mut self, max_polls: u32) -> Self {
        self.max_polls = max_polls;
        self
    }
}
impl BrowserWaitConfig {
    /// Validate before allocating timers or performing provider I/O.
    pub fn validate(&self) -> Result<(), BrowserError> {
        if self.confirmations == 0
            || self.timeout_ms == 0
            || self.timeout_ms > i32::MAX as u32
            || self.poll_interval_ms == 0
            || self.poll_interval_ms > self.timeout_ms
            || !(1..=100_000).contains(&self.max_polls)
        {
            return Err(BrowserError::InvalidConfig);
        }
        Ok(())
    }
}

#[cfg(test)]
mod socket_config_tests {
    use super::*;
    #[test]
    fn socket_limits_bound_outgoing_buffers_and_notification_state() {
        let mut config = BrowserSocketConfig::default();
        config.validate().unwrap();
        config.rpc.max_request_bytes = 16 * 1024 * 1024;
        config.rpc.max_response_bytes = 1;
        config.rpc.max_in_flight = 128;
        assert!(config.validate().is_err());
        config.rpc.max_in_flight = 8;
        config.validate().unwrap();
        for (subscriptions, notifications, bytes) in [
            (0, 1, 1),
            (129, 1, 1),
            (1, 0, 1),
            (1, 1025, 1),
            (1, 1, 0),
            (1, 1, usize::MAX),
        ] {
            config.max_subscriptions = subscriptions;
            config.max_notifications = notifications;
            config.max_queued_bytes = bytes;
            assert!(config.validate().is_err());
        }
    }
}