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
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
use crate::{BrowserConfig, BrowserError, decode_response, encode, read_method};
use quai_crypto::{RecoverableSignature, hash_message};
use quai_primitives::{QuaiAddress, Shard};
use quai_rpc::{Endpoint, RemoteError, RpcError, Transport, U256, parse_quantity};
use serde_json::{Value, json};
use std::{cell::Cell, fmt, rc::Rc};
use wasm_bindgen::prelude::*;
mod submission;
mod wallet_send;
pub use submission::InjectedSubmissionTransport;
pub use wallet_send::{
    WalletSendAcknowledgement, WalletSendError, WalletSendIdentity, WalletSendObservation,
};

#[wasm_bindgen(module = "/src/bridge.js")]
extern "C" {
    #[wasm_bindgen(catch, js_name = watchProvider)]
    fn watch_provider(provider: &JsValue) -> Result<JsValue, JsValue>;
    #[wasm_bindgen(js_name = providerRevision)]
    fn provider_revision(watch: &JsValue) -> u32;
    #[wasm_bindgen(js_name = providerEventsSupported)]
    fn provider_events_supported(watch: &JsValue) -> bool;
    #[wasm_bindgen(js_name = providerChanged)]
    fn provider_changed(watch: &JsValue, revision: u32) -> bool;
    #[wasm_bindgen(js_name = closeProviderWatch)]
    fn close_provider_watch(watch: &JsValue);
    #[wasm_bindgen(catch, js_name = newAbort)]
    fn new_abort() -> Result<JsValue, JsValue>;
    #[wasm_bindgen(js_name = abort)]
    fn abort_js(controller: &JsValue);
    #[wasm_bindgen(js_name = errorKind)]
    fn error_kind(error: &JsValue) -> String;
    #[wasm_bindgen(js_name = errorCode)]
    fn error_code(error: &JsValue) -> f64;
    #[wasm_bindgen(catch, js_name = validateProvider)]
    fn validate_provider(provider: &JsValue) -> Result<bool, JsValue>;
    #[wasm_bindgen(catch, js_name = fetchJson)]
    async fn fetch_json(
        url: &str,
        body: &str,
        controller: &JsValue,
        timeout: u32,
        max_bytes: usize,
    ) -> Result<JsValue, JsValue>;
    #[wasm_bindgen(catch, js_name = injectedJson)]
    async fn injected_json(
        provider: &JsValue,
        payload: &str,
        controller: &JsValue,
        timeout: u32,
        max_bytes: usize,
    ) -> Result<JsValue, JsValue>;
    #[wasm_bindgen(catch, js_name = randomBytes)]
    fn random_bytes(length: usize) -> Result<js_sys::Uint8Array, JsValue>;
}
struct ProviderWatch(JsValue);
impl Drop for ProviderWatch {
    fn drop(&mut self) {
        close_provider_watch(&self.0);
    }
}
struct AbortGuard(JsValue);
impl AbortGuard {
    fn new() -> Result<Self, BrowserError> {
        new_abort()
            .map(Self)
            .map_err(|_| BrowserError::InvalidConfig)
    }
}
impl Drop for AbortGuard {
    fn drop(&mut self) {
        abort_js(&self.0);
    }
}
struct Permit(Rc<Cell<usize>>);
impl Drop for Permit {
    fn drop(&mut self) {
        self.0.set(self.0.get() - 1);
    }
}
fn permit(active: &Rc<Cell<usize>>, max: usize) -> Result<Permit, BrowserError> {
    if active.get() >= max {
        return Err(BrowserError::Busy);
    }
    active.set(active.get() + 1);
    Ok(Permit(active.clone()))
}
fn convert(error: JsValue) -> BrowserError {
    let code = error_code(&error) as i64;
    match error_kind(&error).as_str() {
        "timeout" => RpcError::Timeout.into(),
        "response_size" => RpcError::ResponseTooLarge.into(),
        "invalid" => BrowserError::InvalidResult,
        "http" => RpcError::HttpStatus(code as u16).into(),
        "provider" => BrowserError::Provider(code),
        "entropy" => BrowserError::EntropyUnavailable,
        _ => RpcError::Transport.into(),
    }
}
fn rpc_error(error: BrowserError) -> RpcError {
    match error {
        BrowserError::Rpc(error) => error,
        BrowserError::Provider(code) => RpcError::Remote(RemoteError {
            code,
            message: String::new(),
            data: None,
        }),
        BrowserError::InvalidResult => RpcError::InvalidResponse("invalid browser result"),
        BrowserError::ChainMismatch => RpcError::InvalidResponse("injected chain ID mismatch"),
        BrowserError::Busy => RpcError::AtCapacity,
        BrowserError::ContextChanged => {
            RpcError::InvalidResponse("injected provider context changed")
        }
        _ => RpcError::InvalidConfig,
    }
}
fn result_string(value: JsValue) -> Result<String, BrowserError> {
    value.as_string().ok_or(BrowserError::InvalidResult)
}
fn request_valid(method: &str, params: &Value) -> bool {
    !method.is_empty()
        && method.len() <= 128
        && method
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'.'))
        && (params.is_array() || params.is_object())
}

