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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//! ink! selector IR.

use blake2::digest::consts::U32;
use blake2::digest::Digest;
use blake2::Blake2b;
use ra_ap_syntax::ast::HasName;
use ra_ap_syntax::{ast, AstNode, SyntaxKind, TextRange};

use crate::traits::{IsInkCallable, IsInkImplItem};
use crate::tree::utils;
use crate::{InkArg, InkArgKind};

/// The selector of an ink! callable entity.
///
/// Ref: <https://github.com/paritytech/ink/blob/master/crates/ink/ir/src/ir/selector.rs#L21-L29>.
///
/// The selector is four byte array.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Selector([u8; 4]);

impl Selector {
    /// Returns the composed selector of the ink! callable entity.
    ///
    /// Ref: <https://github.com/paritytech/ink/blob/master/crates/ink/ir/src/ir/selector.rs#L74-L126>.
    ///
    /// Ref: <https://github.com/paritytech/ink/blob/master/crates/ink/ir/src/ir/item_impl/callable.rs#L203-L371>.
    pub fn compose<T>(callable: &T) -> Option<Self>
    where
        T: IsInkCallable,
    {
        let selector_bytes: Option<[u8; 4]> = match Self::provided_int_selector(callable) {
            // Manually provided integer selector is converted into bytes.
            Some(manual_int_selector) => Some(manual_int_selector.to_be_bytes()),
            // Otherwise the selector has to be computed, but only if the callable is a valid `fn` item.
            None => {
                Self::ident(callable).map(|callable_ident| {
                    let trait_ident = Self::trait_ident(callable);
                    let namespace = Self::namespace(callable);

                    let pre_hash_bytes = [namespace, trait_ident, Some(callable_ident)]
                        .into_iter()
                        .flatten()
                        .collect::<Vec<String>>()
                        .join("::")
                        .into_bytes();

                    // Computes the BLAKE-2b 256-bit hash for the given input and stores it in output.
                    let mut hasher = <Blake2b<U32>>::new();
                    hasher.update(pre_hash_bytes);
                    let hashed_bytes = hasher.finalize();

                    [
                        hashed_bytes[0],
                        hashed_bytes[1],
                        hashed_bytes[2],
                        hashed_bytes[3],
                    ]
                })
            }
        };

        selector_bytes.map(Self)
    }

    /// Returns the underlying four bytes.
    pub fn to_bytes(&self) -> [u8; 4] {
        self.0
    }

    /// Returns the big-endian `u32` representation of the selector bytes.
    pub fn into_be_u32(self) -> u32 {
        u32::from_be_bytes(self.0)
    }

    /// Returns the manually provided integer selector (if any).
    fn provided_int_selector<T>(callable: &T) -> Option<u32>
    where
        T: IsInkCallable,
    {
        callable.selector_arg()?.as_u32()
    }

    /// Returns the identifier for the callable as a string.
    fn ident<T>(callable: &T) -> Option<String>
    where
        T: IsInkCallable,
    {
        callable.fn_item()?.name().map(|name| name.to_string())
    }

    /// Returns the effective identifier for callable's parent trait (if any).
    ///
    /// Ref: <https://github.com/paritytech/ink/blob/master/crates/ink/ir/src/ir/item_impl/callable.rs#L346-L368>.
    fn trait_ident<T>(callable: &T) -> Option<String>
    where
        T: IsInkCallable,
    {
        match callable.impl_item()?.trait_()? {
            ast::Type::PathType(trait_path_type) => {
                let trait_path = trait_path_type.path()?;
                let is_full_path = trait_path.to_string().starts_with("::");
                let trait_ident = if is_full_path {
                    let mut full_path = trait_path.to_string();
                    full_path.retain(|c| !c.is_whitespace());
                    full_path
                } else {
                    trait_path
                        .segments()
                        .last()
                        .map_or(String::new(), |segment| segment.to_string())
                };
                (!trait_ident.is_empty()).then_some(trait_ident)
            }
            _ => None,
        }
    }

