xrpl_hooks/api/
util.rs

1use super::*;
2
3/// Convert a 20 byte Account ID to an r-address
4#[inline(always)]
5pub fn util_raddr(raddr_out: &mut [u8], accid: &[u8]) -> Result<u64> {
6    buf_write_read(raddr_out, accid, _c::util_raddr)
7}
8
9/// Convert an r-address into a 20 byte Account ID
10#[inline(always)]
11pub fn util_accid(accid_out: &mut [u8], raddr_in: &[u8]) -> Result<u64> {
12    buf_write_read(accid_out, raddr_in, _c::util_accid)
13}
14
15/// Verify a cryptographic signature
16#[inline(always)]
17pub fn util_verify(payload: &[u8], signature: &[u8], publickey: &[u8]) -> bool {
18    let res = buf_3_read(payload, signature, publickey, _c::util_verify);
19
20    match res {
21        Ok(0) => false,
22        Ok(1) => true,
23        _ => false,
24    }
25}
26
27/// Compute an sha512-half over some data
28#[inline(always)]
29pub fn util_sha512h(hash_out: &mut [u8], data_in: &[u8]) -> Result<u64> {
30    buf_write_read(hash_out, data_in, _c::util_sha512h)
31}
32
33/// Compute a serialized keylet of a given type
34#[inline(always)]
35pub fn util_keylet(keylet: &mut [u8], keylet_type: KeyletType) -> Result<u64> {
36    let write_ptr = keylet.as_mut_ptr() as _;
37    let write_len = keylet.len() as _;
38
39    match keylet_type {
40        KeyletType::Hook(accid) => buf_read_and_zeroes(keylet, accid, _c::KEYLET_HOOK),
41
42        KeyletType::HookState(accid, statekey) => {
43            buf_2_read_and_zeroes(keylet, accid, statekey, _c::KEYLET_HOOK_STATE)
44        }
45
46        KeyletType::Account(accid) => buf_read_and_zeroes(keylet, accid, _c::KEYLET_ACCOUNT),
47
48        KeyletType::Amendments => all_zeroes(keylet, _c::KEYLET_AMENDMENTS),
49
50        KeyletType::Child(key) => buf_read_and_zeroes(keylet, key, _c::KEYLET_CHILD),
51
52        KeyletType::Skip(opt) => match opt {
53            None => all_zeroes(keylet, _c::KEYLET_SKIP),
54
55            Some((ledger_index, num)) => {
56                let res = unsafe {
57                    _c::util_keylet(
58                        write_ptr,
59                        write_len,
60                        _c::KEYLET_SKIP,
61                        ledger_index,
62                        num,
63                        0,
64                        0,
65                        0,
66                        0,
67                    )
68                };
69
70                result_u64(res)
71            }
72        },
73
74        KeyletType::Fees => all_zeroes(keylet, _c::KEYLET_FEES),
75
76        KeyletType::NegativeUnl => all_zeroes(keylet, _c::KEYLET_NEGATIVE_UNL),
77
78        KeyletType::Line(accid_high, accid_low, currency_code) => {
79            let res = unsafe {
80                _c::util_keylet(
81                    write_ptr,
82                    write_len,
83                    _c::KEYLET_LINE,
84                    accid_high.as_ptr() as _,
85                    accid_high.len() as _,
86                    accid_low.as_ptr() as _,
87                    accid_low.len() as _,
88                    currency_code.as_ptr() as _,
89                    currency_code.len() as _,
90                )
91            };
92
93            result_u64(res)
94        }
95
96        KeyletType::Offer(accid, num) => buf_read_and_1_arg(keylet, accid, num, _c::KEYLET_OFFER),
97
98        KeyletType::Quality(serialized_keylet, bits_high, bits_low) => buf_read_and_2_args(
99            keylet,
100            serialized_keylet,
101            bits_high,
102            bits_low,
103            _c::KEYLET_QUALITY,
104        ),
105
106        KeyletType::EmittedDir => all_zeroes(keylet, _c::KEYLET_EMITTED_DIR),
107
108        KeyletType::Signers(accid) => buf_read_and_zeroes(keylet, accid, _c::KEYLET_SIGNERS),
109
110        KeyletType::Check(accid, num) => buf_read_and_1_arg(keylet, accid, num, _c::KEYLET_CHECK),
111
112        KeyletType::DepositPreauth(accid_1, accid_2) => {
113            buf_2_read_and_zeroes(keylet, accid_1, accid_2, _c::KEYLET_DEPOSIT_PREAUTH)
114        }
115
116        KeyletType::Unchecked(key) => buf_read_and_zeroes(keylet, key, _c::KEYLET_UNCHECKED),
117
118        KeyletType::OwnerDir(accid) => buf_read_and_zeroes(keylet, accid, _c::KEYLET_OWNER_DIR),
119
120        KeyletType::Page(key, bits_high, bits_low) => {
121            buf_read_and_2_args(keylet, key, bits_high, bits_low, _c::KEYLET_PAGE)
122        }
123
124        KeyletType::Escrow(accid, num) => buf_read_and_1_arg(keylet, accid, num, _c::KEYLET_ESCROW),
125
126        KeyletType::Paychan(accid_1, accid_2, num) => {
127            let res = unsafe {
128                _c::util_keylet(
129                    write_ptr,
130                    write_len,
131                    _c::KEYLET_PAYCHAN,
132                    accid_1.as_ptr() as _,
133                    accid_1.len() as _,
134                    accid_2.as_ptr() as _,
135                    accid_2.len() as _,
136                    num,
137                    0,
138                )
139            };
140
141            result_u64(res)
142        }
143
144        KeyletType::Emitted(key) => buf_read_and_zeroes(keylet, key, _c::KEYLET_EMITTED),
145    }
146}