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