1#![doc = include_str!("../README.md")]
2#![cfg_attr(target_arch = "wasm32", no_std)]
3
4#[cfg(not(target_arch = "wasm32"))]
5extern crate std;
6
7pub use xrpl_macros::r_address;
9
10pub mod core;
11pub mod ctx;
12pub mod host;
13pub mod sfield;
14pub mod types;
15
16#[cfg(doc)]
23#[doc = include_str!("../docs/comprehensive-guide.md")]
24pub mod guide {}
25
26#[cfg(target_arch = "wasm32")]
29#[panic_handler]
30fn panic(_info: &::core::panic::PanicInfo) -> ! {
31 ::core::arch::wasm32::unreachable();
34}
35
36#[inline(always)]
37fn hex_char_to_nibble(c: u8) -> Option<u8> {
38 #[cfg(target_arch = "wasm32")]
40 {
41 if c >= b'0' && c <= b'9' {
43 Some(c - b'0')
44 } else if c >= b'a' && c <= b'f' {
45 Some(c - b'a' + 10)
46 } else if c >= b'A' && c <= b'F' {
47 Some(c - b'A' + 10)
48 } else {
49 None
50 }
51 }
52 #[cfg(not(target_arch = "wasm32"))]
53 {
54 match c {
57 b'0'..=b'9' => Some(c - b'0'),
58 b'a'..=b'f' => Some(c - b'a' + 10),
59 b'A'..=b'F' => Some(c - b'A' + 10),
60 _ => None,
61 }
62 }
63}
64
65#[inline(always)]
78pub fn decode_hex_32(hex: &[u8; 64]) -> Option<[u8; 32]> {
79 let mut out = [0u8; 32];
80
81 macro_rules! decode_byte {
83 ($i:expr) => {{
84 let high = hex_char_to_nibble(hex[$i * 2])?;
85 let low = hex_char_to_nibble(hex[$i * 2 + 1])?;
86 out[$i] = (high << 4) | low;
87 }};
88 }
89
90 decode_byte!(0);
91 decode_byte!(1);
92 decode_byte!(2);
93 decode_byte!(3);
94 decode_byte!(4);
95 decode_byte!(5);
96 decode_byte!(6);
97 decode_byte!(7);
98 decode_byte!(8);
99 decode_byte!(9);
100 decode_byte!(10);
101 decode_byte!(11);
102 decode_byte!(12);
103 decode_byte!(13);
104 decode_byte!(14);
105 decode_byte!(15);
106 decode_byte!(16);
107 decode_byte!(17);
108 decode_byte!(18);
109 decode_byte!(19);
110 decode_byte!(20);
111 decode_byte!(21);
112 decode_byte!(22);
113 decode_byte!(23);
114 decode_byte!(24);
115 decode_byte!(25);
116 decode_byte!(26);
117 decode_byte!(27);
118 decode_byte!(28);
119 decode_byte!(29);
120 decode_byte!(30);
121 decode_byte!(31);
122
123 Some(out)
124}
125
126#[inline(always)]
139pub fn decode_hex_20(hex: &[u8; 40]) -> Option<[u8; 20]> {
140 let mut out = [0u8; 20];
141
142 macro_rules! decode_byte {
144 ($i:expr) => {{
145 let high = hex_char_to_nibble(hex[$i * 2])?;
146 let low = hex_char_to_nibble(hex[$i * 2 + 1])?;
147 out[$i] = (high << 4) | low;
148 }};
149 }
150
151 decode_byte!(0);
152 decode_byte!(1);
153 decode_byte!(2);
154 decode_byte!(3);
155 decode_byte!(4);
156 decode_byte!(5);
157 decode_byte!(6);
158 decode_byte!(7);
159 decode_byte!(8);
160 decode_byte!(9);
161 decode_byte!(10);
162 decode_byte!(11);
163 decode_byte!(12);
164 decode_byte!(13);
165 decode_byte!(14);
166 decode_byte!(15);
167 decode_byte!(16);
168 decode_byte!(17);
169 decode_byte!(18);
170 decode_byte!(19);
171
172 Some(out)
173}