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    BridgeEventDeprecated = 6, /* Deprecated. Should not be reused. Introduced for bridge
70                                * purposes but was never included in messages. */
71    ConsensusBlock = 7, // Used for consensus authority signature on block's digest.
72    DiscoveryPeers = 8, // Used for reporting peer addresses in discovery
73}
74
75impl TryFrom<u8> for IntentScope {
76    type Error = eyre::Report;
77    fn try_from(value: u8) -> Result<Self, Self::Error> {
78        bcs::from_bytes(&[value]).map_err(|_| eyre!("Invalid IntentScope"))
79    }
80}
81
82/// An intent is a compact struct serves as the domain separator for a message
83/// that a signature commits to. It consists of three parts: [enum IntentScope]
84/// (what the type of the message is), [enum IntentVersion], [enum AppId] (what
85/// application that the signature refers to). It is used to construct [struct
86/// IntentMessage] that what a signature commits to.
87///
88/// The serialization of an Intent is a 3-byte array where each field is
89/// represented by a byte.
90#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)]
91pub struct Intent {
92    pub scope: IntentScope,
93    pub version: IntentVersion,
94    pub app_id: AppId,
95}
96
97impl Intent {
98    pub fn to_bytes(&self) -> [u8; INTENT_PREFIX_LENGTH] {
99        [self.scope as u8, self.version as u8, self.app_id as u8]
100    }
101
102    pub fn from_bytes(bytes: &[u8]) -> Result<Self, eyre::Report> {
103        if bytes.len() != INTENT_PREFIX_LENGTH {
104            return Err(eyre!("Invalid Intent"));
105        }
106        Ok(Self {
107            scope: bytes[0].try_into()?,
108            version: bytes[1].try_into()?,
109            app_id: bytes[2].try_into()?,
110        })
111    }
112}
113
114impl FromStr for Intent {
115    type Err = eyre::Report;
116    fn from_str(s: &str) -> Result<Self, Self::Err> {
117        let bytes: Vec<u8> = decode_bytes_hex(s).map_err(|_| eyre!("Invalid Intent"))?;
118        Self::from_bytes(bytes.as_slice())
119    }
120}
121
122impl Intent {
123    pub fn iota_app(scope: IntentScope) -> Self {
124        Self {
125            version: IntentVersion::V0,
126            scope,
127            app_id: AppId::Iota,
128        }
129    }
130
131    pub fn iota_transaction() -> Self {
132        Self {
133            scope: IntentScope::TransactionData,
134            version: IntentVersion::V0,
135            app_id: AppId::Iota,
136        }
137    }
138
139    pub fn personal_message() -> Self {
140        Self {
141            scope: IntentScope::PersonalMessage,
142            version: IntentVersion::V0,
143            app_id: AppId::Iota,
144        }
145    }
146
147    pub fn consensus_app(scope: IntentScope) -> Self {
148        Self {
149            scope,
150            version: IntentVersion::V0,
151            app_id: AppId::Consensus,
152        }
153    }
154}
155
156/// Intent Message is a wrapper around a message with its intent. The message
157/// can be any type that implements [trait Serialize]. *ALL* signatures in IOTA
158/// must commits to the intent message, not the message itself. This guarantees
159/// any intent message signed in the system cannot collide with another since
160/// they are domain separated by intent.
161///
162/// The serialization of an IntentMessage is compact: it only appends three
163/// bytes to the message itself.
164#[derive(Debug, PartialEq, Eq, Serialize, Clone, Hash, Deserialize)]
165pub struct IntentMessage<T> {
166    pub intent: Intent,
167    pub value: T,
168}
169
170impl<T> IntentMessage<T> {
171    pub fn new(intent: Intent, value: T) -> Self {
172        Self { intent, value }
173    }
174}
175
176/// A person message that wraps around a byte array.
177#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
178pub struct PersonalMessage {
179    pub message: Vec<u8>,
180}
181
182pub trait SecureIntent: Serialize + private::SealedIntent {}
183
184pub(crate) mod private {
185    use super::IntentMessage;
186
187    pub trait SealedIntent {}
188    impl<T> SealedIntent for IntentMessage<T> {}
189}
190
191/// A 1-byte domain separator for hashing Object ID in IOTA. It is starting from
192/// 0xf0 to ensure no hashing collision for any ObjectID vs IotaAddress which is
193/// derived as the hash of `flag || pubkey`. See
194/// `iota_types::crypto::SignatureScheme::flag()`.
195#[derive(Serialize_repr, Deserialize_repr, Copy, Clone, PartialEq, Eq, Debug, Hash)]
196#[repr(u8)]
197pub enum HashingIntentScope {
198    ChildObjectId = 0xf0,
199    RegularObjectId = 0xf1,
200}