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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

#![crate_type = "proc-macro"]
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Literal;
use quote::{quote, quote_spanned};
use sha3::{Digest, Keccak256};
use syn::{
    parse_macro_input, spanned::Spanned, Attribute, Expr, ExprLit, Ident, ItemEnum, Lit, LitStr,
};

struct Bytes(Vec<u8>);

impl ::std::fmt::Debug for Bytes {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
        let data = &self.0;
        write!(f, "[")?;
        if !data.is_empty() {
            write!(f, "{:#04x}u8", data[0])?;
            for unit in data.iter().skip(1) {
                write!(f, ", {:#04x}", unit)?;
            }
        }
        write!(f, "]")
    }
}

#[proc_macro]
pub fn keccak256(input: TokenStream) -> TokenStream {
    let lit_str = parse_macro_input!(input as LitStr);

    let hash = Keccak256::digest(&lit_str.value());

    let bytes = Bytes(hash.to_vec());
    let eval_str = format!("{:?}", bytes);
    let eval_ts: proc_macro2::TokenStream = eval_str.parse().unwrap_or_else(|_| {
        panic!(
            "Failed to parse the string \"{}\" to TokenStream.",
            eval_str
        );
    });
    quote!(#eval_ts).into()
}

/// This macro allows to associate to each variant of an enumeration a discriminant (of type u32
/// whose value corresponds to the first 4 bytes of the Hash Keccak256 of the character string
///indicated by the user of this macro.
///
/// Usage:
///
/// ```ignore
/// #[generate_function_selector]
/// enum Action {
/// 	Toto = "toto()",
/// 	Tata = "tata()",
/// }
/// ```
///
/// Extended to:
///
/// ```rust
/// #[repr(u32)]
/// enum Action {
/// 	Toto = 119097542u32,
/// 	Tata = 1414311903u32,
/// }
/// ```
#[proc_macro_attribute]
pub fn generate_function_selector(_: TokenStream, input: TokenStream) -> TokenStream {
    let item = parse_macro_input!(input as ItemEnum);

    let ItemEnum {
        attrs,
        vis,
        enum_token,
        ident,
        variants,
        ..
    } = item;

    let mut ident_expressions: Vec<Ident> = vec![];
    let mut variant_expressions: Vec<Expr> = vec![];
    let mut variant_attrs: Vec<Vec<Attribute>> = vec![];
    for variant in variants {
        match variant.discriminant {
            Some((_, Expr::Lit(ExprLit { lit, .. }))) =>
                if let Lit::Str(lit_str) = lit {
                    let digest = Keccak256::digest(&lit_str.value());
                    let selector = u32::from_be_bytes([digest[0], digest[1], digest[2], digest[3]]);
                    ident_expressions.push(variant.ident);
                    variant_expressions.push(Expr::Lit(ExprLit {
                        lit: Lit::Verbatim(Literal::u32_suffixed(selector)),
                        attrs: Default::default(),
                    }));
                    variant_attrs.push(variant.attrs);
                } else {
                    return quote_spanned! {
                        lit.span() => compile_error("Expected literal string");
                    }
                    .into()
                },
            Some((_eg, expr)) =>
                return quote_spanned! {
                    expr.span() => compile_error("Expected literal");
                }
                .into(),
            None =>
                return quote_spanned! {
                    variant.span() => compile_error("Each variant must have a discriminant");
                }
                .into(),
        }
    }

    (quote! {
        #(#attrs)*
        #[derive(num_enum::TryFromPrimitive, num_enum::IntoPrimitive)]
        #[repr(u32)]
        #vis #enum_token #ident {
            #(
                #(#variant_attrs)*
                #ident_expressions = #variant_expressions,
            )*
        }
    })
    .into()
}