Skip to content

Commit a585fb2

Browse files
Merge pull request #468 from helium/connor/move-msg-verify
Move MsgVerify from oracles to proto
2 parents fd87983 + 9dc05ae commit a585fb2

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ name = "helium_proto"
1010
path = "src/lib.rs"
1111

1212
[workspace]
13-
members = ["beacon"]
13+
members = ["beacon", "msg-signature", "msg-signature-macro"]
1414

1515
[workspace.dependencies]
1616
tonic = "0.14"
@@ -30,9 +30,9 @@ tonic-prost = { workspace = true, optional = true }
3030
bytes = { workspace = true }
3131
prost = { workspace = true }
3232
serde = { workspace = true }
33-
serde_json = { workspace = true }
3433
strum = { version = "0.27", features = ["derive"] }
3534
strum_macros = "0.27"
35+
msg-signature = { path = "msg-signature" }
3636

3737
[build-dependencies]
3838
tonic-prost-build = "0.14"

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const MESSAGES: &[&str] = &[
3737
macro_rules! config {
3838
($config:expr) => {
3939
$config
40+
.type_attribute(".", "#[derive(msg_signature::MsgHasSignature)]")
4041
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
4142
.enum_attribute(
4243
".helium.service_provider",

msg-signature-macro/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "msg-signature-macro"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
syn = { version = "2" }
11+
quote = "1"

msg-signature-macro/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use proc_macro::TokenStream;
2+
use quote::quote;
3+
use syn::{DeriveInput, parse_macro_input};
4+
5+
#[proc_macro_derive(MsgHasSignature)]
6+
pub fn msg_signature_derive(input: TokenStream) -> TokenStream {
7+
let input = parse_macro_input!(input as DeriveInput);
8+
let name = input.ident;
9+
10+
let has_signature = match &input.data {
11+
syn::Data::Struct(data_struct) => match &data_struct.fields {
12+
syn::Fields::Named(fields_named) => fields_named
13+
.named
14+
.iter()
15+
.any(|field| field.ident.as_ref().unwrap() == "signature"),
16+
_ => false,
17+
},
18+
_ => false,
19+
};
20+
21+
if has_signature {
22+
let expanded = quote! {
23+
impl msg_signature::MsgHasSignature for #name {
24+
fn signature(&self) -> &[u8] {
25+
&self.signature
26+
}
27+
28+
fn without_signature(&self) -> Self {
29+
let mut clone = self.clone();
30+
clone.signature = vec![];
31+
clone
32+
}
33+
34+
fn set_signature(&mut self, signature: Vec<u8>) {
35+
self.signature = signature;
36+
}
37+
38+
fn clear_signature(&mut self) {
39+
self.signature = vec![];
40+
}
41+
}
42+
};
43+
44+
return TokenStream::from(expanded);
45+
}
46+
47+
TokenStream::new()
48+
}

msg-signature/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "msg-signature"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
msg-signature-macro = { path = "../msg-signature-macro" }

msg-signature/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pub use msg_signature_macro::MsgHasSignature;
2+
3+
pub trait MsgHasSignature {
4+
fn signature(&self) -> &[u8];
5+
fn without_signature(&self) -> Self;
6+
fn set_signature(&mut self, signature: Vec<u8>);
7+
fn clear_signature(&mut self);
8+
}

0 commit comments

Comments
 (0)