miden_client/note_transport/mod.rs
1pub mod errors;
2pub mod generated;
3#[cfg(feature = "tonic")]
4pub mod grpc;
5
6use alloc::boxed::Box;
7use alloc::collections::{BTreeMap, BTreeSet};
8use alloc::string::String;
9use alloc::sync::Arc;
10use alloc::vec::Vec;
11
12use futures::Stream;
13use miden_protocol::address::Address;
14use miden_protocol::block::BlockNumber;
15use miden_protocol::note::{Note, NoteDetails, NoteDetailsCommitment, NoteHeader, NoteId, NoteTag};
16use miden_protocol::utils::serde::Serializable;
17use miden_standards::note::{NoteFile, NoteSyncHint};
18use miden_tx::auth::TransactionAuthenticator;
19use miden_tx::utils::serde::{
20 ByteReader,
21 ByteWriter,
22 Deserializable,
23 DeserializationError,
24 SliceReader,
25};
26
27pub use self::errors::NoteTransportError;
28use crate::sync::NoteTagSource;
29use crate::{Client, ClientError};
30
31pub const NOTE_TRANSPORT_TESTNET_ENDPOINT: &str = "https://transport.miden.io";
32pub const NOTE_TRANSPORT_DEVNET_ENDPOINT: &str = "https://transport.devnet.miden.io";
33pub const NOTE_TRANSPORT_CURSOR_STORE_SETTING: &str = "note_transport_cursor";
34
35/// Settings key for the note-transport backfill bookkeeping: a serialized `Vec<NoteTag>` of the
36/// `User`- and `Account`-source tags whose full history has already been fetched up to the global
37/// cursor. [`Client::sync_note_transport`] diffs the currently tracked tags against this set to
38/// find tags added after the cursor advanced, and backfills only those. Reusing the settings k/v
39/// avoids a Store-trait schema change while surviving process restarts.
40pub const NOTE_TRANSPORT_COVERED_TAGS_KEY: &str = "note_transport_covered_tags";
41
42/// Settings key for the durable relay outbox: a serialized `Vec<NoteInfo>` of
43/// private notes whose transport delivery has not yet succeeded.
44/// `send_private_note` appends (replacing any entry with the same note id)
45/// before relaying; [`Client::flush_relay_outbox`] drains entries that re-send
46/// successfully. Reusing the settings k/v avoids a Store-trait schema change
47/// while surviving process restarts.
48pub const NOTE_TRANSPORT_OUTBOX_KEY: &str = "note_transport_outbox";
49
50/// Client note transport methods.
51impl<AUTH> Client<AUTH> {
52 /// Check if note transport connection is configured
53 pub fn is_note_transport_enabled(&self) -> bool {
54 self.note_transport_api.is_some()
55 }
56
57 /// Returns the Note Transport client
58 ///
59 /// Errors if the note transport is not configured.
60 pub(crate) fn get_note_transport_api(
61 &self,
62 ) -> Result<Arc<dyn NoteTransportClient>, NoteTransportError> {
63 self.note_transport_api.clone().ok_or(NoteTransportError::Disabled)
64 }
65
66 /// Send a note through the note transport network.
67 ///
68 /// The note will be end-to-end encrypted (unimplemented, currently plaintext)
69 /// using the provided recipient's `address` details.
70 /// The recipient will be able to retrieve this note through the note's [`NoteTag`].
71 ///
72 /// **Durability.** The relay payload is persisted to the outbox before the
73 /// transport call. If the call fails or is interrupted, the entry stays in
74 /// the outbox and is retried on the next [`Client::flush_relay_outbox`]
75 /// (which [`Client::sync_note_transport`] runs), so a transient transport
76 /// failure does not drop the note. The receiver dedupes by note id, so a
77 /// re-send after a partial success is harmless.
78 ///
79 /// Prefer [`Client::send_private_note_with_block_hint`], which also relays a block hint so the
80 /// recipient gets deterministic delivery instead of relying on its lookback heuristic.
81 #[deprecated(
82 since = "0.15.2",
83 note = "use `Client::send_private_note_with_block_hint` to relay a block hint for deterministic delivery"
84 )]
85 pub async fn send_private_note(
86 &mut self,
87 note: Note,
88 address: &Address,
89 ) -> Result<(), ClientError> {
90 self.relay_private_note(note, address, None).await
91 }
92
93 /// Send a note through the note transport network, relaying a block hint to the recipient.
94 ///
95 /// `block_hint` is the block from which the recipient should start scanning for the note's
96 /// on-chain commitment, instead of relying on its lookback heuristic. Any block at or before
97 /// the commitment is correct, and the chain tip at send time is a safe choice. A tighter value
98 /// just means less for the recipient to scan.
99 ///
100 /// The same durability guarantees as [`Client::send_private_note`] apply: the hint is
101 /// persisted with the relay payload, so a retried send preserves it.
102 pub async fn send_private_note_with_block_hint(
103 &mut self,
104 note: Note,
105 address: &Address,
106 block_hint: BlockNumber,
107 ) -> Result<(), ClientError> {
108 self.relay_private_note(note, address, Some(block_hint)).await
109 }
110
111 /// Shared relay path for [`Client::send_private_note`] and
112 /// [`Client::send_private_note_with_block_hint`]. `block_hint` is the optional block from which
113 /// the recipient should start scanning for the note's commitment.
114 async fn relay_private_note(
115 &self,
116 note: Note,
117 _address: &Address,
118 block_hint: Option<BlockNumber>,
119 ) -> Result<(), ClientError> {
120 let api = self.get_note_transport_api()?;
121
122 let header = *note.header();
123 let note_id = header.id();
124 let details = NoteDetails::from(note);
125 let details_bytes = details.to_bytes();
126 // e2ee impl hint:
127 // address.key().encrypt(details_bytes)
128
129 // Persist the payload before the network call so a failed or
130 // interrupted `send_note` leaves a recoverable record rather than
131 // losing the only copy with the call frame. The hint travels with the
132 // entry so a retried send relays the same value.
133 let entry = NoteInfo {
134 header,
135 details_bytes: details_bytes.clone(),
136 block_hint,
137 };
138 let mut outbox = self.load_relay_outbox().await?;
139 // Replace any existing entry for this note id so the latest payload
140 // wins when a still-pending note is re-sent.
141 outbox.retain(|e| e.header.id() != note_id);
142 outbox.push(entry);
143 self.save_relay_outbox(outbox).await?;
144
145 // Dispatch to the hint-carrying API only when a hint is present, otherwise use the plain
146 // `send_note`. The transport exposes a separate method per scenario.
147 match block_hint {
148 Some(block_hint) => {
149 api.send_note_with_block_hint(header, details_bytes, block_hint).await?;
150 },
151 None => {
152 api.send_note(header, details_bytes).await?;
153 },
154 }
155
156 // Relay succeeded — drop the entry. A failed store write here is
157 // tolerable: the next flush re-sends and the receiver dedupes by note
158 // id, so a stale entry never causes loss.
159 let mut outbox = self.load_relay_outbox().await?;
160 outbox.retain(|e| e.header.id() != note_id);
161 self.save_relay_outbox(outbox).await?;
162
163 Ok(())
164 }
165
166 /// Re-attempt every relay payload in the durable outbox. Each entry is a
167 /// private note whose previous transport delivery failed. Successful
168 /// re-sends are dropped; failures are kept for the next call. Every entry
169 /// is attempted independently, so one persistently-failing note does not
170 /// block the others.
171 ///
172 /// [`Client::sync_note_transport`] runs this automatically and ignores its
173 /// error, so a relay failure can't block a sync. Callers driving retries
174 /// themselves can invoke it directly and inspect the returned error.
175 pub async fn flush_relay_outbox(&self) -> Result<(), ClientError> {
176 let api = self.get_note_transport_api()?;
177
178 let entries = self.load_relay_outbox().await?;
179 if entries.is_empty() {
180 return Ok(());
181 }
182
183 // Attempt every entry independently so a single persistently-failing
184 // note can't block the rest. The outbox holds only the caller's own
185 // failed sends, so it stays small and this is not a meaningful burst.
186 let mut remaining = Vec::new();
187 let mut last_err: Option<NoteTransportError> = None;
188
189 for entry in entries {
190 let relayed = match entry.block_hint {
191 Some(block_hint) => {
192 api.send_note_with_block_hint(
193 entry.header,
194 entry.details_bytes.clone(),
195 block_hint,
196 )
197 .await
198 },
199 None => api.send_note(entry.header, entry.details_bytes.clone()).await,
200 };
201 match relayed {
202 Ok(()) => {},
203 Err(err) => {
204 tracing::warn!(?err, "relay-outbox entry retry failed; will retry next sync");
205 remaining.push(entry);
206 last_err = Some(err);
207 },
208 }
209 }
210
211 self.save_relay_outbox(remaining).await?;
212
213 if let Some(err) = last_err {
214 return Err(err.into());
215 }
216 Ok(())
217 }
218
219 /// Load the durable relay outbox.
220 ///
221 /// Returns an empty `Vec` if the outbox key is absent. On deserialization
222 /// failure (schema mismatch or storage corruption) the entry is dropped and
223 /// an empty `Vec` is returned — leaving unreadable bytes in place would
224 /// block every subsequent relay because each sync would re-read them.
225 async fn load_relay_outbox(&self) -> Result<Vec<NoteInfo>, ClientError> {
226 let bytes = self
227 .store
228 .get_setting(String::from(NOTE_TRANSPORT_OUTBOX_KEY))
229 .await
230 .map_err(ClientError::StoreError)?;
231 let Some(bytes) = bytes else {
232 return Ok(Vec::new());
233 };
234 match Vec::<NoteInfo>::read_from_bytes(&bytes) {
235 Ok(entries) => Ok(entries),
236 Err(err) => {
237 tracing::warn!(?err, "dropping unreadable relay outbox; resetting to empty");
238 self.store
239 .remove_setting(String::from(NOTE_TRANSPORT_OUTBOX_KEY))
240 .await
241 .map_err(ClientError::StoreError)?;
242 Ok(Vec::new())
243 },
244 }
245 }
246
247 /// Persist the relay outbox, removing the key entirely when empty so the
248 /// settings table doesn't accumulate empty-vec blobs.
249 async fn save_relay_outbox(&self, entries: Vec<NoteInfo>) -> Result<(), ClientError> {
250 let key = String::from(NOTE_TRANSPORT_OUTBOX_KEY);
251 if entries.is_empty() {
252 return self.store.remove_setting(key).await.map_err(ClientError::StoreError);
253 }
254 let bytes = entries.to_bytes();
255 self.store.set_setting(key, bytes).await.map_err(ClientError::StoreError)
256 }
257
258 /// The set of tracked tags eligible for history backfill.
259 ///
260 /// Only `User`- and `Account`-source tags qualify: those are the tags a consumer explicitly
261 /// started tracking (via [`Client::add_note_tag`], account import, or address creation) and may
262 /// therefore have historical private notes sitting below the global cursor. `Note`-source tags
263 /// are created by transport delivery and note import, so backfilling them would re-fetch tags
264 /// the fetch path itself just registered; `Subscription` tags are excluded for the same reason.
265 async fn backfill_candidate_tags(&self) -> Result<BTreeSet<NoteTag>, ClientError> {
266 let tags = self
267 .store
268 .get_note_tags()
269 .await?
270 .into_iter()
271 .filter(|record| {
272 matches!(record.source, NoteTagSource::User | NoteTagSource::Account(_))
273 })
274 .map(|record| record.tag)
275 .collect();
276 Ok(tags)
277 }
278
279 /// Load the set of tags whose history has already been fetched up to the global cursor.
280 ///
281 /// Returns an empty set when the key is absent (e.g. a store that predates the feature). On a
282 /// deserialization failure the entry is dropped and an empty set is returned: re-treating every
283 /// tracked tag as new only triggers a one-off backfill, which dedupes, whereas leaving
284 /// unreadable bytes in place would fail every subsequent sync.
285 async fn load_covered_tags(&self) -> Result<BTreeSet<NoteTag>, ClientError> {
286 let bytes = self
287 .store
288 .get_setting(String::from(NOTE_TRANSPORT_COVERED_TAGS_KEY))
289 .await
290 .map_err(ClientError::StoreError)?;
291 let Some(bytes) = bytes else {
292 return Ok(BTreeSet::new());
293 };
294 match BTreeSet::<NoteTag>::read_from_bytes(&bytes) {
295 Ok(tags) => Ok(tags),
296 Err(err) => {
297 tracing::warn!(?err, "dropping unreadable covered-tags set; resetting to empty");
298 self.store
299 .remove_setting(String::from(NOTE_TRANSPORT_COVERED_TAGS_KEY))
300 .await
301 .map_err(ClientError::StoreError)?;
302 Ok(BTreeSet::new())
303 },
304 }
305 }
306
307 /// Persist the covered-tags set, removing the key entirely when empty so the settings table
308 /// doesn't accumulate empty-vec blobs.
309 async fn save_covered_tags(&self, tags: &BTreeSet<NoteTag>) -> Result<(), ClientError> {
310 let key = String::from(NOTE_TRANSPORT_COVERED_TAGS_KEY);
311 if tags.is_empty() {
312 return self.store.remove_setting(key).await.map_err(ClientError::StoreError);
313 }
314 self.store
315 .set_setting(key, tags.to_bytes())
316 .await
317 .map_err(ClientError::StoreError)
318 }
319}
320
321impl<AUTH> Client<AUTH>
322where
323 AUTH: TransactionAuthenticator + Sync + 'static,
324{
325 /// Per-sync cap on the number of newly tracked tags to backfill. Bounds the burst when many
326 /// tags are registered at once (e.g. restoring many accounts or addresses). Deferred tags stay
327 /// uncovered and are picked up on subsequent syncs.
328 pub const MAX_BACKFILL_TAGS_PER_SYNC: usize = 64;
329
330 /// Safety cap on the per-tag backfill drain. A well-behaved server eventually returns no
331 /// forward cursor progress, ending the loop; this bound only guards against a server that
332 /// advances the cursor indefinitely without ever returning an empty batch. It is far above any
333 /// honest per-tag backlog, so reaching it signals a server bug rather than real history.
334 const MAX_BACKFILL_ITERATIONS: usize = 1_000;
335
336 /// Fetch notes for tracked note tags.
337 ///
338 /// The client will query the configured note transport node for all tracked note tags.
339 /// To list tracked tags please use [`Client::get_note_tags`]. To add a new note tag please use
340 /// [`Client::add_note_tag`].
341 /// Only notes directed at your addresses will be stored and readable given the use of
342 /// end-to-end encryption (unimplemented).
343 /// Fetched notes will be stored into the client's store.
344 ///
345 /// An internal pagination mechanism is employed to reduce the number of downloaded notes: this
346 /// fetches only notes past the stored cursor. Historical notes for a newly tracked tag are
347 /// recovered automatically by [`Client::sync_note_transport`], which backfills each new tag.
348 pub async fn fetch_private_notes(&mut self) -> Result<(), ClientError> {
349 let note_tags: Vec<NoteTag> =
350 self.store.get_unique_note_tags().await?.into_iter().collect();
351 let cursor = self.store.get_note_transport_cursor().await?;
352
353 let (_, new_cursor) = self.fetch_transport_notes(cursor, ¬e_tags).await?;
354 self.store.update_note_transport_cursor(new_cursor).await?;
355
356 Ok(())
357 }
358
359 /// Backfill historical private notes for tags added after the global cursor advanced.
360 ///
361 /// The global transport cursor is shared across all tracked tags and only moves forward, so a
362 /// tag that starts being tracked late never sees its notes that already sit below the cursor.
363 /// This diffs the tracked `User`/`Account` tags (see [`Self::backfill_candidate_tags`]) against
364 /// the persisted covered set (see [`NOTE_TRANSPORT_COVERED_TAGS_KEY`]) and drains each newly
365 /// tracked tag from the start, fetching only that tag's own history rather than re-scanning
366 /// everything. Tags no longer tracked are dropped from the covered set so a later re-add
367 /// backfills again instead of resuming from a stale mark. Imports dedupe, so the overlap with
368 /// the steady-state stream is harmless.
369 ///
370 /// At most [`Self::MAX_BACKFILL_TAGS_PER_SYNC`] tags are backfilled per call; any remainder
371 /// stays uncovered and is picked up on the next sync. Returns the ids of notes imported here.
372 pub(crate) async fn backfill_new_tags(&mut self) -> Result<Vec<NoteId>, ClientError> {
373 let candidates = self.backfill_candidate_tags().await?;
374 let loaded = self.load_covered_tags().await?;
375
376 // Drop tags no longer tracked. Keeping a removed tag marked covered would make a later
377 // re-add skip its backlog, silently missing notes that arrived while it was untracked.
378 let mut covered: BTreeSet<NoteTag> = loaded.intersection(&candidates).copied().collect();
379 if covered.len() != loaded.len() {
380 self.save_covered_tags(&covered).await?;
381 }
382
383 let new_tags: Vec<NoteTag> = candidates.difference(&covered).copied().collect();
384
385 let mut imported_ids = Vec::new();
386 for tag in new_tags.into_iter().take(Self::MAX_BACKFILL_TAGS_PER_SYNC) {
387 imported_ids.extend(self.backfill_tag(tag).await?);
388 covered.insert(tag);
389 // Persist after each tag so a crash mid-backfill keeps completed tags covered. A redo
390 // is harmless because imports dedupe; the dangerous direction (marking covered before
391 // the import lands) never happens.
392 self.save_covered_tags(&covered).await?;
393 }
394
395 Ok(imported_ids)
396 }
397
398 /// Drain a single tag's full history from the transport, paging until the cursor stops
399 /// advancing. Uses a local cursor and never touches the global one, so it cannot regress
400 /// steady-state progress. Returns the ids of the notes it imported.
401 async fn backfill_tag(&mut self, tag: NoteTag) -> Result<Vec<NoteId>, ClientError> {
402 let mut imported_ids = Vec::new();
403 let mut cursor = NoteTransportCursor::init();
404 for _ in 0..Self::MAX_BACKFILL_ITERATIONS {
405 let (ids, new_cursor) = self.fetch_transport_notes(cursor, &[tag]).await?;
406 imported_ids.extend(ids);
407 // Terminate on any lack of forward progress. A well-behaved server returns
408 // `new_cursor == cursor` when there are no new notes for this tag (since
409 // `rcursor = max(cursor, max_seq_returned)`); using `<=` also handles implementations
410 // that return an `init()` cursor on empty batches (see the in-tree mock transport).
411 if new_cursor <= cursor {
412 return Ok(imported_ids);
413 }
414 cursor = new_cursor;
415 }
416
417 Err(ClientError::NoteTransportError(NoteTransportError::PaginationDidNotTerminate(
418 Self::MAX_BACKFILL_ITERATIONS,
419 )))
420 }
421
422 /// Fetch one batch of notes from the note transport network for the provided tags.
423 ///
424 /// The server paginates; this method issues one RPC and returns the imported details
425 /// commitments together with the new cursor. The returned cursor equals the input cursor when
426 /// the batch was empty (i.e. no new notes). Callers that want to drain a tag's full backlog
427 /// should loop until `new_cursor == cursor` (see [`Client::backfill_new_tags`]). Callers that
428 /// do steady-state polling (see [`Client::sync_state`] / [`Client::fetch_private_notes`])
429 /// should call this once per tick with the stored cursor.
430 ///
431 /// Downloaded notes are imported into the local store. Persistence of the returned cursor is
432 /// left to the caller so that drain loops can guard against regression of an already-advanced
433 /// stored cursor.
434 pub(crate) async fn fetch_transport_notes(
435 &mut self,
436 cursor: NoteTransportCursor,
437 tags: &[NoteTag],
438 ) -> Result<(Vec<NoteId>, NoteTransportCursor), ClientError> {
439 // Fallback lookback window, in blocks, used only for notes the transport delivered
440 // without a sender-provided block hint. Scanning back from sync height handles
441 // the race where a note is committed on-chain just before the NTL delivers its data.
442 // Without it, check_expected_notes would scan from sync_height forward and miss the
443 // already-committed note. A sender-provided hint is deterministic and always preferred.
444 const NOTE_LOOKBACK_BLOCKS: u32 = 20;
445
446 let mut notes = Vec::new();
447 // TODO: perhaps we should not need to map received IDs with details commitments, and
448 // instead we may allow `InputNoteRecord` to optionally keep NoteIds. Then within
449 // `import_note` we could match everything by ID and remove this map check
450 let mut id_by_commitment: BTreeMap<NoteDetailsCommitment, NoteId> = BTreeMap::new();
451 let (note_infos, rcursor) =
452 self.get_note_transport_api()?.fetch_notes(tags, cursor).await?;
453 for note_info in ¬e_infos {
454 // e2ee impl hint:
455 // for key in self.store.decryption_keys() try
456 // key.decrypt(details_bytes_encrypted)
457 let note = rejoin_note(¬e_info.header, ¬e_info.details_bytes)?;
458
459 // The header carries the attachment-aware (on-chain) note id; the rejoined note has
460 // empty attachments and would hash to a different id, so key off the header.
461 id_by_commitment.insert(note.details_commitment(), note_info.header.id());
462 notes.push((note, note_info.block_hint));
463 }
464
465 let sync_height = self.get_sync_height().await?;
466 let fallback_after_block_num =
467 BlockNumber::from(sync_height.as_u32().saturating_sub(NOTE_LOOKBACK_BLOCKS));
468
469 let mut note_requests = Vec::with_capacity(notes.len());
470 for (note, block_hint) in notes {
471 let tag = note.metadata().tag();
472 // Prefer the sender-provided hint, falling back to the lookback window when absent.
473 let after_block_num = block_hint.unwrap_or(fallback_after_block_num);
474 let note_file = NoteFile::ExpectedNote {
475 details: note.into(),
476 sync_hint: NoteSyncHint::new(after_block_num, tag),
477 };
478 note_requests.push(note_file);
479 }
480 let imported_commitments = self.import_notes(¬e_requests).await?;
481 let imported_ids = imported_commitments
482 .into_iter()
483 .filter_map(|commitment| id_by_commitment.get(&commitment).copied())
484 .collect();
485
486 Ok((imported_ids, rcursor))
487 }
488}
489
490/// Note transport cursor
491///
492/// Pagination integer used to reduce the number of fetched notes from the note transport network,
493/// avoiding duplicate downloads.
494#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
495pub struct NoteTransportCursor(u64);
496
497/// Note Transport update
498pub struct NoteTransportUpdate {
499 /// Pagination cursor for next fetch
500 pub cursor: NoteTransportCursor,
501 /// Fetched notes
502 pub notes: Vec<Note>,
503}
504
505impl NoteTransportCursor {
506 pub fn new(value: u64) -> Self {
507 Self(value)
508 }
509
510 pub fn init() -> Self {
511 Self::new(0)
512 }
513
514 pub fn value(&self) -> u64 {
515 self.0
516 }
517}
518
519impl From<u64> for NoteTransportCursor {
520 fn from(value: u64) -> Self {
521 Self::new(value)
522 }
523}
524
525/// The main transport client trait for sending and receiving encrypted notes
526#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
527#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
528pub trait NoteTransportClient: Send + Sync {
529 /// Send a note with optionally encrypted details
530 async fn send_note(
531 &self,
532 header: NoteHeader,
533 details: Vec<u8>,
534 ) -> Result<(), NoteTransportError>;
535
536 /// Send a note, relaying a block hint for the recipient's commitment scan.
537 ///
538 /// `block_hint` is the block from which the recipient should start scanning for the
539 /// note's commitment. The default implementation ignores it and delegates to
540 /// [`NoteTransportClient::send_note`], so existing implementors keep compiling. Transports
541 /// that can carry the hint (e.g. the gRPC client) override this.
542 async fn send_note_with_block_hint(
543 &self,
544 header: NoteHeader,
545 details: Vec<u8>,
546 _block_hint: BlockNumber,
547 ) -> Result<(), NoteTransportError> {
548 self.send_note(header, details).await
549 }
550
551 /// Fetch notes for given tags
552 ///
553 /// Downloads notes for given tags.
554 /// Returns notes labelled after the provided cursor (pagination), and an updated cursor.
555 async fn fetch_notes(
556 &self,
557 tag: &[NoteTag],
558 cursor: NoteTransportCursor,
559 ) -> Result<(Vec<NoteInfo>, NoteTransportCursor), NoteTransportError>;
560
561 /// Stream notes for a given tag
562 async fn stream_notes(
563 &self,
564 tag: NoteTag,
565 cursor: NoteTransportCursor,
566 ) -> Result<Box<dyn NoteStream>, NoteTransportError>;
567}
568
569/// Stream trait for note streaming
570pub trait NoteStream:
571 Stream<Item = Result<Vec<NoteInfo>, NoteTransportError>> + Send + Unpin
572{
573}
574
575/// Information about a note fetched from the note transport network
576#[derive(Debug, Clone)]
577pub struct NoteInfo {
578 /// Note header
579 pub header: NoteHeader,
580 /// Note details, can be encrypted
581 pub details_bytes: Vec<u8>,
582 /// Sender-provided block hint: the block from which the recipient should start scanning for
583 /// the note's on-chain commitment, instead of applying its default lookback window. `None`
584 /// when the sender did not provide a hint.
585 pub block_hint: Option<BlockNumber>,
586}
587
588impl NoteInfo {
589 /// Build a [`NoteInfo`] without a block hint (`block_hint` is `None`).
590 ///
591 /// Use the [`NoteInfo::block_hint`] field directly to attach a hint.
592 pub fn new(header: NoteHeader, details_bytes: Vec<u8>) -> Self {
593 Self { header, details_bytes, block_hint: None }
594 }
595}
596
597// SERIALIZATION
598// ================================================================================================
599
600impl Serializable for NoteInfo {
601 fn write_into<W: ByteWriter>(&self, target: &mut W) {
602 self.header.write_into(target);
603 self.details_bytes.write_into(target);
604 self.block_hint.write_into(target);
605 }
606}
607
608impl Deserializable for NoteInfo {
609 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
610 let header = NoteHeader::read_from(source)?;
611 let details_bytes = Vec::<u8>::read_from(source)?;
612 let block_hint = Option::<BlockNumber>::read_from(source)?;
613 Ok(NoteInfo { header, details_bytes, block_hint })
614 }
615}
616
617impl Serializable for NoteTransportCursor {
618 fn write_into<W: ByteWriter>(&self, target: &mut W) {
619 self.0.write_into(target);
620 }
621}
622
623impl Deserializable for NoteTransportCursor {
624 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
625 let value = u64::read_from(source)?;
626 Ok(Self::new(value))
627 }
628}
629
630fn rejoin_note(header: &NoteHeader, details_bytes: &[u8]) -> Result<Note, DeserializationError> {
631 let mut reader = SliceReader::new(details_bytes);
632 let details = NoteDetails::read_from(&mut reader)?;
633 // The transport wire format only carries `NoteHeader` + serialized `NoteDetails`, not the
634 // attachments collection. We rejoin with empty attachments; this matches the original note
635 // only when it had no attachments in the first place.
636 let partial_metadata = *header.metadata().partial_metadata();
637 Ok(Note::new(
638 details.assets().clone(),
639 partial_metadata,
640 details.recipient().clone(),
641 ))
642}