quai_browser/browser/
wallet_send.rs1use super::*;
3use quai_consensus::{QuaiTransaction, SignedQuaiTransaction};
4use quai_primitives::Hash32;
5use quai_provider::{Inclusion, Provider, ProviderError};
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11#[non_exhaustive]
12pub struct WalletSendIdentity {
13 pub from: QuaiAddress,
15 pub chain_id: U256,
17 pub nonce: u64,
19 pub signing_digest: Hash32,
21}
22impl WalletSendIdentity {
23 pub fn new(from: QuaiAddress, transaction: &QuaiTransaction) -> Result<Self, BrowserError> {
25 Ok(Self {
26 from,
27 chain_id: transaction.chain_id,
28 nonce: transaction.nonce,
29 signing_digest: transaction
30 .signing_digest()
31 .map_err(|_| BrowserError::InvalidConfig)?,
32 })
33 }
34}
35#[derive(Debug, thiserror::Error)]
37#[non_exhaustive]
38pub enum WalletSendError {
39 #[error("wallet transaction preflight failed: {0}")]
41 Preflight(#[source] BrowserError),
42 #[error("wallet transaction outcome is ambiguous: {source}")]
44 Ambiguous {
45 identity: WalletSendIdentity,
47 reported_hash: Option<Hash32>,
50 #[source]
52 source: BrowserError,
53 },
54}
55impl WalletSendError {
56 pub fn acceptance_is_ambiguous(&self) -> bool {
58 matches!(self, Self::Ambiguous { .. })
59 }
60}
61#[derive(Clone)]
64pub struct WalletSendAcknowledgement {
65 identity: WalletSendIdentity,
66 reported_hash: Hash32,
67 requested: QuaiTransaction,
68}
69impl fmt::Debug for WalletSendAcknowledgement {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 f.debug_struct("WalletSendAcknowledgement")
72 .field("identity", &self.identity)
73 .field("reported_hash", &self.reported_hash)
74 .finish()
75 }
76}
77impl WalletSendAcknowledgement {
78 pub fn from_reported_hash(
82 from: QuaiAddress,
83 requested: QuaiTransaction,
84 reported_hash: Hash32,
85 ) -> Result<Self, BrowserError> {
86 if requested.chain_id == U256::ZERO || !valid_hash(from, reported_hash) {
87 return Err(BrowserError::InvalidResult);
88 }
89 let identity = WalletSendIdentity::new(from, &requested)?;
90 Ok(Self {
91 identity,
92 reported_hash,
93 requested,
94 })
95 }
96 pub fn identity(&self) -> WalletSendIdentity {
98 self.identity
99 }
100 pub fn reported_hash(&self) -> Hash32 {
102 self.reported_hash
103 }
104 pub fn requested_transaction(&self) -> &QuaiTransaction {
106 &self.requested
107 }
108 pub async fn observe<T: Transport>(
112 &self,
113 provider: &Provider<T>,
114 ) -> Result<Option<WalletSendObservation>, ProviderError> {
115 let zone = self.identity.from.zone();
116 let actual = provider.chain_id(zone.into()).await?;
117 if actual != self.identity.chain_id {
118 return Err(ProviderError::ChainMismatch {
119 expected: self.identity.chain_id,
120 actual,
121 });
122 }
123 let Some(observed) = provider.transaction(zone, self.reported_hash).await? else {
124 return Ok(None);
125 };
126 let signed = observed.verified_quai()?;
127 let matches_request =
128 signed.from() == self.identity.from && signed.transaction() == &self.requested;
129 Ok(Some(WalletSendObservation {
130 signed,
131 matches_request,
132 inclusion: observed.inclusion,
133 }))
134 }
135}
136#[derive(Debug)]
138pub struct WalletSendObservation {
139 signed: SignedQuaiTransaction,
140 matches_request: bool,
141 inclusion: Option<Inclusion>,
142}
143impl WalletSendObservation {
144 pub fn signed(&self) -> &SignedQuaiTransaction {
146 &self.signed
147 }
148 pub fn matches_request(&self) -> bool {
150 self.matches_request
151 }
152 pub fn inclusion(&self) -> Option<Inclusion> {
154 self.inclusion
155 }
156}
157impl InjectedProvider {
158 pub async fn send_quai_transaction(
164 &self,
165 address: QuaiAddress,
166 transaction: &QuaiTransaction,
167 ) -> Result<WalletSendAcknowledgement, WalletSendError> {
168 let params = self
169 .transaction_params(address, transaction, "quai_sendTransaction")
170 .map_err(WalletSendError::Preflight)?;
171 let identity =
172 WalletSendIdentity::new(address, transaction).map_err(WalletSendError::Preflight)?;
173 let revision = self.context_revision();
174 self.check_chain()
175 .await
176 .map_err(WalletSendError::Preflight)?;
177 let accounts = Self::accounts_from(
178 self.raw("quai_accounts", json!([]))
179 .await
180 .map_err(WalletSendError::Preflight)?,
181 )
182 .map_err(WalletSendError::Preflight)?;
183 if !accounts.contains(&address) {
184 return Err(WalletSendError::Preflight(BrowserError::AccountUnavailable));
185 }
186 if provider_changed(&self.watch.0, revision) {
187 return Err(WalletSendError::Preflight(BrowserError::ContextChanged));
188 }
189 let value = self
190 .raw("quai_sendTransaction", params)
191 .await
192 .map_err(|source| WalletSendError::Ambiguous {
193 identity,
194 reported_hash: None,
195 source,
196 })?;
197 let reported_hash = value.as_str().and_then(|s| s.parse::<Hash32>().ok());
198 self.verify_context(revision, Some(address))
199 .await
200 .map_err(|source| WalletSendError::Ambiguous {
201 identity,
202 reported_hash,
203 source,
204 })?;
205 let valid = reported_hash
206 .filter(|hash| valid_hash(address, *hash))
207 .ok_or(WalletSendError::Ambiguous {
208 identity,
209 reported_hash,
210 source: BrowserError::InvalidResult,
211 })?;
212 Ok(WalletSendAcknowledgement {
213 identity,
214 reported_hash: valid,
215 requested: transaction.clone(),
216 })
217 }
218}
219fn valid_hash(address: QuaiAddress, hash: Hash32) -> bool {
220 let bytes = hash.bytes();
221 hash != Hash32::ZERO
222 && bytes[0] == address.zone().byte()
223 && bytes[2] == address.zone().byte()
224 && bytes[1] & 0x80 == 0
225 && bytes[3] & 0x80 == 0
226}