/// Browser Fetch transport with streaming response limits, CORS and credential omission.
///
/// Clones share concurrency/ID limits. Dropping the request aborts its Fetch controller;
/// completion or cancellation does not prove whether a remote write was accepted.
#[derive(Clone)]
pub struct BrowserFetchTransport {
    config: BrowserConfig,
    active: Rc<Cell<usize>>,
    next_id: Rc<Cell<u64>>,
}
impl fmt::Debug for BrowserFetchTransport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BrowserFetchTransport")
            .field("config", &self.config)
            .finish_non_exhaustive()
    }
}
impl BrowserFetchTransport {
    /// Construct without network access. Available in windows and dedicated workers.
    pub fn new(config: BrowserConfig) -> Result<Self, BrowserError> {
        config.validate()?;
        Ok(Self {
            config,
            active: Rc::new(Cell::new(0)),
            next_id: Rc::new(Cell::new(1)),
        })
    }
    /// Execute one explicit RPC call; never retries or follows redirects.
    pub async fn send(
        &self,
        endpoint: &Endpoint,
        method: &str,
        params: Value,
    ) -> Result<Value, BrowserError> {
        if !matches!(endpoint.scheme(), "http" | "https") || !request_valid(method, &params) {
            return Err(BrowserError::InvalidConfig);
        }
        let _permit = permit(&self.active, self.config.max_in_flight)?;
        let id = self.next_id.get();
        if id > 9_007_199_254_740_991 {
            return Err(RpcError::RequestIdExhausted.into());
        }
        self.next_id.set(id + 1);
        let body = encode(
            &json!({"jsonrpc":"2.0","id":id,"method":method,"params":params}),
            self.config.max_request_bytes,
        )?;
        let guard = AbortGuard::new()?;
        let result = fetch_json(
            endpoint.as_str(),
            &body,
            &guard.0,
            self.config.request_timeout_ms,
            self.config.max_response_bytes,
        )
        .await
        .map_err(convert)?;
        let result = result_string(result)?;
        Ok(decode_response(result.as_bytes(), id)?)
    }
}
impl Transport for BrowserFetchTransport {
    async fn request(
        &self,
        endpoint: &Endpoint,
        method: &str,
        params: Value,
    ) -> Result<Value, RpcError> {
        self.send(endpoint, method, params).await.map_err(rpc_error)
    }
}

