Skip to main content

thru_base/
lib.rs

1pub mod bintrie;
2pub mod bintrie_error;
3pub mod bintrie_proof;
4pub mod bintrie_types;
5pub mod block_parser;
6pub mod crypto_utils;
7pub mod rpc_types;
8pub mod tn_account;
9pub mod tn_block_packet;
10pub mod tn_public_address;
11pub mod tn_runtime_utils;
12pub mod signer;
13pub mod tn_signature;
14pub mod tn_signature_encoding;
15pub mod tn_signature_legacy;
16pub mod tn_state_proof;
17pub mod tn_tools;
18pub mod txn_lib;
19pub mod txn_tools;
20pub mod version;
21
22#[cfg(test)]
23mod txn_tools_test;
24
25#[cfg(test)]
26mod bintrie_tests;
27
28pub const TN_VM_ERROR_REVERT: i32 = -765; /* TN_RUNTIME_TXN_VM_REVERT fffffd03 */
29pub const TN_VM_ERROR_FEE_PAYER_ACCOUNT_DOES_NOT_EXIST: i32 = -508; /* TN_RUNTIME_TXN_ERR_FEE_PAYER_ACCOUNT_DOES_NOT_EXIST fffffe04 */
30
31pub const TN_EXECUTION_RESULT: u64 = 0xfffffffffffffffc; /* TN_VM_ERR_SIGFAULT  */
32pub const TN_USER_ERROR_CODE_SYSCALL_INSUFFICIENT_BALANCE: u64 = 0xffffffffffffffdc; /* TN_VM_ERR_SYSCALL_INSUFFICIENT_BALANCE */
33
34// re-export types
35pub use bintrie::{BinTrie, BinTriePair};
36pub use tn_state_proof::{StateProof, StateProofBody, StateProofHeader, StateProofType};
37pub use tn_tools::KeyPair;
38pub use tn_tools::Pubkey;
39pub use tn_tools::Signature;
40pub use txn_lib::Transaction;
41pub use txn_tools::TransactionBuilder;
42
43// re-export crypto utilities
44pub use crypto_utils::{derive_manager_program_accounts, derive_uploader_program_accounts};
45
46// re-export public address utilities
47pub use tn_public_address::{
48    create_program_defined_account_address, create_program_defined_account_address_string,
49    pack_seed, tn_pubkey_to_address_string, tn_public_address_decode, tn_public_address_encode,
50};
51
52// re-export runtime utilities
53pub use bintrie_error::BinTrieError;
54pub use bintrie_proof::{NonExistenceProof, Proof};
55pub use bintrie_types::{Hash as BinTrieHash, Pubkey as BinTriePubkey};
56pub use tn_runtime_utils::tn_vm_error_str;
57
58/// Checks if the given byte slice is a valid C-style null-terminated string containing only printable ASCII characters.
59pub fn is_c_printable_ascii_null_terminated(bytes: &[u8]) -> bool {
60    // Must contain at least one null byte
61    let Some(pos) = bytes.iter().position(|&b| b == 0) else {
62        return false;
63    };
64    // All bytes before the first null must be printable ASCII (0x20..=0x7E)
65    for &b in &bytes[..pos] {
66        if !(b >= 0x20 && b <= 0x7E) {
67            return false;
68        }
69    }
70    true
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_is_c_printable_ascii_null_terminated() {
79        // Valid cases
80        assert!(is_c_printable_ascii_null_terminated(b"hello\0"));
81        assert!(is_c_printable_ascii_null_terminated(b"!@#$%^&*()_+\0"));
82        assert!(is_c_printable_ascii_null_terminated(b" \0")); // space is printable
83        assert!(is_c_printable_ascii_null_terminated(b"~\0")); // tilde is printable
84        assert!(is_c_printable_ascii_null_terminated(b"\0")); // empty string
85        // Valid: data after null is ignored
86        assert!(is_c_printable_ascii_null_terminated(
87            b"hello\0not_printed\x01\x02"
88        ));
89
90        // Invalid: not null-terminated
91        assert!(!is_c_printable_ascii_null_terminated(b"hello"));
92        // Invalid: contains non-printable ASCII (tab)
93        assert!(!is_c_printable_ascii_null_terminated(b"hel\tlo\0"));
94        // Invalid: contains non-printable ASCII (DEL)
95        assert!(!is_c_printable_ascii_null_terminated(b"hel\x7Flo\0"));
96        // Invalid: empty slice
97        assert!(!is_c_printable_ascii_null_terminated(b""));
98        // Invalid: non-printable before null
99        assert!(!is_c_printable_ascii_null_terminated(b"foo\x19\0"));
100    }
101}