ink-wrapper 0.8.0

Tool for generating type-safe code for calling an ink smart contract based on the metadata file for that contract.
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
use std::collections::HashMap;

use ink_metadata::{ConstructorSpec, EventSpec, InkProject, MessageParamSpec, MessageSpec};
use proc_macro2::Ident;
use quote::*;
use scale_info::{
    form::PortableForm, Type, TypeDef, TypeDefArray, TypeDefCompact, TypeDefComposite,
    TypeDefPrimitive, TypeDefSequence, TypeDefTuple, TypeDefVariant,
};

use crate::extensions::*;

type MessageList<'a> = Vec<&'a MessageSpec<PortableForm>>;

/// Generates the full wrapper for the contract.
pub fn generate(
    metadata: &InkProject,
    code_hash: String,
    wasm_path: Option<String>,
) -> proc_macro2::TokenStream {
    let (top_level_messages, trait_messages) = group_messages(metadata);

    let code_hash = hex_to_bytes(&code_hash);

    let upload = define_upload(wasm_path);

    let custom_types = define_custom_types(metadata);

    let events = define_events(metadata);

    let traits = define_traits(metadata, trait_messages);

    let impl_instance = define_impl_instance(metadata, top_level_messages);

    quote! {
        // This file was auto-generated with ink-wrapper (https://crates.io/crates/ink-wrapper).")

        use scale::Encode as _;

        #[allow(dead_code)]
        pub const CODE_HASH: [u8; 32] = [#(#code_hash),*];

        #(#custom_types)*

        pub mod event {
            #[allow(dead_code, clippy::large_enum_variant)]
            #[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
            pub enum Event {
                #(#events),*
            }
        }


        #[derive(Debug, Clone, Copy)]
        pub struct Instance {
            account_id: ink_primitives::AccountId,
        }

        impl From<ink_primitives::AccountId> for Instance {
            fn from(account_id: ink_primitives::AccountId) -> Self {
                Self { account_id }
            }
        }

        impl From<Instance> for ink_primitives::AccountId {
            fn from(instance: Instance) -> Self {
                instance.account_id
            }
        }

        impl ink_wrapper_types::EventSource for Instance {
            type Event = event::Event;
        }

        #(#traits)*

        #upload

        #impl_instance
    }
}

fn define_custom_types(
    metadata: &InkProject,
) -> impl Iterator<Item = proc_macro2::TokenStream> + '_ {
    metadata
        .registry()
        .types
        .iter()
        .filter(|typ| typ.ty.is_custom())
        .map(|typ| define_type(&typ.ty, metadata))
}

fn define_events(metadata: &InkProject) -> impl Iterator<Item = proc_macro2::TokenStream> + '_ {
    metadata
        .spec()
        .events()
        .iter()
        .map(|event| define_event(event, metadata))
}

fn define_traits(
    metadata: &InkProject,
    trait_messages: HashMap<String, MessageList>,
) -> Vec<proc_macro2::TokenStream> {
    trait_messages
        .iter()
        .map(|(trait_name, messages)| define_trait(trait_name, messages, metadata))
        .collect()
}

fn define_impl_instance(
    metadata: &InkProject,
    top_level_messages: Vec<&MessageSpec<PortableForm>>,
) -> proc_macro2::TokenStream {
    let constructors = metadata
        .spec()
        .constructors()
        .iter()
        .map(|constructor| define_constructor(constructor, metadata));
    let messages = top_level_messages
        .iter()
        .map(|message| define_message(message, "pub", metadata));

    quote! {
        impl Instance {
            #(#constructors)*

            #(#messages)*
        }
    }
}

