we_cdk/macros/crypto.rs
1/// Fast hash
2///
3/// # Usage
4/// ```
5/// use we_cdk::*;
6///
7/// #[action]
8/// fn _constructor() {
9/// let hash: Binary = fast_hash!("hello");
10/// }
11/// ```
12#[macro_export]
13macro_rules! fast_hash {
14 ($bytes:expr) => {{
15 let (error, ptr, len) = wevm::v0::bindings::fast_hash($bytes.as_ptr(), $bytes.len());
16 error!(error);
17 core::slice::from_raw_parts(ptr, len)
18 }};
19}
20
21/// Secure hash
22///
23/// # Usage
24/// ```
25/// use we_cdk::*;
26///
27/// #[action]
28/// fn _constructor() {
29/// let hash: Binary = secure_hash!("hello");
30/// }
31/// ```
32#[macro_export]
33macro_rules! secure_hash {
34 ($bytes:expr) => {{
35 let (error, ptr, len) = wevm::v0::bindings::secure_hash($bytes.as_ptr(), $bytes.len());
36 error!(error);
37 core::slice::from_raw_parts(ptr, len)
38 }};
39}
40
41/// Hash an array of bytes using BLAKE2b-256 (https://en.wikipedia.org/wiki/BLAKE_%28hash_function%29)
42///
43/// # Usage
44/// ```
45/// use we_cdk::*;
46///
47/// #[action]
48/// fn _constructor() {
49/// let hash: Binary = blake2b256!("hello");
50/// }
51/// ```
52#[macro_export]
53macro_rules! blake2b256 {
54 ($bytes:expr) => {{
55 let (error, ptr, len) = wevm::v0::bindings::blake2b256($bytes.as_ptr(), $bytes.len());
56 error!(error);
57 core::slice::from_raw_parts(ptr, len)
58 }};
59}
60
61/// Hash an array of bytes using Keccak-256 (https://keccak.team/files/Keccak-submission-3.pdf)
62///
63/// # Usage
64/// ```
65/// use we_cdk::*;
66///
67/// #[action]
68/// fn _constructor() {
69/// let hash: Binary = keccak256!("hello");
70/// }
71/// ```
72#[macro_export]
73macro_rules! keccak256 {
74 ($bytes:expr) => {{
75 let (error, ptr, len) = wevm::v0::bindings::keccak256($bytes.as_ptr(), $bytes.len());
76 error!(error);
77 core::slice::from_raw_parts(ptr, len)
78 }};
79}
80
81/// Hash an array of bytes using SHA-256 (https://en.wikipedia.org/wiki/SHA-2)
82///
83/// # Usage
84/// ```
85/// use we_cdk::*;
86///
87/// #[action]
88/// fn _constructor() {
89/// let hash: Binary = sha256!("hello");
90/// }
91/// ```
92#[macro_export]
93macro_rules! sha256 {
94 ($bytes:expr) => {{
95 let (error, ptr, len) = wevm::v0::bindings::sha256($bytes.as_ptr(), $bytes.len());
96 error!(error);
97 core::slice::from_raw_parts(ptr, len)
98 }};
99}
100
101/// Signature verify
102///
103/// # Usage
104/// ```
105/// use we_cdk::*;
106///
107/// #[action]
108/// fn _constructor() {
109/// let message = "uncle";
110/// let signature: Binary = base58!("B4ViRpS6wZ73hhTtP4hhrfV46rR3uoUn7jgsH5yfkKMpbJUxMmu48jf3QSdibRkQBN7Tkx9jReKDq1Rmp9acxPG");
111/// let public_key: Binary = base58!("4KxUVD9NtyRJjU3BCvPgJSttoJX7cb3DMdDTNucLN121");
112/// let result: Boolean = sig_verify!(message, signature, public_key);
113/// }
114/// ```
115#[macro_export]
116macro_rules! sig_verify {
117 ($message:expr, $signature:expr, $public_key: expr) => {{
118 let (error, result) = wevm::v0::bindings::sig_verify(
119 $message.as_ptr(),
120 $message.len(),
121 $signature.as_ptr(),
122 $signature.len(),
123 $public_key.as_ptr(),
124 $public_key.len(),
125 );
126 error!(error);
127 result
128 }};
129}