/// Explicit adapter for an application-selected Quai EIP-1193 provider object.
///
/// The configured endpoint is an exact routing identity, not a fetched URL. The
/// encoded shard is included in every request as in pinned quais.js. Construction
/// never requests accounts or signatures. Generic Transport is read-only; explicit
/// methods are the only route to supported permission-sensitive wallet requests.
#[derive(Clone)]
pub struct InjectedProvider {
    provider: JsValue,
    watch: Rc<ProviderWatch>,
    endpoint: Endpoint,
    shard: Shard,
    expected_chain: U256,
    config: BrowserConfig,
    active: Rc<Cell<usize>>,
}
impl fmt::Debug for InjectedProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("InjectedProvider")
            .field("endpoint", &self.endpoint)
            .field("shard", &self.shard)
            .field("expected_chain", &self.expected_chain)
            .finish_non_exhaustive()
    }
}
impl InjectedProvider {
    /// Bind an explicitly selected JS provider, endpoint identity, shard and expected chain.
    /// No global provider discovery or wallet prompt occurs.
    pub fn new(
        provider: JsValue,
        endpoint: Endpoint,
        shard: Shard,
        expected_chain: U256,
        config: BrowserConfig,
    ) -> Result<Self, BrowserError> {
        config.validate()?;
        if !validate_provider(&provider).map_err(|_| BrowserError::InvalidConfig)? {
            return Err(BrowserError::InvalidConfig);
        }
        let watch = Rc::new(ProviderWatch(watch_provider(&provider).map_err(convert)?));
        Ok(Self {
            provider,
            watch,
            endpoint,
            shard,
            expected_chain,
            config,
            active: Rc::new(Cell::new(0)),
        })
    }
    /// Monotonic account/chain/disconnect revision shared by adapter clones.
    /// Applications can invalidate displayed balances and cached authorization on change.
    pub fn context_revision(&self) -> u32 {
        provider_revision(&self.watch.0)
    }
    /// Whether this provider exposes removable EIP-1193 event listeners.
    pub fn monitors_context_events(&self) -> bool {
        provider_events_supported(&self.watch.0)
    }
    async fn verify_context(
        &self,
        revision: u32,
        account: Option<QuaiAddress>,
    ) -> Result<(), BrowserError> {
        if provider_changed(&self.watch.0, revision) {
            return Err(BrowserError::ContextChanged);
        }
        if !self.monitors_context_events() {
            // Request-only providers cannot report transient changes. Recheck their
            // current chain and exposed account before releasing a result.
            self.check_chain().await?;
            if let Some(account) = account
                && !Self::accounts_from(self.raw("quai_accounts", json!([])).await?)?
                    .contains(&account)
            {
                return Err(BrowserError::AccountUnavailable);
            }
        }
        if provider_changed(&self.watch.0, revision) {
            return Err(BrowserError::ContextChanged);
        }
        Ok(())
    }
    async fn raw(&self, method: &str, params: Value) -> Result<Value, BrowserError> {
        let _permit = permit(&self.active, self.config.max_in_flight)?;
        let payload = encode(
            &json!({"method":method,"params":params,"shard":self.shard.encoded()}),
            self.config.max_request_bytes,
        )?;
        let guard = AbortGuard::new()?;
        let result = injected_json(
            &self.provider,
            &payload,
            &guard.0,
            self.config.request_timeout_ms,
            self.config.max_response_bytes,
        )
        .await
        .map_err(convert)?;
        serde_json::from_str(&result_string(result)?).map_err(|_| BrowserError::InvalidResult)
    }
    async fn check_chain(&self) -> Result<Value, BrowserError> {
        let value = self.raw("quai_chainId", json!([])).await?;
        let chain = value
            .as_str()
            .ok_or(BrowserError::InvalidResult)
            .and_then(|value| parse_quantity(value).map_err(|_| BrowserError::InvalidResult))?;
        if chain != self.expected_chain {
            return Err(BrowserError::ChainMismatch);
        }
        Ok(value)
    }
    /// Read through the fixed injected wallet after a fresh chain check.
    /// Account/signing/sending permission methods cannot pass this allowlist.
    pub async fn read(
        &self,
        endpoint: &Endpoint,
        method: &str,
        params: Value,
    ) -> Result<Value, BrowserError> {
        if endpoint != &self.endpoint || !request_valid(method, &params) {
            return Err(BrowserError::InvalidConfig);
        }
        if !read_method(method) {
            return Err(BrowserError::AuthorizationRequired);
        }
        let revision = self.context_revision();
        let chain = self.check_chain().await?;
        if method == "quai_chainId" {
            if provider_changed(&self.watch.0, revision) {
                return Err(BrowserError::ContextChanged);
            }
            return Ok(chain);
        }
        let result = self.raw(method, params).await?;
        self.verify_context(revision, None).await?;
        Ok(result)
    }
    fn accounts_from(value: Value) -> Result<Vec<QuaiAddress>, BrowserError> {
        let values = value
            .as_array()
            .filter(|values| values.len() <= 1024)
            .ok_or(BrowserError::InvalidResult)?;
        let mut accounts = Vec::with_capacity(values.len());
        for value in values {
            let address = value
                .as_str()
                .ok_or(BrowserError::InvalidResult)?
                .parse::<QuaiAddress>()
                .map_err(|_| BrowserError::InvalidResult)?;
            if accounts.contains(&address) {
                return Err(BrowserError::InvalidResult);
            }
            accounts.push(address)
        }
        Ok(accounts)
    }
    /// Request wallet account access explicitly. Call only in the application's user-approved flow.
    /// User rejection preserves numeric EIP-1193 code 4001. No retry occurs.
    pub async fn request_accounts(&self) -> Result<Vec<QuaiAddress>, BrowserError> {
        self.check_chain().await?;
        let accounts = Self::accounts_from(self.raw("quai_requestAccounts", json!([])).await?)?;
        self.check_chain().await?;
        Ok(accounts)
    }
    /// Read accounts already exposed by the wallet; never requests additional permission.
    pub async fn accounts(&self) -> Result<Vec<QuaiAddress>, BrowserError> {
        let revision = self.context_revision();
        self.check_chain().await?;
        let accounts = Self::accounts_from(self.raw("quai_accounts", json!([])).await?)?;
        self.verify_context(revision, None).await?;
        Ok(accounts)
    }
    /// Explicitly ask the wallet to sign these bytes with personal_sign.
    ///
    /// Checks the configured chain, account exposure and sender zone before calling.
    /// Recovers the exact message signature and requires the requested account.
    /// Dropping this future cannot dismiss or revoke
    /// a wallet approval already presented by the injected provider.
    pub async fn personal_sign(
        &self,
        address: QuaiAddress,
        message: &[u8],
    ) -> Result<[u8; 65], BrowserError> {
        if self.shard != Shard::Zone(address.zone())
            || message.len() > self.config.max_request_bytes / 2
        {
            return Err(BrowserError::InvalidConfig);
        }
        let revision = self.context_revision();
        self.check_chain().await?;
        if !Self::accounts_from(self.raw("quai_accounts", json!([])).await?)?.contains(&address) {
            return Err(BrowserError::AccountUnavailable);
        }
        let mut hex = String::with_capacity(2 + message.len() * 2);
        hex.push_str("0x");
        const DIGITS: &[u8] = b"0123456789abcdef";
        for byte in message {
            hex.push(DIGITS[(byte >> 4) as usize] as char);
            hex.push(DIGITS[(byte & 15) as usize] as char)
        }
        if provider_changed(&self.watch.0, revision) {
            return Err(BrowserError::ContextChanged);
        }
        let value = self
            .raw(
                "personal_sign",
                json!([hex, address.to_string().to_ascii_lowercase()]),
            )
            .await?;
        self.verify_context(revision, Some(address)).await?;
        Self::verified_signature(value, address, &hash_message(message))
    }
    /// Explicitly request EIP-712 v4 signing after domain, chain and account checks.
    /// The immutable document determines both the serialized request and recovered
    /// signature hash. No implicit permission request or automatic retry occurs.
    pub async fn sign_typed_data(
        &self,
        address: QuaiAddress,
        data: &quai_signer::TypedData,
        policy: quai_signer::DomainPolicy,
    ) -> Result<[u8; 65], BrowserError> {
        if self.shard != Shard::Zone(address.zone()) {
            return Err(BrowserError::InvalidConfig);
        }
        policy
            .validate(data, self.expected_chain)
            .map_err(|_| BrowserError::ChainMismatch)?;
        let document = data
            .to_rpc_json()
            .map_err(|_| BrowserError::InvalidResult)?;
        let revision = self.context_revision();
        self.check_chain().await?;
        if !Self::accounts_from(self.raw("quai_accounts", json!([])).await?)?.contains(&address) {
            return Err(BrowserError::AccountUnavailable);
        }
        if provider_changed(&self.watch.0, revision) {
            return Err(BrowserError::ContextChanged);
        }
        let result = self
            .raw(
                "quai_signTypedData_v4",
                json!([address.to_string().to_ascii_lowercase(), document]),
            )
            .await?;
        self.verify_context(revision, Some(address)).await?;
        Self::verified_signature(result, address, data.signing_hash().bytes())
    }
    fn verified_signature(
        value: Value,
        address: QuaiAddress,
        digest: &[u8; 32],
    ) -> Result<[u8; 65], BrowserError> {
        let encoded = value
            .as_str()
            .filter(|s| {
                s.len() == 132
                    && s.starts_with("0x")
                    && s.as_bytes()[2..].iter().all(u8::is_ascii_hexdigit)
            })
            .ok_or(BrowserError::InvalidResult)?;
        let mut signature = [0; 65];
        for (index, byte) in signature.iter_mut().enumerate() {
            *byte = u8::from_str_radix(&encoded[2 + index * 2..4 + index * 2], 16)
                .map_err(|_| BrowserError::InvalidResult)?;
        }
        let parsed = RecoverableSignature::from_quais_bytes(&signature)
            .map_err(|_| BrowserError::InvalidResult)?;
        let recovered = parsed
            .recover_prehash(digest)
            .map_err(|_| BrowserError::InvalidResult)?;
        if recovered.address() != address.address() {
            return Err(BrowserError::InvalidResult);
        }
        Ok(signature)
    }

