Skip to content

Commit 898aec3

Browse files
committed
feat(models): add CapSignatureDomain
1 parent aee496e commit 898aec3

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

src/models/global_version.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ decl_global_capability! {
223223
///
224224
/// Mask: `0x400000000`
225225
CapOmitMasterBlockHistory = 34,
226+
227+
/// Apply signature domain to verified data.
228+
///
229+
/// It is a newer version of [`GlobalCapability::CapSignatureWithId`]
230+
/// and has a higher priority if both are enabled at the same time.
231+
///
232+
/// Mask: `0x800000000`
233+
CapSignatureDomain = 35,
226234
}
227235
}
228236

src/models/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Blockchain models.
22
3-
pub use account::*;
4-
pub use block::*;
5-
pub use config::*;
6-
pub use currency::*;
7-
pub use global_version::*;
8-
pub use message::*;
9-
pub use shard::*;
10-
pub use transaction::*;
11-
pub use vm::*;
3+
pub use self::account::*;
4+
pub use self::block::*;
5+
pub use self::config::*;
6+
pub use self::currency::*;
7+
pub use self::global_version::*;
8+
pub use self::message::*;
9+
pub use self::shard::*;
10+
pub use self::signature_domain::*;
11+
pub use self::transaction::*;
12+
pub use self::vm::*;
1213

1314
pub mod account;
1415
pub mod block;
@@ -17,6 +18,7 @@ pub mod currency;
1718
pub mod global_version;
1819
pub mod message;
1920
pub mod shard;
21+
pub mod signature_domain;
2022
pub mod transaction;
2123
pub mod vm;
2224

src/models/signature_domain.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Signature domain models.
2+
3+
use std::borrow::Cow;
4+
5+
use ed25519_dalek::Signer;
6+
use tl_proto::{TlRead, TlWrite};
7+
8+
/// Signature domain variants.
9+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TlRead, TlWrite)]
10+
#[tl(
11+
boxed,
12+
scheme_inline = r#"
13+
signature_domain.l2#71b34ee1 global_id:int = SignatureDomain;
14+
signature_domain.empty#e1d571b = SignatureDomain;
15+
"#
16+
)]
17+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18+
#[cfg_attr(feature = "serde", serde(tag = "type", content = "id"))]
19+
pub enum SignatureDomain {
20+
/// Special variant to NOT add any prefix for the verified data.
21+
/// Can be used to verify mainnet signatures from L2 networks.
22+
#[tl(id = "signature_domain.empty")]
23+
Empty,
24+
/// Non-empty variant. Hash of its TL representation
25+
/// is used as a prefix for the verified data.
26+
#[tl(id = "signature_domain.l2")]
27+
L2 {
28+
/// Global id of the network.
29+
global_id: i32,
30+
},
31+
}
32+
33+
impl SignatureDomain {
34+
/// Signs arbitrary data using the key and optional signature id.
35+
pub fn sign(&self, key: &ed25519_dalek::SigningKey, data: &[u8]) -> ed25519_dalek::Signature {
36+
let data = self.apply(data);
37+
key.sign(&data)
38+
}
39+
40+
/// Prepares arbitrary data for signing.
41+
pub fn apply<'a>(&self, data: &'a [u8]) -> Cow<'a, [u8]> {
42+
if let Self::Empty = self {
43+
Cow::Borrowed(data)
44+
} else {
45+
let hash = tl_proto::hash(self);
46+
47+
let mut result = Vec::with_capacity(32 + data.len());
48+
result.extend_from_slice(&hash);
49+
result.extend_from_slice(data);
50+
Cow::Owned(result)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)