// If wasm_path is defined, returns a function that uploads the contract to the chain.
// If `None`, returns empty `quote!{}` - a noop.
fn define_upload(wasm_path: Option<String>) -> proc_macro2::TokenStream {
    match wasm_path {
        Some(wasm_path) => quote! {
            #[allow(dead_code)]
            pub fn upload() -> ink_wrapper_types::UploadCall
            {
                let wasm = include_bytes!(#wasm_path);
                ink_wrapper_types::UploadCall::new(wasm.to_vec(), CODE_HASH)
            }
        },
        None => quote! {},
    }
}

/// Define a group of messages with a common prefix (e.g. `PSP22::`).
///
/// These messages will be grouped into a trait and implemented for the contract to avoid name clashes.
fn define_trait<'a, 'b>(
    trait_name: &str,
    messages: &[&'b MessageSpec<PortableForm>],
    metadata: &'a InkProject,
) -> proc_macro2::TokenStream
where
    'a: 'b,
{
    let trait_name = format_ident!("{}", trait_name);
    let trait_messages = messages
        .iter()
        .map(|m| define_message_head(m, "", metadata));

    let impl_messages = messages.iter().map(|m| define_message(m, "", metadata));
    quote! {
        pub trait #trait_name {
            #(#trait_messages;)*
        }

        impl #trait_name for Instance {
            #(#impl_messages)*
        }
    }
}

fn define_message_head(
    message: &MessageSpec<PortableForm>,
    visibility: &str,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    if message.mutates() {
        define_mutator_head(message, visibility, metadata)
    } else {
        define_reader_head(message, visibility, metadata)
    }
}

/// Group messages by their "trait" prefix (for example groups all messages with a `PSP22::` prefix together).
///
/// Returns the "main" group without any prefix as a special group (first member of the result pair).
fn group_messages(metadata: &InkProject) -> (MessageList, HashMap<String, MessageList>) {
    let mut top_level_messages = Vec::new();
    let mut trait_messages = HashMap::new();

    for message in metadata.spec().messages() {
        match message.trait_name() {
            None => top_level_messages.push(message),
            Some(trait_name) => {
                trait_messages
                    .entry(trait_name.clone())
                    .or_insert_with(Vec::new)
                    .push(message);
            }
        }
    }

    (top_level_messages, trait_messages)
}

/// Generates a type definition for a custom type used in the contract.
fn define_type(typ: &Type<PortableForm>, metadata: &InkProject) -> proc_macro2::TokenStream {
    match &typ.type_def {
        TypeDef::Variant(variant) => define_enum(typ, variant, metadata),
        TypeDef::Composite(composite) => define_composite(typ, composite, metadata),
        _ => quote! {},
    }
}

fn named_variant(
    name: &str,
    fields: &[(String, u32)],
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let fields = fields.iter().map(|(name, typ)| {
        let typ = type_ref(*typ, metadata);
        let name = format_ident!("{}", name);
        quote! {
            #name: #typ
        }
    });
    let name = format_ident!("{}", name);
    quote! {
        #name {
            #(#fields),*
        }
    }
}

fn unnamed_variant(name: &str, fields: &[u32], metadata: &InkProject) -> proc_macro2::TokenStream {
    let fields = fields.iter().map(|typ| {
        let typ = type_ref(*typ, metadata);
        quote! {
            #typ
        }
    });
    let name = format_ident!("{}", name);
    quote! {
        #name (
            #(#fields),*
        )
    }
}

/// Generates a type definition for an enum.
fn define_enum(
    typ: &Type<PortableForm>,
    variant: &TypeDefVariant<PortableForm>,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let typ = typ.qualified_name();
    let variants = variant
        .variants
        .iter()
        .map(|variant| match variant.aggregate_fields() {
            Fields::Named(fields) => named_variant(&variant.name, &fields, metadata),
            Fields::Unnamed(fields) => unnamed_variant(&variant.name, &fields, metadata),
        });
    quote! {
        #[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
        pub enum #typ {
            #(#variants),*
        }
    }
}

/// Generates a type definition for a struct.
fn define_composite(
    typ: &Type<PortableForm>,
    composite: &TypeDefComposite<PortableForm>,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    match composite.aggregate_fields() {
        Fields::Named(fields) => {
            let typ = typ.qualified_name();
            let fields = fields.iter().map(|(name, typ)| {
                let typ = type_ref(*typ, metadata);
                let name = format_ident!("{}", name);
                quote! {
                    pub #name: #typ
                }
            });
            quote! {
                #[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
                pub struct #typ {
                    #(#fields),*
                }
            }
        }
        Fields::Unnamed(unnamed) => {
            let typ = typ.qualified_name();
            let fields = unnamed.iter().map(|typ| {
                let typ = type_ref(*typ, metadata);
                quote! {
                    pub #typ
                }
            });
            quote! {
                #[derive(Debug, Clone, PartialEq, Eq, scale::Encode, scale::Decode)]
                pub struct #typ (
                    #(#fields),*
                );
            }
        }
    }
}

