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 (owner, type_tag) if the object exists and is a valid balance accumulator field.
223    pub fn get_owner_and_type_for_object(
224        &self,
225        object_id: ObjectID,
226        accumulator_version: Option<SequenceNumber>,
227    ) -> UserInputResult<(SuiAddress, TypeTag)> {
228        let object = AccumulatorValue::load_object_by_id(
229            self.child_object_resolver.as_ref(),
230            accumulator_version,
231            object_id,
232        )
233        .map_err(|e| invalid_res_error!("could not load coin reservation object id {}", e))?
234        .ok_or_else(|| invalid_res_error!("coin reservation object id {} not found", object_id))?;
235
236        let move_object = object.data.try_as_move().unwrap();
237
238        let type_tag: TypeTag = move_object
239            .type_()
240            .balance_accumulator_field_type_maybe()
241            .ok_or_else(|| {
242                invalid_res_error!(
243                    "coin reservation object id {} is not a balance accumulator field",
244                    object_id
245                )
246            })?;
247
248        let (key, _): (AccumulatorKey, AccumulatorValue) = move_object
249            .try_into()
250            .map_err(|e| invalid_res_error!("could not load coin reservation object id {}", e))?;
251
252        Ok((key.owner, type_tag))
253    }
254
255    pub fn resolve_funds_withdrawal(
256        &self,
257        sender: SuiAddress,
258        coin_reservation: ParsedObjectRefWithdrawal,
259        accumulator_version: Option<SequenceNumber>,
260    ) -> UserInputResult<FundsWithdrawalArg> {
261        let (owner, type_tag) = self.get_owner_and_type_for_object(
262            coin_reservation.unmasked_object_id,
263            accumulator_version,
264        )?;
265
266        if sender != owner {
267            return Err(invalid_res_error!(
268                "coin reservation object id {} is owned by {}, not sender {}",
269                coin_reservation.unmasked_object_id,
270                owner,
271                sender
272            ));
273        }
274
275        Ok(FundsWithdrawalArg::balance_from_sender(
276            coin_reservation.reservation_amount(),
277            type_tag,
278        ))
279    }
280}
281
282impl CoinReservationResolverTrait for CoinReservationResolver {
283    fn resolve_funds_withdrawal(
284        &self,
285        sender: SuiAddress,
286        coin_reservation: ParsedObjectRefWithdrawal,
287        accumulator_version: Option<SequenceNumber>,
288    ) -> UserInputResult<FundsWithdrawalArg> {
289        CoinReservationResolver::resolve_funds_withdrawal(
290            self,
291            sender,
292            coin_reservation,
293            accumulator_version,
294        )
295    }
296}
297
298impl CoinReservationResolverTrait for &'_ CoinReservationResolver {
299    fn resolve_funds_withdrawal(
300        &self,
301        sender: SuiAddress,
302        coin_reservation: ParsedObjectRefWithdrawal,
303        accumulator_version: Option<SequenceNumber>,
304    ) -> UserInputResult<FundsWithdrawalArg> {
305        CoinReservationResolver::resolve_funds_withdrawal(
306            self,
307            sender,
308            coin_reservation,
309            accumulator_version,
310        )
311    }
312}
313#[cfg(test)]
314mod tests {
315    use super::*;
316
317    #[test]
318    fn test_parse_normal_digest() {
319        let digest = ObjectDigest::new([0; 32]);
320        assert!(ParsedDigest::try_from(digest).is_err());
321    }
322
323    #[test]
324    fn test_is_coin_reservation_digest() {
325        let digest = ObjectDigest::random();
326        assert!(!ParsedDigest::is_coin_reservation_digest(&digest));
327
328        let digest = ParsedDigest {
329            epoch_id: 42,
330            reservation_amount: 1232348999,
331        }
332        .into();
333        assert!(ParsedDigest::is_coin_reservation_digest(&digest));
334    }
335
336    #[test]
337    fn test_encode_and_parse_digest() {
338        let parsed_digest = ParsedDigest {
339            epoch_id: 42,
340            reservation_amount: 1232348999,
341        };
342
343        let digest = ObjectDigest::from(parsed_digest);
344        assert_eq!(parsed_digest, ParsedDigest::try_from(digest).unwrap());
345    }
346
347    #[test]
348    fn test_parse_object_ref() {
349        let object_ref = (
350            ObjectID::new([0; 32]),
351            SequenceNumber::new(),
352            ObjectDigest::new([0; 32]),
353        );
354
355        assert!(
356            ParsedObjectRefWithdrawal::parse(&object_ref, ChainIdentifier::default()).is_none()
357        );
358    }
359
360    #[test]
361    fn test_parse_object_ref_with_valid_digest() {
362        let chain_id = ChainIdentifier::random();
363
364        let id = ObjectID::random();
365        let parsed_obj_ref = ParsedObjectRefWithdrawal {
366            unmasked_object_id: id,
367            parsed_digest: ParsedDigest {
368                epoch_id: 42,
369                reservation_amount: 1232348999,
370            },
371        };
372        let encoded_obj_ref = parsed_obj_ref.encode(SequenceNumber::new(), chain_id);
373
374        assert_ne!(encoded_obj_ref.0, id, "object id should be masked");
375
376        let parsed_obj_ref = ParsedObjectRefWithdrawal::parse(&encoded_obj_ref, chain_id).unwrap();
377        assert_eq!(parsed_obj_ref.unmasked_object_id, id);
378        assert_eq!(parsed_obj_ref.parsed_digest.epoch_id, 42);
379        assert_eq!(parsed_obj_ref.parsed_digest.reservation_amount, 1232348999);
380    }
381}