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
use crate::api::ErrorApi;

use super::{
    token_identifier_util::IDENTIFIER_MAX_LENGTH, BigFloatApi, BigIntApi, EllipticCurveApi,
    ManagedBufferApi,
};

pub trait ManagedTypeApiImpl:
    BigIntApi + BigFloatApi + EllipticCurveApi + ManagedBufferApi + ErrorApi
{
    fn mb_to_big_int_unsigned(
        &self,
        buffer_handle: Self::ManagedBufferHandle,
        dest: Self::BigIntHandle,
    );

    fn mb_to_big_int_signed(
        &self,
        buffer_handle: Self::ManagedBufferHandle,
        dest: Self::BigIntHandle,
    );

    fn mb_from_big_int_unsigned(
        &self,
        big_int_handle: Self::BigIntHandle,
        dest: Self::ManagedBufferHandle,
    );

    fn mb_from_big_int_signed(
        &self,
        big_int_handle: Self::BigIntHandle,
        dest: Self::ManagedBufferHandle,
    );

    fn mb_to_big_float(&self, buffer_handle: Self::ManagedBufferHandle, dest: Self::BigFloatHandle);

    fn mb_from_big_float(
        &self,
        big_float_handle: Self::BigFloatHandle,
        dest: Self::ManagedBufferHandle,
    );

    fn validate_token_identifier(&self, token_id_handle: Self::ManagedBufferHandle) -> bool {
        let token_id_len = self.mb_len(token_id_handle.clone());
        if token_id_len > IDENTIFIER_MAX_LENGTH {
            return false;
        }

        let mut static_buffer = [0u8; IDENTIFIER_MAX_LENGTH];
        let static_buffer_slice = &mut static_buffer[..token_id_len];

        let load_result = self.mb_load_slice(token_id_handle, 0, static_buffer_slice);
        if load_result.is_err() {
            return false;
        }

        super::token_identifier_util::validate_token_identifier(static_buffer_slice)
    }
}