1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
extern crate proc_macro;
extern crate syn;
#[proc_macro]
pub fn generate_schname(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input_string = get_input_string(input);
let calculated_hash = calculate_hash_from_input(&input_string);
generate_output_schname_tokenstream(calculated_hash)
}
#[proc_macro]
pub fn generate_hash(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input_string = get_input_string(input);
let calculated_hash = calculate_hash_from_input(&input_string);
generate_output_u32_tokenstream(calculated_hash)
}
fn get_input_string(input: proc_macro::TokenStream) -> String {
let args: Vec<proc_macro::TokenTree> = input.into_iter().collect();
let string_arg : String = match &args.get(0) {
Some(proc_macro::TokenTree::Literal(literal)) => literal.to_string(),
_ => panic!("No value found")
};
String::from(string_arg.trim_matches('\"'))
}
fn calculate_hash_from_input(input: &str) -> u32{
let input_string = input.to_string();
let calculated_hash = calculate_blake2b_hash(&input_string);
calculated_hash
}
fn generate_output_schname_tokenstream(calculated_hash : u32) -> proc_macro::TokenStream {
let expanded = quote::quote! {
ScHname(#calculated_hash)
};
let output = proc_macro::TokenStream::from(expanded);
output
}
fn generate_output_u32_tokenstream(calculated_hash : u32) -> proc_macro::TokenStream {
let expanded = quote::quote! {
#calculated_hash
};
let output = proc_macro::TokenStream::from(expanded);
output
}
use std::convert::TryInto;
use blake2::digest::{Update, VariableOutput};
use blake2::VarBlake2b;
fn calculate_blake2b_hash(data_string : &str) -> u32{
let data = data_string.as_bytes();
let mut result = 0;
let mut hasher = VarBlake2b::new(32).unwrap();
hasher.update(data);
hasher.finalize_variable(|res| {
let mut hname_bytes : &[u8] = &res[..4];
let are_first_4_bytes_zero = hname_bytes[..4].iter().all(|x| *x == 0_u8);
if are_first_4_bytes_zero {
hname_bytes = &hname_bytes[4..8]
}
result = u32::from_le_bytes(hname_bytes.try_into().expect("could not convert bytes to little endian u32"));
});
return result;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fairroulette() {
let result = calculate_blake2b_hash("fairroulette");
assert_eq!(0xdf79d138, result);
}
#[test]
fn lock_bets() {
let result = calculate_blake2b_hash("lockBets");
assert_eq!(0xe163b43c, result);
}
#[test]
fn pay_winners() {
let result = calculate_blake2b_hash("payWinners");
assert_eq!(0xfb2b0144, result);
}
#[test]
fn donate_with_feedback() {
let result = calculate_blake2b_hash("donatewithfeedback");
assert_eq!(0x696d7f66, result);
}
#[test]
fn tip_100() {
let result = calculate_blake2b_hash("implements(ScHname,ScHname)->bool");
assert_eq!(0xeae53bfb_u32, result);
}
}