sui_sdk_types/crypto/intent.rs
1/// A Signing Intent
2///
3/// An intent is a compact struct serves as the domain separator for a message that a signature
4/// commits to. It consists of three parts:
5/// 1. [enum IntentScope] (what the type of the message is)
6/// 2. [enum IntentVersion]
7/// 3. [enum AppId] (what application that the signature refers to).
8///
9/// The serialization of an Intent is a 3-byte array where each field is represented by a byte and
10/// it is prepended onto a message before it is signed in Sui.
11///
12/// # BCS
13///
14/// The BCS serialized form for this type is defined by the following ABNF:
15///
16/// ```text
17/// intent = intent-scope intent-version intent-app-id
18/// ```
19#[derive(Clone, Copy, Debug, PartialEq, Eq)]
20pub struct Intent {
21 pub scope: IntentScope,
22 pub version: IntentVersion,
23 pub app_id: IntentAppId,
24}
25
26impl Intent {
27 pub fn new(scope: IntentScope, version: IntentVersion, app_id: IntentAppId) -> Self {
28 Self {
29 scope,
30 version,
31 app_id,
32 }
33 }
34
35 pub fn to_bytes(self) -> [u8; 3] {
36 [self.scope as u8, self.version as u8, self.app_id as u8]
37 }
38
39 pub fn scope(self) -> IntentScope {
40 self.scope
41 }
42
43 pub fn version(self) -> IntentVersion {
44 self.version
45 }
46
47 pub fn app_id(self) -> IntentAppId {
48 self.app_id
49 }
50}
51
52/// Byte signifying the scope of an [`Intent`]
53///
54/// # BCS
55///
56/// The BCS serialized form for this type is defined by the following ABNF:
57///
58/// ```text
59/// intent-scope = u8
60/// ```
61#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62#[repr(u8)]
63#[non_exhaustive]
64pub enum IntentScope {
65 TransactionData = 0, // Used for a user signature on a transaction data.
66 TransactionEffects = 1, // Used for an authority signature on transaction effects.
67 CheckpointSummary = 2, // Used for an authority signature on a checkpoint summary.
68 PersonalMessage = 3, // Used for a user signature on a personal message.
69 SenderSignedTransaction = 4, // Used for an authority signature on a user signed transaction.
70 ProofOfPossession = 5, // Used as a signature representing an authority's proof of possession of its authority protocol key.
71 HeaderDigest = 6, // Used for narwhal authority signature on header digest.
72 BridgeEventUnused = 7, // for bridge purposes but it's currently not included in messages.
73 ConsensusBlock = 8, // Used for consensus authority signature on block's digest
74}
75
76/// Byte signifying the version of an [`Intent`]
77///
78/// # BCS
79///
80/// The BCS serialized form for this type is defined by the following ABNF:
81///
82/// ```text
83/// intent-version = u8
84/// ```
85#[derive(Clone, Copy, Debug, PartialEq, Eq)]
86#[repr(u8)]
87#[non_exhaustive]
88pub enum IntentVersion {
89 V0 = 0,
90}
91
92/// Byte signifying the application id of an [`Intent`]
93///
94/// # BCS
95///
96/// The BCS serialized form for this type is defined by the following ABNF:
97///
98/// ```text
99/// intent-app-id = u8
100/// ```
101#[derive(Clone, Copy, Debug, PartialEq, Eq)]
102#[repr(u8)]
103#[non_exhaustive]
104pub enum IntentAppId {
105 Sui = 0,
106 Narwhal = 1,
107 Consensus = 2,
108}