sui_types/
coin_reservation.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module defines the protocol for specifying an address balance reservation
5//! via an ObjectRef, in order to provide backward compatibility for clients that do
6//! not understand address balances.
7//!
8//! The layout of the reservation ObjectRef is as follows:
9//!
10//!    (ObjectID, SequenceNumber, ObjectDigest)
11//!
12//! The ObjectID points to an accumulator object (i.e. a dynamic field of the accumulator root object).
13//! This identifies both the owner and type (e.g. SUI, USDC, etc) of the balance being spent.
14//!
15//! It is masked by XORing with the current chain identifier (i.e. genesis checkpoint digest).
16//! This prevents cross-chain replay, as an attacker would have to mine an address and currency
17//! type such that `dynamic_field_key(address, type) = V` such that
18//! `V ^ FOREIGN_CHAIN_IDENTIFIER = TARGET_ACCUMULATOR_OBJECT_ID ^ TARGET_CHAIN_IDENTIFIER`
19//! and then trick the target into signing a transaction as V on the foreign chain.
20//!
21//! The masking also allows read APIs to positively identify attempts to read a "fake" object ID, as
22//! follows:
23//!   1. First, read the requested object ID.
24//!   2. If it does not exist, unmask the ID using the local chain identifier and read it again.
25//!   3. If it exists on the second attempt, the ID must have originated by masking an accumulator object ID.
26//!
27//! The SequenceNumber is a monotonically increasing version number, typically the version of the
28//! accumulator root object. It is not used by the protocol, but is intended to help the
29//! caching behavior of old clients.
30//!
31//! ObjectDigest contains the remainder of the payload:
32//!
33//! 1. The amount of the reservation [8 bytes]
34//! 2. The epoch(s) in which the tx is valid [4 bytes] (good enough for 12 million years of 24 hour epochs).
35//! 3. A magic number to identify this ObjectRef as a coin reservation [20 bytes].
36
37use std::sync::Arc;
38
39use move_core_types::language_storage::TypeTag;
40use thiserror::Error;
41
42use crate::{
43    accumulator_root::{AccumulatorKey, AccumulatorValue},
44    base_types::{ObjectID, ObjectRef, SequenceNumber, SuiAddress},
45    committee::EpochId,
46    digests::{ChainIdentifier, ObjectDigest},
47    error::{UserInputError, UserInputResult},
48    storage::ChildObjectResolver,
49    transaction::FundsWithdrawalArg,
50};
51
52macro_rules! invalid_res_error {
53    ($($args:tt)*) => {
54        UserInputError::InvalidWithdrawReservation {
55            error: format!($($args)*),
56        }
57    };
58}
59
60/// Trait for resolving funds withdrawal from a coin reservation
61pub trait CoinReservationResolverTrait {
62    // Used to check validity of the transaction. If the coin_reservation does not
63    // point to an existing accumulator object, the transaction will be rejected.
64    fn resolve_funds_withdrawal(
65        &self,
66        // Note: must be the sender. We do not support sponsorship.
67        sender: SuiAddress,
68        coin_reservation: ParsedObjectRefWithdrawal,
69        // The version of the accumulator root object to use for MVCC lookup.
70        // If None, use the latest version.
71        accumulator_version: Option<SequenceNumber>,
72    ) -> UserInputResult<FundsWithdrawalArg>;
73}
74
75pub const COIN_RESERVATION_MAGIC: [u8; 20] = [
76    0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac, 0xac,
77    0xac, 0xac, 0xac, 0xac,
78];
79
80#[derive(Clone, Copy, PartialEq, Eq, Debug)]
81pub struct ParsedDigest {
82    epoch_id: u32,
83    reservation_amount: u64,
84}
85
86impl ParsedDigest {
87    pub fn epoch_id(&self) -> EpochId {
88        self.epoch_id as EpochId
89    }
90
91    pub fn reservation_amount(&self) -> u64 {
92        self.reservation_amount
93    }
94
95    pub fn is_coin_reservation_digest(digest: &ObjectDigest) -> bool {
96        let inner = digest.inner();
97        // check if the last 20 bytes of digest match the magic number
98        let last_20_bytes: &[u8; 20] = inner[12..32].try_into().unwrap();
99        *last_20_bytes == COIN_RESERVATION_MAGIC
100    }
101}
102
103#[derive(Debug, Error)]
104#[error("Invalid digest")]
105pub struct ParsedDigestError;
106
107impl TryFrom<ObjectDigest> for ParsedDigest {
108    type Error = ParsedDigestError;
109
110    fn try_from(digest: ObjectDigest) -> Result<Self, Self::Error> {
111        if ParsedDigest::is_coin_reservation_digest(&digest) {
112            let inner = digest.inner();
113            let reservation_amount_bytes: &[u8; 8] = inner[0..8].try_into().unwrap();
114            let epoch_bytes: &[u8; 4] = inner[8..12].try_into().unwrap();
115
116            let epoch_id = u32::from_le_bytes(*epoch_bytes);
117            let reservation_amount = u64::from_le_bytes(*reservation_amount_bytes);
118
119            Ok(Self {
120                epoch_id,
121                reservation_amount,
122            })
123        } else {
124            Err(ParsedDigestError)
125        }
126    }
127}
128
129impl From<ParsedDigest> for ObjectDigest {
130    fn from(parsed: ParsedDigest) -> Self {
131        let mut inner = [0; 32];
132        inner[0..8].copy_from_slice(&parsed.reservation_amount.to_le_bytes());
133        inner[8..12].copy_from_slice(&parsed.epoch_id.to_le_bytes());
134        inner[12..32].copy_from_slice(&COIN_RESERVATION_MAGIC);
135        ObjectDigest::new(inner)
136    }
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub struct ParsedObjectRefWithdrawal {
141    pub unmasked_object_id: ObjectID,
142    pub parsed_digest: ParsedDigest,
143}
144
145impl ParsedObjectRefWithdrawal {
146    pub fn new(unmasked_object_id: ObjectID, epoch_id: EpochId, reservation_amount: u64) -> Self {
147        Self {
148            unmasked_object_id,
149            parsed_digest: ParsedDigest {
150                epoch_id: epoch_id.try_into().unwrap(),
151                reservation_amount,
152            },
153        }
154    }
155
156    pub fn reservation_amount(&self) -> u64 {
157        self.parsed_digest.reservation_amount()
158    }
159
160    pub fn epoch_id(&self) -> EpochId {
161        self.parsed_digest.epoch_id()
162    }
163
164    pub fn encode(&self, version: SequenceNumber, chain_identifier: ChainIdentifier) -> ObjectRef {
165        let digest = self.parsed_digest.into();
166        let masked_id = mask_or_unmask_id(self.unmasked_object_id, chain_identifier);
167        (masked_id, version, digest)
168    }
169
170    pub fn parse(object_ref: &ObjectRef, chain_identifier: ChainIdentifier) -> Option<Self> {
171        let (object_id, _version, digest) = object_ref;
172        let parsed_digest = ParsedDigest::try_from(*digest).ok()?;
173
174        let unmasked_object_id = mask_or_unmask_id(*object_id, chain_identifier);
175
176        Some(ParsedObjectRefWithdrawal {
177            unmasked_object_id,
178            parsed_digest,
179        })
180    }
181}
182
183pub fn mask_or_unmask_id(object_id: ObjectID, chain_identifier: ChainIdentifier) -> ObjectID {
184    let mask_bytes: &[u8; 32] = chain_identifier.as_bytes();
185
186    let object_id_bytes: [u8; 32] = object_id.into_bytes();
187    let mut masked_object_id_bytes = [0; 32];
188    for i in 0..32 {
189        masked_object_id_bytes[i] = object_id_bytes[i] ^ mask_bytes[i];
190    }
191    ObjectID::new(masked_object_id_bytes)
192}
193
194/// Creates a fake ObjectRef representing an address balance, suitable for returning from
195/// JSON-RPC APIs to backward-compatible clients. The object_id is masked with the chain
196/// identifier to prevent cross-chain replay.
197pub fn encode_object_ref(
198    unmasked_object_id: ObjectID,
199    version: SequenceNumber,
200    epoch: EpochId,
201    balance: u64,
202    chain_identifier: ChainIdentifier,
203) -> ObjectRef {
204    ParsedObjectRefWithdrawal::new(unmasked_object_id, epoch, balance)
205        .encode(version, chain_identifier)
206}
207
208/// Resolves coin reservations by looking up the accumulator object to determine
209/// the owner and type of the balance being withdrawn.
210pub struct CoinReservationResolver {
211    child_object_resolver: Arc<dyn ChildObjectResolver + Send + Sync>,
212}
213
214impl CoinReservationResolver {
215    pub fn new(child_object_resolver: Arc<dyn ChildObjectResolver + Send + Sync>) -> Self {
216        Self {
217            child_object_resolver,
218        }
219    }
220
221    /// Looks up the type tag and owner for a given accumulator object ID.
222    /// Returns `Ok(Some((owner, type_tag)))` if the object exists and is a valid balance
223    /// accumulator field, `Ok(None)` if the object does not exist (a transient condition
224    /// on this node), and `Err(_)` for any other failure (which is permanent for this
225    /// `object_id` and therefore safe to cache).
226    pub fn get_owner_and_type_for_object(
227        &self,
228        object_id: ObjectID,
229        accumulator_version: Option<SequenceNumber>,
230    ) -> UserInputResult<Option<(SuiAddress, TypeTag)>> {
231        let Some(object) = AccumulatorValue::load_object_by_id(
232            self.child_object_resolver.as_ref(),
233            accumulator_version,
234            object_id,
235        )
236        .map_err(|e| invalid_res_error!("could not load coin reservation object id {}", e))?
237        else {
238            return Ok(None);
239        };
240
241        let move_object = object.data.try_as_move().unwrap();
242
243        let type_tag: TypeTag = move_object
244            .type_()
245            .balance_accumulator_field_type_maybe()
246            .ok_or_else(|| {
247                invalid_res_error!(
248                    "coin reservation object id {} is not a balance accumulator field",
249                    object_id
250                )
251            })?;
252
253        let (key, _): (AccumulatorKey, AccumulatorValue) = move_object
254            .try_into()
255            .map_err(|e| invalid_res_error!("could not load coin reservation object id {}", e))?;
256
257        Ok(Some((key.owner, type_tag)))
258    }
259
260    pub fn resolve_funds_withdrawal(
261        &self,
262        sender: SuiAddress,
263        coin_reservation: ParsedObjectRefWithdrawal,
264        accumulator_version: Option<SequenceNumber>,
265    ) -> UserInputResult<FundsWithdrawalArg> {
266        let (owner, type_tag) = self
267            .get_owner_and_type_for_object(
268                coin_reservation.unmasked_object_id,
269                accumulator_version,
270            )?
271            .ok_or_else(|| {
272                invalid_res_error!(
273                    "coin reservation object id {} not found",
274                    coin_reservation.unmasked_object_id
275                )
276            })?;
277
278        if sender != owner {
279            return Err(invalid_res_error!(
280                "coin reservation object id {} is owned by {}, not sender {}",
281                coin_reservation.unmasked_object_id,
282                owner,
283                sender
284            ));
285        }
286
287        Ok(FundsWithdrawalArg::balance_from_sender(
288            coin_reservation.reservation_amount(),
289            type_tag,
290        ))
291    }
292}
293
294impl CoinReservationResolverTrait for CoinReservationResolver {
295    fn resolve_funds_withdrawal(
296        &self,
297        sender: SuiAddress,
298        coin_reservation: ParsedObjectRefWithdrawal,
299        accumulator_version: Option<SequenceNumber>,
300    ) -> UserInputResult<FundsWithdrawalArg> {
301        CoinReservationResolver::resolve_funds_withdrawal(
302            self,
303            sender,
304            coin_reservation,
305            accumulator_version,
306        )
307    }
308}
309
310impl CoinReservationResolverTrait for &'_ CoinReservationResolver {
311    fn resolve_funds_withdrawal(
312        &self,
313        sender: SuiAddress,
314        coin_reservation: ParsedObjectRefWithdrawal,
315        accumulator_version: Option<SequenceNumber>,
316    ) -> UserInputResult<FundsWithdrawalArg> {
317        CoinReservationResolver::resolve_funds_withdrawal(
318            self,
319            sender,
320            coin_reservation,
321            accumulator_version,
322        )
323    }
324}
325#[cfg(test)]
326mod tests {
327    use super::*;
328
329    #[test]
330    fn test_parse_normal_digest() {
331        let digest = ObjectDigest::new([0; 32]);
332        assert!(ParsedDigest::try_from(digest).is_err());
333    }
334
335    #[test]
336    fn test_is_coin_reservation_digest() {
337        let digest = ObjectDigest::random();
338        assert!(!ParsedDigest::is_coin_reservation_digest(&digest));
339
340        let digest = ParsedDigest {
341            epoch_id: 42,
342            reservation_amount: 1232348999,
343        }
344        .into();
345        assert!(ParsedDigest::is_coin_reservation_digest(&digest));
346    }
347
348    #[test]
349    fn test_encode_and_parse_digest() {
350        let parsed_digest = ParsedDigest {
351            epoch_id: 42,
352            reservation_amount: 1232348999,
353        };
354
355        let digest = ObjectDigest::from(parsed_digest);
356        assert_eq!(parsed_digest, ParsedDigest::try_from(digest).unwrap());
357    }
358
359    #[test]
360    fn test_parse_object_ref() {
361        let object_ref = (
362            ObjectID::new([0; 32]),
363            SequenceNumber::new(),
364            ObjectDigest::new([0; 32]),
365        );
366
367        assert!(
368            ParsedObjectRefWithdrawal::parse(&object_ref, ChainIdentifier::default()).is_none()
369        );
370    }
371
372    #[test]
373    fn test_parse_object_ref_with_valid_digest() {
374        let chain_id = ChainIdentifier::random();
375
376        let id = ObjectID::random();
377        let parsed_obj_ref = ParsedObjectRefWithdrawal {
378            unmasked_object_id: id,
379            parsed_digest: ParsedDigest {
380                epoch_id: 42,
381                reservation_amount: 1232348999,
382            },
383        };
384        let encoded_obj_ref = parsed_obj_ref.encode(SequenceNumber::new(), chain_id);
385
386        assert_ne!(encoded_obj_ref.0, id, "object id should be masked");
387
388        let parsed_obj_ref = ParsedObjectRefWithdrawal::parse(&encoded_obj_ref, chain_id).unwrap();
389        assert_eq!(parsed_obj_ref.unmasked_object_id, id);
390        assert_eq!(parsed_obj_ref.parsed_digest.epoch_id, 42);
391        assert_eq!(parsed_obj_ref.parsed_digest.reservation_amount, 1232348999);
392    }
393}