Skip to main content

quai_browser/browser/
submission.rs

1//! Explicit signed-payload submission capability for application-selected wallets.
2use super::*;
3use quai_consensus::{SignedQiOperation, SignedQuaiTransaction};
4
5/// Explicit transport capability for canonical, already-signed Quai/Qi payloads.
6/// Compose with `quai_provider::Provider` to retain its expected transaction ID,
7/// acknowledgement validation and ambiguous-submission errors. All other wallet
8/// signing/permission/sending methods remain unavailable through `Transport`.
9#[derive(Clone, Debug)]
10pub struct InjectedSubmissionTransport(InjectedProvider);
11impl InjectedProvider {
12    /// Opt into forwarding verified signed bytes through this wallet's RPC.
13    /// Construction performs no network requests or permission/signing prompts.
14    /// The original `InjectedProvider` transport retains its read-only allowlist.
15    pub fn signed_submission_transport(&self) -> InjectedSubmissionTransport {
16        InjectedSubmissionTransport(self.clone())
17    }
18}
19impl Transport for InjectedSubmissionTransport {
20    async fn request(
21        &self,
22        endpoint: &Endpoint,
23        method: &str,
24        params: Value,
25    ) -> Result<Value, RpcError> {
26        if method != "quai_sendRawTransaction" {
27            return self
28                .0
29                .read(endpoint, method, params)
30                .await
31                .map_err(rpc_error);
32        }
33        if endpoint != &self.0.endpoint {
34            return Err(RpcError::InvalidConfig);
35        }
36        let values = params
37            .as_array()
38            .filter(|p| p.len() == 1)
39            .ok_or(RpcError::InvalidConfig)?;
40        let bytes = values[0]
41            .as_str()
42            .ok_or(RpcError::InvalidConfig)
43            .and_then(|text| {
44                quai_primitives::get_bytes(text).map_err(|_| RpcError::InvalidConfig)
45            })?;
46        let (chain, zone) = if let Ok(signed) = SignedQuaiTransaction::decode(&bytes) {
47            (signed.transaction().chain_id, signed.from().zone())
48        } else {
49            let signed = SignedQiOperation::decode(&bytes).map_err(|_| RpcError::InvalidConfig)?;
50            (
51                signed.transaction().chain_id,
52                signed.origin_zone().map_err(|_| RpcError::InvalidConfig)?,
53            )
54        };
55        if chain != self.0.expected_chain || Shard::Zone(zone) != self.0.shard {
56            return Err(RpcError::InvalidConfig);
57        }
58        // Validate the complete envelope before touching the selected wallet.
59        encode(
60            &json!({"method":method,"params":params,"shard":self.0.shard.encoded()}),
61            self.0.config.max_request_bytes,
62        )?;
63        let revision = self.0.context_revision();
64        self.0.check_chain().await.map_err(rpc_error)?;
65        if provider_changed(&self.0.watch.0, revision) {
66            return Err(rpc_error(BrowserError::ContextChanged));
67        }
68        // Once raw submission starts, any error/cancellation is ambiguous. The
69        // high-level Provider retains that distinction and never retries writes.
70        let result = self.0.raw(method, params).await.map_err(rpc_error)?;
71        self.0
72            .verify_context(revision, None)
73            .await
74            .map_err(rpc_error)?;
75        Ok(result)
76    }
77}