/// Generates a function wrapping a contract constructor.
fn define_constructor(
    constructor: &ConstructorSpec<PortableForm>,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let data_ident = &new_name("data", constructor.args());
    let docs = quote_docs(constructor.docs());
    let label = format_ident!("{}", constructor.label());
    let args = message_args(constructor.args(), metadata);
    let ret_res = if *constructor.payable() {
        quote! { ink_wrapper_types::InstantiateCallNeedsValue<Self> }
    } else {
        quote! { ink_wrapper_types::InstantiateCall<Self> }
    };
    let data = gather_args(constructor.selector().to_bytes(), constructor.args());
    let body = if *constructor.payable() {
        quote! {
            let #data_ident = #data;
            ink_wrapper_types::InstantiateCallNeedsValue::new(CODE_HASH, #data_ident)
        }
    } else {
        quote! {
            let #data_ident = #data;
            ink_wrapper_types::InstantiateCall::new(CODE_HASH, #data_ident)
        }
    };

    quote! {
        #docs
        #[allow(dead_code, clippy::too_many_arguments)]
        pub fn #label ( #args ) -> #ret_res {
            #body
        }
    }
}

/// Generates a function wrapping a contract message send.
fn define_message(
    message: &MessageSpec<PortableForm>,
    visibility: &str,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    if message.mutates() {
        define_mutator(message, visibility, metadata)
    } else {
        define_reader(message, visibility, metadata)
    }
}

/// Generates a function wrapping a contract message that only reads from the contract.
fn define_reader(
    message: &MessageSpec<PortableForm>,
    visibility: &str,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let data_ident = &new_name("data", message.args());
    let docs = quote_docs(message.docs());
    let reader_head = define_reader_head(message, visibility, metadata);
    let args = gather_args(message.selector().to_bytes(), message.args());

    quote! {
        #docs
        #[allow(dead_code, clippy::too_many_arguments)]
        #reader_head
        {
            let #data_ident = #args;
            ink_wrapper_types::ReadCall::new(self.account_id, #data_ident)
        }
    }
}

fn quote_visibility(visibility: &str) -> proc_macro2::TokenStream {
    if visibility.is_empty() {
        quote! {}
    } else {
        let v = format_ident!("{}", visibility);
        quote! { #v }
    }
}

fn define_reader_head(
    message: &MessageSpec<PortableForm>,
    visibility: &str,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let method = format_ident!("{}", message.method_name());
    let args = message_args(message.args(), metadata);
    let read_call_type = type_ref(message.return_type().opt_type().unwrap().ty().id, metadata);
    let ret_type = if message.payable() {
        quote! { ink_wrapper_types::ReadCallNeedsValue<#read_call_type> }
    } else {
        quote! { ink_wrapper_types::ReadCall<#read_call_type> }
    };
    let visibility = quote_visibility(visibility);
    quote! {
        #visibility fn #method (&self, #args) -> #ret_type
    }
}

/// Generates a function wrapping a contract message that mutates the contract.
fn define_mutator(
    message: &MessageSpec<PortableForm>,
    visibility: &str,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let data_ident = &new_name("data", message.args());
    let data = gather_args(message.selector().to_bytes(), message.args());
    let docs = quote_docs(message.docs());
    let mutator_head = define_mutator_head(message, visibility, metadata);
    let res = if message.payable() {
        quote! {
            ink_wrapper_types::ExecCallNeedsValue::new(self.account_id, #data_ident)
        }
    } else {
        quote! {
            ink_wrapper_types::ExecCall::new(self.account_id, #data_ident)
        }
    };
    quote! {
        #docs
        #[allow(dead_code, clippy::too_many_arguments)]
        #mutator_head
        {
            let #data_ident = #data;
            #res
        }
    }
}

fn define_mutator_head(
    message: &MessageSpec<PortableForm>,
    visibility: &str,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let method = format_ident!("{}", message.method_name());
    let message_args = message_args(message.args(), metadata);
    let exec_call_type = type_ref(message.return_type().opt_type().unwrap().ty().id, metadata);
    let ret_type = if message.payable() {
        quote! { ink_wrapper_types::ExecCallNeedsValue<#exec_call_type> }
    } else {
        quote! { ink_wrapper_types::ExecCall<#exec_call_type> }
    };
    let visibility = quote_visibility(visibility);
    quote! {
        #visibility fn #method (&self, #message_args) -> #ret_type
    }
}

/// Generates a block of statements that pack the selector and arguments into a SCALE encoded vector of bytes.
///
/// The intention is to assign the result to a variable.
fn gather_args(
    selector: &[u8],
    args: &[MessageParamSpec<PortableForm>],
) -> proc_macro2::TokenStream {
    let selector_deref: Vec<u8> = selector.to_vec();
    if args.is_empty() {
        quote! {
            vec![#(#selector_deref),*]
        }
    } else {
        let data_ident = format_ident!("{}", new_name("data", args));
        let args = args.iter().map(|arg| {
            let arg_label = format_ident!("{}", arg.label());
            quote! { #arg_label.encode_to(&mut #data_ident) }
        });
        quote!({
            let mut #data_ident = vec![#(#selector_deref),*];
            #(#args;)*
            #data_ident
        })
    }
}

/// Generates a list of arguments for a constructor/message wrapper.
fn message_args(
    args: &[MessageParamSpec<PortableForm>],
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let args = args.iter().map(|arg| {
        let arg_label = format_ident!("{}", arg.label());
        let arg_type = type_ref(arg.ty().ty().id, metadata);
        quote! { #arg_label: #arg_type }
    });
    quote! { #(#args),* }
}

/// Generates an event definition as a variant in the `Event` enum.
///
/// Note that these definitions are hidden in a module to avoid name clashes (just in case someone uses `Event` as a
/// type name), so references to types defined in the contract need to be prefixed with `super::`.
fn define_event(
    event: &EventSpec<PortableForm>,
    metadata: &InkProject,
) -> proc_macro2::TokenStream {
    let event_docs = quote_docs(event.docs());
    let event_label = format_ident!("{}", event.label());
    let event_fields = event.args().iter().map(|field| {
        let field_docs = quote_docs(field.docs());
        let field_label = format_ident!("{}", field.label());
        let field_type = type_ref_prefix(field.ty().ty().id, metadata, "super");
        quote! {
           #field_docs
           #field_label: #field_type
        }
    });
    quote! {
        #event_docs
        #event_label {
            #(#event_fields),*
        }
    }
}

/// Generates a type reference to the given type (for example to use as an argument type, return type, etc.).
fn type_ref(id: u32, metadata: &InkProject) -> proc_macro2::TokenStream {
    type_ref_prefix(id, metadata, "")
}

/// Generates a type reference to the given type (for example to use as an argument type, return type, etc.).
///
/// The `prefix` is prepended to the type name if the type is a custom type.
fn type_ref_prefix(id: u32, metadata: &InkProject, prefix: &str) -> proc_macro2::TokenStream {
    let typ = resolve(metadata, id);

    match &typ.type_def {
        TypeDef::Primitive(primitive) => {
            let t = type_ref_primitive(primitive);
            quote! { #t }
        }
        TypeDef::Tuple(tuple) => type_ref_tuple(tuple, metadata, prefix),
        TypeDef::Composite(_) => type_ref_generic(typ, metadata, prefix),
        TypeDef::Variant(_) => type_ref_generic(typ, metadata, prefix),
        TypeDef::Array(array) => type_ref_array(array, metadata, prefix),
        TypeDef::Sequence(sequence) => type_ref_sequence(sequence, metadata, prefix),
        TypeDef::Compact(compact) => type_ref_compact(compact, metadata, prefix),
        TypeDef::BitSequence(_) => panic!("Bit sequences are not supported yet."),
    }
}

/// Generates a type reference to a (potentially generic) type by name.
fn type_ref_generic(
    typ: &Type<PortableForm>,
    metadata: &InkProject,
    prefix: &str,
) -> proc_macro2::TokenStream {
    let generics = if typ.type_params.is_empty() {
        quote! {}
    } else {
        let generics = typ.type_params.iter().map(|param| {
            let param = param.ty.unwrap();
            let param = type_ref_prefix(param.id, metadata, prefix);
            quote! { #param }
        });
        quote! { <#(#generics),*> }
    };

    let prefix = if prefix.is_empty() || !typ.is_custom() {
        quote! {}
    } else {
        let prefix_ident = format_ident!("{}", prefix);
        quote! { #prefix_ident:: }
    };

    let qualified_name = typ.qualified_name();
    quote! { #prefix #qualified_name #generics }
}

/// Generates a type reference to a primitive type.
fn type_ref_primitive(primitive: &TypeDefPrimitive) -> proc_macro2::TokenStream {
    match primitive {
        TypeDefPrimitive::U8 => quote! { u8 },
        TypeDefPrimitive::I8 => quote! { i8 },
        TypeDefPrimitive::U16 => quote! { u16 },
        TypeDefPrimitive::I16 => quote! { i16 },
        TypeDefPrimitive::U32 => quote! { u32 },
        TypeDefPrimitive::I32 => quote! { i32 },
        TypeDefPrimitive::U64 => quote! { u64 },
        TypeDefPrimitive::I64 => quote! { i64 },
        TypeDefPrimitive::I128 => quote! { i128 },
        TypeDefPrimitive::U128 => quote! { u128 },
        TypeDefPrimitive::U256 => quote! { u256 },
        TypeDefPrimitive::I256 => quote! { i256 },
        TypeDefPrimitive::Bool => quote! { bool },
        TypeDefPrimitive::Char => quote! { char },
        TypeDefPrimitive::Str => quote! { String },
    }
}

/// Generates a type reference to a tuple type.
fn type_ref_tuple(
    tuple: &TypeDefTuple<PortableForm>,
    metadata: &InkProject,
    prefix: &str,
) -> proc_macro2::TokenStream {
    let typs = tuple
        .fields
        .iter()
        .map(|t| type_ref_prefix(t.id, metadata, prefix));
    quote! {
        (#(#typs),*)
    }
}

/// Generates a type reference to an array type.
fn type_ref_array(
    array: &TypeDefArray<PortableForm>,
    metadata: &InkProject,
    prefix: &str,
) -> proc_macro2::TokenStream {
    let typ = type_ref_prefix(array.type_param.id, metadata, prefix);
    // Cast to usize as otherwise we will get compilation errors on other archs.
    let len = array.len as usize;
    quote! {
        [ #typ ; #len ]
    }
}

/// Generates a type reference to a sequence type.
fn type_ref_sequence(
    sequence: &TypeDefSequence<PortableForm>,
    metadata: &InkProject,
    prefix: &str,
) -> proc_macro2::TokenStream {
    let typ = type_ref_prefix(sequence.type_param.id, metadata, prefix);
    quote! {
        Vec<#typ>
    }
}

/// Generates a type reference to a compact type.
fn type_ref_compact(
    compact: &TypeDefCompact<PortableForm>,
    metadata: &InkProject,
    prefix: &str,
) -> proc_macro2::TokenStream {
    let typ = type_ref_prefix(compact.type_param.id, metadata, prefix);
    quote! {
        scale::Compact<#typ>
    }
}

fn quote_docs(lines: &[String]) -> proc_macro2::TokenStream {
    if lines.is_empty() {
        quote! {}
    } else {
        let d = lines
            .iter()
            .map(|l| l.trim())
            .collect::<Vec<_>>()
            .join("")
            .to_string();
        quote! { #[doc = #d] }
    }
}

/// Resolves the type with the given ID.
///
/// Panics if the type cannot be found in the metadata. We should only use types that are mentioned in the metadata
/// file, so any type that cannot be found is a bug in the code generator or the metadata file.
fn resolve(metadata: &InkProject, id: u32) -> &Type<PortableForm> {
    metadata
        .registry()
        .resolve(id)
        .unwrap_or_else(|| panic!("Type {} not found", id))
}

/// Generates a name not already used by one of the arguments.
fn new_name(name: &str, args: &[MessageParamSpec<PortableForm>]) -> Ident {
    let mut name = name.to_string();

    while args.iter().any(|arg| arg.label() == &name) {
        name.push('_');
    }

    format_ident!("{}", name)
}

/// Parses a hex string ("0x1234...") into a byte vector.
fn hex_to_bytes(hex: &str) -> Vec<u8> {
    hex::decode(hex.replace("0x", "")).unwrap()
}