shared_crypto/
intent.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::str::FromStr;
6
7use eyre::eyre;
8use fastcrypto::encoding::decode_bytes_hex;
9use serde::{Deserialize, Serialize};
10use serde_repr::{Deserialize_repr, Serialize_repr};
11
12pub const INTENT_PREFIX_LENGTH: usize = 3;
13
14/// The version here is to distinguish between signing different versions of the
15/// struct or enum. Serialized output between two different versions of the same
16/// struct/enum might accidentally (or maliciously on purpose) match.
17#[derive(Serialize_repr, Deserialize_repr, Copy, Clone, PartialEq, Eq, Debug, Hash)]
18#[repr(u8)]
19pub enum IntentVersion {
20    V0 = 0,
21}
22
23impl TryFrom<u8> for IntentVersion {
24    type Error = eyre::Report;
25    fn try_from(value: u8) -> Result<Self, Self::Error> {
26        bcs::from_bytes(&[value]).map_err(|_| eyre!("Invalid IntentVersion"))
27    }
28}
29
30/// This enums specifies the application ID. Two intents in two different
31/// applications (i.e., IOTA, Ethereum etc) should never collide, so
32/// that even when a signing key is reused, nobody can take a signature
33/// designated for app_1 and present it as a valid signature for an (any) intent
34/// in app_2.
35#[derive(Serialize_repr, Deserialize_repr, Copy, Clone, PartialEq, Eq, Debug, Hash)]
36#[repr(u8)]
37pub enum AppId {
38    Iota = 0,
39    Consensus = 1,
40}
41
42// TODO(joyqvq): Use num_derive
43impl TryFrom<u8> for AppId {
44    type Error = eyre::Report;
45    fn try_from(value: u8) -> Result<Self, Self::Error> {
46        bcs::from_bytes(&[value]).map_err(|_| eyre!("Invalid AppId"))
47    }
48}
49
50impl Default for AppId {
51    fn default() -> Self {
52        Self::Iota
53    }
54}
55
56/// This enums specifies the intent scope. Two intents for different scope
57/// should never collide, so no signature provided for one intent scope can be
58/// used for another, even when the serialized data itself may be the same.
59#[derive(Serialize_repr, Deserialize_repr, Copy, Clone, PartialEq, Eq, Debug, Hash)]
60#[repr(u8)]
61pub enum IntentScope {
62    TransactionData = 0,         // Used for a user signature on a transaction data.
63    TransactionEffects = 1,      // Used for an authority signature on transaction effects.
64    CheckpointSummary = 2,       // Used for an authority signature on a checkpoint summary.
65    PersonalMessage = 3,         // Used for a user signature on a personal message.
66    SenderSignedTransaction = 4, // Used for an authority signature on a user signed transaction.
67    ProofOfPossession = 5,       /* Used as a signature representing an authority's proof of
68                                  * possession of its authority key. */
69    BridgeEventUnused = 6, // for bridge purposes but it's currently not included in messages.
70    ConsensusBlock = 7,    // Used for consensus authority signature on block's digest
71}
72
73impl TryFrom<u8> for IntentScope {
74    type Error = eyre::Report;
75    fn try_from(value: u8) -> Result<Self, Self::Error> {
76        bcs::from_bytes(&[value]).map_err(|_| eyre!("Invalid IntentScope"))
77    }
78}
79
80/// An intent is a compact struct serves as the domain separator for a message
81/// that a signature commits to. It consists of three parts: [enum IntentScope]
82/// (what the type of the message is), [enum IntentVersion], [enum AppId] (what
83/// application that the signature refers to). It is used to construct [struct
84/// IntentMessage] that what a signature commits to.
85///
86/// The serialization of an Intent is a 3-byte array where each field is
87/// represented by a byte.
88#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)]
89pub struct Intent {
90    pub scope: IntentScope,
91    pub version: IntentVersion,
92    pub app_id: AppId,
93}
94
95impl Intent {
96    pub fn to_bytes(&self) -> [u8; INTENT_PREFIX_LENGTH] {
97        [self.scope as u8, self.version as u8, self.app_id as u8]
98    }
99
100    pub fn from_bytes(bytes: &[u8]) -> Result<Self, eyre::Report> {
101        if bytes.len() != INTENT_PREFIX_LENGTH {
102            return Err(eyre!("Invalid Intent"));
103        }
104        Ok(Self {
105            scope: bytes[0].try_into()?,
106            version: bytes[1].try_into()?,
107            app_id: bytes[2].try_into()?,
108        })
109    }
110}
111
112impl FromStr for Intent {
113    type Err = eyre::Report;
114    fn from_str(s: &str) -> Result<Self, Self::Err> {
115        let bytes: Vec<u8> = decode_bytes_hex(s).map_err(|_| eyre!("Invalid Intent"))?;
116        Self::from_bytes(bytes.as_slice())
117    }
118}
119
120impl Intent {
121    pub fn iota_app(scope: IntentScope) -> Self {
122        Self {
123            version: IntentVersion::V0,
124            scope,
125            app_id: AppId::Iota,
126        }
127    }
128
129    pub fn iota_transaction() -> Self {
130        Self {
131            scope: IntentScope::TransactionData,
132            version: IntentVersion::V0,
133            app_id: AppId::Iota,
134        }
135    }
136
137    pub fn personal_message() -> Self {
138        Self {
139            scope: IntentScope::PersonalMessage,
140            version: IntentVersion::V0,
141            app_id: AppId::Iota,
142        }
143    }
144
145    pub fn consensus_app(scope: IntentScope) -> Self {
146        Self {
147            scope,
148            version: IntentVersion::V0,
149            app_id: AppId::Consensus,
150        }
151    }
152}
153
154/// Intent Message is a wrapper around a message with its intent. The message
155/// can be any type that implements [trait Serialize]. *ALL* signatures in IOTA
156/// must commits to the intent message, not the message itself. This guarantees
157/// any intent message signed in the system cannot collide with another since
158/// they are domain separated by intent.
159///
160/// The serialization of an IntentMessage is compact: it only appends three
161/// bytes to the message itself.
162#[derive(Debug, PartialEq, Eq, Serialize, Clone, Hash, Deserialize)]
163pub struct IntentMessage<T> {
164    pub intent: Intent,
165    pub value: T,
166}
167
168impl<T> IntentMessage<T> {
169    pub fn new(intent: Intent, value: T) -> Self {
170        Self { intent, value }
171    }
172}
173
174/// A person message that wraps around a byte array.
175#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
176pub struct PersonalMessage {
177    pub message: Vec<u8>,
178}
179
180pub trait SecureIntent: Serialize + private::SealedIntent {}
181
182pub(crate) mod private {
183    use super::IntentMessage;
184
185    pub trait SealedIntent {}
186    impl<T> SealedIntent for IntentMessage<T> {}
187}
188
189/// A 1-byte domain separator for hashing Object ID in IOTA. It is starting from
190/// 0xf0 to ensure no hashing collision for any ObjectID vs IotaAddress which is
191/// derived as the hash of `flag || pubkey`. See
192/// `iota_types::crypto::SignatureScheme::flag()`.
193#[derive(Serialize_repr, Deserialize_repr, Copy, Clone, PartialEq, Eq, Debug, Hash)]
194#[repr(u8)]
195pub enum HashingIntentScope {
196    ChildObjectId = 0xf0,
197    RegularObjectId = 0xf1,
198}