    fn transaction_params(
        &self,
        address: QuaiAddress,
        transaction: &quai_consensus::QuaiTransaction,
        method: &str,
    ) -> Result<Value, BrowserError> {
        if transaction.chain_id != self.expected_chain || transaction.chain_id == U256::ZERO {
            return Err(BrowserError::ChainMismatch);
        }
        if self.shard != Shard::Zone(address.zone()) {
            return Err(BrowserError::InvalidConfig);
        }
        transaction
            .unsigned_bytes()
            .map_err(|_| BrowserError::InvalidConfig)?;
        let access_list: Vec<_> = transaction.access_list.iter().map(|entry| {
            json!({"address":entry.address.to_string(),"storageKeys":entry.storage_keys.iter().map(ToString::to_string).collect::<Vec<_>>()})
        }).collect();
        let mut rpc = json!({
            "type":"0x0", "chainId":format!("{:#x}",transaction.chain_id),
            "from":address.to_string().to_ascii_lowercase(),
            "nonce":format!("{:#x}",transaction.nonce),
            "value":format!("{:#x}",transaction.value),
            "gas":format!("{:#x}",transaction.gas_limit),
            "gasPrice":format!("{:#x}",transaction.gas_price),
            "data":quai_primitives::hexlify(&transaction.data).map_err(|_|BrowserError::InvalidConfig)?,
            "accessList":access_list,
        });
        if let Some(to) = transaction.to {
            rpc["to"] = json!(to.to_string().to_ascii_lowercase());
        }
        let params = json!([rpc]);
        // Check the exact outgoing envelope before even passive wallet reads.
        encode(
            &json!({"method":method,"params":params,"shard":self.shard.encoded()}),
            self.config.max_request_bytes,
        )?;
        Ok(params)
    }
    /// Explicitly request signing of an exact, fully populated type-0 Quai
    /// transaction. Does not estimate, fill fields, request accounts or broadcast.
    /// Canonical returned protobuf must recover the requested exposed account
    /// and match every unsigned field, including ordered access-list entries.
    /// Dropping the future cannot retract an already displayed wallet approval.
    pub async fn sign_quai_transaction(
        &self,
        address: QuaiAddress,
        transaction: &quai_consensus::QuaiTransaction,
    ) -> Result<quai_consensus::SignedQuaiTransaction, BrowserError> {
        let params = self.transaction_params(address, transaction, "quai_signTransaction")?;
        let revision = self.context_revision();
        self.check_chain().await?;
        if !Self::accounts_from(self.raw("quai_accounts", json!([])).await?)?.contains(&address) {
            return Err(BrowserError::AccountUnavailable);
        }
        if provider_changed(&self.watch.0, revision) {
            return Err(BrowserError::ContextChanged);
        }
        let value = self.raw("quai_signTransaction", params).await?;
        self.verify_context(revision, Some(address)).await?;
        let bytes = value
            .as_str()
            .ok_or(BrowserError::InvalidResult)
            .and_then(|text| {
                quai_primitives::get_bytes(text).map_err(|_| BrowserError::InvalidResult)
            })?;
        let signed = quai_consensus::SignedQuaiTransaction::decode(&bytes)
            .map_err(|_| BrowserError::InvalidResult)?;
        if signed.from() != address || signed.transaction() != transaction {
            return Err(BrowserError::InvalidResult);
        }
        Ok(signed)
    }
}
impl Transport for InjectedProvider {
    async fn request(
        &self,
        endpoint: &Endpoint,
        method: &str,
        params: Value,
    ) -> Result<Value, RpcError> {
        self.read(endpoint, method, params).await.map_err(rpc_error)
    }
}

/// Fill at most 65,536 bytes using secure-context Web Crypto, with no fallback PRNG.
/// No wallet key storage or generation is performed automatically.
pub fn fill_random(destination: &mut [u8]) -> Result<(), BrowserError> {
    if destination.len() > 65_536 {
        return Err(BrowserError::EntropyUnavailable);
    }
    let bytes = random_bytes(destination.len()).map_err(|_| BrowserError::EntropyUnavailable)?;
    if bytes.length() as usize != destination.len() {
        return Err(BrowserError::EntropyUnavailable);
    }
    bytes.copy_to(destination);
    Ok(())
}