xrpl_wasm_std/lib.rs
1#![doc = include_str!("../README.md")]
2#![no_std]
3
4pub mod core;
5pub mod host;
6pub mod sfield;
7pub mod types;
8
9/// Additional guides and how-tos
10#[cfg(doc)]
11pub mod guides {
12 /// XRPL Field Access and Locators guide
13 #[doc = include_str!("../../docs/FIELD_ACCESS.md")]
14 pub mod field_access {}
15
16 /// XRPL Float Operations (IOU format and math)
17 #[doc = include_str!("../../docs/FLOAT_OPERATIONS.md")]
18 pub mod float_operations {}
19}
20
21/// This function is called on panic but only in the WASM architecture. In non-WASM (e.g., in the
22/// Host Simulator) the standard lib is available, which includes a panic handler.
23#[cfg(target_arch = "wasm32")]
24#[panic_handler]
25fn panic(_info: &::core::panic::PanicInfo) -> ! {
26 // This instruction will halt execution of the WASM module.
27 // It's the WASM equivalent of a trap or an unrecoverable error.
28 ::core::arch::wasm32::unreachable();
29}
30
31fn hex_char_to_nibble(c: u8) -> Option<u8> {
32 match c {
33 b'0'..=b'9' => Some(c - b'0'),
34 b'a'..=b'f' => Some(c - b'a' + 10),
35 b'A'..=b'F' => Some(c - b'A' + 10),
36 _ => None,
37 }
38}
39
40/// Decode a 64-hex-character string into a 32-byte array.
41///
42/// The input must be exactly 64 hexadecimal ASCII bytes (lower- or upper-case).
43/// Returns `None` if any character is not a valid hex digit.
44///
45/// Example:
46/// ```
47/// # use xrpl_wasm_std::decode_hex_32;
48/// let hex = *b"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
49/// let bytes = decode_hex_32(&hex).unwrap();
50/// assert_eq!(bytes.len(), 32);
51/// ```
52pub fn decode_hex_32(hex: &[u8; 64]) -> Option<[u8; 32]> {
53 let mut out = [0u8; 32];
54 let mut i = 0;
55 while i < 32 {
56 let high = hex_char_to_nibble(hex[i * 2])?;
57 let low = hex_char_to_nibble(hex[i * 2 + 1])?;
58 out[i] = (high << 4) | low;
59 i += 1;
60 }
61 Some(out)
62}
63
64/// Decode a 40-hex-character string into a 20-byte array.
65///
66/// The input must be exactly 40 hexadecimal ASCII bytes.
67/// Returns `None` if any character is not a valid hex digit.
68///
69/// Example:
70/// ```
71/// # use xrpl_wasm_std::decode_hex_20;
72/// let hex = *b"00112233445566778899aabbccddeeff00112233";
73/// let bytes = decode_hex_20(&hex).unwrap();
74/// assert_eq!(bytes.len(), 20);
75/// ```
76pub fn decode_hex_20(hex: &[u8; 40]) -> Option<[u8; 20]> {
77 let mut out = [0u8; 20];
78 let mut i = 0;
79 while i < 20 {
80 let high = hex_char_to_nibble(hex[i * 2])?;
81 let low = hex_char_to_nibble(hex[i * 2 + 1])?;
82 out[i] = (high << 4) | low;
83 i += 1;
84 }
85 Some(out)
86}