    /// Returns the identifier for callable's parent ink! impl namespace argument (if any).
    fn namespace<T>(callable: &T) -> Option<String>
    where
        T: IsInkCallable,
    {
        utils::ink_arg_by_kind(callable.impl_item()?.syntax(), InkArgKind::Namespace)?
            .value()?
            .as_string()
    }
}

/// An ink! selector argument.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectorArg {
    /// The kind of the ink! selector.
    kind: SelectorArgKind,
    /// The ink! attribute argument for the selector.
    arg: InkArg,
}

impl SelectorArg {
    /// Returns true if the syntax node can be converted into an ink! impl item.
    pub fn can_cast(arg: &InkArg) -> bool {
        *arg.kind() == InkArgKind::Selector
    }

    /// Converts an ink! attribute argument into a Selector IR type.
    pub fn cast(arg: InkArg) -> Option<Self> {
        Self::can_cast(&arg).then_some(Self {
            kind: if let Some(value) = arg.value() {
                match value.kind() {
                    SyntaxKind::INT_NUMBER => SelectorArgKind::Integer,
                    SyntaxKind::UNDERSCORE | SyntaxKind::UNDERSCORE_EXPR => {
                        SelectorArgKind::Wildcard
                    }
                    _ => SelectorArgKind::Other,
                }
            } else {
                SelectorArgKind::Other
            },
            arg,
        })
    }

    /// Returns the ink! selector argument kind.
    pub fn kind(&self) -> &SelectorArgKind {
        &self.kind
    }

    /// Returns the ink! attribute argument for ink! selector.
    pub fn arg(&self) -> &InkArg {
        &self.arg
    }

    /// Returns true if the value is a wildcard/underscore expression.
    pub fn is_wildcard(&self) -> bool {
        self.kind == SelectorArgKind::Wildcard
    }

    /// Converts the value if it's an integer literal (decimal or hexadecimal) into a `u32`.
    pub fn as_u32(&self) -> Option<u32> {
        self.arg.value()?.as_u32()
    }

    /// Returns the text range of the ink! selector argument.
    pub fn text_range(&self) -> TextRange {
        self.arg.text_range()
    }
}

