af_sui_types/
hash.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use sui_sdk_types::hash::Hasher;
use sui_sdk_types::{Intent, IntentAppId, IntentScope, IntentVersion, SigningDigest};

use crate::sui::transaction::ObjectArg;
use crate::{Digest, Object, ObjectDigest, Owner, TransactionData, TransactionDigest};

impl Object {
    pub fn digest(&self) -> ObjectDigest {
        const SALT: &str = "Object::";
        let digest = type_digest(SALT, self);
        ObjectDigest::new(digest.into_inner())
    }

    /// Input for transactions that interact with this object.
    pub fn object_arg(&self, mutable: bool) -> ObjectArg {
        use Owner::*;
        let id = self.id();
        match self.owner {
            AddressOwner(_) | ObjectOwner(_) | Immutable => {
                ObjectArg::ImmOrOwnedObject((id, self.version(), self.digest()))
            }
            Shared {
                initial_shared_version,
            }
            | ConsensusV2 {
                start_version: initial_shared_version,
                ..
            } => ObjectArg::SharedObject {
                id,
                initial_shared_version,
                mutable,
            },
        }
    }
}

impl TransactionData {
    pub fn digest(&self) -> TransactionDigest {
        const SALT: &str = "TransactionData::";
        let digest = type_digest(SALT, self);
        TransactionDigest::new(digest.into_inner())
    }
}

fn type_digest<T: serde::Serialize>(salt: &str, ty: &T) -> Digest {
    let mut hasher = Hasher::new();
    hasher.update(salt);
    bcs::serialize_into(&mut hasher, ty).expect("All types used are BCS-compatible");
    Digest::new(hasher.finalize().into_inner())
}

impl TransactionData {
    pub fn signing_digest(&self) -> SigningDigest {
        const INTENT: Intent = Intent {
            scope: IntentScope::TransactionData,
            version: IntentVersion::V0,
            app_id: IntentAppId::Sui,
        };
        let digest = signing_digest(INTENT, self);
        digest.into_inner()
    }
}

fn signing_digest<T: serde::Serialize + ?Sized>(intent: Intent, ty: &T) -> sui_sdk_types::Digest {
    let mut hasher = Hasher::new();
    hasher.update(intent.to_bytes());
    bcs::serialize_into(&mut hasher, ty).expect("T is BCS-compatible");
    hasher.finalize()
}