sui_crypto/
lib.rs

1#![cfg_attr(doc_cfg, feature(doc_cfg))]
2
3use sui_sdk_types::PersonalMessage;
4use sui_sdk_types::Transaction;
5use sui_sdk_types::UserSignature;
6
7pub use signature::Error as SignatureError;
8pub use signature::Signer;
9pub use signature::Verifier;
10
11#[cfg(feature = "bls12381")]
12#[cfg_attr(doc_cfg, doc(cfg(feature = "bls12381")))]
13pub mod bls12381;
14
15#[cfg(feature = "ed25519")]
16#[cfg_attr(doc_cfg, doc(cfg(feature = "ed25519")))]
17pub mod ed25519;
18
19#[cfg(feature = "secp256k1")]
20#[cfg_attr(doc_cfg, doc(cfg(feature = "secp256k1")))]
21pub mod secp256k1;
22
23#[cfg(feature = "secp256r1")]
24#[cfg_attr(doc_cfg, doc(cfg(feature = "secp256r1")))]
25pub mod secp256r1;
26
27#[cfg(feature = "passkey")]
28#[cfg_attr(doc_cfg, doc(cfg(feature = "passkey")))]
29pub mod passkey;
30
31#[cfg(feature = "zklogin")]
32#[cfg_attr(doc_cfg, doc(cfg(feature = "zklogin")))]
33pub mod zklogin;
34
35#[cfg(any(
36    feature = "ed25519",
37    feature = "secp256r1",
38    feature = "secp256k1",
39    feature = "zklogin"
40))]
41#[cfg_attr(
42    doc_cfg,
43    doc(cfg(any(
44        feature = "ed25519",
45        feature = "secp256r1",
46        feature = "secp256k1",
47        feature = "zklogin"
48    )))
49)]
50pub mod simple;
51
52#[cfg(any(
53    feature = "ed25519",
54    feature = "secp256r1",
55    feature = "secp256k1",
56    feature = "zklogin"
57))]
58#[cfg_attr(
59    doc_cfg,
60    doc(cfg(any(
61        feature = "ed25519",
62        feature = "secp256r1",
63        feature = "secp256k1",
64        feature = "zklogin"
65    )))
66)]
67pub mod multisig;
68
69#[cfg(any(
70    feature = "ed25519",
71    feature = "secp256r1",
72    feature = "secp256k1",
73    feature = "zklogin"
74))]
75#[cfg_attr(
76    doc_cfg,
77    doc(cfg(any(
78        feature = "ed25519",
79        feature = "secp256r1",
80        feature = "secp256k1",
81        feature = "zklogin"
82    )))
83)]
84#[doc(inline)]
85pub use multisig::UserSignatureVerifier;
86
87/// Interface for signing user transactions and messages in Sui
88///
89/// # Note
90///
91/// There is a blanket implementation of `SuiSigner` for all `T` where `T:
92/// `[`Signer`]`<`[`UserSignature`]`>` so it is generally recommended for a signer to implement
93/// `Signer<UserSignature>` and rely on the blanket implementation which handles the proper
94/// construction of the signing message.
95pub trait SuiSigner {
96    fn sign_transaction(&self, transaction: &Transaction) -> Result<UserSignature, SignatureError>;
97    fn sign_personal_message(
98        &self,
99        message: &PersonalMessage<'_>,
100    ) -> Result<UserSignature, SignatureError>;
101}
102
103impl<T: Signer<UserSignature>> SuiSigner for T {
104    fn sign_transaction(&self, transaction: &Transaction) -> Result<UserSignature, SignatureError> {
105        let msg = transaction.signing_digest();
106        self.try_sign(&msg)
107    }
108
109    fn sign_personal_message(
110        &self,
111        message: &PersonalMessage<'_>,
112    ) -> Result<UserSignature, SignatureError> {
113        let msg = message.signing_digest();
114        self.try_sign(&msg)
115    }
116}
117
118/// Interface for verifying user transactions and messages in Sui
119///
120/// # Note
121///
122/// There is a blanket implementation of `SuiVerifier` for all `T` where `T:
123/// `[`Verifier`]`<`[`UserSignature`]`>` so it is generally recommended for a signer to implement
124/// `Verifier<UserSignature>` and rely on the blanket implementation which handles the proper
125/// construction of the signing message.
126pub trait SuiVerifier {
127    fn verify_transaction(
128        &self,
129        transaction: &Transaction,
130        signature: &UserSignature,
131    ) -> Result<(), SignatureError>;
132    fn verify_personal_message(
133        &self,
134        message: &PersonalMessage<'_>,
135        signature: &UserSignature,
136    ) -> Result<(), SignatureError>;
137}
138
139impl<T: Verifier<UserSignature>> SuiVerifier for T {
140    fn verify_transaction(
141        &self,
142        transaction: &Transaction,
143        signature: &UserSignature,
144    ) -> Result<(), SignatureError> {
145        let message = transaction.signing_digest();
146        self.verify(&message, signature)
147    }
148
149    fn verify_personal_message(
150        &self,
151        message: &PersonalMessage<'_>,
152        signature: &UserSignature,
153    ) -> Result<(), SignatureError> {
154        let message = message.signing_digest();
155        self.verify(&message, signature)
156    }
157}