/// The ink! selector argument kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SelectorArgKind {
    Integer,
    Wildcard,
    Other,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::*;
    use crate::traits::FromInkAttribute;
    use crate::{Constructor, InkAttribute, Message};
    use ra_ap_syntax::ast;
    use ra_ap_syntax::SourceFile;
    use test_utils::quote_as_str;

    fn first_ink_entity_of_type<T>(code: &str) -> T
    where
        T: FromInkAttribute + IsInkCallable,
    {
        SourceFile::parse(code)
            .tree()
            .syntax()
            .descendants()
            .find_map(|node| T::cast(InkAttribute::cast(ast::Attr::cast(node)?)?))
            .unwrap()
    }

    #[test]
    fn compose_works() {
        for (code, expected_constructor_selector, expected_message_selector) in [
            (
                quote_as_str! {
                    impl MyContract {
                        #[ink(constructor, selector=10)]
                        pub fn my_constructor() -> Self {}

                        #[ink(message, selector=10)]
                        pub fn my_message(&self) {}
                    }
                },
                0x0000000A,
                0x0000000A,
            ),
            (
                quote_as_str! {
                    impl MyContract {
                        #[ink(constructor, selector=0xA)]
                        pub fn my_constructor() -> Self {}

                        #[ink(message, selector=0xA)]
                        pub fn my_message(&self) {}
                    }
                },
                0x0000000A,
                0x0000000A,
            ),
            (
                quote_as_str! {
                    impl MyContract {
                        #[ink(constructor, selector=_)]
                        pub fn my_constructor() -> Self {}

                        #[ink(message, selector=_)]
                        pub fn my_message(&self) {}
                    }
                },
                0xE11C2FAF, // First 4-bytes of Blake2b-256 hash of "my_constructor"
                0x6A469E03, // First 4-bytes of Blake2b-256 hash of "my_message"
            ),
            (
                quote_as_str! {
                    impl MyContract {
                        #[ink(constructor)]
                        pub fn my_constructor() -> Self {}

                        #[ink(message)]
                        pub fn my_message(&self) {}
                    }
                },
                0xE11C2FAF, // First 4-bytes of Blake2b-256 hash of "my_constructor"
                0x6A469E03, // First 4-bytes of Blake2b-256 hash of "my_message"
            ),
            (
                quote_as_str! {
                    impl MyContract {
                        #[ink(constructor)]
                        pub fn my_constructor() -> Self {}

                        #[ink(message)]
                        pub fn my_message(&self) {}
                    }
                },
                0xE11C2FAF, // First 4-bytes of Blake2b-256 hash of "my_constructor"
                0x6A469E03, // First 4-bytes of Blake2b-256 hash of "my_message"
            ),
            (
                quote_as_str! {
                    impl MyTrait for MyContract {
                        #[ink(constructor)]
                        fn my_constructor() -> Self {}

                        #[ink(message)]
                        fn my_message(&self) {}
                    }
                },
                0x235E720C, // First 4-bytes of Blake2b-256 hash of "MyTrait::my_constructor"
                0x04C49446, // First 4-bytes of Blake2b-256 hash of "MyTrait::my_message"
            ),
            (
                quote_as_str! {
                    impl ::my_full::long_path::MyTrait for MyContract {
                        #[ink(constructor)]
                        fn my_constructor() -> Self {}

                        #[ink(message)]
                        fn my_message(&self) {}
                    }
                },
                0x647E8959, // First 4-bytes of Blake2b-256 hash of "::my_full::long_path::MyTrait::my_constructor"
                0x2EC56327, // First 4-bytes of Blake2b-256 hash of "::my_full::long_path::MyTrait::my_message"
            ),
            (
                quote_as_str! {
                    impl relative_path::MyTrait for MyContract {
                        #[ink(constructor)]
                        fn my_constructor() -> Self {}

                        #[ink(message)]
                        fn my_message(&self) {}
                    }
                },
                0x235E720C, // First 4-bytes of Blake2b-256 hash of "MyTrait::my_constructor"
                0x04C49446, // First 4-bytes of Blake2b-256 hash of "MyTrait::my_message"
            ),
            (
                quote_as_str! {
                    #[ink(namespace="my_namespace")]
                    impl MyContract {
                        #[ink(constructor)]
                        pub fn my_constructor() -> Self {}

                        #[ink(message)]
                        pub fn my_message(&self) {}
                    }
                },
                0x3a739109, // First 4-bytes of Blake2b-256 hash of "my_namespace::my_constructor"
                0xABE89C04, // First 4-bytes of Blake2b-256 hash of "my_namespace::my_message"
            ),
        ] {
            // Parse ink! constructor and ink! message.
            let constructor: Constructor = first_ink_entity_of_type(code);
            let message: Message = first_ink_entity_of_type(code);

            // Check selectors.
            assert_eq!(
                Selector::compose(&constructor).unwrap().into_be_u32(),
                expected_constructor_selector
            );
            assert_eq!(
                Selector::compose(&message).unwrap().into_be_u32(),
                expected_message_selector
            );
        }
    }

    #[test]
    fn cast_arg_works() {
        for (code, expected_kind, expected_is_wildcard, expected_u32_value) in [
            (
                quote_as_str! {
                    #[ink(selector=10)]
                },
                SelectorArgKind::Integer,
                false,
                Some(10u32),
            ),
            (
                quote_as_str! {
                    #[ink(selector=0xA)]
                },
                SelectorArgKind::Integer,
                false,
                Some(10u32),
            ),
            (
                quote_as_str! {
                    #[ink(selector=_)]
                },
                SelectorArgKind::Wildcard,
                true,
                None,
            ),
        ] {
            // Parse ink! selector argument.
            let selector_arg = SelectorArg::cast(
                parse_first_ink_attribute(code)
                    .args()
                    .iter()
                    .find(|arg| *arg.kind() == InkArgKind::Selector)
                    .unwrap()
                    .clone(),
            )
            .unwrap();

            assert_eq!(*selector_arg.kind(), expected_kind);

            assert_eq!(selector_arg.is_wildcard(), expected_is_wildcard);

            assert_eq!(selector_arg.as_u32(), expected_u32_value);
        }
    }
}